From 1385899416a4396385ad421ae1f532be7103738a Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sun, 8 Oct 2006 12:49:22 +0000 Subject: attempt to regularize atoi mess. --- networking/arping.c | 8 +++--- networking/ftpgetput.c | 14 ++++----- networking/httpd.c | 2 +- networking/ifconfig.c | 3 +- networking/inetd.c | 29 +++++++------------ networking/interface.c | 2 +- networking/ipcalc.c | 48 ++++++++++++------------------- networking/nc.c | 22 +++++++------- networking/ping.c | 16 +++++------ networking/ping6.c | 16 +++++------ networking/route.c | 14 ++++----- networking/telnetd.c | 4 +-- networking/tftp.c | 4 +-- networking/traceroute.c | 74 ++++++++++++++---------------------------------- networking/udhcp/dhcpc.c | 4 +-- networking/udhcp/files.c | 8 ++---- networking/vconfig.c | 10 +++---- networking/wget.c | 10 +++---- 18 files changed, 119 insertions(+), 169 deletions(-) (limited to 'networking') diff --git a/networking/arping.c b/networking/arping.c index 1ff6f90be..b9605985c 100644 --- a/networking/arping.c +++ b/networking/arping.c @@ -41,8 +41,8 @@ enum cfg_e { static int cfg; static int s; -static int count = -1; -static int timeout; +static unsigned count = UINT_MAX; +static unsigned timeout; static int sent; static int brd_sent; static int received; @@ -276,9 +276,9 @@ int arping_main(int argc, char **argv) &_count, &_timeout, &device, &source); cfg |= opt & 0x3f; /* set respective flags */ if (opt & 0x40) /* -c: count */ - count = atoi(_count); + count = xatou(_count); if (opt & 0x80) /* -w: timeout */ - timeout = atoi(_timeout); + timeout = xatoul_range(_timeout, 0, INT_MAX/2000); if (opt & 0x100) { /* -i: interface */ if (strlen(device) > IF_NAMESIZE) { bb_error_msg_and_die("interface name '%s' is too long", diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index 492854153..902528f93 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c @@ -42,15 +42,15 @@ static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf) char *buf_ptr; if (fgets(buf, 510, stream) == NULL) { - bb_perror_msg_and_die("fgets()"); + bb_perror_msg_and_die("fgets"); } buf_ptr = strstr(buf, "\r\n"); if (buf_ptr) { *buf_ptr = '\0'; } - } while (! isdigit(buf[0]) || buf[3] != ' '); + } while (!isdigit(buf[0]) || buf[3] != ' '); - return atoi(buf); + return xatou(buf); } static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf) @@ -60,14 +60,14 @@ static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf) buf_ptr = strrchr(buf, ','); *buf_ptr = '\0'; - port_num = atoi(buf_ptr + 1); + port_num = xatoul_range(buf_ptr + 1, 0, 255); buf_ptr = strrchr(buf, ','); *buf_ptr = '\0'; - port_num += atoi(buf_ptr + 1) * 256; + port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256; - server->s_in->sin_port=htons(port_num); - return(xconnect(server->s_in)); + server->s_in->sin_port = htons(port_num); + return xconnect(server->s_in); } static FILE *ftp_login(ftp_host_info_t *server) diff --git a/networking/httpd.c b/networking/httpd.c index 0e471ba58..f3fe49cae 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -1951,7 +1951,7 @@ int httpd_main(int argc, char *argv[]) #endif #if ENABLE_FEATURE_HTTPD_WITHOUT_INETD if (opt & OPT_PORT) - config->port = bb_xgetlarg(s_port, 10, 1, 0xffff); + config->port = xatou16(s_port); #if ENABLE_FEATURE_HTTPD_SETUID if (opt & OPT_SETUID) { char *e; diff --git a/networking/ifconfig.c b/networking/ifconfig.c index 4d346c47f..59b6f0acc 100644 --- a/networking/ifconfig.c +++ b/networking/ifconfig.c @@ -379,7 +379,8 @@ int ifconfig_main(int argc, char **argv) safe_strncpy(host, *argv, (sizeof host)); #ifdef CONFIG_FEATURE_IPV6 - if ((prefix = strchr(host, '/'))) { + prefix = strchr(host, '/'); + if (prefix) { if (safe_strtoi(prefix + 1, &prefix_len) || (prefix_len < 0) || (prefix_len > 128)) { diff --git a/networking/inetd.c b/networking/inetd.c index e22115a5c..966425385 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -149,8 +149,6 @@ #define _PATH_INETDPID "/var/run/inetd.pid" -#define TOOMANY 0 /* don't start more than TOOMANY */ - #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ #define RETRYTIME (60*10) /* retry after bind or server fail */ @@ -297,7 +295,7 @@ static const struct builtin builtins[] = { static int global_queuelen = 128; static int nsock, maxsock; static fd_set allsock; -static int toomany = TOOMANY; +static int toomany; static int timingout; static struct servent *sp; static uid_t uid; @@ -588,10 +586,10 @@ static servtab_t *getconfigent(void) sep = new_servtab(); /* memset(sep, 0, sizeof *sep); */ -more: + more: /* freeconfig(sep); */ - while ((cp = nextline()) && *cp == '#'); + while ((cp = nextline()) && *cp == '#') /* skip comment line */; if (cp == NULL) { /* free(sep); */ return NULL; @@ -680,7 +678,7 @@ more: } else if (*ccp != '\0') goto badafterall; #else - bb_error_msg("%s: rpc services not supported", sep->se_service); + bb_error_msg("%s: rpc services not supported", sep->se_service); #endif } } @@ -692,7 +690,7 @@ more: char *s = strchr(arg, '.'); if (s) { *s++ = '\0'; - sep->se_max = atoi(s); + sep->se_max = xatoi(s); } else sep->se_max = toomany; } @@ -928,7 +926,7 @@ static void config(int sig ATTRIBUTE_UNUSED) */ if ( #ifdef INETD_FEATURE_ENABLED - cp->se_bi == 0 && + cp->se_bi == 0 && #endif (sep->se_wait == 1 || cp->se_wait == 0)) sep->se_wait = cp->se_wait; @@ -974,7 +972,7 @@ static void config(int sig ATTRIBUTE_UNUSED) #ifdef CONFIG_FEATURE_INETD_RPC if (isrpcservice(sep)) { struct rpcent *rp; - + // FIXME: atoi_or_else(str, 0) would be handy here sep->se_rpcprog = atoi(sep->se_service); if (sep->se_rpcprog == 0) { rp = getrpcbyname(sep->se_service); @@ -990,9 +988,9 @@ static void config(int sig ATTRIBUTE_UNUSED) register_rpc(sep); } else #endif - { + { u_short port = htons(atoi(sep->se_service)); - + // FIXME: atoi_or_else(str, 0) would be handy here if (!port) { /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname)); if (isdigit(protoname[strlen(protoname) - 1])) @@ -1255,13 +1253,7 @@ inetd_main(int argc, char *argv[]) opt = getopt32(argc, argv, "R:f", &stoomany); if(opt & 1) { - char *e; - - toomany = strtoul(stoomany, &e, 0); - if (!(toomany >= 0 && *e == '\0')) { - toomany = TOOMANY; - bb_perror_msg("-R %s: bad value for service invocation rate", stoomany); - } + toomany = xatoi_u(stoomany); } argc -= optind; argv += optind; @@ -1317,7 +1309,6 @@ inetd_main(int argc, char *argv[]) sigaddset(&sa.sa_mask, SIGHUP); sa.sa_handler = retry; sigaction(SIGALRM, &sa, NULL); - /* doconfig(); */ config(SIGHUP); sa.sa_handler = config; sigaction(SIGHUP, &sa, NULL); diff --git a/networking/interface.c b/networking/interface.c index c3cc7c0bc..8e2498d27 100644 --- a/networking/interface.c +++ b/networking/interface.c @@ -363,7 +363,7 @@ static int nstrcmp(const char *a, const char *b) } if (isdigit(*a) && isdigit(*b)) { - return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1; + return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1; } return *a - *b; } diff --git a/networking/ipcalc.c b/networking/ipcalc.c index 576dfc853..909373cbb 100644 --- a/networking/ipcalc.c +++ b/networking/ipcalc.c @@ -18,8 +18,6 @@ #include #include -#define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;} - #define CLASS_A_NETMASK ntohl(0xFF000000) #define CLASS_B_NETMASK ntohl(0xFFFF0000) #define CLASS_C_NETMASK ntohl(0xFFFFFF00) @@ -56,6 +54,7 @@ static int get_prefix(unsigned long netmask) int get_prefix(unsigned long netmask); #endif + #define NETMASK 0x01 #define BROADCAST 0x02 #define NETWORK 0x04 @@ -78,12 +77,9 @@ int get_prefix(unsigned long netmask); #else #define long_options 0 #endif - - - int ipcalc_main(int argc, char **argv) { - unsigned long mode; + unsigned opt; int have_netmask = 0; in_addr_t netmask, broadcast, network, ipaddr; struct in_addr a; @@ -92,17 +88,18 @@ int ipcalc_main(int argc, char **argv) if (ENABLE_FEATURE_IPCALC_LONG_OPTIONS) applet_long_options = long_options; - mode = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs")); - + opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs")); argc -= optind; argv += optind; - if (mode & (BROADCAST | NETWORK | NETPREFIX)) { + if (opt & (BROADCAST | NETWORK | NETPREFIX)) { if (argc > 2 || argc <= 0) bb_show_usage(); } else { if (argc != 1) bb_show_usage(); } + if (opt & SILENT) + logmode = LOGMODE_NONE; /* Suppress error_msg() output */ ipstr = argv[0]; if (ENABLE_FEATURE_IPCALC_FANCY) { @@ -111,17 +108,13 @@ int ipcalc_main(int argc, char **argv) prefixstr = ipstr; - while(*prefixstr) { + while (*prefixstr) { if (*prefixstr == '/') { *prefixstr = (char)0; prefixstr++; if (*prefixstr) { - unsigned int msk; - - if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) { - IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s", prefixstr), - exit(EXIT_FAILURE)); - } + unsigned msk; + netprefix = xatoul_range(prefixstr, 0, 32); netmask = 0; msk = 0x80000000; while (netprefix > 0) { @@ -142,21 +135,18 @@ int ipcalc_main(int argc, char **argv) ipaddr = inet_aton(ipstr, &a); if (ipaddr == 0) { - IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]), - exit(EXIT_FAILURE)); + bb_error_msg_and_die("bad IP address: %s", argv[0]); } ipaddr = a.s_addr; if (argc == 2) { if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { - IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both"), - exit(EXIT_FAILURE)); + bb_error_msg_and_die("use prefix or netmask, not both"); } netmask = inet_aton(argv[1], &a); if (netmask == 0) { - IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]), - exit(EXIT_FAILURE)); + bb_error_msg_and_die("bad netmask: %s", argv[1]); } netmask = a.s_addr; } else { @@ -166,34 +156,32 @@ int ipcalc_main(int argc, char **argv) netmask = get_netmask(ipaddr); } - if (mode & NETMASK) { + if (opt & NETMASK) { printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); } - if (mode & BROADCAST) { + if (opt & BROADCAST) { broadcast = (ipaddr & netmask) | ~netmask; printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); } - if (mode & NETWORK) { + if (opt & NETWORK) { network = ipaddr & netmask; printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); } if (ENABLE_FEATURE_IPCALC_FANCY) { - if (mode & NETPREFIX) { + if (opt & NETPREFIX) { printf("PREFIX=%i\n", get_prefix(netmask)); } - if (mode & HOSTNAME) { + if (opt & HOSTNAME) { struct hostent *hostinfo; int x; hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); if (!hostinfo) { - IPCALC_MSG(bb_herror_msg_and_die( - "cannot find hostname for %s", argv[0]),); - exit(EXIT_FAILURE); + bb_herror_msg_and_die("cannot find hostname for %s", argv[0]); } for (x = 0; hostinfo->h_name[x]; x++) { hostinfo->h_name[x] = tolower(hostinfo->h_name[x]); diff --git a/networking/nc.c b/networking/nc.c index f8b3fb2dd..bde5e6600 100644 --- a/networking/nc.c +++ b/networking/nc.c @@ -11,13 +11,15 @@ static void timeout(int signum) { - bb_error_msg_and_die("Timed out"); + bb_error_msg_and_die("timed out"); } int nc_main(int argc, char **argv) { - int do_listen = 0, lport = 0, delay = 0, wsecs = 0, execflag = 0, opt, - sfd = 0, cfd; + int sfd = 0, cfd; + unsigned opt; + unsigned lport = 0, wsecs = 0, delay = 0; + unsigned do_listen = 0, execflag = 0; struct sockaddr_in address; struct hostent *hostinfo; fd_set readfds, testfds; @@ -30,8 +32,8 @@ int nc_main(int argc, char **argv) if (ENABLE_NC_SERVER && opt=='l') do_listen++; else if (ENABLE_NC_SERVER && opt=='p') lport = bb_lookup_port(optarg, "tcp", 0); - else if (ENABLE_NC_EXTRA && opt=='w') wsecs = atoi(optarg); - else if (ENABLE_NC_EXTRA && opt=='i') delay = atoi(optarg); + else if (ENABLE_NC_EXTRA && opt=='w') wsecs = xatou(optarg); + else if (ENABLE_NC_EXTRA && opt=='i') delay = xatou(optarg); else if (ENABLE_NC_EXTRA && opt=='f') infile = optarg; else if (ENABLE_NC_EXTRA && opt=='e' && optind!=argc) { execflag++; @@ -40,11 +42,10 @@ int nc_main(int argc, char **argv) } } - // For listen or file we need zero arguments, dialout is 2. // For exec we need at least one more argument at the end, more ok - opt = (do_listen || infile) ? 0 : 2 + execflag; + opt = (do_listen || infile) ? 0 : 2 + execflag; if (execflag ? argc-optind < opt : argc-optind!=opt || (infile && do_listen)) bb_show_usage(); @@ -66,7 +67,6 @@ int nc_main(int argc, char **argv) if (lport != 0) { address.sin_port = lport; - xbind(sfd, (struct sockaddr *) &address, sizeof(address)); } @@ -83,7 +83,8 @@ int nc_main(int argc, char **argv) fdprintf(2, "%d\n", SWAP_BE16(address.sin_port)); } repeatyness: - if ((cfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0) + cfd = accept(sfd, (struct sockaddr *) &address, &addrlen); + if (cfd < 0) bb_perror_msg_and_die("accept"); if (!execflag) close(sfd); @@ -116,10 +117,11 @@ repeatyness: // With more than one -l, repeatedly act as server. - if (do_listen>1 && vfork()) { + if (do_listen > 1 && vfork()) { // This is a bit weird as cleanup goes, since we wind up with no // stdin/stdout/stderr. But it's small and shouldn't hurt anything. // We check for cfd == 0 above. + logmode = LOGMODE_NONE; close(0); close(1); close(2); diff --git a/networking/ping.c b/networking/ping.c index a81472f96..8ca8be9b9 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -157,9 +157,9 @@ int ping_main(int argc, char **argv) static struct sockaddr_in pingaddr; static struct sockaddr_in sourceaddr; static int pingsock = -1; -static int datalen; /* intentionally uninitialized to work around gcc bug */ +static unsigned datalen; /* intentionally uninitialized to work around gcc bug */ -static long ntransmitted, nreceived, nrepeats, pingcount; +static unsigned long ntransmitted, nreceived, nrepeats, pingcount; static int myid, options; static unsigned long tmin = ULONG_MAX, tmax, tsum; static char rcvd_tbl[MAX_DUP_CHK / 8]; @@ -179,12 +179,12 @@ static void pingstats(int junk) signal(SIGINT, SIG_IGN); printf("\n--- %s ping statistics ---\n", hostent->h_name); - printf("%ld packets transmitted, ", ntransmitted); - printf("%ld packets received, ", nreceived); + printf("%lu packets transmitted, ", ntransmitted); + printf("%lu packets received, ", nreceived); if (nrepeats) - printf("%ld duplicates, ", nrepeats); + printf("%lu duplicates, ", nrepeats); if (ntransmitted) - printf("%ld%% packet loss\n", + printf("%lu%% packet loss\n", (ntransmitted - nreceived) * 100 / ntransmitted); if (nreceived) printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", @@ -427,13 +427,13 @@ int ping_main(int argc, char **argv) if (--argc <= 0) bb_show_usage(); argv++; - pingcount = atoi(*argv); + pingcount = xatoul(*argv); break; case 's': if (--argc <= 0) bb_show_usage(); argv++; - datalen = atoi(*argv); + datalen = xatou16(*argv); break; case 'I': if (--argc <= 0) diff --git a/networking/ping6.c b/networking/ping6.c index 6079c40d9..0d6a739bf 100644 --- a/networking/ping6.c +++ b/networking/ping6.c @@ -145,10 +145,10 @@ int ping6_main(int argc, char **argv) /* full(er) version */ static struct sockaddr_in6 pingaddr; static int pingsock = -1; -static int datalen; /* intentionally uninitialized to work around gcc bug */ +static unsigned datalen; /* intentionally uninitialized to work around gcc bug */ static int if_index; -static long ntransmitted, nreceived, nrepeats, pingcount; +static unsigned long ntransmitted, nreceived, nrepeats, pingcount; static int myid, options; static unsigned long tmin = ULONG_MAX, tmax, tsum; static char rcvd_tbl[MAX_DUP_CHK / 8]; @@ -168,12 +168,12 @@ static void pingstats(int junk) signal(SIGINT, SIG_IGN); printf("\n--- %s ping statistics ---\n", hostent->h_name); - printf("%ld packets transmitted, ", ntransmitted); - printf("%ld packets received, ", nreceived); + printf("%lu packets transmitted, ", ntransmitted); + printf("%lu packets received, ", nreceived); if (nrepeats) - printf("%ld duplicates, ", nrepeats); + printf("%lu duplicates, ", nrepeats); if (ntransmitted) - printf("%ld%% packet loss\n", + printf("%lu%% packet loss\n", (ntransmitted - nreceived) * 100 / ntransmitted); if (nreceived) printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", @@ -439,13 +439,13 @@ int ping6_main(int argc, char **argv) if (--argc <= 0) bb_show_usage(); argv++; - pingcount = atoi(*argv); + pingcount = xatoul(*argv); break; case 's': if (--argc <= 0) bb_show_usage(); argv++; - datalen = atoi(*argv); + datalen = xatou16(*argv); break; case 'I': if (--argc <= 0) diff --git a/networking/route.c b/networking/route.c index 2e6e017b6..65b40fcd6 100644 --- a/networking/route.c +++ b/networking/route.c @@ -178,7 +178,7 @@ static void INET_setroute(int action, char **args) if(prefix) { int prefix_len; - prefix_len = bb_xgetularg10_bnd(prefix+1, 0, 32); + prefix_len = xatoul_range(prefix+1, 0, 32); mask_in_addr(rt) = htonl( ~ (0xffffffffUL >> prefix_len)); *prefix = '\0'; #if HAVE_NEW_ADDRT @@ -218,7 +218,7 @@ static void INET_setroute(int action, char **args) #if HAVE_NEW_ADDRT if (k == KW_IPVx_METRIC) { - rt.rt_metric = bb_xgetularg10(args_m1) + 1; + rt.rt_metric = xatoul(args_m1) + 1; continue; } #endif @@ -259,20 +259,20 @@ static void INET_setroute(int action, char **args) if (k == KW_IPVx_MSS) { /* Check valid MSS bounds. */ rt.rt_flags |= RTF_MSS; - rt.rt_mss = bb_xgetularg10_bnd(args_m1, 64, 32768); + rt.rt_mss = xatoul_range(args_m1, 64, 32768); continue; } if (k == KW_IPVx_WINDOW) { /* Check valid window bounds. */ rt.rt_flags |= RTF_WINDOW; - rt.rt_window = bb_xgetularg10_bnd(args_m1, 128, INT_MAX); + rt.rt_window = xatoul_range(args_m1, 128, INT_MAX); continue; } #ifdef RTF_IRTT if (k == KW_IPVx_IRTT) { rt.rt_flags |= RTF_IRTT; - rt.rt_irtt = bb_xgetularg10(args_m1); + rt.rt_irtt = xatoul(args_m1); rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */ #if 0 /* FIXME: do we need to check anything of this? */ if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) { @@ -353,7 +353,7 @@ static void INET6_setroute(int action, char **args) char *cp; if ((cp = strchr(target, '/'))) { /* Yes... const to non is ok. */ *cp = 0; - prefix_len = bb_xgetularg10_bnd(cp+1, 0, 128); + prefix_len = xatoul_range(cp+1, 0, 128); } else { prefix_len = 128; } @@ -384,7 +384,7 @@ static void INET6_setroute(int action, char **args) } if (k == KW_IPVx_METRIC) { - rt.rtmsg_metric = bb_xgetularg10(args_m1); + rt.rtmsg_metric = xatoul(args_m1); continue; } diff --git a/networking/telnetd.c b/networking/telnetd.c index 1c4dede39..c6789e19c 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -369,7 +369,7 @@ telnetd_main(int argc, char **argv) sockaddr_type sa; int master_fd; int on = 1; - int portnbr = 23; + unsigned portnbr = 23; struct in_addr bind_addr = { .s_addr = 0x0 }; char *opt_portnbr, *opt_bindaddr; #endif /* CONFIG_FEATURE_TELNETD_INETD */ @@ -393,7 +393,7 @@ telnetd_main(int argc, char **argv) //if (opt & 1) // -f //if (opt & 2) // -l #ifndef CONFIG_FEATURE_TELNETD_INETD - if (opt & 4) portnbr = atoi(opt_portnbr); // -p + if (opt & 4) portnbr = xatou16(opt_portnbr); // -p if (opt & 8) // -b if (inet_aton(opt_bindaddr, &bind_addr) == 0) bb_show_usage(); #endif /* CONFIG_FEATURE_TELNETD_INETD */ diff --git a/networking/tftp.c b/networking/tftp.c index bfe94aca2..6213d6622 100644 --- a/networking/tftp.c +++ b/networking/tftp.c @@ -373,7 +373,7 @@ static int tftp(const int cmd, const struct hostent *host, res = tftp_option_get(&buf[2], len - 2, OPTION_BLOCKSIZE); if (res) { - int blksize = atoi(res); + int blksize = xatoi_u(res); if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) { @@ -516,7 +516,7 @@ int tftp_main(int argc, char **argv) #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE if (sblocksize) { - blocksize = atoi(sblocksize); + blocksize = xatoi_u(sblocksize); if (!tftp_blocksize_check(blocksize, 0)) { return EXIT_FAILURE; } diff --git a/networking/traceroute.c b/networking/traceroute.c index 4af523625..84ce8ae69 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -416,7 +416,7 @@ ifaddrlist(struct IFADDRLIST **ipaddrp) ++al; ++nipaddr; } - if(nipaddr == 0) + if (nipaddr == 0) bb_error_msg_and_die ("Can't find any network interfaces"); (void)close(fd); @@ -494,34 +494,6 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from) */ -/* String to value with optional min and max. Handles decimal and hex. */ -static int -str2val(const char *str, const char *what, int mi, int ma) -{ - const char *cp; - int val; - char *ep; - - if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { - cp = str + 2; - val = (int)strtol(cp, &ep, 16); - } else - val = (int)strtol(str, &ep, 10); - if (*ep != '\0') { - bb_error_msg_and_die("\"%s\" bad value for %s", str, what); - } - if (val < mi && mi >= 0) { - if (mi == 0) - bb_error_msg_and_die("%s must be >= %d", what, mi); - else - bb_error_msg_and_die("%s must be > %d", what, mi - 1); - } - if (val > ma && ma >= 0) - bb_error_msg_and_die("%s must be <= %d", what, ma); - return val; -} - - /* * Subtract 2 timeval structs: out = out - in. * Out is assumed to be >= in. @@ -828,7 +800,7 @@ inetname(struct sockaddr_in *from) char name[257]; if (!nflag && from->sin_addr.s_addr != INADDR_ANY) { - if(INET_rresolve(name, sizeof(name), from, 0x4000, 0xffffffff) >= 0) + if (INET_rresolve(name, sizeof(name), from, 0x4000, 0xffffffff) >= 0) n = name; } ina = inet_ntoa(from->sin_addr); @@ -974,7 +946,7 @@ traceroute_main(int argc, char *argv[]) #endif ); - if(op & USAGE_OP_DONT_FRAGMNT) + if (op & USAGE_OP_DONT_FRAGMNT) off = IP_DF; #ifdef CONFIG_FEATURE_TRACEROUTE_USE_ICMP useicmp = op & USAGE_OP_USE_ICMP; @@ -983,34 +955,34 @@ traceroute_main(int argc, char *argv[]) #ifdef CONFIG_FEATURE_TRACEROUTE_VERBOSE verbose = op & USAGE_OP_VERBOSE; #endif - if(op & USAGE_OP_IP_CHKSUM) { + if (op & USAGE_OP_IP_CHKSUM) { doipcksum = 0; - bb_error_msg("Warning: ip checksums disabled"); + bb_error_msg("warning: ip checksums disabled"); } if (tos_str) - tos = str2val(tos_str, "tos", 0, 255); - if(max_ttl_str) - max_ttl = str2val(max_ttl_str, "max ttl", 1, 255); - if(port_str) - port = (u_short)str2val(port_str, "port", 1, (1 << 16) - 1); - if(nprobes_str) - nprobes = str2val(nprobes_str, "nprobes", 1, -1); - if(source) { + tos = xatoul_range(tos_str, 0, 255); + if (max_ttl_str) + max_ttl = xatoul_range(max_ttl_str, 1, 255); + if (port_str) + port = xatou16(port_str); + if (nprobes_str) + nprobes = xatoul_range(nprobes_str, 1, INT_MAX); + if (source) { /* * set the ip source address of the outbound * probe (e.g., on a multi-homed host). */ if (getuid()) bb_error_msg_and_die("-s %s: permission denied", source); } - if(waittime_str) - waittime = str2val(waittime_str, "wait time", 2, 24 * 60 * 60); - if(pausemsecs_str) - pausemsecs = str2val(pausemsecs_str, "pause msecs", 0, 60 * 60 * 1000); - if(first_ttl_str) - first_ttl = str2val(first_ttl_str, "first ttl", 1, 255); + if (waittime_str) + waittime = xatoul_range(waittime_str, 2, 24 * 60 * 60); + if (pausemsecs_str) + pausemsecs = xatoul_range(pausemsecs_str, 0, 60 * 60 * 1000); + if (first_ttl_str) + first_ttl = xatoul_range(first_ttl_str, 1, 255); #ifdef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE - if(sourse_route_list) { + if (sourse_route_list) { llist_t *l_sr; for(l_sr = sourse_route_list; l_sr; ) { @@ -1046,8 +1018,7 @@ traceroute_main(int argc, char *argv[]) switch (argc - optind) { case 2: - packlen = str2val(argv[optind + 1], - "packet length", minpacket, maxpacket); + packlen = xatoul_range(argv[optind + 1], minpacket, maxpacket); /* Fall through */ case 1: @@ -1055,8 +1026,7 @@ traceroute_main(int argc, char *argv[]) hi = gethostinfo(hostname); setsin(to, hi->addrs[0]); if (hi->n > 1) - bb_error_msg( - "Warning: %s has multiple addresses; using %s", + bb_error_msg("warning: %s has multiple addresses; using %s", hostname, inet_ntoa(to->sin_addr)); hostname = hi->name; hi->name = NULL; diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 9ab6aee8c..f2cf82f05 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -262,10 +262,10 @@ int udhcpc_main(int argc, char *argv[]) client_config.script = optarg; break; case 'T': - client_config.timeout = atoi(optarg); + client_config.timeout = xatoi_u(optarg); break; case 't': - client_config.retries = atoi(optarg); + client_config.retries = xatoi_u(optarg); break; case 'v': printf("version %s\n\n", BB_VER); diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index d9dfb8965..52d383869 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c @@ -35,7 +35,8 @@ static int read_ip(const char *line, void *arg) int retval = 1; if (!inet_aton(line, addr)) { - if ((host = gethostbyname(line))) + host = gethostbyname(line); + if (host) addr->s_addr = *((unsigned long *) host->h_addr_list[0]); else retval = 0; } @@ -72,10 +73,7 @@ static int read_str(const char *line, void *arg) static int read_u32(const char *line, void *arg) { - uint32_t *dest = arg; - char *endptr; - *dest = strtoul(line, &endptr, 0); - return endptr[0] == '\0'; + return safe_strtou32(line, (uint32_t*)arg) == 0; } diff --git a/networking/vconfig.c b/networking/vconfig.c index 6c808eb2f..003c1a8f7 100644 --- a/networking/vconfig.c +++ b/networking/vconfig.c @@ -144,14 +144,14 @@ int vconfig_main(int argc, char **argv) * doing so wouldn't save that much space and would also make maintainence * more of a pain. */ if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ - ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1); + ifr.u.flag = xatoul_range(p, 0, 1); /* DM: in order to set reorder header, qos must be set */ - ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7); + ifr.vlan_qos = xatoul_range(argv[3], 0, 7); } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ - ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1); + ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1); } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ - ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX); - ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7); + ifr.u.skb_priority = xatou(p); + ifr.vlan_qos = xatoul_range(argv[3], 0, 7); } } diff --git a/networking/wget.c b/networking/wget.c index eda0bb87c..e7b19f2ef 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -297,7 +297,7 @@ read_response: s = buf; while (*s != '\0' && !isspace(*s)) ++s; while (isspace(*s)) ++s; - switch (status = atoi(s)) { + switch (status = xatoi(s)) { case 0: case 100: while (gethdr(buf, sizeof(buf), sfp, &n) != NULL) @@ -406,9 +406,9 @@ read_response: bb_error_msg_and_die("PASV: %s", buf+4); s = strrchr(buf, ','); *s = 0; - port = atoi(s+1); + port = xatol_range(s+1, 0, 255); s = strrchr(buf, ','); - port += atoi(s+1) * 256; + port += xatol_range(s+1, 0, 255) * 256; s_in.sin_port = htons(port); dfp = open_socket(&s_in); @@ -562,7 +562,7 @@ static void parse_url(char *src_url, struct host_info *h) cp = strchr(pp, ':'); if (cp != NULL) { *cp++ = '\0'; - h->port = htons(atoi(cp)); + h->port = htons(xatou16(cp)); } } @@ -646,7 +646,7 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf) } } while (!isdigit(buf[0]) || buf[3] != ' '); - return atoi(buf); + return xatoi(buf); } #ifdef CONFIG_FEATURE_WGET_STATUSBAR -- cgit v1.2.3