Home | History | Annotate | Line # | Download | only in ypcat
ypcat.c revision 1.1
      1 #include <sys/param.h>
      2 #include <sys/types.h>
      3 #include <sys/socket.h>
      4 #include <stdio.h>
      5 #include <ctype.h>
      6 
      7 #include <rpc/rpc.h>
      8 #include <rpc/xdr.h>
      9 #include <rpcsvc/yp_prot.h>
     10 #include <rpcsvc/ypclnt.h>
     11 
     12 struct ypalias {
     13 	char *alias, *name;
     14 } ypaliases[] = {
     15 	{ "passwd", "passwd.byname" },
     16 	{ "group", "group.byname" },
     17 	{ "networks", "networks.byaddr" },
     18 	{ "hosts", "hosts.byaddr" },
     19 	{ "protocols", "protocols.bynumber" },
     20 	{ "services", "services.byname" },
     21 	{ "aliases", "mail.aliases" },
     22 	{ "ethers", "ethers.byname" },
     23 };
     24 
     25 int key;
     26 
     27 usage()
     28 {
     29 	fprintf(stderr, "Usage:\n");
     30 	fprintf(stderr, "\typcat [-k] [-d domainname] [-t] mapname\n");
     31 	fprintf(stderr, "\typcat -x\n");
     32 	exit(1);
     33 }
     34 
     35 printit(instatus, inkey, inkeylen, inval, invallen, indata)
     36 int instatus;
     37 char *inkey;
     38 int inkeylen;
     39 char *inval;
     40 int invallen;
     41 char *indata;
     42 {
     43 	if(instatus != YP_TRUE)
     44 		return instatus;
     45 	if(key)
     46 		printf("%*.*s ", inkeylen, inkeylen, inkey);
     47 	printf("%*.*s\n", invallen, invallen, inval);
     48 	return 0;
     49 }
     50 
     51 int
     52 main(argc, argv)
     53 char **argv;
     54 {
     55 	char *domainname;
     56 	struct ypall_callback ypcb;
     57 	char *inmap;
     58 	extern char *optarg;
     59 	extern int optind;
     60 	int notrans;
     61 	int c, r, i;
     62 
     63 	notrans = key = 0;
     64 	yp_get_default_domain(&domainname);
     65 
     66 	while( (c=getopt(argc, argv, "xd:kt")) != -1)
     67 		switch(c) {
     68 		case 'x':
     69 			for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
     70 				printf("Use \"%s\" for \"%s\"\n",
     71 					ypaliases[i].alias,
     72 					ypaliases[i].name);
     73 			exit(0);
     74 		case 'd':
     75 			domainname = optarg;
     76 			break;
     77 		case 't':
     78 			notrans++;
     79 			break;
     80 		case 'k':
     81 			key++;
     82 			break;
     83 		default:
     84 			usage();
     85 		}
     86 
     87 	if(optind + 1 != argc )
     88 		usage();
     89 
     90 	inmap = argv[optind];
     91 	for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
     92 		if( strcmp(inmap, ypaliases[i].alias) == 0)
     93 			inmap = ypaliases[i].name;
     94 	ypcb.foreach = printit;
     95 	ypcb.data = NULL;
     96 
     97 	r = yp_all(domainname, inmap, &ypcb);
     98 	switch(r) {
     99 	case 0:
    100 		break;
    101 	case YPERR_YPBIND:
    102 		fprintf(stderr, "ypcat: not running ypbind\n");
    103 		exit(1);
    104 	default:
    105 		fprintf(stderr, "No such map %s. Reason: %s\n",
    106 			inmap, yperr_string(r));
    107 		exit(1);
    108 	}
    109 	exit(0);
    110 }
    111