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