Ticket #1684: netstat.cpp.2.diff
File netstat.cpp.2.diff, 2.1 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 sResolveNames = 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.sin_addr, 63 sizeof(sockaddr_in), AF_INET); 64 servent* service = getservbyport(ntohs(address.sin_port), NULL); 64 hostent* host = NULL; 65 servent* service = NULL; 66 if (sResolveNames) { 67 host = gethostbyaddr((const char*)&(address.sin_addr), sizeof(in_addr), AF_INET); 68 service = getservbyport(ntohs(address.sin_port), NULL); 69 } 65 70 66 71 const char *hostName; 67 72 if (host != NULL) … … 94 99 void 95 100 usage(int status) 96 101 { 97 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"); 98 106 99 107 exit(status); 100 108 } … … 141 149 int 142 150 main(int argc, char** argv) 143 151 { 144 if (argc > 1 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) 145 usage(0); 152 int optionIndex = 0; 153 int opt; 154 static struct option longOptions[] = { 155 {"help", no_argument, 0, 'h'}, 156 {"numeric", no_argument, 0, 'n'}, 157 {0, 0, 0, 0} 158 }; 146 159 160 do { 161 opt = getopt_long(argc, argv, "hn", longOptions, &optionIndex); 162 switch (opt) { 163 case -1: 164 // end of arguments, do nothing 165 break; 166 167 case 'n': 168 sResolveNames = 0; 169 break; 170 171 case 'h': 172 default: 173 usage(0); 174 break; 175 } 176 } while (opt != -1); 177 147 178 int stack = open(NET_STACK_DRIVER_PATH, O_RDWR); 148 179 if (stack < 0) { 149 180 fprintf(stderr, "%s: The networking stack doesn't seem to be available.\n", … … 152 183 } 153 184 154 185 bool printProgram = true; 155 // TODO: add some program options... :-)186 // TODO: add some more program options... :-) 156 187 157 188 printf("Proto Recv-Q Send-Q Local Address Foreign Address State Program\n"); 158 189