137131Sbostic /*
261111Sbostic * Copyright (c) 1989, 1993
361111Sbostic * The Regents of the University of California. All rights reserved.
437131Sbostic *
542624Sbostic * %sccs.include.redist.c%
637131Sbostic */
737131Sbostic
826555Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*66429Sbostic static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 03/21/94";
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;
2047718Sbostic 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 *
getgrent()2837131Sbostic getgrent()
292014Swnj {
3047718Sbostic if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
3147718Sbostic return(NULL);
3237131Sbostic return(&_gr_group);
332014Swnj }
342014Swnj
3537131Sbostic struct group *
getgrnam(name)3637131Sbostic getgrnam(name)
3746597Sdonn const char *name;
382014Swnj {
3937131Sbostic int rval;
4037131Sbostic
4137131Sbostic if (!start_gr())
4247718Sbostic return(NULL);
4337131Sbostic rval = grscan(1, 0, name);
4437131Sbostic if (!_gr_stayopen)
4537131Sbostic endgrent();
4647718Sbostic return(rval ? &_gr_group : NULL);
4737131Sbostic }
4837131Sbostic
4937131Sbostic struct group *
5046597Sdonn #ifdef __STDC__
getgrgid(gid_t gid)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())
6047718Sbostic return(NULL);
6147718Sbostic rval = grscan(1, gid, NULL);
6237131Sbostic if (!_gr_stayopen)
6337131Sbostic endgrent();
6447718Sbostic return(rval ? &_gr_group : NULL);
6537131Sbostic }
6637131Sbostic
6737131Sbostic static
start_gr()6837131Sbostic start_gr()
6937131Sbostic {
7037131Sbostic if (_gr_fp) {
7137131Sbostic rewind(_gr_fp);
7237131Sbostic return(1);
732014Swnj }
7447718Sbostic return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
752014Swnj }
762014Swnj
7746597Sdonn int
setgrent()7837131Sbostic setgrent()
792014Swnj {
8037131Sbostic return(setgroupent(0));
812014Swnj }
822014Swnj
8346597Sdonn int
setgroupent(stayopen)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
endgrent()9437131Sbostic endgrent()
9537131Sbostic {
9637131Sbostic if (_gr_fp) {
9737131Sbostic (void)fclose(_gr_fp);
9847718Sbostic _gr_fp = NULL;
992014Swnj }
1002014Swnj }
10132592Sbostic
10237131Sbostic static
grscan(search,gid,name)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;
132*66429Sbostic cp = NULL;
133*66429Sbostic for (m = _gr_group.gr_mem = members;; bp++) {
134*66429Sbostic if (m == &members[MAXGRP - 1])
13537131Sbostic break;
136*66429Sbostic if (*bp == ',') {
137*66429Sbostic if (cp) {
138*66429Sbostic *bp = '\0';
139*66429Sbostic *m++ = cp;
140*66429Sbostic cp = NULL;
141*66429Sbostic }
142*66429Sbostic } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
143*66429Sbostic if (cp) {
144*66429Sbostic *bp = '\0';
145*66429Sbostic *m++ = cp;
14637131Sbostic }
14737131Sbostic break;
148*66429Sbostic } else if (cp == NULL)
149*66429Sbostic cp = bp;
15037131Sbostic }
151*66429Sbostic *m = NULL;
15237131Sbostic return(1);
15337131Sbostic }
15437131Sbostic /* NOTREACHED */
15537131Sbostic }
156