11.5Ssevan/*	$NetBSD: hesinfo.c,v 1.5 2016/09/05 00:40:28 sevan 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.5Ssevan__RCSID("$NetBSD: hesinfo.c,v 1.5 2016/09/05 00:40:28 sevan 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.1Slukemint
391.4Smattmain(int argc, char **argv)
401.1Slukem{
411.1Slukem	char  **list, **p, *bindname, *name, *type;
421.1Slukem	int     lflag = 0, errflg = 0, bflag = 0, c;
431.1Slukem	void   *context;
441.1Slukem
451.1Slukem	while ((c = getopt(argc, argv, "lb")) != -1) {
461.1Slukem		switch (c) {
471.1Slukem		case 'l':
481.1Slukem			lflag = 1;
491.1Slukem			break;
501.1Slukem		case 'b':
511.1Slukem			bflag = 1;
521.1Slukem			break;
531.1Slukem		default:
541.1Slukem			errflg++;
551.1Slukem			break;
561.1Slukem		}
571.1Slukem	}
581.1Slukem	if (argc - optind != 2 || errflg) {
591.3Sjmmv		fprintf(stderr, "usage: %s [-bl] name type\n", getprogname());
601.1Slukem		fprintf(stderr, "\t-l selects long format\n");
611.1Slukem		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
621.1Slukem		exit(2);
631.1Slukem	}
641.1Slukem	name = argv[optind];
651.1Slukem	type = argv[optind + 1];
661.1Slukem
671.1Slukem	if (hesiod_init(&context) < 0) {
681.1Slukem		if (errno == ENOEXEC)
691.1Slukem			warnx(
701.1Slukem			    "hesiod_init: Invalid Hesiod configuration file.");
711.1Slukem		else
721.1Slukem			warn("hesiod_init");
731.1Slukem	}
741.1Slukem	/* Display bind name if requested. */
751.1Slukem	if (bflag) {
761.1Slukem		if (lflag)
771.1Slukem			printf("hes_to_bind(%s, %s) expands to\n", name, type);
781.1Slukem		bindname = hesiod_to_bind(context, name, type);
791.1Slukem		if (!bindname) {
801.1Slukem			if (lflag)
811.1Slukem				printf("nothing\n");
821.1Slukem			if (errno == ENOENT)
831.1Slukem				warnx("hesiod_to_bind: Unknown rhs-extension.");
841.1Slukem			else
851.1Slukem				warn("hesiod_to_bind");
861.1Slukem			exit(1);
871.1Slukem		}
881.1Slukem		printf("%s\n", bindname);
891.1Slukem		free(bindname);
901.1Slukem		if (lflag)
911.1Slukem			printf("which ");
921.1Slukem	}
931.1Slukem	if (lflag)
941.1Slukem		printf("resolves to\n");
951.1Slukem
961.1Slukem	/* Do the hesiod resolve and check for errors. */
971.1Slukem	list = hesiod_resolve(context, name, type);
981.1Slukem	if (!list) {
991.1Slukem		if (lflag)
1001.1Slukem			printf("nothing\n");
1011.1Slukem		if (errno == ENOENT)
1021.1Slukem			warnx("hesiod_resolve: Hesiod name not found.");
1031.1Slukem		else
1041.1Slukem			warn("hesiod_resolve");
1051.1Slukem		exit(1);
1061.1Slukem	}
1071.1Slukem	/* Display the results. */
1081.1Slukem	for (p = list; *p; p++)
1091.1Slukem		printf("%s\n", *p);
1101.1Slukem
1111.1Slukem	hesiod_free_list(context, list);
1121.1Slukem	hesiod_end(context);
1131.1Slukem	exit(0);
1141.1Slukem}
115