From e33f30a012de730926e49dd4b5c5d9c510d621be Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 9 Aug 2009 10:58:06 +0200 Subject: Don't print redundant information or rdns --- src/host.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/host.c b/src/host.c index f9d6d6d..6ba2a85 100644 --- a/src/host.c +++ b/src/host.c @@ -5,11 +5,15 @@ #include #include +#include #include #include #include #include +#define INPUT_HOST 1 +#define INPUT_IP 2 + /** * \brief convert addrinfo to simple IP address * @@ -45,6 +49,7 @@ int main(int argc, char **argv) { char hostname[NI_MAXHOST]; char ip_address[INET6_ADDRSTRLEN]; int ret; + int input_type = 0; if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); @@ -60,13 +65,22 @@ int main(int argc, char **argv) { for (address = result; address != NULL; address = address->ai_next) { if (addr_to_ip(address, ip_address, sizeof(ip_address)) == 0) continue; - printf("%-40s ", ip_address); - ret = getnameinfo(address->ai_addr, address->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); - if (ret != 0) { - fprintf(stderr, "getnameinfo: %s\n", gai_strerror(ret)); - continue; + if (input_type == 0) { + if (strcmp(ip_address, argv[1]) == 0) + input_type = INPUT_IP; + else + input_type = INPUT_HOST; + } + if (input_type == INPUT_HOST) + puts(ip_address); + else { + ret = getnameinfo(address->ai_addr, address->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); + if (ret != 0) { + fprintf(stderr, "getnameinfo: %s\n", gai_strerror(ret)); + continue; + } + puts(hostname); } - puts(hostname); } freeaddrinfo(address); return(EXIT_SUCCESS); -- cgit v1.2.3