-/* $NetBSD: progress.c,v 1.12 2006/04/20 23:20:55 hubertf Exp $ */
+/* $NetBSD: progress.c,v 1.17 2008/05/26 04:53:11 dholland Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* 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.
- * 3. Neither the name of The NetBSD Foundation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: progress.c,v 1.12 2006/04/20 23:20:55 hubertf Exp $");
+__RCSID("$NetBSD: progress.c,v 1.17 2008/05/26 04:53:11 dholland Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/ftp.h>
-#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <signal.h>
+#include <inttypes.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
* declared */
#include "progressbar.h"
+static void broken_pipe(int unused);
static void usage(void);
int main(int, char *[]);
+static void
+broken_pipe(int unused)
+{
+ signal(SIGPIPE, SIG_DFL);
+ progressmeter(1);
+ kill(getpid(), SIGPIPE);
+}
+
static void
usage(void)
{
fprintf(stderr,
- "usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n",
- getprogname());
- exit(1);
+ "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n"
+ " %*.s [-p prefix] cmd [args...]\n",
+ getprogname(), (int) strlen(getprogname()), "");
+ exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
- static char fb_buf[BUFSIZ];
+ char *fb_buf;
char *infile = NULL;
pid_t pid = 0, gzippid = 0, deadpid;
int ch, fd, outpipe[2];
int ws, gzipstat, cmdstat;
int eflag = 0, lflag = 0, zflag = 0;
ssize_t nr, nw, off;
+ size_t buffersize;
struct stat statb;
struct ttysize ts;
/* defaults: Read from stdin, 0 filesize (no completion estimate) */
fd = STDIN_FILENO;
filesize = 0;
- prefix=NULL;
+ buffersize = 64 * 1024;
+ prefix = NULL;
- while ((ch = getopt(argc, argv, "ef:l:p:z")) != -1)
+ while ((ch = getopt(argc, argv, "b:ef:l:p:z")) != -1)
switch (ch) {
+ case 'b':
+ buffersize = (size_t) strsuftoll("buffer size", optarg,
+ 0, SIZE_T_MAX);
+ break;
case 'e':
eflag++;
break;
case 'z':
zflag++;
break;
- default:
case '?':
+ default:
usage();
/* NOTREACHED */
}
if (argc < 1)
usage();
+
if (infile && (fd = open(infile, O_RDONLY, 0)) < 0)
err(1, "%s", infile);
progress = 1;
ttyout = eflag ? stderr : stdout;
- if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1) {
+ if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1)
ttywidth = 80;
- } else
+ else
ttywidth = ts.ts_cols;
+ fb_buf = malloc(buffersize);
+ if (fb_buf == NULL)
+ err(1, "malloc for buffersize");
+
if (pipe(outpipe) < 0)
err(1, "output pipe");
pid = fork();
}
close(outpipe[0]);
+ signal(SIGPIPE, broken_pipe);
progressmeter(-1);
- while ((nr = read(fd, fb_buf, BUFSIZ)) > 0)
+
+ while ((nr = read(fd, fb_buf, buffersize)) > 0)
for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
if ((nw = write(outpipe[1], fb_buf + off,
- (size_t) nr)) < 0)
+ (size_t) nr)) < 0) {
+ progressmeter(1);
err(1, "writing %u bytes to output pipe",
(unsigned) nr);
+ }
close(outpipe[1]);
gzipstat = 0;
}
progressmeter(1);
+ signal(SIGPIPE, SIG_DFL);
+
+ free(fb_buf);
+
exit(cmdstat ? cmdstat : gzipstat);
}