xref: /openbsd/regress/lib/libc/asr/bin/getnetnamadr.c (revision d89ec533)
1 /*	$OpenBSD: getnetnamadr.c,v 1.2 2018/12/15 15:16:12 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, "R:en")) !=  -1) {
48 		switch(ch) {
49 		case 'R':
50 			parseresopt(optarg);
51 			break;
52 		case 'e':
53 			long_err += 1;
54 			break;
55 		case 'n':
56 			nflag = 1;
57 			break;
58 		default:
59 			usage();
60 			/* NOTREACHED */
61 		}
62 	}
63 	argc -= optind;
64 	argv += optind;
65 
66 	for(i = 0; i < argc; i++) {
67 
68 		if (i)
69 			printf("\n");
70 		printf("===> \"%s\"\n", argv[i]);
71 		host = gethostarg(argv[i]);
72 
73 		errno = 0;
74 		h_errno = 0;
75 		gai_errno = 0;
76 		rrset_errno = 0;
77 
78 		if (nflag)
79 			n = getnetbyname(host);
80 		else
81 			n = getnetbyaddr(inet_network(host), AF_INET);
82 		if (n)
83 			print_netent(n);
84 		print_errors();
85 	}
86 
87 	return (0);
88 }
89