From 5666ded179caf3cddaee6a6d65ccc5c77b9e8d8f Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 9 Aug 2009 11:30:42 +0200 Subject: Added getopt (-4 / -6) --- src/host.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/host.c b/src/host.c index 6ba2a85..88e06a5 100644 --- a/src/host.c +++ b/src/host.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,13 @@ #define INPUT_HOST 1 #define INPUT_IP 2 +/** + * \brief Print some usage information + */ + +#define PRINT_USAGE \ + fprintf(stderr, "Usage: %s [-46] \n", argv[0]) + /** * \brief convert addrinfo to simple IP address * @@ -50,23 +58,38 @@ int main(int argc, char **argv) { char ip_address[INET6_ADDRSTRLEN]; int ret; int input_type = 0; + char option; - if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + while ((option = getopt(argc, argv, "46")) != -1) { + switch (option) { + case '4': + hints.ai_family = AF_INET; + break; + case '6': + hints.ai_family = AF_INET6; + break; + default: + PRINT_USAGE; + return EXIT_FAILURE; + } + } + + if (optind >= argc) { + PRINT_USAGE; return EXIT_FAILURE; } - ret = getaddrinfo(argv[1], NULL, &hints, &result); + ret = getaddrinfo(argv[optind], NULL, &hints, &result); if (ret != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } for (address = result; address != NULL; address = address->ai_next) { if (addr_to_ip(address, ip_address, sizeof(ip_address)) == 0) continue; if (input_type == 0) { - if (strcmp(ip_address, argv[1]) == 0) + if (strcmp(ip_address, argv[optind]) == 0) input_type = INPUT_IP; else input_type = INPUT_HOST; -- cgit v1.2.3