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