Home | History | Annotate | Line # | Download | only in yptest
yptest.c revision 1.3
      1 /*	$NetBSD: yptest.c,v 1.3 1997/10/13 07:44:01 lukem 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/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: yptest.c,v 1.3 1997/10/13 07:44:01 lukem Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 #include <err.h>
     41 #include <stdio.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 int	main __P((int, char *[]));
     51 static	int yptest_foreach __P((int, char *, int, char *, int, char *));
     52 
     53 extern	char *__progname;		/* from crt0.o */
     54 
     55 int
     56 main(argc, argv)
     57 	int argc;
     58 	char *argv[];
     59 {
     60 	char *Domain, *Value, *Key2;
     61 	char *Map = "passwd.byname";
     62 	char *Key = "root";
     63 	int KeyLen, ValLen, Status, Order;
     64 	struct ypall_callback Callback;
     65 	struct ypmaplist *ypml, *y;
     66 
     67 	if (argc != 1) {
     68 		fprintf(stderr, "usage: %s\n", __progname);
     69 		exit(1);
     70 	}
     71 
     72 	if (yp_get_default_domain(&Domain))
     73 		errx(1, "can't get YP domain name");
     74 
     75 	printf("Test 1: yp_match\n");
     76 	KeyLen = strlen(Key);
     77 	Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen);
     78 	printf("%*.*s\n", ValLen, ValLen, Value);
     79 
     80 	printf("\nTest 2: yp_first\n");
     81 	Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen);
     82 	printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen, Value);
     83 
     84 	printf("\nTest 3: yp_next\n");
     85 	while (Status == 0) {
     86 		Status = yp_next(Domain, Map, Key2, KeyLen, &Key2,
     87 		    &KeyLen, &Value, &ValLen);
     88 		if (Status == 0)
     89 			printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2,
     90 			    ValLen, ValLen, Value);
     91 	}
     92 
     93 	printf("\nTest 4: yp_master\n");
     94 	Status = yp_master(Domain, Map, &Key2);
     95 	printf("%s\n", Key2);
     96 
     97 	printf("\nTest 5: yp_order\n");
     98 	Status = yp_order(Domain, Map, &Order);
     99 	printf("%d\n", Order);
    100 
    101 	printf("\nTest 6: yp_maplist\n");
    102 	ypml = NULL;
    103 	switch (yp_maplist(Domain, &ypml)) {
    104 	case 0:
    105 		for (y = ypml; y;) {
    106 			ypml = y;
    107 			printf("%s\n", ypml->ypml_name);
    108 			y = ypml->ypml_next;
    109 		}
    110 	}
    111 
    112 	printf("\nTest 7: yp_all\n");
    113 	Callback.foreach = yptest_foreach;
    114 	Status = yp_all(Domain, Map, &Callback);
    115 	exit(0);
    116 }
    117 
    118 static int
    119 yptest_foreach(status, key, keylen, val, vallen, data)
    120 	int status;
    121 	char *key;
    122 	int keylen;
    123 	char *val;
    124 	int vallen;
    125 	char *data;
    126 {
    127 
    128 	if (status == YP_NOMORE)
    129 		return 0;
    130 
    131 	/* key avslutas med NUL */
    132 	/* val avslutas med NUL */
    133 	key[keylen] = '\0';
    134 	val[vallen] = '\0';
    135 	printf("%s %s\n", key, val);
    136 	return 0;
    137 }
    138