Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/networking/ipcalc.c

    r821 r1765  
    1 /* vi: set sw=4 ts=4 ai: */
     1/* vi: set sw=4 ts=4: */
    22/*
    33 * Mini ipcalc implementation for busybox
     
    1313 */
    1414
    15 #include "busybox.h"
    16 #include <ctype.h>
    1715#include <getopt.h>
    1816#include <sys/socket.h>
    1917#include <arpa/inet.h>
    2018
    21 #define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
     19#include "libbb.h"
    2220
    2321#define CLASS_A_NETMASK ntohl(0xFF000000)
     
    3937}
    4038
    41 #ifdef CONFIG_FEATURE_IPCALC_FANCY
     39#if ENABLE_FEATURE_IPCALC_FANCY
    4240static int get_prefix(unsigned long netmask)
    4341{
     
    4644
    4745    netmask = htonl(netmask);
    48     while(msk) {
     46    while (msk) {
    4947        if (netmask & msk)
    5048            ret++;
     
    5755#endif
    5856
     57
    5958#define NETMASK   0x01
    6059#define BROADCAST 0x02
     
    6564
    6665#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
    67     static const struct option long_options[] = {
    68         {"netmask",     no_argument, NULL, 'm'},
    69         {"broadcast",   no_argument, NULL, 'b'},
    70         {"network",     no_argument, NULL, 'n'},
    71 #ifdef CONFIG_FEATURE_IPCALC_FANCY
    72         {"prefix",      no_argument, NULL, 'p'},
    73         {"hostname",    no_argument, NULL, 'h'},
    74         {"silent",      no_argument, NULL, 's'},
    75 #endif
    76         {NULL, 0, NULL, 0}
    77     };
    78 #else
    79 #define long_options 0
     66    static const char ipcalc_longopts[] ALIGN1 =
     67        "netmask\0"   No_argument "m"
     68        "broadcast\0" No_argument "b"
     69        "network\0"   No_argument "n"
     70# if ENABLE_FEATURE_IPCALC_FANCY
     71        "prefix\0"    No_argument "p"
     72        "hostname\0"  No_argument "h"
     73        "silent\0"    No_argument "s"
     74# endif
     75        ;
    8076#endif
    8177
    82 
    83 
     78int ipcalc_main(int argc, char **argv);
    8479int ipcalc_main(int argc, char **argv)
    8580{
    86     unsigned long mode;
     81    unsigned opt;
    8782    int have_netmask = 0;
    8883    in_addr_t netmask, broadcast, network, ipaddr;
     
    9085    char *ipstr;
    9186
    92     if (ENABLE_FEATURE_IPCALC_LONG_OPTIONS)
    93         bb_applet_long_options = long_options;
    94 
    95     mode = bb_getopt_ulflags(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
    96 
     87#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
     88    applet_long_options = ipcalc_longopts;
     89#endif
     90    opt = getopt32(argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
    9791    argc -= optind;
    9892    argv += optind;
    99     if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
     93    if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
    10094        if (argc > 2 || argc <= 0)
    10195            bb_show_usage();
     
    10498            bb_show_usage();
    10599    }
     100    if (opt & SILENT)
     101        logmode = LOGMODE_NONE; /* Suppress error_msg() output */
    106102
    107103    ipstr = argv[0];
     
    112108        prefixstr = ipstr;
    113109
    114         while(*prefixstr) {
     110        while (*prefixstr) {
    115111            if (*prefixstr == '/') {
    116112                *prefixstr = (char)0;
    117113                prefixstr++;
    118114                if (*prefixstr) {
    119                     unsigned int msk;
    120 
    121                     if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
    122                         IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
    123                                 exit(EXIT_FAILURE));
    124                     }
     115                    unsigned msk;
     116                    netprefix = xatoul_range(prefixstr, 0, 32);
    125117                    netmask = 0;
    126118                    msk = 0x80000000;
     
    143135
    144136    if (ipaddr == 0) {
    145         IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
    146                 exit(EXIT_FAILURE));
     137        bb_error_msg_and_die("bad IP address: %s", argv[0]);
    147138    }
    148139    ipaddr = a.s_addr;
     
    150141    if (argc == 2) {
    151142        if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
    152             IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both.\n"),
    153                     exit(EXIT_FAILURE));
     143            bb_error_msg_and_die("use prefix or netmask, not both");
    154144        }
    155145
    156146        netmask = inet_aton(argv[1], &a);
    157147        if (netmask == 0) {
    158             IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
    159                     exit(EXIT_FAILURE));
     148            bb_error_msg_and_die("bad netmask: %s", argv[1]);
    160149        }
    161150        netmask = a.s_addr;
     
    167156    }
    168157
    169     if (mode & NETMASK) {
     158    if (opt & NETMASK) {
    170159        printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
    171160    }
    172161
    173     if (mode & BROADCAST) {
     162    if (opt & BROADCAST) {
    174163        broadcast = (ipaddr & netmask) | ~netmask;
    175164        printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
    176165    }
    177166
    178     if (mode & NETWORK) {
     167    if (opt & NETWORK) {
    179168        network = ipaddr & netmask;
    180169        printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
     
    182171
    183172    if (ENABLE_FEATURE_IPCALC_FANCY) {
    184         if (mode & NETPREFIX) {
     173        if (opt & NETPREFIX) {
    185174            printf("PREFIX=%i\n", get_prefix(netmask));
    186175        }
    187176
    188         if (mode & HOSTNAME) {
     177        if (opt & HOSTNAME) {
    189178            struct hostent *hostinfo;
    190179            int x;
     
    192181            hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
    193182            if (!hostinfo) {
    194                 IPCALC_MSG(bb_herror_msg_and_die(
    195                             "cannot find hostname for %s", argv[0]),);
    196                 exit(EXIT_FAILURE);
     183                bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
    197184            }
    198185            for (x = 0; hostinfo->h_name[x]; x++) {
Note: See TracChangeset for help on using the changeset viewer.