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