Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / ypcat / ypcat.c
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/usr.bin/ypcat/ypcat.c,v 1.4.2.2 2002/02/15 00:46:56 des Exp $
30  * $DragonFly: src/usr.bin/ypcat/ypcat.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <rpc/rpc.h>
44 #include <rpc/xdr.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47
48 struct ypalias {
49         char *alias, *name;
50 } ypaliases[] = {
51         { "passwd", "passwd.byname" },
52         { "master.passwd", "master.passwd.byname" },
53         { "group", "group.byname" },
54         { "networks", "networks.byaddr" },
55         { "hosts", "hosts.byaddr" },
56         { "protocols", "protocols.bynumber" },
57         { "services", "services.byname" },
58         { "aliases", "mail.aliases" },
59         { "ethers", "ethers.byname" },
60 };
61
62 int key;
63
64 static void
65 usage()
66 {
67         fprintf(stderr, "%s\n%s\n",
68                 "usage: ypcat [-k] [-d domainname] [-t] mapname",
69                 "       ypcat -x");
70         exit(1);
71 }
72
73 int
74 printit(instatus, inkey, inkeylen, inval, invallen, indata)
75 int instatus;
76 char *inkey;
77 int inkeylen;
78 char *inval;
79 int invallen;
80 char *indata;
81 {
82         if (instatus != YP_TRUE)
83                 return (instatus);
84         if (key)
85                 printf("%*.*s ", inkeylen, inkeylen, inkey);
86         printf("%*.*s\n", invallen, invallen, inval);
87         return (0);
88 }
89
90 int
91 main(argc, argv)
92 char **argv;
93 {
94         char *domainname = NULL;
95         struct ypall_callback ypcb;
96         char *inmap;
97         int notrans;
98         int c, r, i;
99
100         notrans = key = 0;
101
102         while ((c = getopt(argc, argv, "xd:kt")) != -1)
103                 switch (c) {
104                 case 'x':
105                         for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
106                                 printf("Use \"%s\" for \"%s\"\n",
107                                         ypaliases[i].alias,
108                                         ypaliases[i].name);
109                         exit(0);
110                 case 'd':
111                         domainname = optarg;
112                         break;
113                 case 't':
114                         notrans++;
115                         break;
116                 case 'k':
117                         key++;
118                         break;
119                 default:
120                         usage();
121                 }
122
123         if (optind + 1 != argc)
124                 usage();
125
126         if (!domainname)
127                 yp_get_default_domain(&domainname);
128
129         inmap = argv[optind];
130         for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
131                 if (strcmp(inmap, ypaliases[i].alias) == 0)
132                         inmap = ypaliases[i].name;
133         ypcb.foreach = printit;
134         ypcb.data = NULL;
135
136         r = yp_all(domainname, inmap, &ypcb);
137         switch (r) {
138         case 0:
139                 break;
140         case YPERR_YPBIND:
141                 errx(1, "not running ypbind");
142         default:
143                 errx(1, "no such map %s. reason: %s", inmap, yperr_string(r));
144         }
145         exit(0);
146 }