yptest.c revision 1.1 1 /* $NetBSD: yptest.c,v 1.1 1996/08/09 10:15:06 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Mats O Jansson
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/types.h>
35 #include <err.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include <rpc/rpc.h>
41 #include <rpc/xdr.h>
42 #include <rpcsvc/yp_prot.h>
43 #include <rpcsvc/ypclnt.h>
44
45 static int yptest_foreach __P((int, char *, int, char *, int, int *));
46
47 extern char *__progname; /* from crt0.o */
48
49 int
50 main(argc, argv)
51 int argc;
52 char **argv;
53 {
54 char *Domain, *Value, *Key2;
55 char *Map = "passwd.byname";
56 char *Key = "root";
57 int KeyLen, ValLen, Status, Order;
58 struct ypall_callback Callback;
59 struct ypmaplist *ypml, *y;
60
61 if (argc != 1) {
62 fprintf(stderr, "usage: %s\n", __progname);
63 exit(1);
64 }
65
66 if (yp_get_default_domain(&Domain))
67 errx(1, "can't get YP domain name");
68
69 printf("Test 1: yp_match\n");
70 KeyLen = strlen(Key);
71 Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen);
72 printf("%*.*s\n", ValLen, ValLen, Value);
73
74 printf("\nTest 2: yp_first\n");
75 Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen);
76 printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen, Value);
77
78 printf("\nTest 3: yp_next\n");
79 while (Status == 0) {
80 Status = yp_next(Domain, Map, Key2, KeyLen, &Key2,
81 &KeyLen, &Value, &ValLen);
82 if (Status == 0)
83 printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2,
84 ValLen, ValLen, Value);
85 }
86
87 printf("\nTest 4: yp_master\n");
88 Status = yp_master(Domain, Map, &Key2);
89 printf("%s\n", Key2);
90
91 printf("\nTest 5: yp_order\n");
92 Status = yp_order(Domain, Map, &Order);
93 printf("%d\n", Order);
94
95 printf("\nTest 6: yp_maplist\n");
96 ypml = NULL;
97 switch (yp_maplist(Domain, &ypml)) {
98 case 0:
99 for (y = ypml; y;) {
100 ypml = y;
101 printf("%s\n", ypml->ypml_name);
102 y = ypml->ypml_next;
103 }
104 }
105
106 printf("\nTest 7: yp_all\n");
107 Callback.foreach = (int(*)())yptest_foreach;
108 Status = yp_all(Domain, Map, &Callback);
109 }
110
111 static int
112 yptest_foreach(status, key, keylen, val, vallen, data)
113 int status;
114 char *key;
115 int keylen;
116 char *val;
117 int vallen;
118 int *data;
119 {
120
121 if (status == YP_NOMORE)
122 return 0;
123
124 /* key avslutas med NUL */
125 /* val avslutas med NUL */
126 key[keylen] = '\0';
127 val[vallen] = '\0';
128 printf("%s %s\n", key, val);
129 return 0;
130 }
131