DragonFly BSD
DragonFly submit List (threaded) for 2005-01
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Update Base: fetch


From: Jeandre du Toit <jeandre.dutoit@xxxxxxxxx>
Date: Thu, 6 Jan 2005 19:25:18 +0200

Hi here are the patches to upgrade fetch to the latest version of
FBSD. I wasn't sure if I should update the Makefile as well. I will
start work on libfetch now. Please let me know if there are any errors
or things I should do different or even if you have some tips for me
as I am still very new to sending in patches.

One thing I would like to know is the difference between (intmax_t)
and (long long). Should I have left the casts as (long long) because
the printf format stays %lld and not %jd like in FBSD?

Thanks
Jeandre du Toit


usr.bin/fetch/Makefile - updated to v1.17 of FBSD
=============================

--- Makefile.orig	2005-01-06 19:15:31.000000000 +0200
+++ Makefile	2005-01-06 19:15:34.000000000 +0200
@@ -2,12 +2,13 @@
 # $DragonFly: src/usr.bin/fetch/Makefile,v 1.5 2005/01/04 23:08:13 dillon Exp $
 
 PROG=		fetch
+CSTD?=		c99
 WARNS?=		6
 DPADD=		${LIBFETCH}
 LDADD=		-lfetch
-.if !defined(NOCRYPT) && !defined(NO_OPENSSL)
-DPADD+=		${LIBCRYPTO} ${LIBSSL}
-LDADD+=		-lcrypto -lssl
+.if !defined(NO_CRYPT) && !defined(NO_OPENSSL)
+DPADD+=		${LIBSSL} ${LIBCRYPTO}
+LDADD+=		-lssl -lcrypto
 .endif
 
 .include <bsd.prog.mk>



usr.bin/fetch/fetch.c - updated to v1.75 of FBSD
============================

--- fetch.c.orig	2005-01-06 18:28:20.000000000 +0200
+++ fetch.c	2005-01-06 18:59:22.000000000 +0200
@@ -38,6 +38,7 @@
 #include <err.h>
 #include <errno.h>
 #include <signal.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -88,7 +89,7 @@
 
 long	 ftp_timeout;	/* default timeout for FTP transfers */
 long	 http_timeout;	/* default timeout for HTTP transfers */
-u_char	*buf;		/* transfer buffer */
+char	*buf;		/* transfer buffer */
 
 
 /*
@@ -111,7 +112,7 @@
 }
 
 struct xferstat {
-	char		 name[40];
+	char		 name[64];
 	struct timeval	 start;
 	struct timeval	 last;
 	off_t		 size;
@@ -122,43 +123,62 @@
 /*
  * Compute and display ETA
  */
-static void
+static const char *
 stat_eta(struct xferstat *xs)
 {
-	long elapsed, received, expected, eta;
+	static char str[16];
+	long elapsed, eta;
+	off_t received, expected;
 
 	elapsed = xs->last.tv_sec - xs->start.tv_sec;
 	received = xs->rcvd - xs->offset;
 	expected = xs->size - xs->rcvd;
 	eta = (long)((double)elapsed * expected / received);
-	if (eta > 3600) {
-		fprintf(stderr, "%02ld:", eta / 3600);
-		eta %= 3600;
+	if (eta > 3600)
+		snprintf(str, sizeof str, "%02ldh%02ldm",
+		    eta / 3600, (eta % 3600) / 60);
+	else
+		snprintf(str, sizeof str, "%02ldm%02lds",
+		    eta / 60, eta % 60);
+	return (str);
+}
+
+/*
+ * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
+ */
+static const char *prefixes = " kMGTP";
+static const char *
+stat_bytes(off_t bytes)
+{
+	static char str[16];
+	const char *prefix = prefixes;
+
+	while (bytes > 9999 && prefix[1] != '\0') {
+		bytes /= 1024;
+		prefix++;
 	}
-	fprintf(stderr, "%02ld:%02ld", eta / 60, eta % 60);
+	snprintf(str, sizeof str, "%4lld %cB", (intmax_t)bytes, *prefix);
+	return (str);
 }
 
 /*
  * Compute and display transfer rate
  */
-static void
+static const char *
 stat_bps(struct xferstat *xs)
 {
+	static char str[16];
 	double delta, bps;
 
 	delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
 	    - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
 	if (delta == 0.0) {
-		fprintf(stderr, "?? Bps");
-		return;
+		snprintf(str, sizeof str, "?? Bps");
+	} else {
+		bps = (xs->rcvd - xs->offset) / delta;
+		snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
 	}
-	bps = (xs->rcvd - xs->offset) / delta;
-	if (bps > 1024*1024)
-		fprintf(stderr, "%.2f MBps", bps / (1024*1024));
-	else if (bps > 1024)
-		fprintf(stderr, "%.2f kBps", bps / 1024);
-	else
-		fprintf(stderr, "%.2f Bps", bps);
+	return (str);
 }
 
 /*
@@ -170,9 +190,6 @@
 	struct timeval now;
 	int ctty_pgrp;
 
-	if (!v_tty || !v_level)
-		return;
-
 	/* check if we're the foreground process */
 	if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
 	    (pid_t)ctty_pgrp != pgrp)
@@ -183,22 +200,18 @@
 		return;
 	xs->last = now;
 
-	fprintf(stderr, "\rReceiving %s", xs->name);
+	fprintf(stderr, "\r%-46.46s", xs->name);
 	if (xs->size <= 0) {
-		fprintf(stderr, ": %lld bytes", (long long)xs->rcvd);
+		fprintf(stderr, "        %s", stat_bytes(xs->rcvd));
 	} else {
-		fprintf(stderr, " (%lld bytes): %d%%", (long long)xs->size,
-		    (int)((100.0 * xs->rcvd) / xs->size));
-		if (xs->rcvd > 0 && xs->last.tv_sec >= xs->start.tv_sec + 30) {
-			fprintf(stderr, " (ETA ");
-			stat_eta(xs);
-			if (v_level > 1) {
-				fprintf(stderr, " at ");
-				stat_bps(xs);
-			}
-			fprintf(stderr, ")  ");
-		}
-	}
+		fprintf(stderr, "%3d%% of %s",
+		    (int)((100.0 * xs->rcvd) / xs->size),
+		    stat_bytes(xs->size));
+	}
+	fprintf(stderr, " %s", stat_bps(xs));
+	if (xs->size > 0 && xs->rcvd > 0 &&
+	    xs->last.tv_sec >= xs->start.tv_sec + 10)
+		fprintf(stderr, " %s", stat_eta(xs));
 }
 
 /*
@@ -213,7 +226,10 @@
 	xs->size = size;
 	xs->offset = offset;
 	xs->rcvd = offset;
-	stat_display(xs, 1);
+	if (v_tty && v_level > 0)
+		stat_display(xs, 1);
+	else if (v_level > 0)
+		fprintf(stderr, "%-46s", xs->name);
 }
 
 /*
@@ -223,7 +239,8 @@
 stat_update(struct xferstat *xs, off_t rcvd)
 {
 	xs->rcvd = rcvd;
-	stat_display(xs, 0);
+	if (v_tty && v_level > 0)
+		stat_display(xs, 0);
 }
 
 /*
@@ -232,21 +249,14 @@
 static void
 stat_end(struct xferstat *xs)
 {
-	double delta;
-
-	if (!v_level)
-		return;
-
 	gettimeofday(&xs->last, NULL);
-
-	stat_display(xs, 1);
-	fputc('\n', stderr);
-	delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
-	    - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
-	fprintf(stderr, "%lld bytes transferred in %.1f seconds (",
-	    (long long)(xs->rcvd - xs->offset), delta);
-	stat_bps(xs);
-	fprintf(stderr, ")\n");
+	if (v_tty && v_level > 0) {
+		stat_display(xs, 1);
+		putc('\n', stderr);
+	} else if (v_level > 0) {
+		fprintf(stderr, "        %s %s\n",
+		    stat_bytes(xs->size), stat_bps(xs));
+	}
 }
 
 /*
@@ -259,15 +269,14 @@
 	tcflag_t saved_flags;
 	int i, nopwd;
 
-
 	fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
 	    URL->scheme, URL->host, URL->port);
 
 	fprintf(stderr, "Login: ");
 	if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
-		return -1;
-	for (i = 0; URL->user[i]; ++i)
-		if (isspace(URL->user[i]))
+		return (-1);
+	for (i = strlen(URL->user); i >= 0; --i)
+		if (URL->user[i] == '\r' || URL->user[i] == '\n')
 			URL->user[i] = '\0';
 
 	fprintf(stderr, "Password: ");
@@ -283,12 +292,12 @@
 		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
 	}
 	if (nopwd)
-		return -1;
-
-	for (i = 0; URL->pwd[i]; ++i)
-		if (isspace(URL->pwd[i]))
+		return (-1);
+	for (i = strlen(URL->pwd); i >= 0; --i)
+		if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
 			URL->pwd[i] = '\0';
-	return 0;
+
+	return (0);
 }
 
 /*
@@ -308,8 +317,8 @@
 	const char *slash;
 	char *tmppath;
 	int r;
-	u_int timeout;
-	u_char *ptr;
+	unsigned timeout;
+	char *ptr;
 
 	f = of = NULL;
 	tmppath = NULL;
@@ -331,8 +340,8 @@
 	}
 
 	/* if no scheme was specified, take a guess */
-	if (*url->scheme == 0) {
-		if (*url->host == 0)
+	if (!*url->scheme) {
+		if (!*url->host)
 			strcpy(url->scheme, SCHEME_FILE);
 		else if (strncasecmp(url->host, "ftp.", 4) == 0)
 			strcpy(url->scheme, SCHEME_FTP);
@@ -389,7 +398,7 @@
 		if (us.size == -1)
 			printf("Unknown\n");
 		else
-			printf("%lld\n", (long long)us.size);
+			printf("%lld\n", (intmax_t)us.size);
 		goto success;
 	}
 
@@ -407,12 +416,23 @@
 	 * the connection later if we change our minds.
 	 */
 	sb.st_size = -1;
-	if (!o_stdout && stat(path, &sb) == -1 && errno != ENOENT) {
-		warnx("%s: stat()", path);
-		goto failure;
+	if (!o_stdout) {
+		r = stat(path, &sb);
+		if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
+			url->offset = sb.st_size;
+		} else {
+			/*
+			 * Whatever value sb.st_size has now is either
+			 * wrong (if stat(2) failed) or irrelevant (if the
+			 * path does not refer to a regular file)
+			 */
+			sb.st_size = -1;
+			if (r == -1 && errno != ENOENT) {
+				warnx("%s: stat()", path);
+				goto failure;
+			}
+		}
 	}
-	if (!o_stdout && r_flag && S_ISREG(sb.st_mode))
-		url->offset = sb.st_size;
 
 	/* start the transfer */
 	if (timeout)
@@ -433,10 +453,9 @@
 	if (S_size) {
 		if (us.size == -1) {
 			warnx("%s: size unknown", URL);
-			goto failure;
 		} else if (us.size != S_size) {
 			warnx("%s: size mismatch: expected %lld, actual %lld",
-			    URL, (long long)S_size, (long long)us.size);
+			    URL, (intmax_t)S_size, (intmax_t)us.size);
 			goto failure;
 		}
 	}
@@ -455,10 +474,10 @@
 	if (v_level > 1) {
 		if (sb.st_size != -1)
 			fprintf(stderr, "local size / mtime: %lld / %ld\n",
-			    (long long)sb.st_size, (long)sb.st_mtime);
+			    (intmax_t)sb.st_size, (long)sb.st_mtime);
 		if (us.size != -1)
 			fprintf(stderr, "remote size / mtime: %lld / %ld\n",
-			    (long long)us.size, (long)us.mtime);
+			    (intmax_t)us.size, (long)us.mtime);
 	}
 
 	/* open output file */
@@ -476,7 +495,7 @@
 				    "does not match remote", path);
 				goto failure_keep;
 			}
-		} else {
+		} else if (us.size != -1) {
 			if (us.size == sb.st_size)
 				/* nothing to do */
 				goto success;
@@ -484,7 +503,7 @@
 				/* local file too long! */
 				warnx("%s: local file (%lld bytes) is longer "
 				    "than remote file (%lld bytes)", path,
-				    (long long)sb.st_size, (long long)us.size);
+				    (intmax_t)sb.st_size, (intmax_t)us.size);
 				goto failure;
 			}
 			/* we got it, open local file */
@@ -544,18 +563,14 @@
 			asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
 			    (int)(slash - path), path, slash);
 			if (tmppath != NULL) {
-				if (mkstemps(tmppath, strlen(slash)+1) == -1) {
-					warn("%s: mkstemps()", path);
-					goto failure;
-				}
-
+				mkstemps(tmppath, strlen(slash) + 1);
 				of = fopen(tmppath, "w");
 			}
 		}
-
 		if (of == NULL)
-			if ((of = fopen(path, "w")) == NULL) {
-				warn("%s: fopen()", path);
+			of = fopen(path, "w");
+		if (of == NULL) {
+			warn("%s: open()", path);
 			goto failure;
 		}
 	}
@@ -645,7 +660,7 @@
 	/* did the transfer complete normally? */
 	if (us.size != -1 && count < us.size) {
 		warnx("%s appears to be truncated: %lld/%lld bytes",
-		    path, (long long)count, (long long)us.size);
+		    path, (intmax_t)count, (intmax_t)us.size);
 		goto failure_keep;
 	}
 
@@ -683,7 +698,7 @@
 		fetchFreeURL(url);
 	if (tmppath != NULL)
 		free(tmppath);
-	return r;
+	return (r);
 }
 
 static void



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