1 /* $NetBSD: stdhosts.c,v 1.18 2009/04/19 06:06:40 lukem Exp $ */
4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: stdhosts.c,v 1.18 2009/04/19 06:06:40 lukem Exp $");
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
49 int main(int, char *[]);
53 main(int argc
, char *argv
[])
55 struct in_addr host_addr
;
59 char *line
, *k
, *v
, *addr_string
;
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) {
70 af
|= 1 << 6; /*IPv6*/
85 data_file
= fopen(fname
, "r");
86 if (data_file
== NULL
)
95 (line
= fparseln(data_file
, &len
, &line_no
, NULL
,
102 for (k
= v
; *v
&& !isspace((unsigned char)*v
); v
++)
104 while (*v
&& isspace((unsigned char)*v
))
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
) {
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 */
119 printf("%s %s\n", k
, v
);
121 warnx("%s line %lu: syntax error", fname
,
122 (unsigned long)line_no
);
132 fprintf(stderr
, "usage: %s [-n] [file]\n", getprogname());