xref: /csrg-svn/lib/libc/gen/getgrent.c (revision 42624)
137131Sbostic /*
237131Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337131Sbostic  * All rights reserved.
437131Sbostic  *
5*42624Sbostic  * %sccs.include.redist.c%
637131Sbostic  */
737131Sbostic 
826555Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42624Sbostic static char sccsid[] = "@(#)getgrent.c	5.6 (Berkeley) 06/01/90";
1037131Sbostic #endif /* LIBC_SCCS and not lint */
116349Swnj 
1237131Sbostic #include <sys/types.h>
132014Swnj #include <stdio.h>
142014Swnj #include <grp.h>
152014Swnj 
1637131Sbostic static FILE *_gr_fp;
1737131Sbostic static struct group _gr_group;
1837131Sbostic static int _gr_stayopen;
1937131Sbostic static char *_gr_file = _PATH_GROUP;
202014Swnj 
2137131Sbostic #define	MAXGRP		200
2237131Sbostic static char *members[MAXGRP];
2337131Sbostic #define	MAXLINELENGTH	1024
2437131Sbostic static char line[MAXLINELENGTH];
252014Swnj 
2637131Sbostic struct group *
2737131Sbostic getgrent()
282014Swnj {
2937131Sbostic 	if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
3037131Sbostic 		return((struct group *)NULL);
3137131Sbostic 	return(&_gr_group);
322014Swnj }
332014Swnj 
3437131Sbostic struct group *
3537131Sbostic getgrnam(name)
3637131Sbostic 	char *name;
372014Swnj {
3837131Sbostic 	int rval;
3937131Sbostic 
4037131Sbostic 	if (!start_gr())
4137131Sbostic 		return((struct group *)NULL);
4237131Sbostic 	rval = grscan(1, 0, name);
4337131Sbostic 	if (!_gr_stayopen)
4437131Sbostic 		endgrent();
4537131Sbostic 	return(rval ? &_gr_group : (struct group *)NULL);
4637131Sbostic }
4737131Sbostic 
4837131Sbostic struct group *
4937131Sbostic getgrgid(gid)
5037131Sbostic 	int gid;
5137131Sbostic {
5237131Sbostic 	int rval;
5337131Sbostic 
5437131Sbostic 	if (!start_gr())
5537131Sbostic 		return((struct group *)NULL);
5637131Sbostic 	rval = grscan(1, gid, (char *)NULL);
5737131Sbostic 	if (!_gr_stayopen)
5837131Sbostic 		endgrent();
5937131Sbostic 	return(rval ? &_gr_group : (struct group *)NULL);
6037131Sbostic }
6137131Sbostic 
6237131Sbostic static
6337131Sbostic start_gr()
6437131Sbostic {
6537131Sbostic 	if (_gr_fp) {
6637131Sbostic 		rewind(_gr_fp);
6737131Sbostic 		return(1);
682014Swnj 	}
6937131Sbostic 	return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
702014Swnj }
712014Swnj 
7237131Sbostic setgrent()
732014Swnj {
7437131Sbostic 	return(setgroupent(0));
752014Swnj }
762014Swnj 
7737131Sbostic setgroupent(stayopen)
7837131Sbostic 	int stayopen;
792014Swnj {
8037131Sbostic 	if (!start_gr())
8137131Sbostic 		return(0);
8237131Sbostic 	_gr_stayopen = stayopen;
8337131Sbostic 	return(1);
8437131Sbostic }
852014Swnj 
8637131Sbostic void
8737131Sbostic endgrent()
8837131Sbostic {
8937131Sbostic 	if (_gr_fp) {
9037131Sbostic 		(void)fclose(_gr_fp);
9137131Sbostic 		_gr_fp = (FILE *)NULL;
922014Swnj 	}
932014Swnj }
9432592Sbostic 
9537131Sbostic void
9632592Sbostic setgrfile(file)
9732592Sbostic 	char *file;
9832592Sbostic {
9937131Sbostic 	_gr_file = file;
10032592Sbostic }
10137131Sbostic 
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);
13037131Sbostic 		if (search && gid && _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