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