-/* $NetBSD: progress.c,v 1.11 2006/01/12 20:33:20 garbled Exp $ */
+/* $NetBSD: progress.c,v 1.13 2007/02/07 14:06:57 hubertf Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: progress.c,v 1.11 2006/01/12 20:33:20 garbled Exp $");
+__RCSID("$NetBSD: progress.c,v 1.13 2007/02/07 14:06:57 hubertf 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>
fprintf(stderr,
"usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n",
getprogname());
- exit(1);
+ exit(EXIT_FAILURE);
}
/* defaults: Read from stdin, 0 filesize (no completion estimate) */
fd = STDIN_FILENO;
filesize = 0;
- prefix=NULL;
+ prefix = NULL;
while ((ch = getopt(argc, argv, "ef:l:p:z")) != -1)
switch (ch) {
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);
/* stat() to get the filesize unless overridden, or -z */
- if (!zflag && !lflag && (fstat(fd, &statb) == 0))
- filesize = statb.st_size;
+ 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;
+ }
+ }
/* gzip -l the file if we have the name and -z is given */
if (zflag && !lflag && infile != NULL) {
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;
if (pipe(outpipe) < 0)