]> git.cameronkatri.com Git - bsd-progress.git/blobdiff - progress.c
Use the IEC 60027-2 2^n based "KiB", "MiB", "GiB", (etc)
[bsd-progress.git] / progress.c
index 5db052e2e7b1fabbd4332dd5e7e7a54b0e84c2d6..79076f9eaa4ca1c6266af46eead93ee7283df62a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: progress.c,v 1.2 2003/01/22 03:13:32 enami Exp $ */
+/*     $NetBSD: progress.c,v 1.14 2007/02/07 15:21:21 hubertf Exp $ */
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: progress.c,v 1.2 2003/01/22 03:13:32 enami Exp $");
+__RCSID("$NetBSD: progress.c,v 1.14 2007/02/07 15:21:21 hubertf Exp $");
 #endif                         /* not lint */
 
 #include <sys/types.h>
 #endif                         /* not lint */
 
 #include <sys/types.h>
@@ -46,12 +46,12 @@ __RCSID("$NetBSD: progress.c,v 1.2 2003/01/22 03:13:32 enami Exp $");
 #include <netinet/in.h>
 #include <arpa/ftp.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 <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>
 #include <limits.h>
 #include <netdb.h>
 #include <stdio.h>
@@ -73,9 +73,9 @@ static void
 usage(void)
 {
        fprintf(stderr,
 usage(void)
 {
        fprintf(stderr,
-           "usage: %s [-z] [-f file] [-l length] cmd [args...]\n",
+           "usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n",
            getprogname());
            getprogname());
-       exit(1);
+       exit(EXIT_FAILURE);
 }
 
 
 }
 
 
@@ -84,32 +84,42 @@ main(int argc, char *argv[])
 {
        static char fb_buf[BUFSIZ];
        char *infile = NULL;
 {
        static char fb_buf[BUFSIZ];
        char *infile = NULL;
-       pid_t pid;
-       int ch, fd, outpipe[2], waitstat;
-       int lflag = 0, zflag = 0;
+       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;
        struct stat statb;
        ssize_t nr, nw, off;
        struct stat statb;
+       struct ttysize ts;
 
        setprogname(argv[0]);
 
        /* defaults: Read from stdin, 0 filesize (no completion estimate) */
        fd = STDIN_FILENO;
        filesize = 0;
 
        setprogname(argv[0]);
 
        /* defaults: Read from stdin, 0 filesize (no completion estimate) */
        fd = STDIN_FILENO;
        filesize = 0;
+       prefix = NULL;
 
 
-       while ((ch = getopt(argc, argv, "f:l:z")) != -1)
+       while ((ch = getopt(argc, argv, "ef:l:p:z")) != -1)
                switch (ch) {
                switch (ch) {
+               case 'e':
+                       eflag++;
+                       break;
                case 'f':
                        infile = optarg;
                        break;
                case 'l':
                        lflag++;
                case 'f':
                        infile = optarg;
                        break;
                case 'l':
                        lflag++;
-                       filesize = strtoull(optarg, NULL, 0);
+                       filesize = strsuftoll("input size", optarg, 0,
+                           LLONG_MAX);
+                       break;
+               case 'p':
+                       prefix = optarg;
                        break;
                case 'z':
                        zflag++;
                        break;
                        break;
                case 'z':
                        zflag++;
                        break;
-               default:
                case '?':
                case '?':
+               default:
                        usage();
                        /* NOTREACHED */
                }
                        usage();
                        /* NOTREACHED */
                }
@@ -118,18 +128,26 @@ main(int argc, char *argv[])
 
        if (argc < 1)
                usage();
 
        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 (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) {
                FILE *gzipsizepipe;
 
        /* gzip -l the file if we have the name and -z is given */
        if (zflag && !lflag && infile != NULL) {
                FILE *gzipsizepipe;
-               char buf[256], *cmd;
-               long size;
+               char buf[256], *cp, *cmd;
 
                /*
                 * Read second word of last line of gzip -l output. Looks like:
 
                /*
                 * Read second word of last line of gzip -l output. Looks like:
@@ -143,15 +161,14 @@ main(int argc, char *argv[])
                        err(1, "reading compressed file length");
                for (; fgets(buf, 256, gzipsizepipe) != NULL;)
                    continue;
                        err(1, "reading compressed file length");
                for (; fgets(buf, 256, gzipsizepipe) != NULL;)
                    continue;
-               sscanf(buf, "%*d %ld", &size);
-               filesize = size;
+               strtoimax(buf, &cp, 10);
+               filesize = strtoimax(cp, NULL, 10);
                if (pclose(gzipsizepipe) < 0)
                        err(1, "closing compressed file length pipe");
                free(cmd);
        }
        /* Pipe input through gzip -dc if -z is given */
        if (zflag) {
                if (pclose(gzipsizepipe) < 0)
                        err(1, "closing compressed file length pipe");
                free(cmd);
        }
        /* Pipe input through gzip -dc if -z is given */
        if (zflag) {
-               pid_t gzippid;
                int gzippipe[2];
 
                if (pipe(gzippipe) < 0)
                int gzippipe[2];
 
                if (pipe(gzippipe) < 0)
@@ -178,8 +195,12 @@ main(int argc, char *argv[])
        /* Initialize progressbar.c's global state */
        bytes = 0;
        progress = 1;
        /* Initialize progressbar.c's global state */
        bytes = 0;
        progress = 1;
-       ttyout = stdout;
-       ttywidth = 80;
+       ttyout = eflag ? stderr : stdout;
+
+       if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1)
+               ttywidth = 80;
+       else
+               ttywidth = ts.ts_cols;
 
        if (pipe(outpipe) < 0)
                err(1, "output pipe");
 
        if (pipe(outpipe) < 0)
                err(1, "output pipe");
@@ -202,11 +223,37 @@ main(int argc, char *argv[])
                for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
                        if ((nw = write(outpipe[1], fb_buf + off,
                            (size_t) nr)) < 0)
                for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
                        if ((nw = write(outpipe[1], fb_buf + off,
                            (size_t) nr)) < 0)
-                               err(1, "writing %d bytes to output pipe", nr);
+                               err(1, "writing %u bytes to output pipe",
+                                                       (unsigned) nr);
        close(outpipe[1]);
 
        close(outpipe[1]);
 
-       wait(&waitstat);
+       gzipstat = 0;
+       cmdstat = 0;
+       while (pid || gzippid) {
+               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);
+
+               if (deadpid != -1 && errno == EINTR)
+                       continue;
+               if (deadpid == pid) {
+                       pid = 0;
+                       cmdstat = ws;
+                       continue;
+               }
+               if (deadpid == gzippid) {
+                       gzippid = 0;
+                       gzipstat = ws;
+                       continue;
+               }
+               break;
+       }
 
        progressmeter(1);
 
        progressmeter(1);
-       return 0;
+       exit(cmdstat ? cmdstat : gzipstat);
 }
 }