xref: /csrg-svn/lib/libc/gen/getgrent.c (revision 47718)
137131Sbostic /*
237131Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337131Sbostic  * All rights reserved.
437131Sbostic  *
542624Sbostic  * %sccs.include.redist.c%
637131Sbostic  */
737131Sbostic 
826555Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*47718Sbostic static char sccsid[] = "@(#)getgrent.c	5.9 (Berkeley) 04/01/91";
1037131Sbostic #endif /* LIBC_SCCS and not lint */
116349Swnj 
1237131Sbostic #include <sys/types.h>
132014Swnj #include <stdio.h>
1446597Sdonn #include <stdlib.h>
152014Swnj #include <grp.h>
162014Swnj 
1737131Sbostic static FILE *_gr_fp;
1837131Sbostic static struct group _gr_group;
1937131Sbostic static int _gr_stayopen;
20*47718Sbostic static int grscan(), start_gr();
212014Swnj 
2237131Sbostic #define	MAXGRP		200
2337131Sbostic static char *members[MAXGRP];
2437131Sbostic #define	MAXLINELENGTH	1024
2537131Sbostic static char line[MAXLINELENGTH];
262014Swnj 
2737131Sbostic struct group *
2837131Sbostic getgrent()
292014Swnj {
30*47718Sbostic 	if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
31*47718Sbostic 		return(NULL);
3237131Sbostic 	return(&_gr_group);
332014Swnj }
342014Swnj 
3537131Sbostic struct group *
3637131Sbostic getgrnam(name)
3746597Sdonn 	const char *name;
382014Swnj {
3937131Sbostic 	int rval;
4037131Sbostic 
4137131Sbostic 	if (!start_gr())
42*47718Sbostic 		return(NULL);
4337131Sbostic 	rval = grscan(1, 0, name);
4437131Sbostic 	if (!_gr_stayopen)
4537131Sbostic 		endgrent();
46*47718Sbostic 	return(rval ? &_gr_group : NULL);
4737131Sbostic }
4837131Sbostic 
4937131Sbostic struct group *
5046597Sdonn #ifdef __STDC__
5146597Sdonn getgrgid(gid_t gid)
5246597Sdonn #else
5337131Sbostic getgrgid(gid)
5446597Sdonn 	gid_t gid;
5546597Sdonn #endif
5637131Sbostic {
5737131Sbostic 	int rval;
5837131Sbostic 
5937131Sbostic 	if (!start_gr())
60*47718Sbostic 		return(NULL);
61*47718Sbostic 	rval = grscan(1, gid, NULL);
6237131Sbostic 	if (!_gr_stayopen)
6337131Sbostic 		endgrent();
64*47718Sbostic 	return(rval ? &_gr_group : NULL);
6537131Sbostic }
6637131Sbostic 
6737131Sbostic static
6837131Sbostic start_gr()
6937131Sbostic {
7037131Sbostic 	if (_gr_fp) {
7137131Sbostic 		rewind(_gr_fp);
7237131Sbostic 		return(1);
732014Swnj 	}
74*47718Sbostic 	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
752014Swnj }
762014Swnj 
7746597Sdonn int
7837131Sbostic setgrent()
792014Swnj {
8037131Sbostic 	return(setgroupent(0));
812014Swnj }
822014Swnj 
8346597Sdonn int
8437131Sbostic setgroupent(stayopen)
8537131Sbostic 	int stayopen;
862014Swnj {
8737131Sbostic 	if (!start_gr())
8837131Sbostic 		return(0);
8937131Sbostic 	_gr_stayopen = stayopen;
9037131Sbostic 	return(1);
9137131Sbostic }
922014Swnj 
9337131Sbostic void
9437131Sbostic endgrent()
9537131Sbostic {
9637131Sbostic 	if (_gr_fp) {
9737131Sbostic 		(void)fclose(_gr_fp);
98*47718Sbostic 		_gr_fp = NULL;
992014Swnj 	}
1002014Swnj }
10132592Sbostic 
10237131Sbostic static
10337131Sbostic grscan(search, gid, name)
10437131Sbostic 	register int search, gid;
10537131Sbostic 	register char *name;
10637131Sbostic {
10737131Sbostic 	register char *cp, **m;
10841756Smarc 	char *bp;
10937131Sbostic 	char *fgets(), *strsep(), *index();
11037131Sbostic 
11137131Sbostic 	for (;;) {
11237131Sbostic 		if (!fgets(line, sizeof(line), _gr_fp))
11337131Sbostic 			return(0);
11441756Smarc 		bp = line;
11537131Sbostic 		/* skip lines that are too big */
11637131Sbostic 		if (!index(line, '\n')) {
11737131Sbostic 			int ch;
11837131Sbostic 
11937131Sbostic 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
12037131Sbostic 				;
12137131Sbostic 			continue;
12237131Sbostic 		}
12341756Smarc 		_gr_group.gr_name = strsep(&bp, ":\n");
12437131Sbostic 		if (search && name && strcmp(_gr_group.gr_name, name))
12537131Sbostic 			continue;
12641756Smarc 		_gr_group.gr_passwd = strsep(&bp, ":\n");
12741756Smarc 		if (!(cp = strsep(&bp, ":\n")))
12837131Sbostic 			continue;
12937131Sbostic 		_gr_group.gr_gid = atoi(cp);
13043783Sbostic 		if (search && name == NULL && _gr_group.gr_gid != gid)
13137131Sbostic 			continue;
13237131Sbostic 		for (m = _gr_group.gr_mem = members;; ++m) {
13337131Sbostic 			if (m == &members[MAXGRP - 1]) {
13437131Sbostic 				*m = NULL;
13537131Sbostic 				break;
13637131Sbostic 			}
13741756Smarc 			if ((*m = strsep(&bp, ", \n")) == NULL)
13837131Sbostic 				break;
13937131Sbostic 		}
14037131Sbostic 		return(1);
14137131Sbostic 	}
14237131Sbostic 	/* NOTREACHED */
14337131Sbostic }
144