getgrent.c revision 1.3
1/*	$NetBSD: getgrent.c,v 1.3 1999/03/13 19:08:44 sommerfe 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
48static FILE *_gr_fp;
49static struct group _gr_group;
50static int _gr_stayopen;
51static int grscan __P((int, int, const char *));
52static int start_gr __P((void));
53
54#define	MAXGRP		200
55static char *members[MAXGRP];
56#define	MAXLINELENGTH	1024
57static char line[MAXLINELENGTH];
58
59struct group *
60getgrent()
61{
62	if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
63		return(NULL);
64	return(&_gr_group);
65}
66
67struct group *
68getgrnam(name)
69	const char *name;
70{
71	int rval;
72
73	if (!start_gr())
74		return(NULL);
75	rval = grscan(1, 0, name);
76	if (!_gr_stayopen)
77		endgrent();
78	return(rval ? &_gr_group : NULL);
79}
80
81struct group *
82#ifdef __STDC__
83getgrgid(gid_t gid)
84#else
85getgrgid(gid)
86	gid_t gid;
87#endif
88{
89	int rval;
90
91	if (!start_gr())
92		return(NULL);
93	rval = grscan(1, gid, NULL);
94	if (!_gr_stayopen)
95		endgrent();
96	return(rval ? &_gr_group : NULL);
97}
98
99static int
100start_gr()
101{
102	if (_gr_fp) {
103		rewind(_gr_fp);
104		return(1);
105	}
106	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
107}
108
109void
110setgrent()
111{
112	(void) setgroupent(0);
113}
114
115int
116setgroupent(stayopen)
117	int stayopen;
118{
119	if (!start_gr())
120		return(0);
121	_gr_stayopen = stayopen;
122	return(1);
123}
124
125void
126endgrent()
127{
128	if (_gr_fp) {
129		(void)fclose(_gr_fp);
130		_gr_fp = NULL;
131	}
132}
133
134static int
135grscan(search, gid, name)
136	register int search, gid;
137	register const char *name;
138{
139	register char *cp, **m;
140	char *bp;
141
142	for (;;) {
143		if (!fgets(line, sizeof(line), _gr_fp))
144			return(0);
145		bp = line;
146		/* skip lines that are too big */
147		if (!strchr(line, '\n')) {
148			int ch;
149
150			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
151				;
152			continue;
153		}
154		_gr_group.gr_name = strsep(&bp, ":\n");
155		if (search && name && strcmp(_gr_group.gr_name, name))
156			continue;
157		_gr_group.gr_passwd = strsep(&bp, ":\n");
158		if (!(cp = strsep(&bp, ":\n")))
159			continue;
160		_gr_group.gr_gid = atoi(cp);
161		if (search && name == NULL && _gr_group.gr_gid != gid)
162			continue;
163		cp = NULL;
164		if (bp == NULL)
165			continue;
166		for (m = _gr_group.gr_mem = members;; bp++) {
167			if (m == &members[MAXGRP - 1])
168				break;
169			if (*bp == ',') {
170				if (cp) {
171					*bp = '\0';
172					*m++ = cp;
173					cp = NULL;
174				}
175			} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
176				if (cp) {
177					*bp = '\0';
178					*m++ = cp;
179				}
180				break;
181			} else if (cp == NULL)
182				cp = bp;
183		}
184		*m = NULL;
185		return(1);
186	}
187	/* NOTREACHED */
188}
189