xref: /illumos-gate/usr/src/cmd/ypcmd/nick.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1986-1992 by Sun Microsystems Inc.
24  */
25 
26 #include <stdio.h>
27 #include <rpc/rpc.h>
28 #include <rpcsvc/yp_prot.h>
29 #include <rpcsvc/ypclnt.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #define	MAXMAPS 500
34 /* number of nickname file entries arbitrarily limited */
35 
36 static char NICKFILE[] = "/var/yp/nicknames";
37 static char *transtable[MAXMAPS];
38 
39 /*
40  * This will read nicknames from file /var/yp/nicknames
41  * and print it out or save it for future use.
42  */
43 void
maketable(dump)44 maketable(dump)
45 int dump;
46 {
47 	FILE *nickp;
48 	int i = 0;
49 	char nickbuf[2*YPMAXMAP+3], nickname[YPMAXMAP+1], mapname[YPMAXMAP+1];
50 
51 	if ((nickp = fopen(NICKFILE, "r")) == NULL) {
52 		(void) fprintf(stderr, "nickname file %s does not exist\n",
53 			NICKFILE);
54 		exit(1);
55 	}
56 	while (fgets(nickbuf, YPMAXMAP, nickp) != NULL) {
57 		if (strchr(nickbuf, '\n') == NULL) {
58 			(void) fprintf(stderr,
59 				"garbled nickname file %s\n", NICKFILE);
60 			exit(1);
61 		}
62 		(void) memset(nickname, 0, YPMAXMAP+1);
63 		(void) memset(mapname, 0, YPMAXMAP+1);
64 		if (sscanf(nickbuf, "%s %s\n", nickname, mapname) != 2) {
65 			(void) fprintf(stderr,
66 				"garbled nickname file %s\n", NICKFILE);
67 			exit(1);
68 		}
69 		if (!dump) {
70 			transtable[i] = strdup(nickname);
71 			transtable[i+1] = strdup(mapname);
72 			i += 2;
73 			transtable[i] = NULL;
74 		} else {
75 			printf("Use \"%s\"\tfor map \"%s\"\n",
76 				nickname, mapname);
77 		}
78 	}
79 	fclose(nickp);
80 }
81 
82 /*
83  * This will get the mapname for a given nickname from the file.
84  */
85 int
getmapname(nick,map)86 getmapname(nick, map)
87 char *nick, *map;
88 {
89 	FILE *nickp;
90 	char nickbuf[2*YPMAXMAP+3], nickname[YPMAXMAP+1];
91 
92 	if ((nickp = fopen(NICKFILE, "r")) == NULL)
93 		return (0);
94 	while (fgets(nickbuf, YPMAXMAP, nickp) != NULL) {
95 		if (strchr(nickbuf, '\n') == NULL) {
96 			fclose(nickp);
97 			return (0);
98 		}
99 		(void) memset(nickname, 0, YPMAXMAP+1);
100 		(void) memset(map, 0, YPMAXMAP+1);
101 		if (sscanf(nickbuf, "%s %s\n", nickname, map) != 2) {
102 			fclose(nickp);
103 			return (0);
104 		}
105 		if (strcmp(nick, nickname) == 0) {
106 			fclose(nickp);
107 			return (1);
108 		}
109 	}
110 	fclose(nickp);
111 	return (0);
112 }
113