Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / ypserv / stdhosts / stdhosts.c
blobd39054d31fc3f770169499d8324db189b6815060
1 /* $NetBSD: stdhosts.c,v 1.18 2009/04/19 06:06:40 lukem Exp $ */
3 /*
4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: stdhosts.c,v 1.18 2009/04/19 06:06:40 lukem Exp $");
32 #endif
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <netdb.h>
47 #include "protos.h"
49 int main(int, char *[]);
50 void usage(void);
52 int
53 main(int argc, char *argv[])
55 struct in_addr host_addr;
56 FILE *data_file;
57 size_t line_no;
58 size_t len;
59 char *line, *k, *v, *addr_string;
60 const char *fname;
61 int ch;
62 int af = 1 << 4; /*IPv4*/
63 struct addrinfo hints, *res;
65 addr_string = NULL; /* XXX gcc -Wuninitialized */
67 while ((ch = getopt(argc, argv, "n")) != -1) {
68 switch (ch) {
69 case 'n':
70 af |= 1 << 6; /*IPv6*/
71 break;
72 default:
73 usage();
74 /* NOTREACHED */
77 argc -= optind;
78 argv += optind;
80 if (argc > 1)
81 usage();
83 if (argc == 1) {
84 fname = argv[0];
85 data_file = fopen(fname, "r");
86 if (data_file == NULL)
87 err(1, "%s", fname);
88 } else {
89 fname = "<stdin>";
90 data_file = stdin;
93 line_no = 0;
94 for (;
95 (line = fparseln(data_file, &len, &line_no, NULL,
96 FPARSELN_UNESCALL));
97 free(line)) {
98 if (len == 0)
99 continue;
101 v = line;
102 for (k = v; *v && !isspace((unsigned char)*v); v++)
104 while (*v && isspace((unsigned char)*v))
105 *v++ = '\0';
107 memset(&hints, 0, sizeof(hints));
108 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
109 hints.ai_flags = AI_NUMERICHOST;
111 if ((af & (1 << 4)) != 0 && inet_aton(k, &host_addr) == 1 &&
112 (addr_string = inet_ntoa(host_addr)) != NULL) {
113 /* IPv4 */
114 printf("%s %s\n", addr_string, v);
115 } else if ((af & (1 << 6)) != 0 &&
116 getaddrinfo(k, "0", &hints, &res) == 0) {
117 /* IPv6, with scope extension permitted */
118 freeaddrinfo(res);
119 printf("%s %s\n", k, v);
120 } else
121 warnx("%s line %lu: syntax error", fname,
122 (unsigned long)line_no);
125 exit(0);
128 void
129 usage(void)
132 fprintf(stderr, "usage: %s [-n] [file]\n", getprogname());
133 exit(1);