From 661782c919727ee927e35dc059d0dd27d091d326 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sun, 7 Jun 1998 21:02:38 +0000 Subject: More from Gunter. svn path=/trunk/; revision=1925 --- options.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'options.c') diff --git a/options.c b/options.c index ce0f1f68..d18008ad 100644 --- a/options.c +++ b/options.c @@ -13,6 +13,8 @@ #if defined(STDC_HEADERS) #include #include +#else +#include #endif #include "getopt.h" @@ -159,7 +161,39 @@ static int xatoi(char *s, int *errflagptr) return (int) value; /* shut up, I know what I'm doing */ #else - return atoi(s); + int i; + char *dp; +# if defined (STDC_HEADERS) + size_t len; +# else + int len; +# endif + + /* We do only base 10 conversions here (atoi)! */ + + len = strlen(s); + /* check for leading white spaces */ + for (i = 0; (i < len) && isspace(s[i]); i++) + ; + + dp = &s[i]; + + /* check for +/- */ + if (i < len && (s[i] == '+' || s[i] == '-')) i++; + + /* skip over digits */ + for ( /* no init */ ; (i < len) && isdigit(s[i]); i++) + ; + + /* check for trailing garbage */ + if (i != len) { + (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s); + (*errflagptr)++; + return 0; + } + + /* atoi should be safe by now, except for number range over/underflow */ + return atoi(dp); #endif } -- cgit v1.2.3