expand_number() for fetch'es -B and -S switches

Alexander Best arundel at freebsd.org
Thu Sep 2 11:46:55 UTC 2010


On Thu Sep  2 10, Dag-Erling Smørgrav wrote:
> Alexander Best <arundel at freebsd.org> writes:
> > since you're the originator of fetch(1): should i send you a patch to add
> > expand_numer() to the -B switch or do you think fetch is better off as it is
> > now without humanised numbers?
> 
> Sure, but we need to commit the expand_number() patch first.

so how about something like this? the fetch(1) manual would have to be changed
a bit to state that if '-B val' > 1G it silently gets set to 1G. however it
might be better to simply fail in that case, since a failure will occur
anyway, if val exceeds uint64_t (see expand_number(3)).

cheers.
alex

> 
> > i'm not sure, but i think fetch(1) is BSD specific so no POSIX regulations need
> > to be taken into consideration. but you probably know more about this matter.
> 
> fetch(1) is 100% home-grown.
> 
> DES
> -- 
> Dag-Erling Smørgrav - des at des.no

-- 
a13x
-------------- next part --------------
diff --git a/usr.bin/fetch/Makefile b/usr.bin/fetch/Makefile
index 6f0db80..a21ab50 100644
--- a/usr.bin/fetch/Makefile
+++ b/usr.bin/fetch/Makefile
@@ -4,8 +4,8 @@
 
 PROG=		fetch
 CSTD?=		c99
-DPADD=		${LIBFETCH} ${LIBMD}
-LDADD=		-lfetch -lmd
+DPADD=		${LIBFETCH} ${LIBMD} ${LIBUTIL}
+LDADD=		-lfetch -lmd -lutil
 .if ${MK_OPENSSL} != "no"
 DPADD+=		${LIBSSL} ${LIBCRYPTO}
 LDADD+=		-lssl -lcrypto
diff --git a/usr.bin/fetch/fetch.c b/usr.bin/fetch/fetch.c
index 7553bd8..99a82b0 100644
--- a/usr.bin/fetch/fetch.c
+++ b/usr.bin/fetch/fetch.c
@@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <libutil.h>
 #include <signal.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -48,6 +49,7 @@ __FBSDID("$FreeBSD$");
 #include <fetch.h>
 
 #define MINBUFSIZE	4096
+#define MAXBUFSIZE	1073741824
 #define TIMEOUT		120
 
 /* Option flags */
@@ -772,8 +774,9 @@ main(int argc, char *argv[])
 			a_flag = 1;
 			break;
 		case 'B':
-			B_size = (off_t)strtol(optarg, &end, 10);
-			if (*optarg == '\0' || *end != '\0')
+			errno = 0;
+			expand_number(optarg, &B_size);
+			if(errno)
 				errx(1, "invalid buffer size (%s)", optarg);
 			break;
 		case 'b':
@@ -898,6 +901,8 @@ main(int argc, char *argv[])
 	/* allocate buffer */
 	if (B_size < MINBUFSIZE)
 		B_size = MINBUFSIZE;
+	if (B_size > MAXBUFSIZE)
+		B_size = MAXBUFSIZE;
 	if ((buf = malloc(B_size)) == NULL)
 		errx(1, "%s", strerror(ENOMEM));
 


More information about the freebsd-hackers mailing list