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