Skip site navigation (1)Skip section navigation (2)
Date:      08 Jul 2001 15:52:19 +0200
From:      Dag-Erling Smorgrav <des@ofug.org>
To:        audit@freebsd.org
Subject:   fetch(1) const/static cleanup
Message-ID:  <xzpu20nr0j0.fsf@flood.ping.uio.no>

next in thread | raw e-mail | index | archive | help
--=-=-=

Could somebody please test the attached patch on an Alpha?  It
staticicizes all the functions in fetch(1) except main(), constifies a
bunch of strings, and replaces the CFLAGS spammage with WARNS?=2.

(the patch looks weird because it was generated with 'cvs diff -Bb'
from reindented sources - just ignore that)

DES
-- 
Dag-Erling Smorgrav - des@ofug.org


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=fetch-warns.diff

Index: Makefile
===================================================================
RCS file: /home/ncvs/src/usr.bin/fetch/Makefile,v
retrieving revision 1.4
diff -u -B -b -r1.4 Makefile
--- Makefile	2000/06/28 16:55:09	1.4
+++ Makefile	2001/07/08 13:40:40
@@ -2,8 +2,7 @@
 
 MAINTAINER=	des@freebsd.org
 PROG=		fetch
-CFLAGS+=	-Wall -pedantic
-SRCS=		fetch.c
+WARNS?=		2
 DPADD=		${LIBFETCH}
 LDADD=		-lfetch
 
Index: fetch.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/fetch/fetch.c,v
retrieving revision 1.31
diff -u -B -b -r1.31 fetch.c
--- fetch.c	2001/06/01 10:24:58	1.31
+++ fetch.c	2001/07/08 13:44:16
@@ -87,7 +87,7 @@
 u_char	*buf;		/* transfer buffer */
 
 
-void
+static void
 sig_handler(int sig)
 {
     switch (sig) {
@@ -113,7 +113,7 @@
     off_t		 rcvd;
 };
 
-void
+static void
 stat_display(struct xferstat *xs, int force)
 {
     struct timeval now;
@@ -134,8 +134,8 @@
 		(int)((100.0 * xs->rcvd) / xs->size));
 }
 
-void
-stat_start(struct xferstat *xs, char *name, off_t size, off_t offset)
+static void
+stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
 {
     snprintf(xs->name, sizeof xs->name, "%s", name);
     gettimeofday(&xs->start, NULL);
@@ -147,14 +147,14 @@
     stat_display(xs, 1);
 }
 
-void
-stat_update(struct xferstat *xs, off_t rcvd, int force)
+static void
+stat_update(struct xferstat *xs, off_t rcvd)
 {
     xs->rcvd = rcvd;
     stat_display(xs, 0);
 }
 
-void
+static void
 stat_end(struct xferstat *xs)
 {
     double delta;
@@ -180,7 +180,7 @@
 	fprintf(stderr, "(%.2f Bps)\n", bps);
 }
 
-int
+static int
 query_auth(struct url *URL)
 {
     struct termios tios;
@@ -219,8 +219,8 @@
     return 0;
 }
 
-int
-fetch(char *URL, char *path)
+static int
+fetch(char *URL, const char *path)
 {
     struct url *url;
     struct url_stat us;
@@ -303,16 +303,17 @@
     }
 
     /*
-     * If the -r flag was specified, we have to compare the local and
-     * remote files, so we should really do a fetchStat() first, but I
-     * know of at least one HTTP server that only sends the content
-     * size in response to GET requests, and leaves it out of replies
-     * to HEAD requests. Also, in the (frequent) case that the local
-     * and remote files match but the local file is truncated, we have
-     * sufficient information *before* the compare to issue a correct
-     * request. Therefore, we always issue a GET request as if we were
-     * sure the local file was a truncated copy of the remote file; we
-     * can drop the connection later if we change our minds.
+	 * If the -r flag was specified, we have to compare the local
+	 * and remote files, so we should really do a fetchStat()
+	 * first, but I know of at least one HTTP server that only
+	 * sends the content size in response to GET requests, and
+	 * leaves it out of replies to HEAD requests.  Also, in the
+	 * (frequent) case that the local and remote files match but
+	 * the local file is truncated, we have sufficient information
+	 * before the compare to issue a correct request.  Therefore,
+	 * we always issue a GET request as if we were sure the local
+	 * file was a truncated copy of the remote file; we can drop
+	 * the connection later if we change our minds.
      */
     if ((r_flag  || m_flag) && !o_stdout && stat(path, &sb) != -1) {
 	if (r_flag)
@@ -372,8 +373,8 @@
 	    fclose(f);
 	    /* if precious, warn the user and give up */
 	    if (R_flag) {
-		warnx("%s: local modification time does not match remote",
-		      path);
+				warnx("%s: local modification time "
+				    "does not match remote", path);
 		goto failure_keep;
 	    }
 	    url->offset = 0;
@@ -394,10 +395,11 @@
 		      path, sb.st_size, us.size);
 		goto failure;
 	    }
-	    /* we got through, open local file and seek to offset */
+			/* we got it, open local file and seek to offset */
 	    /*
-	     * XXX there's a race condition here - the file we open is not
-	     * necessarily the same as the one we stat()'ed earlier...
+			 * XXX there's a race condition here - the
+			 * file we open is not necessarily the same as
+			 * the one we stat()'ed earlier...
 	     */
 	    if ((of = fopen(path, "a")) == NULL) {
 		warn("%s: fopen()", path);
@@ -416,9 +418,9 @@
     }
     if (!of) {
 	/*
-	 * We don't yet have an output file; either this is a vanilla
-	 * run with no special flags, or the local and remote files
-	 * didn't match.
+		 * We don't yet have an output file; either this is a
+		 * vanilla run with no special flags, or the local and
+		 * remote files didn't match.
 	 */
 	if ((of = fopen(path, "w")) == NULL) {
 	    warn("%s: open()", path);
@@ -453,10 +455,11 @@
 	}
 	if (timeout)
 	    alarm(0);
-	stat_update(&xs, count += size, 0);
+		stat_update(&xs, count += size);
 	for (ptr = buf; size > 0; ptr += wr, size -= wr)
 	    if ((wr = fwrite(ptr, 1, size, of)) < size) {
-		if (ferror(of) && errno == EINTR && !sigalrm && !sigint)
+				if (ferror(of) && errno == EINTR &&
+				    !sigalrm && !sigint)
 		    clearerr(of);
 		else
 		    break;
@@ -539,20 +542,19 @@
     return r;
 }
 
-void
+static void
 usage(void)
 {
-    fprintf(stderr,
-	    "Usage: fetch [-146AFMPRUadlmnpqrsv] [-o outputfile] [-S bytes]\n"
-	    "             [-B bytes] [-T seconds] [-w seconds]\n"
-	    "             [-h host -f file [-c dir] | URL ...]\n"
-	);
+	fprintf(stderr, "%s\n%s\n%s\n",
+	    "Usage: fetch [-146AFMPRUadlmnpqrsv] [-o outputfile] [-S bytes]",
+	    "             [-B bytes] [-T seconds] [-w seconds]",
+	    "             [-h host -f file [-c dir] | URL ...]");
 }
 
 
 #define PARSENUM(NAME, TYPE)		\
-int					\
-NAME(char *s, TYPE *v)			\
+static int						\
+NAME(const char *s, TYPE *v)				\
 {					\
     *v = 0;				\
     for (*v = 0; *s; s++)		\
@@ -572,7 +574,8 @@
 {
     struct stat sb;
     struct sigaction sa;
-    char *p, *q, *s;
+	const char *p, *s;
+	char *q;
     int c, e, r;
 
     while ((c = getopt(argc, argv,
@@ -614,7 +617,8 @@
 	    f_filename = optarg;
 	    break;
 	case 'H':
-	    warnx("The -H option is now implicit, use -U to disable\n");
+			warnx("The -H option is now implicit, "
+			    "use -U to disable");
 	    break;
 	case 'h':
 	    h_hostname = optarg;
@@ -629,7 +633,8 @@
 	case 'M':
 	case 'm':
 	    if (r_flag)
-		errx(1, "the -m and -r flags are mutually exclusive");
+				errx(1, "the -m and -r flags "
+				    "are mutually exclusive");
 	    m_flag = 1;
 	    break;
 	case 'n':
@@ -647,7 +652,8 @@
 	    break;
 	case 'r':
 	    if (m_flag)
-		errx(1, "the -m and -r flags are mutually exclusive");
+				errx(1, "the -m and -r flags "
+				    "are mutually exclusive");
 	    r_flag = 1;
 	    break;
 	case 'S':
@@ -739,7 +745,8 @@
 	} else if (stat(o_filename, &sb) == -1) {
 	    if (errno == ENOENT) {
 		if (argc > 1)
-		    errx(EX_USAGE, "%s is not a directory", o_filename);
+					errx(EX_USAGE, "%s is not a directory",
+					    o_filename);
 	    } else {
 		err(EX_IOERR, "%s", o_filename);
 	    }
@@ -796,12 +803,11 @@
 		 && fetchLastErrCode != FETCH_URL
 		 && fetchLastErrCode != FETCH_RESOLV
 		 && fetchLastErrCode != FETCH_UNKNOWN)) {
-		if (w_secs) {
-		    if (v_level)
-			fprintf(stderr, "Waiting %d seconds before retrying\n",
-				w_secs);
+				if (w_secs && v_level)
+					fprintf(stderr, "Waiting %d seconds "
+					    "before retrying\n", w_secs);
+				if (w_secs)
 		    sleep(w_secs);
-		}
 		if (a_flag)
 		    continue;
 	    }

--=-=-=--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzpu20nr0j0.fsf>