]>
diplodocus.org Git - nmh/blob - test/getfqdn.c
2 * getfqdn.c - Print the FQDN of a host, default to localhost.
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.
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 */
19 main(int argc
, char *argv
[])
21 char buf
[_POSIX_HOST_NAME_MAX
+ 1];
22 const char *hostname
= buf
;
23 struct addrinfo hints
, *res
;
26 /* Borrowed the important code below from LocalName() in sbr/mts.c. */
29 /* First get our local name. */
30 status
= gethostname(buf
, sizeof buf
);
31 } else if (argc
== 2) {
33 } else if (argc
> 2) {
34 fprintf (stderr
, "usage: %s [hostname]\n", argv
[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
;
44 if ((status
= getaddrinfo(hostname
, NULL
, &hints
, &res
)) == 0) {
45 printf ("%s\n", res
->ai_canonname
);