Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-6]: src/usr.bin/ftp Pullup rev 1.110 (requested by grant in tic...



details:   https://anonhg.NetBSD.org/src/rev/a175998cf9af
branches:  netbsd-1-6
changeset: 530101:a175998cf9af
user:      jmc <jmc%NetBSD.org@localhost>
date:      Mon Feb 10 06:03:16 2003 +0000

description:
Pullup rev 1.110 (requested by grant in ticket #1115)
 Separate progress reporting into a utility set of routines. Use
 these to provide a standalone progress binary and integrate support
 for this into sysinst.

diffstat:

 usr.bin/ftp/util.c |  372 +----------------------------------------------------
 1 files changed, 2 insertions(+), 370 deletions(-)

diffs (truncated from 404 to 300 lines):

diff -r 93bee37426ec -r a175998cf9af usr.bin/ftp/util.c
--- a/usr.bin/ftp/util.c        Mon Feb 10 06:02:45 2003 +0000
+++ b/usr.bin/ftp/util.c        Mon Feb 10 06:03:16 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.106 2001/12/26 09:40:16 lukem Exp $ */
+/*     $NetBSD: util.c,v 1.106.2.1 2003/02/10 06:03:16 jmc Exp $       */
 
 /*-
  * Copyright (c) 1997-2001 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.106 2001/12/26 09:40:16 lukem Exp $");
+__RCSID("$NetBSD: util.c,v 1.106.2.1 2003/02/10 06:03:16 jmc Exp $");
 #endif /* not lint */
 
 /*
@@ -800,290 +800,6 @@
        code = ocode;
 }
 
-#ifndef        NO_PROGRESS
-
-/*
- * return non-zero if we're the current foreground process
- */
-int
-foregroundproc(void)
-{
-       static pid_t pgrp = -1;
-
-       if (pgrp == -1)
-               pgrp = getpgrp();
-
-       return (tcgetpgrp(fileno(ttyout)) == pgrp);
-}
-
-
-static void updateprogressmeter(int);
-
-/*
- * SIGALRM handler to update the progress meter
- */
-static void
-updateprogressmeter(int dummy)
-{
-       int oerrno = errno;
-
-       progressmeter(0);
-       errno = oerrno;
-}
-#endif /* NO_PROGRESS */
-
-
-/*
- * List of order of magnitude prefixes.
- * The last is `P', as 2^64 = 16384 Petabytes
- */
-static const char prefixes[] = " KMGTP";
-
-/*
- * Display a transfer progress bar if progress is non-zero.
- * SIGALRM is hijacked for use by this function.
- * - Before the transfer, set filesize to size of file (or -1 if unknown),
- *   and call with flag = -1. This starts the once per second timer,
- *   and a call to updateprogressmeter() upon SIGALRM.
- * - During the transfer, updateprogressmeter will call progressmeter
- *   with flag = 0
- * - After the transfer, call with flag = 1
- */
-static struct timeval start;
-static struct timeval lastupdate;
-
-#define        BUFLEFT (sizeof(buf) - len)
-
-void
-progressmeter(int flag)
-{
-       static off_t lastsize;
-#ifndef NO_PROGRESS
-       struct timeval now, td, wait;
-       off_t cursize, abbrevsize, bytespersec;
-       double elapsed;
-       int ratio, barlength, i, len, remaining;
-
-                       /*
-                        * Work variables for progress bar.
-                        *
-                        * XXX: if the format of the progress bar changes
-                        *      (especially the number of characters in the
-                        *      `static' portion of it), be sure to update
-                        *      these appropriately.
-                        */
-       char            buf[256];       /* workspace for progress bar */
-#define        BAROVERHEAD     43              /* non `*' portion of progress bar */
-                                       /*
-                                        * stars should contain at least
-                                        * sizeof(buf) - BAROVERHEAD entries
-                                        */
-       const char      stars[] =
-"*****************************************************************************"
-"*****************************************************************************"
-"*****************************************************************************";
-
-#endif
-
-       if (flag == -1) {
-               (void)gettimeofday(&start, NULL);
-               lastupdate = start;
-               lastsize = restart_point;
-       }
-#ifndef NO_PROGRESS
-       if (!progress)
-               return;
-       len = 0;
-
-       /*
-        * print progress bar only if we are foreground process.
-        */
-       if (! foregroundproc())
-               return;
-
-       (void)gettimeofday(&now, NULL);
-       cursize = bytes + restart_point;
-       timersub(&now, &lastupdate, &wait);
-       if (cursize > lastsize) {
-               lastupdate = now;
-               lastsize = cursize;
-               wait.tv_sec = 0;
-       }
-
-       len += snprintf(buf + len, BUFLEFT, "\r");
-       if (filesize > 0) {
-               ratio = (int)((double)cursize * 100.0 / (double)filesize);
-               ratio = MAX(ratio, 0);
-               ratio = MIN(ratio, 100);
-               len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
-
-                       /*
-                        * calculate the length of the `*' bar, ensuring that
-                        * the number of stars won't exceed the buffer size 
-                        */
-               barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
-               if (barlength > 0) {
-                       i = barlength * ratio / 100;
-                       len += snprintf(buf + len, BUFLEFT,
-                           "|%.*s%*s|", i, stars, barlength - i, "");
-               }
-       }
-
-       abbrevsize = cursize;
-       for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
-               abbrevsize >>= 10;
-       len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %c%c ",
-           (LLT)abbrevsize,
-           prefixes[i],
-           i == 0 ? ' ' : 'B');
-
-       timersub(&now, &start, &td);
-       elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
-
-       bytespersec = 0;
-       if (bytes > 0) {
-               bytespersec = bytes;
-               if (elapsed > 0.0)
-                       bytespersec /= elapsed;
-       }
-       for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
-               bytespersec >>= 10;
-       len += snprintf(buf + len, BUFLEFT,
-           " " LLFP("3") ".%02d %cB/s ",
-           (LLT)(bytespersec / 1024),
-           (int)((bytespersec % 1024) * 100 / 1024),
-           prefixes[i]);
-
-       if (filesize > 0) {
-               if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
-                       len += snprintf(buf + len, BUFLEFT, "   --:-- ETA");
-               } else if (wait.tv_sec >= STALLTIME) {
-                       len += snprintf(buf + len, BUFLEFT, " - stalled -");
-               } else {
-                       remaining = (int)
-                           ((filesize - restart_point) / (bytes / elapsed) -
-                           elapsed);
-                       if (remaining >= 100 * SECSPERHOUR)
-                               len += snprintf(buf + len, BUFLEFT,
-                                   "   --:-- ETA");
-                       else {
-                               i = remaining / SECSPERHOUR;
-                               if (i)
-                                       len += snprintf(buf + len, BUFLEFT,
-                                           "%2d:", i);
-                               else
-                                       len += snprintf(buf + len, BUFLEFT,
-                                           "   ");
-                               i = remaining % SECSPERHOUR;
-                               len += snprintf(buf + len, BUFLEFT,
-                                   "%02d:%02d ETA", i / 60, i % 60);
-                       }
-               }
-       }
-       if (flag == 1)
-               len += snprintf(buf + len, BUFLEFT, "\n");
-       (void)write(fileno(ttyout), buf, len);
-
-       if (flag == -1) {
-               (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
-               alarmtimer(1);          /* set alarm timer for 1 Hz */
-       } else if (flag == 1) {
-               (void)xsignal(SIGALRM, SIG_DFL);
-               alarmtimer(0);
-       }
-#endif /* !NO_PROGRESS */
-}
-
-/*
- * Display transfer statistics.
- * Requires start to be initialised by progressmeter(-1),
- * direction to be defined by xfer routines, and filesize and bytes
- * to be updated by xfer routines
- * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
- * instead of ttyout.
- */
-void
-ptransfer(int siginfo)
-{
-       struct timeval now, td, wait;
-       double elapsed;
-       off_t bytespersec;
-       int remaining, hh, i, len;
-
-       char buf[256];          /* Work variable for transfer status. */
-
-       if (!verbose && !progress && !siginfo)
-               return;
-
-       (void)gettimeofday(&now, NULL);
-       timersub(&now, &start, &td);
-       elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
-       bytespersec = 0;
-       if (bytes > 0) {
-               bytespersec = bytes;
-               if (elapsed > 0.0)
-                       bytespersec /= elapsed;
-       }
-       len = 0;
-       len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
-           (LLT)bytes, bytes == 1 ? "" : "s", direction);
-       remaining = (int)elapsed;
-       if (remaining > SECSPERDAY) {
-               int days;
-
-               days = remaining / SECSPERDAY;
-               remaining %= SECSPERDAY;
-               len += snprintf(buf + len, BUFLEFT,
-                   "%d day%s ", days, days == 1 ? "" : "s");
-       }
-       hh = remaining / SECSPERHOUR;
-       remaining %= SECSPERHOUR;
-       if (hh)
-               len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
-       len += snprintf(buf + len, BUFLEFT,
-           "%02d:%02d ", remaining / 60, remaining % 60);
-
-       for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
-               bytespersec >>= 10;
-       len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %cB/s)",
-           (LLT)(bytespersec / 1024),
-           (int)((bytespersec % 1024) * 100 / 1024),
-           prefixes[i]);
-
-       if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
-           && bytes + restart_point <= filesize) {
-               remaining = (int)((filesize - restart_point) /
-                                 (bytes / elapsed) - elapsed);
-               hh = remaining / SECSPERHOUR;
-               remaining %= SECSPERHOUR;
-               len += snprintf(buf + len, BUFLEFT, "  ETA: ");
-               if (hh)
-                       len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
-               len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
-                   remaining / 60, remaining % 60);
-               timersub(&now, &lastupdate, &wait);
-               if (wait.tv_sec >= STALLTIME)
-                       len += snprintf(buf + len, BUFLEFT, "  (stalled)");
-       }
-       len += snprintf(buf + len, BUFLEFT, "\n");
-       (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
-}
-
-/*
- * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
- */
-void
-psummary(int notused)
-{
-       int oerrno = errno;
-
-       if (bytes > 0) {



Home | Main Index | Thread Index | Old Index