xref: /csrg-svn/lib/libc/gen/getgrent.c (revision 41756)
137131Sbostic /*
237131Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337131Sbostic  * All rights reserved.
437131Sbostic  *
537131Sbostic  * Redistribution and use in source and binary forms are permitted
637131Sbostic  * provided that the above copyright notice and this paragraph are
737131Sbostic  * duplicated in all such forms and that any documentation,
837131Sbostic  * advertising materials, and other materials related to such
937131Sbostic  * distribution and use acknowledge that the software was developed
1037131Sbostic  * by the University of California, Berkeley.  The name of the
1137131Sbostic  * University may not be used to endorse or promote products derived
1237131Sbostic  * from this software without specific prior written permission.
1337131Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437131Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537131Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637131Sbostic  */
1737131Sbostic 
1826555Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*41756Smarc static char sccsid[] = "@(#)getgrent.c	5.5 (Berkeley) 05/11/90";
2037131Sbostic #endif /* LIBC_SCCS and not lint */
216349Swnj 
2237131Sbostic #include <sys/types.h>
232014Swnj #include <stdio.h>
242014Swnj #include <grp.h>
252014Swnj 
2637131Sbostic static FILE *_gr_fp;
2737131Sbostic static struct group _gr_group;
2837131Sbostic static int _gr_stayopen;
2937131Sbostic static char *_gr_file = _PATH_GROUP;
302014Swnj 
3137131Sbostic #define	MAXGRP		200
3237131Sbostic static char *members[MAXGRP];
3337131Sbostic #define	MAXLINELENGTH	1024
3437131Sbostic static char line[MAXLINELENGTH];
352014Swnj 
3637131Sbostic struct group *
3737131Sbostic getgrent()
382014Swnj {
3937131Sbostic 	if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
4037131Sbostic 		return((struct group *)NULL);
4137131Sbostic 	return(&_gr_group);
422014Swnj }
432014Swnj 
4437131Sbostic struct group *
4537131Sbostic getgrnam(name)
4637131Sbostic 	char *name;
472014Swnj {
4837131Sbostic 	int rval;
4937131Sbostic 
5037131Sbostic 	if (!start_gr())
5137131Sbostic 		return((struct group *)NULL);
5237131Sbostic 	rval = grscan(1, 0, name);
5337131Sbostic 	if (!_gr_stayopen)
5437131Sbostic 		endgrent();
5537131Sbostic 	return(rval ? &_gr_group : (struct group *)NULL);
5637131Sbostic }
5737131Sbostic 
5837131Sbostic struct group *
5937131Sbostic getgrgid(gid)
6037131Sbostic 	int gid;
6137131Sbostic {
6237131Sbostic 	int rval;
6337131Sbostic 
6437131Sbostic 	if (!start_gr())
6537131Sbostic 		return((struct group *)NULL);
6637131Sbostic 	rval = grscan(1, gid, (char *)NULL);
6737131Sbostic 	if (!_gr_stayopen)
6837131Sbostic 		endgrent();
6937131Sbostic 	return(rval ? &_gr_group : (struct group *)NULL);
7037131Sbostic }
7137131Sbostic 
7237131Sbostic static
7337131Sbostic start_gr()
7437131Sbostic {
7537131Sbostic 	if (_gr_fp) {
7637131Sbostic 		rewind(_gr_fp);
7737131Sbostic 		return(1);
782014Swnj 	}
7937131Sbostic 	return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
802014Swnj }
812014Swnj 
8237131Sbostic setgrent()
832014Swnj {
8437131Sbostic 	return(setgroupent(0));
852014Swnj }
862014Swnj 
8737131Sbostic setgroupent(stayopen)
8837131Sbostic 	int stayopen;
892014Swnj {
9037131Sbostic 	if (!start_gr())
9137131Sbostic 		return(0);
9237131Sbostic 	_gr_stayopen = stayopen;
9337131Sbostic 	return(1);
9437131Sbostic }
952014Swnj 
9637131Sbostic void
9737131Sbostic endgrent()
9837131Sbostic {
9937131Sbostic 	if (_gr_fp) {
10037131Sbostic 		(void)fclose(_gr_fp);
10137131Sbostic 		_gr_fp = (FILE *)NULL;
1022014Swnj 	}
1032014Swnj }
10432592Sbostic 
10537131Sbostic void
10632592Sbostic setgrfile(file)
10732592Sbostic 	char *file;
10832592Sbostic {
10937131Sbostic 	_gr_file = file;
11032592Sbostic }
11137131Sbostic 
11237131Sbostic static
11337131Sbostic grscan(search, gid, name)
11437131Sbostic 	register int search, gid;
11537131Sbostic 	register char *name;
11637131Sbostic {
11737131Sbostic 	register char *cp, **m;
118*41756Smarc 	char *bp;
11937131Sbostic 	char *fgets(), *strsep(), *index();
12037131Sbostic 
12137131Sbostic 	for (;;) {
12237131Sbostic 		if (!fgets(line, sizeof(line), _gr_fp))
12337131Sbostic 			return(0);
124*41756Smarc 		bp = line;
12537131Sbostic 		/* skip lines that are too big */
12637131Sbostic 		if (!index(line, '\n')) {
12737131Sbostic 			int ch;
12837131Sbostic 
12937131Sbostic 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
13037131Sbostic 				;
13137131Sbostic 			continue;
13237131Sbostic 		}
133*41756Smarc 		_gr_group.gr_name = strsep(&bp, ":\n");
13437131Sbostic 		if (search && name && strcmp(_gr_group.gr_name, name))
13537131Sbostic 			continue;
136*41756Smarc 		_gr_group.gr_passwd = strsep(&bp, ":\n");
137*41756Smarc 		if (!(cp = strsep(&bp, ":\n")))
13837131Sbostic 			continue;
13937131Sbostic 		_gr_group.gr_gid = atoi(cp);
14037131Sbostic 		if (search && gid && _gr_group.gr_gid != gid)
14137131Sbostic 			continue;
14237131Sbostic 		for (m = _gr_group.gr_mem = members;; ++m) {
14337131Sbostic 			if (m == &members[MAXGRP - 1]) {
14437131Sbostic 				*m = NULL;
14537131Sbostic 				break;
14637131Sbostic 			}
147*41756Smarc 			if ((*m = strsep(&bp, ", \n")) == NULL)
14837131Sbostic 				break;
14937131Sbostic 		}
15037131Sbostic 		return(1);
15137131Sbostic 	}
15237131Sbostic 	/* NOTREACHED */
15337131Sbostic }
154