2008-05-26 08:53:11 +04:00
|
|
|
/* $NetBSD: progress.c,v 1.17 2008/05/26 04:53:11 dholland Exp $ */
|
2003-01-22 03:14:12 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by John Hawkinson.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2008-05-26 08:53:11 +04:00
|
|
|
__RCSID("$NetBSD: progress.c,v 1.17 2008/05/26 04:53:11 dholland Exp $");
|
2003-01-22 03:14:12 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/ftp.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <glob.h>
|
|
|
|
#include <signal.h>
|
2007-02-07 18:21:21 +03:00
|
|
|
#include <inttypes.h>
|
2003-01-22 03:14:12 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <tzfile.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define GLOBAL /* force GLOBAL decls in progressbar.h to be
|
|
|
|
* declared */
|
|
|
|
#include "progressbar.h"
|
|
|
|
|
2008-05-26 08:53:11 +04:00
|
|
|
static void broken_pipe(int unused);
|
2003-01-22 03:14:12 +03:00
|
|
|
static void usage(void);
|
|
|
|
int main(int, char *[]);
|
|
|
|
|
2008-05-26 08:53:11 +04:00
|
|
|
static void
|
|
|
|
broken_pipe(int unused)
|
|
|
|
{
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
progressmeter(1);
|
|
|
|
kill(getpid(), SIGPIPE);
|
|
|
|
}
|
|
|
|
|
2003-01-22 03:14:12 +03:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2007-06-06 21:49:14 +04:00
|
|
|
"usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n"
|
|
|
|
" %*.s [-p prefix] cmd [args...]\n",
|
|
|
|
getprogname(), (int) strlen(getprogname()), "");
|
2007-02-07 17:06:57 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2003-01-22 03:14:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2007-06-06 21:49:14 +04:00
|
|
|
char *fb_buf;
|
2003-01-22 03:14:12 +03:00
|
|
|
char *infile = NULL;
|
2005-02-24 01:32:31 +03:00
|
|
|
pid_t pid = 0, gzippid = 0, deadpid;
|
|
|
|
int ch, fd, outpipe[2];
|
|
|
|
int ws, gzipstat, cmdstat;
|
2006-01-12 23:33:20 +03:00
|
|
|
int eflag = 0, lflag = 0, zflag = 0;
|
2003-01-22 03:14:12 +03:00
|
|
|
ssize_t nr, nw, off;
|
2007-06-06 21:49:14 +04:00
|
|
|
size_t buffersize;
|
2003-01-22 03:14:12 +03:00
|
|
|
struct stat statb;
|
2003-01-22 06:24:21 +03:00
|
|
|
struct ttysize ts;
|
2003-01-22 03:14:12 +03:00
|
|
|
|
|
|
|
setprogname(argv[0]);
|
|
|
|
|
|
|
|
/* defaults: Read from stdin, 0 filesize (no completion estimate) */
|
|
|
|
fd = STDIN_FILENO;
|
|
|
|
filesize = 0;
|
2007-06-06 21:49:14 +04:00
|
|
|
buffersize = 64 * 1024;
|
2007-02-07 17:06:57 +03:00
|
|
|
prefix = NULL;
|
2003-01-22 03:14:12 +03:00
|
|
|
|
2007-06-06 21:49:14 +04:00
|
|
|
while ((ch = getopt(argc, argv, "b:ef:l:p:z")) != -1)
|
2003-01-22 03:14:12 +03:00
|
|
|
switch (ch) {
|
2007-06-06 21:49:14 +04:00
|
|
|
case 'b':
|
|
|
|
buffersize = (size_t) strsuftoll("buffer size", optarg,
|
|
|
|
0, SIZE_T_MAX);
|
|
|
|
break;
|
2006-01-12 23:33:20 +03:00
|
|
|
case 'e':
|
|
|
|
eflag++;
|
|
|
|
break;
|
2003-01-22 03:14:12 +03:00
|
|
|
case 'f':
|
|
|
|
infile = optarg;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag++;
|
2004-04-03 10:19:22 +04:00
|
|
|
filesize = strsuftoll("input size", optarg, 0,
|
|
|
|
LLONG_MAX);
|
2003-01-22 03:14:12 +03:00
|
|
|
break;
|
2004-03-09 20:04:24 +03:00
|
|
|
case 'p':
|
|
|
|
prefix = optarg;
|
|
|
|
break;
|
2003-01-22 03:14:12 +03:00
|
|
|
case 'z':
|
|
|
|
zflag++;
|
|
|
|
break;
|
|
|
|
case '?':
|
2007-02-07 17:06:57 +03:00
|
|
|
default:
|
2003-01-22 03:14:12 +03:00
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
2007-02-07 17:06:57 +03:00
|
|
|
|
2003-01-22 03:14:12 +03:00
|
|
|
if (infile && (fd = open(infile, O_RDONLY, 0)) < 0)
|
|
|
|
err(1, "%s", infile);
|
|
|
|
|
|
|
|
/* stat() to get the filesize unless overridden, or -z */
|
2006-04-21 03:20:55 +04:00
|
|
|
if (!zflag && !lflag && (fstat(fd, &statb) == 0)) {
|
|
|
|
if (S_ISFIFO(statb.st_mode)) {
|
|
|
|
/* stat(2) on pipe may return only the
|
|
|
|
* first few bytes with more coming.
|
|
|
|
* Don't trust!
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
filesize = statb.st_size;
|
|
|
|
}
|
|
|
|
}
|
2003-01-22 03:14:12 +03:00
|
|
|
|
|
|
|
/* gzip -l the file if we have the name and -z is given */
|
|
|
|
if (zflag && !lflag && infile != NULL) {
|
|
|
|
FILE *gzipsizepipe;
|
2005-02-24 01:32:31 +03:00
|
|
|
char buf[256], *cp, *cmd;
|
2003-01-22 03:14:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read second word of last line of gzip -l output. Looks like:
|
|
|
|
* % gzip -l ../etc.tgz
|
|
|
|
* compressed uncompressed ratio uncompressed_name
|
|
|
|
* 119737 696320 82.8% ../etc.tar
|
|
|
|
*/
|
|
|
|
|
|
|
|
asprintf(&cmd, "gzip -l %s", infile);
|
|
|
|
if ((gzipsizepipe = popen(cmd, "r")) == NULL)
|
|
|
|
err(1, "reading compressed file length");
|
|
|
|
for (; fgets(buf, 256, gzipsizepipe) != NULL;)
|
|
|
|
continue;
|
2005-02-24 01:32:31 +03:00
|
|
|
strtoimax(buf, &cp, 10);
|
|
|
|
filesize = strtoimax(cp, NULL, 10);
|
2003-01-22 03:14:12 +03:00
|
|
|
if (pclose(gzipsizepipe) < 0)
|
|
|
|
err(1, "closing compressed file length pipe");
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
/* Pipe input through gzip -dc if -z is given */
|
|
|
|
if (zflag) {
|
|
|
|
int gzippipe[2];
|
|
|
|
|
|
|
|
if (pipe(gzippipe) < 0)
|
|
|
|
err(1, "gzip pipe");
|
|
|
|
gzippid = fork();
|
|
|
|
if (gzippid < 0)
|
|
|
|
err(1, "fork for gzip");
|
|
|
|
|
|
|
|
if (gzippid) {
|
|
|
|
/* parent */
|
|
|
|
dup2(gzippipe[0], fd);
|
|
|
|
close(gzippipe[0]);
|
|
|
|
close(gzippipe[1]);
|
|
|
|
} else {
|
|
|
|
dup2(gzippipe[1], STDOUT_FILENO);
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
close(gzippipe[0]);
|
|
|
|
close(gzippipe[1]);
|
|
|
|
if (execlp("gzip", "gzip", "-dc", NULL))
|
|
|
|
err(1, "exec()ing gzip");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize progressbar.c's global state */
|
|
|
|
bytes = 0;
|
|
|
|
progress = 1;
|
2006-01-12 23:33:20 +03:00
|
|
|
ttyout = eflag ? stderr : stdout;
|
2003-01-22 06:24:21 +03:00
|
|
|
|
2007-02-07 17:06:57 +03:00
|
|
|
if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1)
|
2003-01-22 06:24:21 +03:00
|
|
|
ttywidth = 80;
|
2007-02-07 17:06:57 +03:00
|
|
|
else
|
2003-01-22 06:24:21 +03:00
|
|
|
ttywidth = ts.ts_cols;
|
2003-01-22 03:14:12 +03:00
|
|
|
|
2007-06-06 21:49:14 +04:00
|
|
|
fb_buf = malloc(buffersize);
|
|
|
|
if (fb_buf == NULL)
|
|
|
|
err(1, "malloc for buffersize");
|
|
|
|
|
2003-01-22 03:14:12 +03:00
|
|
|
if (pipe(outpipe) < 0)
|
|
|
|
err(1, "output pipe");
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
err(1, "fork for output pipe");
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
/* child */
|
|
|
|
dup2(outpipe[0], STDIN_FILENO);
|
|
|
|
close(outpipe[0]);
|
|
|
|
close(outpipe[1]);
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
err(1, "could not exec %s", argv[0]);
|
|
|
|
}
|
|
|
|
close(outpipe[0]);
|
|
|
|
|
2008-05-26 08:53:11 +04:00
|
|
|
signal(SIGPIPE, broken_pipe);
|
2003-01-22 03:14:12 +03:00
|
|
|
progressmeter(-1);
|
2008-05-26 08:53:11 +04:00
|
|
|
|
2007-06-06 21:49:14 +04:00
|
|
|
while ((nr = read(fd, fb_buf, buffersize)) > 0)
|
2003-01-22 06:13:32 +03:00
|
|
|
for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
|
|
|
|
if ((nw = write(outpipe[1], fb_buf + off,
|
2008-05-26 08:53:11 +04:00
|
|
|
(size_t) nr)) < 0) {
|
|
|
|
progressmeter(1);
|
2003-01-22 13:44:17 +03:00
|
|
|
err(1, "writing %u bytes to output pipe",
|
|
|
|
(unsigned) nr);
|
2008-05-26 08:53:11 +04:00
|
|
|
}
|
2003-01-22 03:14:12 +03:00
|
|
|
close(outpipe[1]);
|
|
|
|
|
2005-02-24 01:32:31 +03:00
|
|
|
gzipstat = 0;
|
|
|
|
cmdstat = 0;
|
2003-02-12 03:58:34 +03:00
|
|
|
while (pid || gzippid) {
|
2005-02-24 01:32:31 +03:00
|
|
|
deadpid = wait(&ws);
|
|
|
|
/*
|
|
|
|
* We need to exit with an error if the command (or gzip)
|
|
|
|
* exited abnormally.
|
|
|
|
* Unfortunately we can't generate a true 'exited by signal'
|
|
|
|
* error without sending the signal to ourselves :-(
|
|
|
|
*/
|
|
|
|
ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws);
|
2003-02-12 03:58:34 +03:00
|
|
|
|
2005-02-24 01:32:31 +03:00
|
|
|
if (deadpid != -1 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (deadpid == pid) {
|
2003-02-12 03:58:34 +03:00
|
|
|
pid = 0;
|
2005-02-24 01:32:31 +03:00
|
|
|
cmdstat = ws;
|
2003-02-12 03:58:34 +03:00
|
|
|
continue;
|
2005-02-24 01:32:31 +03:00
|
|
|
}
|
|
|
|
if (deadpid == gzippid) {
|
|
|
|
gzippid = 0;
|
|
|
|
gzipstat = ws;
|
2003-02-12 03:58:34 +03:00
|
|
|
continue;
|
2005-02-24 01:32:31 +03:00
|
|
|
}
|
|
|
|
break;
|
2003-02-12 03:58:34 +03:00
|
|
|
}
|
2003-01-22 03:14:12 +03:00
|
|
|
|
|
|
|
progressmeter(1);
|
2008-05-26 08:53:11 +04:00
|
|
|
signal(SIGPIPE, SIG_DFL);
|
2007-06-06 21:49:14 +04:00
|
|
|
|
|
|
|
free(fb_buf);
|
|
|
|
|
2005-02-24 01:32:31 +03:00
|
|
|
exit(cmdstat ? cmdstat : gzipstat);
|
2003-01-22 03:14:12 +03:00
|
|
|
}
|