Ticket #1684: netstat.cpp.diff

File netstat.cpp.diff, 1.7 KB (added by mjw, 17 years ago)
  • netstat.cpp

     
    2222#include <stdlib.h>
    2323#include <string.h>
    2424#include <unistd.h>
     25#include <getopt.h>
    2526
    2627
    2728extern const char* __progname;
    2829const char* kProgramName = __progname;
    2930
     31static int resolve_names = 1;
    3032
    3133struct address_family {
    3234    int         family;
     
    5961        return;
    6062    }
    6163
    62     hostent* host = gethostbyaddr((const char*)_address, sizeof(sockaddr_in), AF_INET);
    63     servent* service = getservbyport(ntohs(address.sin_port), NULL);
     64    hostent* host = NULL;
     65    servent* service = NULL;
     66    if (resolve_names) {
     67        host = gethostbyaddr((const char*)_address, sizeof(sockaddr_in), AF_INET);
     68        service = getservbyport(ntohs(address.sin_port), NULL);
     69    }
    6470
    6571    const char *hostName;
    6672    if (host != NULL)
     
    9399void
    94100usage(int status)
    95101{
    96     printf("usage: %s\n", kProgramName);
     102    printf("usage: %s [options]\n", kProgramName);
     103    printf("options:\n");
     104    printf("    -n  don't resolve names\n");
     105    printf("    -h  this help\n");
    97106
    98107    exit(status);
    99108}
     
    140149int
    141150main(int argc, char** argv)
    142151{
    143     if (argc > 1 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
    144         usage(0);
     152    int option_index = 0;
     153    int c;
     154    static struct option long_options[] =
     155    {
     156        {"help", no_argument, 0, 'h'},
     157        {"numeric", no_argument, 0, 'n'}
     158    };
    145159
     160    while ((c = getopt_long(argc, argv, "hn", long_options, &option_index)) != -1) {
     161        if (c == 'n') {
     162            resolve_names = 0;
     163        } else if (c == 'h' || c == '?') {
     164            usage(0);
     165        }
     166    }
     167
    146168    int stack = open(NET_STACK_DRIVER_PATH, O_RDWR);
    147169    if (stack < 0) {
    148170        fprintf(stderr, "%s: The networking stack doesn't seem to be available.\n",