- prefixstr = ipstr = argv[optind];
-
- while(*prefixstr) {
- if (*prefixstr == '/') {
- *prefixstr = (char)0;
- prefixstr++;
- if (*prefixstr) {
- netprefix = atol(prefixstr);
- if (netprefix > 32) {
- IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
- exit(EXIT_FAILURE));
- }
- if (netprefix) {
- netmask = 0x80000000;
- netprefix--;
- while(netprefix) {
- netmask = 0x80000000 | (netmask >> 1);
- netprefix--;
- }
- netmask = htonl(netmask);
- }
- /* Even if it was 0, we will signify that we have a netmask. This allows */
- /* for specification of default routes, etc which have a 0 netmask/prefix */
- have_netmask = 1;
- }
- break;
- }
- prefixstr++;
- }
- ipaddr = inet_addr(ipstr);
+ prefixstr = ipstr = argv[0];
+
+ 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\n", prefixstr),
+ exit(EXIT_FAILURE));
+ }
+ netmask = 0;
+ msk = 0x80000000;
+ while (netprefix > 0) {
+ netmask |= msk;
+ msk >>= 1;
+ netprefix--;
+ }
+ netmask = htonl(netmask);
+ /* Even if it was 0, we will signify that we have a netmask. This allows */
+ /* for specification of default routes, etc which have a 0 netmask/prefix */
+ have_netmask = 1;
+ }
+ break;
+ }
+ prefixstr++;
+ }
+ ipaddr = inet_aton(ipstr, &a);