hesinfo.c revision 1.4
11.4Smatt/*	$NetBSD: hesinfo.c,v 1.4 2012/03/20 20:34:57 matt Exp $	*/
21.1Slukem
31.1Slukem/* Copyright 1988, 1996 by the Massachusetts Institute of Technology.
41.1Slukem *
51.1Slukem * Permission to use, copy, modify, and distribute this
61.1Slukem * software and its documentation for any purpose and without
71.1Slukem * fee is hereby granted, provided that the above copyright
81.1Slukem * notice appear in all copies and that both that copyright
91.1Slukem * notice and this permission notice appear in supporting
101.1Slukem * documentation, and that the name of M.I.T. not be used in
111.1Slukem * advertising or publicity pertaining to distribution of the
121.1Slukem * software without specific, written prior permission.
131.1Slukem * M.I.T. makes no representations about the suitability of
141.1Slukem * this software for any purpose.  It is provided "as is"
151.1Slukem * without express or implied warranty.
161.1Slukem */
171.1Slukem
181.1Slukem/* This file is a simple driver for the Hesiod library. */
191.1Slukem
201.1Slukem
211.1Slukem#include <sys/cdefs.h>
221.1Slukem#ifndef lint
231.1Slukem#if 0
241.1Slukemstatic char rcsid[] = "#Id: hesinfo.c,v 1.8 1996/12/08 21:29:54 ghudson Exp #";
251.1Slukem#else
261.4Smatt__RCSID("$NetBSD: hesinfo.c,v 1.4 2012/03/20 20:34:57 matt Exp $");
271.1Slukem#endif
281.1Slukem#endif /* not lint */
291.1Slukem
301.1Slukem#include <err.h>
311.1Slukem#include <errno.h>
321.1Slukem#include <hesiod.h>
331.1Slukem#include <stdio.h>
341.1Slukem#include <stdlib.h>
351.1Slukem#include <string.h>
361.1Slukem#include <unistd.h>
371.1Slukem
381.4Smattint	main(int, char **);
391.1Slukem
401.1Slukemint
411.4Smattmain(int argc, char **argv)
421.1Slukem{
431.1Slukem	char  **list, **p, *bindname, *name, *type;
441.1Slukem	int     lflag = 0, errflg = 0, bflag = 0, c;
451.1Slukem	void   *context;
461.1Slukem
471.1Slukem	while ((c = getopt(argc, argv, "lb")) != -1) {
481.1Slukem		switch (c) {
491.1Slukem		case 'l':
501.1Slukem			lflag = 1;
511.1Slukem			break;
521.1Slukem		case 'b':
531.1Slukem			bflag = 1;
541.1Slukem			break;
551.1Slukem		default:
561.1Slukem			errflg++;
571.1Slukem			break;
581.1Slukem		}
591.1Slukem	}
601.1Slukem	if (argc - optind != 2 || errflg) {
611.3Sjmmv		fprintf(stderr, "usage: %s [-bl] name type\n", getprogname());
621.1Slukem		fprintf(stderr, "\t-l selects long format\n");
631.1Slukem		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
641.1Slukem		exit(2);
651.1Slukem	}
661.1Slukem	name = argv[optind];
671.1Slukem	type = argv[optind + 1];
681.1Slukem
691.1Slukem	if (hesiod_init(&context) < 0) {
701.1Slukem		if (errno == ENOEXEC)
711.1Slukem			warnx(
721.1Slukem			    "hesiod_init: Invalid Hesiod configuration file.");
731.1Slukem		else
741.1Slukem			warn("hesiod_init");
751.1Slukem	}
761.1Slukem	/* Display bind name if requested. */
771.1Slukem	if (bflag) {
781.1Slukem		if (lflag)
791.1Slukem			printf("hes_to_bind(%s, %s) expands to\n", name, type);
801.1Slukem		bindname = hesiod_to_bind(context, name, type);
811.1Slukem		if (!bindname) {
821.1Slukem			if (lflag)
831.1Slukem				printf("nothing\n");
841.1Slukem			if (errno == ENOENT)
851.1Slukem				warnx("hesiod_to_bind: Unknown rhs-extension.");
861.1Slukem			else
871.1Slukem				warn("hesiod_to_bind");
881.1Slukem			exit(1);
891.1Slukem		}
901.1Slukem		printf("%s\n", bindname);
911.1Slukem		free(bindname);
921.1Slukem		if (lflag)
931.1Slukem			printf("which ");
941.1Slukem	}
951.1Slukem	if (lflag)
961.1Slukem		printf("resolves to\n");
971.1Slukem
981.1Slukem	/* Do the hesiod resolve and check for errors. */
991.1Slukem	list = hesiod_resolve(context, name, type);
1001.1Slukem	if (!list) {
1011.1Slukem		if (lflag)
1021.1Slukem			printf("nothing\n");
1031.1Slukem		if (errno == ENOENT)
1041.1Slukem			warnx("hesiod_resolve: Hesiod name not found.");
1051.1Slukem		else
1061.1Slukem			warn("hesiod_resolve");
1071.1Slukem		exit(1);
1081.1Slukem	}
1091.1Slukem	/* Display the results. */
1101.1Slukem	for (p = list; *p; p++)
1111.1Slukem		printf("%s\n", *p);
1121.1Slukem
1131.1Slukem	hesiod_free_list(context, list);
1141.1Slukem	hesiod_end(context);
1151.1Slukem	exit(0);
1161.1Slukem}
117