xref: /netbsd/usr.bin/hesinfo/hesinfo.c (revision 6550d01e)
1 /*	$NetBSD: hesinfo.c,v 1.3 2004/01/05 23:23:34 jmmv 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.3 2004/01/05 23:23:34 jmmv 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	main __P((int, char **));
39 
40 int
41 main(argc, argv)
42 	int	argc;
43 	char  **argv;
44 {
45 	char  **list, **p, *bindname, *name, *type;
46 	int     lflag = 0, errflg = 0, bflag = 0, c;
47 	void   *context;
48 
49 	while ((c = getopt(argc, argv, "lb")) != -1) {
50 		switch (c) {
51 		case 'l':
52 			lflag = 1;
53 			break;
54 		case 'b':
55 			bflag = 1;
56 			break;
57 		default:
58 			errflg++;
59 			break;
60 		}
61 	}
62 	if (argc - optind != 2 || errflg) {
63 		fprintf(stderr, "usage: %s [-bl] name type\n", getprogname());
64 		fprintf(stderr, "\t-l selects long format\n");
65 		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
66 		exit(2);
67 	}
68 	name = argv[optind];
69 	type = argv[optind + 1];
70 
71 	if (hesiod_init(&context) < 0) {
72 		if (errno == ENOEXEC)
73 			warnx(
74 			    "hesiod_init: Invalid Hesiod configuration file.");
75 		else
76 			warn("hesiod_init");
77 	}
78 	/* Display bind name if requested. */
79 	if (bflag) {
80 		if (lflag)
81 			printf("hes_to_bind(%s, %s) expands to\n", name, type);
82 		bindname = hesiod_to_bind(context, name, type);
83 		if (!bindname) {
84 			if (lflag)
85 				printf("nothing\n");
86 			if (errno == ENOENT)
87 				warnx("hesiod_to_bind: Unknown rhs-extension.");
88 			else
89 				warn("hesiod_to_bind");
90 			exit(1);
91 		}
92 		printf("%s\n", bindname);
93 		free(bindname);
94 		if (lflag)
95 			printf("which ");
96 	}
97 	if (lflag)
98 		printf("resolves to\n");
99 
100 	/* Do the hesiod resolve and check for errors. */
101 	list = hesiod_resolve(context, name, type);
102 	if (!list) {
103 		if (lflag)
104 			printf("nothing\n");
105 		if (errno == ENOENT)
106 			warnx("hesiod_resolve: Hesiod name not found.");
107 		else
108 			warn("hesiod_resolve");
109 		exit(1);
110 	}
111 	/* Display the results. */
112 	for (p = list; *p; p++)
113 		printf("%s\n", *p);
114 
115 	hesiod_free_list(context, list);
116 	hesiod_end(context);
117 	exit(0);
118 }
119