xref: /openbsd-src/regress/lib/libc/asr/bin/getnetnamadr.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: getnetnamadr.c,v 1.1.1.1 2012/07/13 17:49:54 eric Exp $	*/
2 /*
3  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 
22 #include <err.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "common.h"
30 
31 static void
32 usage(void)
33 {
34 	extern const char * __progname;
35 
36 	fprintf(stderr, "usage: %s [-aen] [host...]\n", __progname);
37 	exit(1);
38 }
39 
40 int
41 main(int argc, char *argv[])
42 {
43 	int			 i, ch, nflag = 0;
44 	struct netent		*n;
45 	char			*host;
46 
47 	while((ch = getopt(argc, argv, "en")) !=  -1) {
48 		switch(ch) {
49 		case 'e':
50 			long_err += 1;
51 			break;
52 		case 'n':
53 			nflag = 1;
54 			break;
55 		default:
56 			usage();
57 			/* NOTREACHED */
58 		}
59 	}
60 	argc -= optind;
61 	argv += optind;
62 
63 	for(i = 0; i < argc; i++) {
64 
65 		if (i)
66 			printf("\n");
67 		printf("===> \"%s\"\n", argv[i]);
68 		host = gethostarg(argv[i]);
69 
70 		errno = 0;
71 		h_errno = 0;
72 		gai_errno = 0;
73 		rrset_errno = 0;
74 
75 		if (nflag)
76 			n = getnetbyname(host);
77 		else
78 			n = getnetbyaddr(inet_network(host), AF_INET);
79 		if (n)
80 			print_netent(n);
81 		print_errors();
82 	}
83 
84 	return (0);
85 }
86