Replace local array size calculations with NELEM().
[dragonfly.git] / usr.bin / ypmatch / ypmatch.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/ypmatch/ypmatch.c,v 1.7.2.2 2002/02/15 00:46:56 des Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include <rpc/rpc.h>
43 #include <rpc/xdr.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46
47 static const struct ypalias {
48         const char *alias, *name;
49 } ypaliases[] = {
50         { "passwd", "passwd.byname" },
51         { "master.passwd", "master.passwd.byname" },
52         { "group", "group.byname" },
53         { "networks", "networks.byaddr" },
54         { "hosts", "hosts.byname" },
55         { "protocols", "protocols.bynumber" },
56         { "services", "services.byname" },
57         { "aliases", "mail.aliases" },
58         { "ethers", "ethers.byname" },
59 };
60
61 static void
62 usage(void)
63 {
64         fprintf(stderr, "%s\n%s\n",
65                 "usage: ypmatch [-d domain] [-t] [-k] key [key ...] mname",
66                 "       ypmatch -x");
67         exit(1);
68 }
69
70 int
71 main(int argc, char *argv[])
72 {
73         char *domainname = NULL;
74         char *inkey, *inmap, *outbuf;
75         int outbuflen, key, notrans;
76         int c, r;
77         u_int i;
78
79         notrans = key = 0;
80
81         while ((c = getopt(argc, argv, "xd:kt")) != -1)
82                 switch (c) {
83                 case 'x':
84                         for (i = 0; i < NELEM(ypaliases); i++)
85                                 printf("Use \"%s\" for \"%s\"\n",
86                                         ypaliases[i].alias,
87                                         ypaliases[i].name);
88                         exit(0);
89                 case 'd':
90                         domainname = optarg;
91                         break;
92                 case 't':
93                         notrans++;
94                         break;
95                 case 'k':
96                         key++;
97                         break;
98                 default:
99                         usage();
100                 }
101
102         if ((argc-optind) < 2)
103                 usage();
104
105         if (!domainname)
106                 yp_get_default_domain(&domainname);
107
108         inmap = argv[argc-1];
109         for (i = 0; (!notrans) && i < NELEM(ypaliases); i++)
110                 if (strcmp(inmap, ypaliases[i].alias) == 0)
111                         inmap = __DECONST(char *, ypaliases[i].name);
112         for (; optind < argc-1; optind++) {
113                 inkey = argv[optind];
114
115                 r = yp_match(domainname, inmap, inkey,
116                         strlen(inkey), &outbuf, &outbuflen);
117                 switch (r) {
118                 case 0:
119                         if (key)
120                                 printf("%s ", inkey);
121                         printf("%*.*s\n", outbuflen, outbuflen, outbuf);
122                         break;
123                 case YPERR_YPBIND:
124                         errx(1, "not running ypbind");
125                 default:
126                         errx(1, "can't match key %s in map %s. reason: %s",
127                                 inkey, inmap, yperr_string(r));
128                 }
129         }
130         exit(0);
131 }