Ticket #1684: netstat.cpp.diff
File netstat.cpp.diff, 1.7 KB (added by , 17 years ago) |
---|
-
netstat.cpp
22 22 #include <stdlib.h> 23 23 #include <string.h> 24 24 #include <unistd.h> 25 #include <getopt.h> 25 26 26 27 27 28 extern const char* __progname; 28 29 const char* kProgramName = __progname; 29 30 31 static int resolve_names = 1; 30 32 31 33 struct address_family { 32 34 int family; … … 59 61 return; 60 62 } 61 63 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 } 64 70 65 71 const char *hostName; 66 72 if (host != NULL) … … 93 99 void 94 100 usage(int status) 95 101 { 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"); 97 106 98 107 exit(status); 99 108 } … … 140 149 int 141 150 main(int argc, char** argv) 142 151 { 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 }; 145 159 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 146 168 int stack = open(NET_STACK_DRIVER_PATH, O_RDWR); 147 169 if (stack < 0) { 148 170 fprintf(stderr, "%s: The networking stack doesn't seem to be available.\n",