Module Name:    src
Committed By:   roy
Date:           Fri Jul 19 10:34:51 UTC 2013

Modified Files:
        src/bin/hostname: hostname.1 hostname.c

Log Message:
Add the following options
-A Display the FQDN of each address on all interfaces.
-a Display alias name(s) of the host.
-d Display the DNS domain.
-f Display the FQDN for the hostname.
-I Display each IP address on all interfaces.
-i Display the IP address(es) for the hostname.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/bin/hostname/hostname.1 \
    src/bin/hostname/hostname.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/hostname/hostname.1
diff -u src/bin/hostname/hostname.1:1.17 src/bin/hostname/hostname.1:1.18
--- src/bin/hostname/hostname.1:1.17	Thu Aug  7 09:05:13 2003
+++ src/bin/hostname/hostname.1	Fri Jul 19 10:34:51 2013
@@ -1,4 +1,4 @@
-.\"	$NetBSD: hostname.1,v 1.17 2003/08/07 09:05:13 agc Exp $
+.\"	$NetBSD: hostname.1,v 1.18 2013/07/19 10:34:51 roy Exp $
 .\"
 .\" Copyright (c) 1983, 1988, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	@(#)hostname.1	8.2 (Berkeley) 4/28/95
 .\"
-.Dd April 28, 1995
+.Dd July 19, 2013
 .Dt HOSTNAME 1
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd set or print name of current host system
 .Sh SYNOPSIS
 .Nm
-.Op Fl s
+.Op Fl AadIifs
 .Op Ar name-of-host
 .Sh DESCRIPTION
 .Nm
@@ -50,14 +50,36 @@ time.
 .Pp
 Options:
 .Bl -tag -width flag
+.It Fl A
+Display the FQDN of each address on all interfaces.
+.It Fl a
+Display alias name(s) of the host.
+.It Fl d
+Display the DNS domain.
+.It Fl f
+Display the FQDN for the hostname.
+.It Fl I
+Display each IP address on all interfaces.
+.It Fl i
+Display the IP address(es) for the hostname.
 .It Fl s
-Trims off any domain information from the printed
-name.
+Display the short hostname.
 .El
+.Sh NOTES
+With the exception of
+.Fl I
+and
+.Fl s ,
+the other options will retrieve their results from the resolver.
 .Sh SEE ALSO
 .Xr domainname 1 ,
+.Xr getaddrinfo 3 ,
 .Xr gethostname 3 ,
-.Xr sethostname 3
+.Xr sethostname 3 ,
+.Xr gethostbyname 3 ,
+.Xr getifaddrs 3 ,
+.Xr getnameinfo 3 ,
+.Xr hosts 5
 .Sh HISTORY
 The
 .Nm
Index: src/bin/hostname/hostname.c
diff -u src/bin/hostname/hostname.c:1.17 src/bin/hostname/hostname.c:1.18
--- src/bin/hostname/hostname.c:1.17	Mon Aug 29 14:51:18 2011
+++ src/bin/hostname/hostname.c	Fri Jul 19 10:34:51 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: hostname.c,v 1.17 2011/08/29 14:51:18 joerg Exp $ */
+/* $NetBSD: hostname.c,v 1.18 2013/07/19 10:34:51 roy Exp $ */
 
 /*
  * Copyright (c) 1988, 1993
@@ -39,13 +39,19 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)hostname.c	8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: hostname.c,v 1.17 2011/08/29 14:51:18 joerg Exp $");
+__RCSID("$NetBSD: hostname.c,v 1.18 2013/07/19 10:34:51 roy Exp $");
 #endif
 #endif /* not lint */
 
 #include <sys/param.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
 
 #include <err.h>
+#include <ifaddrs.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -56,13 +62,36 @@ __dead static void usage(void);
 int
 main(int argc, char *argv[])
 {
-	int ch, sflag;
+	int ch, Aflag, aflag, dflag, Iflag, iflag, fflag, sflag, i;
 	char *p, hostname[MAXHOSTNAMELEN + 1];
+	struct addrinfo hints, *ainfos, *ai;
+	struct hostent *hent;
+	struct ifaddrs *ifa, *ifp;
+	struct sockaddr_in6 *sin6;
+	char buf[MAX(MAXHOSTNAMELEN + 1, INET6_ADDRSTRLEN)];
 
 	setprogname(argv[0]);
-	sflag = 0;
-	while ((ch = getopt(argc, argv, "s")) != -1)
+	Aflag = aflag = dflag = Iflag = iflag = fflag = sflag = 0;
+	while ((ch = getopt(argc, argv, "AadIifs")) != -1)
 		switch (ch) {
+		case 'A':
+			Aflag = 1;
+			break;
+		case 'a':
+			aflag = 1;
+			break;
+		case 'd':
+			dflag = 1;
+			break;
+		case 'I':
+			Iflag = 1;
+			break;
+		case 'i':
+			iflag = 1;
+			break;
+		case 'f':
+			fflag = 1;
+			break;
 		case 's':
 			sflag = 1;
 			break;
@@ -79,13 +108,89 @@ main(int argc, char *argv[])
 	if (*argv) {
 		if (sethostname(*argv, strlen(*argv)))
 			err(1, "sethostname");
+	} else if (Aflag || Iflag) {
+		if (getifaddrs(&ifa) == -1)
+			err(1, "getifaddrs");
+		for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
+			if (ifp->ifa_addr == NULL ||
+			    ifp->ifa_flags & IFF_LOOPBACK ||
+			    !(ifp->ifa_flags & IFF_UP))
+				continue;
+
+			switch(ifp->ifa_addr->sa_family) {
+			case AF_INET:
+				break;
+			case AF_INET6:
+				/* Skip link local addresses */
+				sin6 = (struct sockaddr_in6 *)ifp->ifa_addr;
+				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
+				    IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
+					continue;
+				break;
+			default:
+				/* We only translate IPv4 or IPv6 addresses */
+				continue;
+			}
+			i = getnameinfo(ifp->ifa_addr, ifp->ifa_addr->sa_len,
+			    buf, sizeof(buf), NULL, 0,
+			    Iflag ? NI_NUMERICHOST: NI_NAMEREQD);
+			if (i == -1) {
+				if (Iflag && i != EAI_NONAME)
+					errx(1, "getnameinfo: %s",
+					    gai_strerror(i));
+			} else
+				printf("%s\n", buf);
+		}
+		freeifaddrs(ifa);
 	} else {
 		if (gethostname(hostname, sizeof(hostname)))
 			err(1, "gethostname");
 		hostname[sizeof(hostname) - 1] = '\0';
-		if (sflag && (p = strchr(hostname, '.')))
-			*p = '\0';
-		(void)printf("%s\n", hostname);
+		if (aflag) {
+			if ((hent = gethostbyname(hostname)) == NULL)
+				errx(1, "gethostbyname: %s",
+				    hstrerror(h_errno));
+			for (i = 0; hent->h_aliases[i]; i++)
+				printf("%s\n", hent->h_aliases[i]);
+		} else if (dflag || iflag || fflag) {
+			memset(&hints, 0, sizeof(hints));
+			hints.ai_family = AF_UNSPEC;
+			hints.ai_socktype = SOCK_DGRAM;
+			hints.ai_flags = AI_CANONNAME;
+			if (getaddrinfo(hostname, NULL, &hints, &ainfos) == -1)
+				err(1, "getaddrinfo");
+			if (ainfos) {
+				if (dflag) {
+					if ((p = strchr(ainfos->ai_canonname,
+					    '.')))
+						printf("%s\n", p + 1);
+				} else if (iflag) {
+					for (ai = ainfos; ai; ai = ai->ai_next)
+					{
+						i = getnameinfo(ai->ai_addr,
+						    ai->ai_addrlen,
+						    buf, sizeof(buf), NULL, 0,
+						    NI_NUMERICHOST);
+						if (i == -1)
+							errx(1,
+							    "getnameinfo: %s",
+							    gai_strerror(i));
+						printf("%s\n", buf);
+					}
+				} else {
+					if (sflag &&
+					    (p = strchr(ainfos->ai_canonname,
+					    '.')))
+						*p = '\0';
+					printf("%s\n", ainfos->ai_canonname);
+				}
+				freeaddrinfo(ainfos);
+			}
+		} else {
+			if (sflag && (p = strchr(hostname, '.')))
+				*p = '\0';
+			printf("%s\n", hostname);
+		}
 	}
 	exit(0);
 	/* NOTREACHED */
@@ -94,7 +199,7 @@ main(int argc, char *argv[])
 static void
 usage(void)
 {
-	(void)fprintf(stderr, "usage: %s [-s] [name-of-host]\n",
+	(void)fprintf(stderr, "usage: %s [-AadIifs] [name-of-host]\n",
 	    getprogname());
 	exit(1);
 	/* NOTREACHED */

Reply via email to