]>
diplodocus.org Git - nmh/blob - test/getcanon.c
1 /* getcanon.c - Print the canonical name of a host, default to localhost.
3 * This code is Copyright (c) 2012, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include <sys/socket.h>
10 #include <sys/types.h>
19 main(int argc
, char *argv
[])
21 char buf
[_POSIX_HOST_NAME_MAX
+ 1];
23 struct addrinfo hints
, *res
;
25 /* Borrowed the important code below from LocalName() in sbr/mts.c. */
28 fprintf(stderr
, "usage: %s [hostname]\n", argv
[0]);
34 /* First get our local name. */
35 if (gethostname(buf
, sizeof buf
)) {
36 fprintf(stderr
, "gethostname() failed: %s\n", strerror(errno
));
42 /* Now fully qualify the hostname. */
43 memset(&hints
, 0, sizeof hints
);
44 hints
.ai_flags
= AI_CANONNAME
;
45 hints
.ai_family
= AF_UNSPEC
;
47 if (getaddrinfo(hostname
, NULL
, &hints
, &res
)) {
51 puts(res
->ai_canonname
);