with -n, permit non-IPv4 address in the output.

option name synchronized with solaris 8.
This commit is contained in:
itojun 2000-07-30 02:25:08 +00:00
parent d760e0b407
commit 863848734f
2 changed files with 44 additions and 12 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: stdhosts.8,v 1.5 1999/03/07 11:58:26 mycroft Exp $
.\" $NetBSD: stdhosts.8,v 1.6 2000/07/30 02:25:08 itojun Exp $
.\"
.\" Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
.\" All rights reserved.
@ -37,6 +37,7 @@
.Nd a YP filter program
.Sh SYNOPSIS
.Nm ""
.Op Fl n
.Op Ar file
.Sh DESCRIPTION
.Nm
@ -44,7 +45,7 @@ parses the
.Xr hosts 5
style input stream (stdin, or
.Ar file
if given), and outputs lines containing only the IP address and host name.
if given), and outputs lines containing only the IPv4 address and host name.
.Pp
.Nm
is used by other
@ -52,6 +53,9 @@ is used by other
programs when creating some of the
.Tn YP
maps.
.Pp
.Fl n
allows other address types in the output, including IPv6 addresses.
.Sh SEE ALSO
.Xr yp 8 ,
.Xr ypserv 8

View File

@ -1,4 +1,4 @@
/* $NetBSD: stdhosts.c,v 1.11 1999/07/25 09:01:05 lukem Exp $ */
/* $NetBSD: stdhosts.c,v 1.12 2000/07/30 02:25:08 itojun Exp $ */
/*
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: stdhosts.c,v 1.11 1999/07/25 09:01:05 lukem Exp $");
__RCSID("$NetBSD: stdhosts.c,v 1.12 2000/07/30 02:25:08 itojun Exp $");
#endif
#include <sys/types.h>
@ -47,6 +47,8 @@ __RCSID("$NetBSD: stdhosts.c,v 1.11 1999/07/25 09:01:05 lukem Exp $");
#include <stdlib.h>
#include <string.h>
#include <util.h>
#include <unistd.h>
#include <netdb.h>
#include "protos.h"
@ -65,14 +67,30 @@ main(argc, argv)
size_t line_no;
size_t len;
char *line, *k, *v, *addr_string, *fname;
int ch;
int af = 1 << 4; /*IPv4*/
struct addrinfo hints, *res;
addr_string = NULL; /* XXX gcc -Wuninitialized */
if (argc > 2)
while ((ch = getopt(argc, argv, "n")) != EOF) {
switch (ch) {
case 'n':
af |= 1 << 6; /*IPv6*/
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc > 1)
usage();
if (argc == 2) {
fname = argv[1];
if (argc == 1) {
fname = argv[0];
data_file = fopen(fname, "r");
if (data_file == NULL)
err(1, "%s", fname);
@ -95,12 +113,22 @@ main(argc, argv)
while (*v && isspace(*v))
*v++ = '\0';
if (inet_aton(k, &host_addr) == 0 ||
(addr_string = inet_ntoa(host_addr)) == NULL) {
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
hints.ai_flags = AI_NUMERICHOST;
if ((af & (1 << 4)) != 0 && inet_aton(k, &host_addr) == 1 &&
(addr_string = inet_ntoa(host_addr)) != NULL) {
/* IPv4 */
printf("%s %s\n", addr_string, v);
} else if ((af & (1 << 6)) != 0 &&
getaddrinfo(k, "0", &hints, &res) == 0) {
/* IPv6, with scope extension permitted */
freeaddrinfo(res);
printf("%s %s\n", k, v);
} else
warnx("%s line %lu: syntax error", fname,
(unsigned long)line_no);
} else
printf("%s %s\n", addr_string, v);
}
exit(0);
@ -110,6 +138,6 @@ void
usage()
{
fprintf(stderr, "usage: %s [file]\n", __progname);
fprintf(stderr, "usage: %s [-n] [file]\n", __progname);
exit(1);
}