Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/ftp * finish replacing snprintf() with sprintf(), fo...



details:   https://anonhg.NetBSD.org/src/rev/6b24e954acb5
branches:  trunk
changeset: 476660:6b24e954acb5
user:      lukem <lukem%NetBSD.org@localhost>
date:      Fri Sep 24 00:48:24 1999 +0000

description:
* finish replacing snprintf() with sprintf(), for portability reasons.
  add lots of comments about how to size up the buffers, and add extra
  checks to hopefully ensure that there won't be an overflow (unless
  someone modifies the length of the sprintf()s).
* as part of the above, slightly rework the way the `*' bar is calculated.
  also fixes a display bug when > 160 stars were needed to be printed.
  the maximum progress bar width at this time is 256.
* remove some code that checks the port that was #if 0-ed out as part of the
  ipv6 migration; it's not going to be used again.

diffstat:

 usr.bin/ftp/cmds.c |   23 +--------
 usr.bin/ftp/util.c |  122 +++++++++++++++++++++++++++-------------------------
 2 files changed, 67 insertions(+), 78 deletions(-)

diffs (275 lines):

diff -r a496cf985506 -r 6b24e954acb5 usr.bin/ftp/cmds.c
--- a/usr.bin/ftp/cmds.c        Fri Sep 24 00:37:52 1999 +0000
+++ b/usr.bin/ftp/cmds.c        Fri Sep 24 00:48:24 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cmds.c,v 1.58 1999/09/22 07:18:31 lukem Exp $  */
+/*     $NetBSD: cmds.c,v 1.59 1999/09/24 00:48:24 lukem Exp $  */
 
 /*
  * Copyright (C) 1997 and 1998 WIDE Project.
@@ -107,7 +107,7 @@
 #if 0
 static char sccsid[] = "@(#)cmds.c     8.6 (Berkeley) 10/9/94";
 #else
-__RCSID("$NetBSD: cmds.c,v 1.58 1999/09/22 07:18:31 lukem Exp $");
+__RCSID("$NetBSD: cmds.c,v 1.59 1999/09/24 00:48:24 lukem Exp $");
 #endif
 #endif /* not lint */
 
@@ -966,25 +966,8 @@
                else if (argc == 2 && strcasecmp(argv[1], "off") == 0)
                        gatemode = 0;
                else {
-                       if (argc == 3) {
-#if 0
-                               char *ep;
-                               long port;
-
-                               port = strtol(argv[2], &ep, 10);
-                               if (port < 0 || port > MAX_IN_PORT_T ||
-                                   *ep != '\0') {
-                                       fprintf(ttyout,
-                                           "%s: bad gateport value.\n",
-                                           argv[2]);
-                                       code = -1;
-                                       return;
-                               }
-                               gateport = htons(port);
-#else
+                       if (argc == 3)
                                gateport = strdup(argv[2]);
-#endif
-                       }
                        strncpy(gsbuf, argv[1], sizeof(gsbuf) - 1);
                        gsbuf[sizeof(gsbuf) - 1] = '\0';
                        gateserver = gsbuf;
diff -r a496cf985506 -r 6b24e954acb5 usr.bin/ftp/util.c
--- a/usr.bin/ftp/util.c        Fri Sep 24 00:37:52 1999 +0000
+++ b/usr.bin/ftp/util.c        Fri Sep 24 00:48:24 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.58 1999/09/22 07:18:37 lukem Exp $  */
+/*     $NetBSD: util.c,v 1.59 1999/09/24 00:48:24 lukem Exp $  */
 
 /*-
  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.58 1999/09/22 07:18:37 lukem Exp $");
+__RCSID("$NetBSD: util.c,v 1.59 1999/09/24 00:48:24 lukem Exp $");
 #endif /* not lint */
 
 /*
@@ -139,26 +139,8 @@
                port = gateport;
        else
                port = ftpport;
-#if 0
-       if (argc > 2) {
-               char *ep;
-               long nport;
-
-               nport = strtol(argv[2], &ep, 10);
-               if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
-                       fprintf(ttyout, "%s: bad port number '%s'.\n",
-                           argv[1], argv[2]);
-                       fprintf(ttyout, "usage: %s host-name [port]\n",
-                           argv[0]);
-                       code = -1;
-                       return;
-               }
-               port = htons((in_port_t)nport);
-       }
-#else
        if (argc > 2)
                port = strdup(argv[2]);
-#endif
 
        if (gatemode) {
                if (gateserver == NULL || *gateserver == '\0')
@@ -835,7 +817,30 @@
        off_t cursize, abbrevsize, bytespersec;
        double elapsed;
        int ratio, barlength, i, len, remaining;
-       char buf[256];
+
+                       /*
+                        * 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.
+                        *
+                        * XXX: sprintf() is used because it's more portable,
+                        *      so `buf' must be big enough to hold the
+                        *      largest values output.
+                        */
+       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) {
@@ -870,22 +875,23 @@
        ratio = (int)((double)cursize * 100.0 / (double)filesize);
        ratio = MAX(ratio, 0);
        ratio = MIN(ratio, 100);
-       len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio);
+       len += sprintf(buf + len, "\r%3d%% ", ratio);
 
-       barlength = ttywidth - 43;
+                       /*
+                        * 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, sizeof(buf) - len,
-                   "|%.*s%*s|", i,
-"*****************************************************************************"
-"*****************************************************************************",
-                   barlength - i, "");
+               len += sprintf(buf + len, "|%.*s%*s|",
+                   i, stars, barlength - i, "");
        }
 
        abbrevsize = cursize;
        for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
                abbrevsize >>= 10;
-       len += snprintf(buf + len, sizeof(buf) - len,
+       len += sprintf(buf + len,
 #ifndef NO_QUAD
            " %5qd %c%c ", (long long)abbrevsize,
 #else
@@ -905,7 +911,7 @@
        }
        for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
                bytespersec >>= 10;
-       len += snprintf(buf + len, sizeof(buf) - len,
+       len += sprintf(buf + len,
 #ifndef NO_QUAD
            " %3qd.%02d %cB/s ", (long long)bytespersec / 1024,
 #else
@@ -915,28 +921,23 @@
            prefixes[i]);
 
        if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
-               len += snprintf(buf + len, sizeof(buf) - len,
-                   "   --:-- ETA");
+               len += sprintf(buf + len, "   --:-- ETA");
        } else if (wait.tv_sec >= STALLTIME) {
-               len += snprintf(buf + len, sizeof(buf) - len,
-                   " - stalled -");
+               len += sprintf(buf + len, " - stalled -");
        } else {
                remaining = (int)
                    ((filesize - restart_point) / (bytes / elapsed) - elapsed);
                if (remaining >= 100 * SECSPERHOUR)
-                       len += snprintf(buf + len, sizeof(buf) - len,
-                           "   --:-- ETA");
+                       len += sprintf(buf + len, "   --:-- ETA");
                else {
                        i = remaining / SECSPERHOUR;
                        if (i)
-                               len += snprintf(buf + len, sizeof(buf) - len,
-                                   "%2d:", i);
+                               len += sprintf(buf + len, "%2d:", i);
                        else
-                               len += snprintf(buf + len, sizeof(buf) - len,
-                                   "   ");
+                               len += sprintf(buf + len, "   ");
                        i = remaining % SECSPERHOUR;
-                       len += snprintf(buf + len, sizeof(buf) - len,
-                           "%02d:%02d ETA", i / 60, i % 60);
+                       len += sprintf(buf + len, "%02d:%02d ETA",
+                           i / 60, i % 60);
                }
        }
        (void)write(fileno(ttyout), buf, len);
@@ -969,7 +970,15 @@
        double elapsed;
        off_t bytespersec;
        int remaining, hh, i, len;
-       char buf[100];
+
+                       /*
+                        * Work variable for transfer status.
+                        *
+                        * XXX: sprintf() is used because it's more portable,
+                        *      so `buf' must be large enough to hold the
+                        *      largest message length.
+                        */
+       char buf[256];
 
        if (!verbose && !progress && !siginfo)
                return;
@@ -984,7 +993,7 @@
                        bytespersec /= elapsed;
        }
        len = 0;
-       len += snprintf(buf + len, sizeof(buf) - len,
+       len += sprintf(buf + len,
 #ifndef NO_QUAD
            "%qd byte%s %s in ", (long long)bytes,
 #else
@@ -997,19 +1006,18 @@
 
                days = remaining / SECSPERDAY;
                remaining %= SECSPERDAY;
-               len += snprintf(buf + len, sizeof(buf) - len,
-                   "%d day%s ", days, days == 1 ? "" : "s");
+               len += sprintf(buf + len, "%d day%s ",
+                   days, days == 1 ? "" : "s");
        }
        hh = remaining / SECSPERHOUR;
        remaining %= SECSPERHOUR;
        if (hh)
-               len += snprintf(buf + len, sizeof(buf) - len, "%2d:", hh);
-       len += snprintf(buf + len, sizeof(buf) - len,
-           "%02d:%02d ", remaining / 60, remaining % 60);
+               len += sprintf(buf + len, "%2d:", hh);
+       len += sprintf(buf + len, "%02d:%02d ", remaining / 60, remaining % 60);
 
        for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
                bytespersec >>= 10;
-       len += snprintf(buf + len, sizeof(buf) - len,
+       len += sprintf(buf + len,
 #ifndef NO_QUAD
            "(%qd.%02d %cB/s)", (long long)bytespersec / 1024,
 #else
@@ -1024,18 +1032,16 @@
                                  (bytes / elapsed) - elapsed);
                hh = remaining / SECSPERHOUR;
                remaining %= SECSPERHOUR;
-               len += snprintf(buf + len, sizeof(buf) - len, "  ETA: ");
+               len += sprintf(buf + len, "  ETA: ");
                if (hh)
-                       len += snprintf(buf + len, sizeof(buf) - len, "%2d:",
-                           hh);
-               len += snprintf(buf + len, sizeof(buf) - len,
-                   "%02d:%02d", remaining / 60, remaining % 60);
+                       len += sprintf(buf + len, "%2d:", hh);
+               len += sprintf(buf + len, "%02d:%02d",
+                   remaining / 60, remaining % 60);
                timersub(&now, &lastupdate, &wait);
                if (wait.tv_sec >= STALLTIME)
-                       len += snprintf(buf + len, sizeof(buf) - len,
-                           "  (stalled)");
+                       len += sprintf(buf + len, "  (stalled)");
        }
-       len += snprintf(buf + len, sizeof(buf) - len, "\n");
+       len += sprintf(buf + len, "\n");
        (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
 }
 



Home | Main Index | Thread Index | Old Index