#define root and root's parent fts_level values in <fts.h>
[unix-history] / usr / src / lib / libc / gen / getgrent.c
CommitLineData
6c6c970d
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
6c6c970d
KB
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
c5980113 9static char sccsid[] = "@(#)getgrent.c 5.8 (Berkeley) %G%";
6c6c970d 10#endif /* LIBC_SCCS and not lint */
bb22887e 11
6c6c970d 12#include <sys/types.h>
41746c4f 13#include <stdio.h>
c5980113 14#include <stdlib.h>
41746c4f
BJ
15#include <grp.h>
16
6c6c970d
KB
17static FILE *_gr_fp;
18static struct group _gr_group;
19static int _gr_stayopen;
c5980113
DS
20static const char *_gr_file = _PATH_GROUP;
21static start_gr();
22static grscan();
41746c4f 23
6c6c970d
KB
24#define MAXGRP 200
25static char *members[MAXGRP];
26#define MAXLINELENGTH 1024
27static char line[MAXLINELENGTH];
41746c4f 28
6c6c970d
KB
29struct group *
30getgrent()
41746c4f 31{
6c6c970d
KB
32 if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
33 return((struct group *)NULL);
34 return(&_gr_group);
41746c4f
BJ
35}
36
6c6c970d
KB
37struct group *
38getgrnam(name)
c5980113 39 const char *name;
6c6c970d
KB
40{
41 int rval;
42
43 if (!start_gr())
44 return((struct group *)NULL);
45 rval = grscan(1, 0, name);
46 if (!_gr_stayopen)
47 endgrent();
48 return(rval ? &_gr_group : (struct group *)NULL);
49}
50
51struct group *
c5980113
DS
52#ifdef __STDC__
53getgrgid(gid_t gid)
54#else
6c6c970d 55getgrgid(gid)
c5980113
DS
56 gid_t gid;
57#endif
6c6c970d
KB
58{
59 int rval;
60
61 if (!start_gr())
62 return((struct group *)NULL);
63 rval = grscan(1, gid, (char *)NULL);
64 if (!_gr_stayopen)
65 endgrent();
66 return(rval ? &_gr_group : (struct group *)NULL);
67}
68
69static
70start_gr()
41746c4f 71{
6c6c970d
KB
72 if (_gr_fp) {
73 rewind(_gr_fp);
74 return(1);
41746c4f 75 }
6c6c970d 76 return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
41746c4f
BJ
77}
78
c5980113 79int
6c6c970d 80setgrent()
41746c4f 81{
6c6c970d 82 return(setgroupent(0));
41746c4f
BJ
83}
84
c5980113 85int
6c6c970d
KB
86setgroupent(stayopen)
87 int stayopen;
88{
89 if (!start_gr())
90 return(0);
91 _gr_stayopen = stayopen;
92 return(1);
93}
94
95void
96endgrent()
41746c4f 97{
6c6c970d
KB
98 if (_gr_fp) {
99 (void)fclose(_gr_fp);
100 _gr_fp = (FILE *)NULL;
41746c4f 101 }
41746c4f 102}
5a029ac1 103
6c6c970d 104void
5a029ac1 105setgrfile(file)
c5980113 106 const char *file;
5a029ac1 107{
6c6c970d
KB
108 _gr_file = file;
109}
110
111static
112grscan(search, gid, name)
113 register int search, gid;
114 register char *name;
115{
116 register char *cp, **m;
0f38f0b7 117 char *bp;
6c6c970d
KB
118 char *fgets(), *strsep(), *index();
119
120 for (;;) {
121 if (!fgets(line, sizeof(line), _gr_fp))
122 return(0);
0f38f0b7 123 bp = line;
6c6c970d
KB
124 /* skip lines that are too big */
125 if (!index(line, '\n')) {
126 int ch;
127
128 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
129 ;
130 continue;
131 }
0f38f0b7 132 _gr_group.gr_name = strsep(&bp, ":\n");
6c6c970d
KB
133 if (search && name && strcmp(_gr_group.gr_name, name))
134 continue;
0f38f0b7
MT
135 _gr_group.gr_passwd = strsep(&bp, ":\n");
136 if (!(cp = strsep(&bp, ":\n")))
6c6c970d
KB
137 continue;
138 _gr_group.gr_gid = atoi(cp);
7d9b2097 139 if (search && name == NULL && _gr_group.gr_gid != gid)
6c6c970d
KB
140 continue;
141 for (m = _gr_group.gr_mem = members;; ++m) {
142 if (m == &members[MAXGRP - 1]) {
143 *m = NULL;
144 break;
145 }
0f38f0b7 146 if ((*m = strsep(&bp, ", \n")) == NULL)
6c6c970d
KB
147 break;
148 }
149 return(1);
150 }
151 /* NOTREACHED */
5a029ac1 152}