X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/32fde56da3936f39cd11a4eee52600164076e8e1..63621a81d16ab743de6b57d47578a9a2c670ad22:/test/getcanon.c diff --git a/test/getcanon.c b/test/getcanon.c index 8d7f15fd..3e75f32f 100644 --- a/test/getcanon.c +++ b/test/getcanon.c @@ -20,37 +20,36 @@ int main(int argc, char *argv[]) { char buf[_POSIX_HOST_NAME_MAX + 1]; - const char *hostname = buf; + const char *hostname; struct addrinfo hints, *res; - int status = 0; /* Borrowed the important code below from LocalName() in sbr/mts.c. */ - if (argc < 2) { - /* First get our local name. */ - status = gethostname(buf, sizeof buf); - } else if (argc == 2) { - hostname = argv[1]; - } else if (argc > 2) { + if (argc > 2) { fprintf(stderr, "usage: %s [hostname]\n", argv[0]); return 1; } - - if (status == 0) { - /* Now fully qualify the hostname. */ - memset(&hints, 0, sizeof hints); - hints.ai_flags = AI_CANONNAME; - hints.ai_family = AF_UNSPEC; - - if ((status = getaddrinfo(hostname, NULL, &hints, &res)) == 0) { - printf("%s\n", res->ai_canonname); - freeaddrinfo(res); - } else { - printf("%s\n", hostname); + if (argc == 2) + hostname = argv[1]; + else { + /* First get our local name. */ + if (gethostname(buf, sizeof buf)) { + fprintf(stderr, "gethostname() failed: %s\n", strerror(errno)); + return 1; } - } else { - fprintf(stderr, "gethostname() failed: %s\n", strerror(errno)); + hostname = buf; + } + + /* Now fully qualify the hostname. */ + memset(&hints, 0, sizeof hints); + hints.ai_flags = AI_CANONNAME; + hints.ai_family = AF_UNSPEC; + + if (getaddrinfo(hostname, NULL, &hints, &res)) { + puts(hostname); + return 1; } + puts(res->ai_canonname); - return status; + return 0; }