tools/llvm: Do not build with symbols
[minix3.git] / tests / lib / libc / net / h_hostent.c
blobbfcdd0db4a8420e52cd1d95ba701b6867ceb21f4
1 /* $NetBSD: h_hostent.c,v 1.1 2013/08/16 15:29:45 christos Exp $ */
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: h_hostent.c,v 1.1 2013/08/16 15:29:45 christos Exp $");
34 #include <stdio.h>
35 #include <string.h>
36 #include <nsswitch.h>
37 #include <netdb.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <err.h>
43 #include <netinet/in.h>
44 #include <sys/types.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
48 #include "hostent.h"
50 static void
51 phostent(const struct hostent *h)
53 size_t i;
54 char buf[1024];
55 const int af = h->h_length == NS_INADDRSZ ? AF_INET : AF_INET6;
57 printf("name=%s, length=%d, addrtype=%d, aliases=[",
58 h->h_name, h->h_length, h->h_addrtype);
60 for (i = 0; h->h_aliases[i]; i++)
61 printf("%s%s", i == 0 ? "" : " ", h->h_aliases[i]);
63 printf("] addr_list=[");
65 for (i = 0; h->h_addr_list[i]; i++)
66 printf("%s%s", i == 0 ? "" : " ", inet_ntop(af,
67 h->h_addr_list[i], buf, (socklen_t)sizeof(buf)));
69 printf("]\n");
72 static void
73 usage(void)
75 (void)fprintf(stderr, "Usage: %s [-f <hostsfile>] "
76 "[-t <any|dns|nis|files>] "
77 "[-46a] <name|address>\n", getprogname());
78 exit(EXIT_FAILURE);
81 static void
82 getby(int (*f)(void *, void *, va_list), struct getnamaddr *info, ...)
84 va_list ap;
85 int e;
87 va_start(ap, info);
88 e = (*f)(info, NULL, ap);
89 va_end(ap);
90 switch (e) {
91 case NS_SUCCESS:
92 phostent(info->hp);
93 break;
94 default:
95 printf("error %d\n", e);
96 break;
100 static void
101 geta(struct hostent *hp) {
102 if (hp == NULL)
103 printf("error %d\n", h_errno);
104 else
105 phostent(hp);
109 main(int argc, char *argv[])
111 int (*f)(void *, void *, va_list) = NULL;
112 const char *type = "any";
113 int c, af, e, byaddr, len;
114 struct hostent hent;
115 struct getnamaddr info;
116 char buf[4096];
118 af = AF_INET;
119 byaddr = 0;
120 len = 0;
121 info.hp = &hent;
122 info.buf = buf;
123 info.buflen = sizeof(buf);
124 info.he = &e;
126 while ((c = getopt(argc, argv, "46af:t:")) != -1) {
127 switch (c) {
128 case '4':
129 af = AF_INET;
130 break;
131 case '6':
132 af = AF_INET6;
133 break;
134 case 'a':
135 byaddr++;
136 break;
137 case 't':
138 type = optarg;
139 break;
140 case 'f':
141 _hf_sethostsfile(optarg);
142 break;
143 default:
144 usage();
148 argc -= optind;
149 argv += optind;
151 if (argc != 1)
152 usage();
154 switch (*type) {
155 case 'a':
156 break;
157 case 'd':
158 f = byaddr ? _dns_gethtbyaddr : _dns_gethtbyname;
159 break;
160 #ifdef YP
161 case 'n':
162 f = byaddr ? _yp_gethtbyaddr : _yp_gethtbyname;
163 break;
164 #endif
165 case 'f':
166 f = byaddr ? _hf_gethtbyaddr : _hf_gethtbyname;
167 break;
168 default:
169 errx(EXIT_FAILURE, "Unknown db type `%s'", type);
172 if (byaddr) {
173 struct in6_addr addr;
174 af = strchr(*argv, ':') ? AF_INET6 : AF_INET;
175 len = af == AF_INET ? NS_INADDRSZ : NS_IN6ADDRSZ;
176 if (inet_pton(af, *argv, &addr) == -1)
177 err(EXIT_FAILURE, "Can't parse `%s'", *argv);
178 if (*type == 'a')
179 geta(gethostbyaddr((const char *)&addr, len, af));
180 else
181 getby(f, &info, &addr, len, af);
182 } else {
183 if (*type == 'a')
184 geta(gethostbyname2(*argv, af));
185 else
186 getby(f, &info, *argv, len, af);
189 return 0;