xref: /netbsd-src/usr.bin/hesinfo/hesinfo.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: hesinfo.c,v 1.5 2016/09/05 00:40:28 sevan Exp $	*/
2 
3 /* Copyright 1988, 1996 by the Massachusetts Institute of Technology.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17 
18 /* This file is a simple driver for the Hesiod library. */
19 
20 
21 #include <sys/cdefs.h>
22 #ifndef lint
23 #if 0
24 static char rcsid[] = "#Id: hesinfo.c,v 1.8 1996/12/08 21:29:54 ghudson Exp #";
25 #else
26 __RCSID("$NetBSD: hesinfo.c,v 1.5 2016/09/05 00:40:28 sevan Exp $");
27 #endif
28 #endif /* not lint */
29 
30 #include <err.h>
31 #include <errno.h>
32 #include <hesiod.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 int
39 main(int argc, char **argv)
40 {
41 	char  **list, **p, *bindname, *name, *type;
42 	int     lflag = 0, errflg = 0, bflag = 0, c;
43 	void   *context;
44 
45 	while ((c = getopt(argc, argv, "lb")) != -1) {
46 		switch (c) {
47 		case 'l':
48 			lflag = 1;
49 			break;
50 		case 'b':
51 			bflag = 1;
52 			break;
53 		default:
54 			errflg++;
55 			break;
56 		}
57 	}
58 	if (argc - optind != 2 || errflg) {
59 		fprintf(stderr, "usage: %s [-bl] name type\n", getprogname());
60 		fprintf(stderr, "\t-l selects long format\n");
61 		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
62 		exit(2);
63 	}
64 	name = argv[optind];
65 	type = argv[optind + 1];
66 
67 	if (hesiod_init(&context) < 0) {
68 		if (errno == ENOEXEC)
69 			warnx(
70 			    "hesiod_init: Invalid Hesiod configuration file.");
71 		else
72 			warn("hesiod_init");
73 	}
74 	/* Display bind name if requested. */
75 	if (bflag) {
76 		if (lflag)
77 			printf("hes_to_bind(%s, %s) expands to\n", name, type);
78 		bindname = hesiod_to_bind(context, name, type);
79 		if (!bindname) {
80 			if (lflag)
81 				printf("nothing\n");
82 			if (errno == ENOENT)
83 				warnx("hesiod_to_bind: Unknown rhs-extension.");
84 			else
85 				warn("hesiod_to_bind");
86 			exit(1);
87 		}
88 		printf("%s\n", bindname);
89 		free(bindname);
90 		if (lflag)
91 			printf("which ");
92 	}
93 	if (lflag)
94 		printf("resolves to\n");
95 
96 	/* Do the hesiod resolve and check for errors. */
97 	list = hesiod_resolve(context, name, type);
98 	if (!list) {
99 		if (lflag)
100 			printf("nothing\n");
101 		if (errno == ENOENT)
102 			warnx("hesiod_resolve: Hesiod name not found.");
103 		else
104 			warn("hesiod_resolve");
105 		exit(1);
106 	}
107 	/* Display the results. */
108 	for (p = list; *p; p++)
109 		printf("%s\n", *p);
110 
111 	hesiod_free_list(context, list);
112 	hesiod_end(context);
113 	exit(0);
114 }
115