xref: /freebsd/usr.bin/hesinfo/hesinfo.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: hesinfo.c,v 1.1 1999/01/25 22:45:55 lukem 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 #include <err.h>
23 #include <errno.h>
24 #include <hesiod.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 int
31 main(int argc, char **argv)
32 {
33 	char  **list, **p, *bindname, *name, *type;
34 	int     lflag = 0, errflg = 0, bflag = 0, c;
35 	void   *context;
36 
37 	while ((c = getopt(argc, argv, "lb")) != -1) {
38 		switch (c) {
39 		case 'l':
40 			lflag = 1;
41 			break;
42 		case 'b':
43 			bflag = 1;
44 			break;
45 		default:
46 			errflg++;
47 			break;
48 		}
49 	}
50 	if (argc - optind != 2 || errflg) {
51 		fprintf(stderr, "usage: hesinfo [-bl] name type\n");
52 		fprintf(stderr, "\t-l selects long format\n");
53 		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
54 		exit(2);
55 	}
56 	name = argv[optind];
57 	type = argv[optind + 1];
58 
59 	if (hesiod_init(&context) < 0) {
60 		if (errno == ENOEXEC)
61 			warnx(
62 			    "hesiod_init: Invalid Hesiod configuration file.");
63 		else
64 			warn("hesiod_init");
65 	}
66 	/* Display bind name if requested. */
67 	if (bflag) {
68 		if (lflag)
69 			printf("hes_to_bind(%s, %s) expands to\n", name, type);
70 		bindname = hesiod_to_bind(context, name, type);
71 		if (!bindname) {
72 			if (lflag)
73 				printf("nothing\n");
74 			if (errno == ENOENT)
75 				warnx("hesiod_to_bind: Unknown rhs-extension.");
76 			else
77 				warn("hesiod_to_bind");
78 			exit(1);
79 		}
80 		printf("%s\n", bindname);
81 		free(bindname);
82 		if (lflag)
83 			printf("which ");
84 	}
85 	if (lflag)
86 		printf("resolves to\n");
87 
88 	/* Do the hesiod resolve and check for errors. */
89 	list = hesiod_resolve(context, name, type);
90 	if (!list) {
91 		if (lflag)
92 			printf("nothing\n");
93 		if (errno == ENOENT)
94 			warnx("hesiod_resolve: Hesiod name not found.");
95 		else
96 			warn("hesiod_resolve");
97 		exit(1);
98 	}
99 	/* Display the results. */
100 	for (p = list; *p; p++)
101 		printf("%s\n", *p);
102 
103 	hesiod_free_list(context, list);
104 	hesiod_end(context);
105 	exit(0);
106 }
107