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