8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / ypcmd / revnetgroup / getgroup.c
blob7a48de096a48dfe27a6eaa0debbe7e9740d5599d
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
24 * Copyright (c) 1996, by Sun Microsystems, Inc.
25 * All rights reserved.
28 #ident "%Z%%M% %I% %E% SMI" /* SMI4.1 1.5 */
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include "table.h"
34 #include "util.h"
35 #include "getgroup.h"
37 #define MAXGROUPLEN 1024
40 * Stolen mostly, from getnetgrent.c
42 * my_getgroup() performs the same function as _getgroup(), but operates
43 * on /etc/netgroup directly, rather than doing yp lookups.
45 * /etc/netgroup must first loaded into a hash table so the matching
46 * function can look up lines quickly.
50 /* To check for cycles in netgroups */
51 struct list {
52 char *name;
53 struct list *nxt;
57 extern stringtable ngtable; /* stored info from /etc/netgroup */
59 static struct grouplist *grouplist; /* stores a list of users in a group */
61 static char *any();
62 static char *match();
63 static char *fill();
64 static void freegrouplist();
65 static void doit();
69 static void
70 freegrouplist()
72 struct grouplist *gl;
74 for (gl = grouplist; gl != NULL; gl = gl->gl_nxt) {
75 FREE(gl->gl_name);
76 FREE(gl->gl_domain);
77 FREE(gl->gl_machine);
78 FREE(gl);
80 grouplist = NULL;
86 struct grouplist *
87 my_getgroup(group)
88 char *group;
90 freegrouplist();
91 doit(group, (struct list *) NULL);
92 return (grouplist);
100 * recursive function to find the members of netgroup "group". "list" is
101 * the path followed through the netgroups so far, to check for cycles.
103 static void
104 doit(group, list)
105 char *group;
106 struct list *list;
108 register char *p, *q;
109 register struct list *ls;
110 struct list tmplist;
111 char *val;
112 struct grouplist *gpls;
116 * check for non-existing groups
118 if ((val = match(group)) == NULL) {
119 return;
124 * check for cycles
126 for (ls = list; ls != NULL; ls = ls->nxt) {
127 if (strcmp(ls->name, group) == 0) {
128 (void) fprintf(stderr,
129 "Cycle detected in /etc/netgroup: %s.\n",
130 group);
131 return;
136 ls = &tmplist;
137 ls->name = group;
138 ls->nxt = list;
139 list = ls;
141 p = val;
142 while (p != NULL) {
143 while (*p == ' ' || *p == '\t')
144 p++;
145 if (*p == EOS || *p == '#')
146 break;
147 if (*p == '(') {
148 gpls = MALLOC(struct grouplist);
149 p++;
151 if (!(p = fill(p, &gpls->gl_machine, ','))) {
152 goto syntax_error;
154 if (!(p = fill(p, &gpls->gl_name, ','))) {
155 goto syntax_error;
157 if (!(p = fill(p, &gpls->gl_domain, ')'))) {
158 goto syntax_error;
160 gpls->gl_nxt = grouplist;
161 grouplist = gpls;
162 } else {
163 q = any(p, " \t\n#");
164 if (q && *q == '#')
165 break;
166 *q = EOS;
167 doit(p, list);
168 *q = ' ';
170 p = any(p, " \t");
172 return;
174 syntax_error:
175 (void) fprintf(stderr, "syntax error in /etc/netgroup\n");
176 (void) fprintf(stderr, "--- %s %s\n", group, val);
183 * Fill a buffer "target" selectively from buffer "start".
184 * "termchar" terminates the information in start, and preceding
185 * or trailing white space is ignored. If the buffer "start" is
186 * empty, "target" is filled with "*". The location just after the
187 * terminating character is returned.
189 static char *
190 fill(start, target, termchar)
191 char *start;
192 char **target;
193 char termchar;
195 register char *p;
196 register char *q;
197 register char *r;
198 int size;
200 for (p = start; *p == ' ' || *p == '\t'; p++)
202 r = strchr(p, termchar);
203 if (r == (char *)NULL) {
204 return ((char *)NULL);
206 if (p == r) {
207 *target = NULL;
208 } else {
209 for (q = r-1; *q == ' ' || *q == '\t'; q--)
211 size = q-p+1;
212 STRNCPY(*target, p, size);
214 return (r+1);
219 * scans cp, looking for a match with any character
220 * in match. Returns pointer to place in cp that matched
221 * (or NULL if no match)
223 static char *
224 any(cp, match)
225 register char *cp;
226 char *match;
228 register char *mp, c;
230 while (c = *cp) {
231 for (mp = match; *mp; mp++)
232 if (*mp == c)
233 return (cp);
234 cp++;
236 return (NULL);
242 * The equivalent of yp_match. Returns the match, or NULL if there is none.
244 static char *
245 match(group)
246 char *group;
248 return (lookup(ngtable, group));