Updated build-nmh-cygwin for release of Cygwin nmh 1.5-2. To use:
[mmh] / test / getfqdn.c
1 /*
2  * getfqdn.c - Print the FQDN of a host, default to localhost.
3  *
4  * This code is Copyright (c) 2012, by the authors of nmh.  See the
5  * COPYRIGHT file in the root directory of the nmh distribution for
6  * complete copyright information.
7  */
8
9 #include <netdb.h>   /* for getaddrinfo */
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include <unistd.h>  /* for gethostname */
13 #include <limits.h>  /* for _POSIX_HOST_NAME_MAX */
14 #include <string.h>  /* for memset */
15 #include <stdio.h>
16
17
18 int
19 main(int argc, char *argv[])
20 {
21   char buf[_POSIX_HOST_NAME_MAX + 1];
22   const char *hostname = buf;
23   struct addrinfo hints, *res;
24   int status = 0;
25
26   /* Borrowed the important code below from LocalName() in sbr/mts.c. */
27
28   if (argc < 2) {
29     /* First get our local name. */
30     status = gethostname(buf, sizeof buf);
31   } else if (argc == 2) {
32     hostname = argv[1];
33   } else if (argc > 2) {
34     fprintf (stderr, "usage: %s [hostname]\n", argv[0]);
35     return 1;
36   }
37
38   if (status == 0) {
39     /* Now fully qualify the hostname. */
40     memset(&hints, 0, sizeof hints);
41     hints.ai_flags = AI_CANONNAME;
42     hints.ai_family = AF_UNSPEC;
43
44     if ((status = getaddrinfo(hostname, NULL, &hints, &res)) == 0) {
45       printf ("%s\n", res->ai_canonname);
46       freeaddrinfo(res);
47     }
48   }
49
50   return status;
51 }