From 132067b08106d5b662e19edb9a5c2640b7a40a60 Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Sat, 22 Jul 2006 17:31:00 +0000 Subject: Added getopt() processing of [-t {a|aaaa}]. --- ares/ahost.c | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'ares') diff --git a/ares/ahost.c b/ares/ahost.c index e0c32d9e9..40cd08439 100644 --- a/ares/ahost.c +++ b/ares/ahost.c @@ -1,4 +1,6 @@ /* Copyright 1998 by the Massachusetts Institute of Technology. + * + * $Id$ * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -30,6 +32,10 @@ #include #include +#ifdef HAVE_GETOPT_H +#include +#endif + #include "ares.h" #include "ares_dns.h" #include "inet_ntop.h" @@ -52,7 +58,7 @@ static void usage(void); int main(int argc, char **argv) { ares_channel channel; - int status, nfds; + int status, nfds, c, addr_family = AF_INET; fd_set read_fds, write_fds; struct timeval *tvp, tv; struct in_addr addr4; @@ -64,7 +70,28 @@ int main(int argc, char **argv) WSAStartup(wVersionRequested, &wsaData); #endif - if (argc <= 1) + while ((c = getopt(argc,argv,"t:h")) != -1) + { + switch (c) + { + case 't': + if (!strcasecmp(optarg,"a")) + addr_family = AF_INET; + else if (!strcasecmp(optarg,"aaaa")) + addr_family = AF_INET6; + else + usage(); + break; + case 'h': + default: + usage(); + break; + } + } + + argc -= optind; + argv += optind; + if (argc < 1) usage(); status = ares_init(&channel); @@ -75,7 +102,7 @@ int main(int argc, char **argv) } /* Initiate the queries, one per command-line argument. */ - for (argv++; *argv; argv++) + for ( ; *argv; argv++) { if (ares_inet_pton(AF_INET, *argv, &addr4) == 1) { @@ -89,8 +116,7 @@ int main(int argc, char **argv) } else { - /* assume user wants A-records */ - ares_gethostbyname(channel, *argv, AF_INET, callback, *argv); + ares_gethostbyname(channel, *argv, addr_family, callback, *argv); } } @@ -132,8 +158,8 @@ static void callback(void *arg, int status, struct hostent *host) { int i; - printf (", Aliases: "); - for (i = 0; host->h_aliases[i]; i++) + printf (", Aliases: "); + for (i = 0; host->h_aliases[i]; i++) printf("%s ", host->h_aliases[i]); } #endif @@ -143,6 +169,6 @@ static void callback(void *arg, int status, struct hostent *host) static void usage(void) { - fprintf(stderr, "usage: ahost {host|addr} ...\n"); + fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n"); exit(1); } -- cgit v1.2.3