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