1/* $NetBSD: ypcat.c,v 1.15 2011/01/12 18:28:19 christos Exp $	*/
2
3/*
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: ypcat.c,v 1.15 2011/01/12 18:28:19 christos Exp $");
32#endif
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <sys/errno.h>
38#include <ctype.h>
39#include <err.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include <rpc/rpc.h>
46#include <rpc/xdr.h>
47#include <rpcsvc/yp_prot.h>
48#include <rpcsvc/ypclnt.h>
49
50#include "ypalias_init.h"
51
52static int	printit(int, char *, int, char *, int, char *);
53static void	usage(void) __attribute__((__noreturn__));
54
55static int	compressspace;
56
57
58int
59main(int argc, char *argv[])
60{
61	char *domainname;
62	struct ypall_callback ypcb;
63	const char *inmap;
64	int notrans;
65	int c, r;
66	size_t i;
67	const struct ypalias *ypaliases;
68	int key;
69
70	setprogname(*argv);
71	domainname = NULL;
72	notrans = key = 0;
73	ypaliases = ypalias_init();
74	while((c = getopt(argc, argv, "d:kstx")) != -1) {
75		switch (c) {
76		case 'd':
77			domainname = optarg;
78			break;
79
80		case 'k':
81			key++;
82			break;
83
84		case 's':
85			compressspace++;
86			break;
87
88		case 'x':
89			for (i = 0; ypaliases[i].alias; i++)
90				printf("Use \"%s\" for \"%s\"\n",
91				    ypaliases[i].alias,
92				    ypaliases[i].name);
93			return 0;
94
95		case 't':
96			notrans++;
97			break;
98
99		default:
100			usage();
101		}
102	}
103
104	argc -= optind;
105	argv += optind;
106
107	if (argc != 1)
108		usage();
109
110	if (domainname == NULL)
111		yp_get_default_domain(&domainname);
112
113	inmap = argv[0];
114	if (notrans == 0) {
115		for (i = 0; ypaliases[i].alias; i++)
116			if (strcmp(inmap, ypaliases[i].alias) == 0)
117				inmap = ypaliases[i].name;
118	}
119
120	ypcb.foreach = printit;
121	ypcb.data = key ? (void *)&key : NULL;
122
123	r = yp_all(domainname, inmap, &ypcb);
124	switch (r) {
125	case 0:
126		break;
127
128	case YPERR_YPBIND:
129		errx(1, "not running ypbind");
130
131	default:
132		errx(1, "no such map %s.  Reason: %s", inmap, yperr_string(r));
133	}
134	return 0;
135}
136
137static int
138printit(int instatus, char *inkey, int inkeylen, char *inval,
139    int invallen, char *indata)
140{
141
142	if (instatus != YP_TRUE)
143		return instatus;
144	if (indata)
145		(void)printf("%*.*s", inkeylen, inkeylen, inkey);
146	if (invallen) {
147		if (indata)
148			(void)putc(' ', stdout);
149		if (compressspace) {
150			int i;
151			int hadspace = 0;
152
153			for (i = 0; i < invallen; i++) {
154				if (isspace((unsigned char)inval[i])) {
155					if (hadspace)
156						continue;
157					hadspace = 1;
158					(void)putc(' ', stdout);
159				} else {
160					hadspace = 0;
161					(void)putc(inval[i], stdout);
162				}
163			}
164		} else
165			(void)printf("%*.*s", invallen, invallen, inval);
166	}
167	(void)putc('\n', stdout);
168	return 0;
169}
170
171static void
172usage(void)
173{
174
175	(void)fprintf(stderr, "Usage: %s [-kst] [-d domainname] mapname\n",
176	    getprogname());
177	(void)fprintf(stderr, "       %s -x\n", getprogname());
178	exit(1);
179}
180