]> git.cameronkatri.com Git - bsd-progress.git/blobdiff - progressbar.c
Don't try to determining the size of what's coming when reading from a pipe.
[bsd-progress.git] / progressbar.c
index c1a3a2810e96255b59ce12f5de8035b7e470e8a8..43b185187c9248939739ac6ae2a8bfe70c41b712 100644 (file)
@@ -1,16 +1,12 @@
-/*     $NetBSD: progressbar.c,v 1.1 2003/01/21 16:08:08 jhawk Exp $    */
+/*     $NetBSD: progressbar.c,v 1.11 2005/07/19 00:41:05 lukem Exp $   */
 
 /*-
- * Copyright (c) 1997-2003 The NetBSD Foundation, Inc.
+ * Copyright (c) 1997-2005 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Luke Mewburn.
  *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
- * NASA Ames Research Center.
- *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- * Copyright (c) 1985, 1989, 1993, 1994
- *     The Regents of the University of California.  All rights reserved.
- *
- * 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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
-__RCSID("$NetBSD: progressbar.c,v 1.1 2003/01/21 16:08:08 jhawk Exp $");
+__RCSID("$NetBSD: progressbar.c,v 1.11 2005/07/19 00:41:05 lukem Exp $");
 #endif /* not lint */
 
 /*
@@ -89,13 +52,14 @@ __RCSID("$NetBSD: progressbar.c,v 1.1 2003/01/21 16:08:08 jhawk Exp $");
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <tzfile.h>
 #include <unistd.h>
 
 #include "progressbar.h"
 
-#if !defined(NO_PROGRESS) && !defined(STANDALONE_PROGRESS)
+#if !defined(NO_PROGRESS)
 /*
  * return non-zero if we're the current foreground process
  */
@@ -109,10 +73,9 @@ foregroundproc(void)
 
        return (tcgetpgrp(fileno(ttyout)) == pgrp);
 }
-#endif /* !defined(NO_PROGRESS) && !defined(STANDALONE_PROGRESS) */
+#endif /* !defined(NO_PROGRESS) */
 
 
-#ifndef        NO_PROGRESS
 static void updateprogressmeter(int);
 
 /*
@@ -126,8 +89,6 @@ updateprogressmeter(int dummy)
        progressmeter(0);
        errno = oerrno;
 }
-#endif /* NO_PROGRESS */
-
 
 /*
  * List of order of magnitude prefixes.
@@ -160,7 +121,7 @@ progressmeter(int flag)
        struct timeval td;
        off_t abbrevsize, bytespersec;
        double elapsed;
-       int ratio, barlength, i, len, remaining;
+       int ratio, i, remaining, barlength;
 
                        /*
                         * Work variables for progress bar.
@@ -170,7 +131,10 @@ progressmeter(int flag)
                         *      `static' portion of it), be sure to update
                         *      these appropriately.
                         */
+#endif
+       size_t          len;
        char            buf[256];       /* workspace for progress bar */
+#ifndef NO_PROGRESS
 #define        BAROVERHEAD     43              /* non `*' portion of progress bar */
                                        /*
                                         * stars should contain at least
@@ -230,16 +194,15 @@ progressmeter(int flag)
                return;
        len = 0;
 
-#ifndef STANDALONE_PROGRESS
        /*
         * print progress bar only if we are foreground process.
         */
        if (! foregroundproc())
                return;
-#endif /* !STANDALONE_PROGRESS */
-
 
        len += snprintf(buf + len, BUFLEFT, "\r");
+       if (prefix)
+         len += snprintf(buf + len, BUFLEFT, "%s", prefix);
        if (filesize > 0) {
                ratio = (int)((double)cursize * 100.0 / (double)filesize);
                ratio = MAX(ratio, 0);
@@ -248,13 +211,15 @@ progressmeter(int flag)
 
                        /*
                         * calculate the length of the `*' bar, ensuring that
-                        * the number of stars won't exceed the buffer size 
+                        * the number of stars won't exceed the buffer size
                         */
                barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
+               if (prefix)
+                       barlength -= (int)strlen(prefix);
                if (barlength > 0) {
                        i = barlength * ratio / 100;
                        len += snprintf(buf + len, BUFLEFT,
-                           "|%.*s%*s|", i, stars, barlength - i, "");
+                           "|%.*s%*s|", i, stars, (int)(barlength - i), "");
                }
        }
 
@@ -331,7 +296,8 @@ ptransfer(int siginfo)
        struct timeval now, td, wait;
        double elapsed;
        off_t bytespersec;
-       int remaining, hh, i, len;
+       int remaining, hh, i;
+       size_t len;
 
        char buf[256];          /* Work variable for transfer status. */