getgrent.c revision 1.2.4.2 1 /* $NetBSD: getgrent.c,v 1.2.4.2 1995/10/13 18:10:24 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 /*
38 * Copied from: lib/libc/gen/getgrent.c
39 * and then gutted, leaving only /etc/group support.
40 */
41
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <grp.h>
47
48 static FILE *_gr_fp;
49 static struct group _gr_group;
50 static int _gr_stayopen;
51 static int grscan(), start_gr();
52
53 #define MAXGRP 200
54 static char *members[MAXGRP];
55 #define MAXLINELENGTH 1024
56 static char line[MAXLINELENGTH];
57
58 struct group *
59 getgrent()
60 {
61 if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
62 return(NULL);
63 return(&_gr_group);
64 }
65
66 struct group *
67 getgrnam(name)
68 const char *name;
69 {
70 int rval;
71
72 if (!start_gr())
73 return(NULL);
74 rval = grscan(1, 0, name);
75 if (!_gr_stayopen)
76 endgrent();
77 return(rval ? &_gr_group : NULL);
78 }
79
80 struct group *
81 #ifdef __STDC__
82 getgrgid(gid_t gid)
83 #else
84 getgrgid(gid)
85 gid_t gid;
86 #endif
87 {
88 int rval;
89
90 if (!start_gr())
91 return(NULL);
92 rval = grscan(1, gid, NULL);
93 if (!_gr_stayopen)
94 endgrent();
95 return(rval ? &_gr_group : NULL);
96 }
97
98 static int
99 start_gr()
100 {
101 if (_gr_fp) {
102 rewind(_gr_fp);
103 return(1);
104 }
105 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
106 }
107
108 void
109 setgrent()
110 {
111 (void) setgroupent(0);
112 }
113
114 int
115 setgroupent(stayopen)
116 int stayopen;
117 {
118 if (!start_gr())
119 return(0);
120 _gr_stayopen = stayopen;
121 return(1);
122 }
123
124 void
125 endgrent()
126 {
127 if (_gr_fp) {
128 (void)fclose(_gr_fp);
129 _gr_fp = NULL;
130 }
131 }
132
133 static int
134 grscan(search, gid, name)
135 register int search, gid;
136 register char *name;
137 {
138 register char *cp, **m;
139 char *bp;
140
141 for (;;) {
142 if (!fgets(line, sizeof(line), _gr_fp))
143 return(0);
144 bp = line;
145 /* skip lines that are too big */
146 if (!strchr(line, '\n')) {
147 int ch;
148
149 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
150 ;
151 continue;
152 }
153 _gr_group.gr_name = strsep(&bp, ":\n");
154 if (search && name && strcmp(_gr_group.gr_name, name))
155 continue;
156 _gr_group.gr_passwd = strsep(&bp, ":\n");
157 if (!(cp = strsep(&bp, ":\n")))
158 continue;
159 _gr_group.gr_gid = atoi(cp);
160 if (search && name == NULL && _gr_group.gr_gid != gid)
161 continue;
162 cp = NULL;
163 if (bp == NULL)
164 continue;
165 for (m = _gr_group.gr_mem = members;; bp++) {
166 if (m == &members[MAXGRP - 1])
167 break;
168 if (*bp == ',') {
169 if (cp) {
170 *bp = '\0';
171 *m++ = cp;
172 cp = NULL;
173 }
174 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
175 if (cp) {
176 *bp = '\0';
177 *m++ = cp;
178 }
179 break;
180 } else if (cp == NULL)
181 cp = bp;
182 }
183 *m = NULL;
184 return(1);
185 }
186 /* NOTREACHED */
187 }
188