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