(no message)
[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 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
2ce81398 18#if defined(LIBC_SCCS) && !defined(lint)
0f38f0b7 19static char sccsid[] = "@(#)getgrent.c 5.5 (Berkeley) %G%";
6c6c970d 20#endif /* LIBC_SCCS and not lint */
bb22887e 21
6c6c970d 22#include <sys/types.h>
41746c4f
BJ
23#include <stdio.h>
24#include <grp.h>
25
6c6c970d
KB
26static FILE *_gr_fp;
27static struct group _gr_group;
28static int _gr_stayopen;
29static char *_gr_file = _PATH_GROUP;
41746c4f 30
6c6c970d
KB
31#define MAXGRP 200
32static char *members[MAXGRP];
33#define MAXLINELENGTH 1024
34static char line[MAXLINELENGTH];
41746c4f 35
6c6c970d
KB
36struct group *
37getgrent()
41746c4f 38{
6c6c970d
KB
39 if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
40 return((struct group *)NULL);
41 return(&_gr_group);
41746c4f
BJ
42}
43
6c6c970d
KB
44struct group *
45getgrnam(name)
46 char *name;
47{
48 int rval;
49
50 if (!start_gr())
51 return((struct group *)NULL);
52 rval = grscan(1, 0, name);
53 if (!_gr_stayopen)
54 endgrent();
55 return(rval ? &_gr_group : (struct group *)NULL);
56}
57
58struct group *
59getgrgid(gid)
60 int gid;
61{
62 int rval;
63
64 if (!start_gr())
65 return((struct group *)NULL);
66 rval = grscan(1, gid, (char *)NULL);
67 if (!_gr_stayopen)
68 endgrent();
69 return(rval ? &_gr_group : (struct group *)NULL);
70}
71
72static
73start_gr()
41746c4f 74{
6c6c970d
KB
75 if (_gr_fp) {
76 rewind(_gr_fp);
77 return(1);
41746c4f 78 }
6c6c970d 79 return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
41746c4f
BJ
80}
81
6c6c970d 82setgrent()
41746c4f 83{
6c6c970d 84 return(setgroupent(0));
41746c4f
BJ
85}
86
6c6c970d
KB
87setgroupent(stayopen)
88 int stayopen;
89{
90 if (!start_gr())
91 return(0);
92 _gr_stayopen = stayopen;
93 return(1);
94}
95
96void
97endgrent()
41746c4f 98{
6c6c970d
KB
99 if (_gr_fp) {
100 (void)fclose(_gr_fp);
101 _gr_fp = (FILE *)NULL;
41746c4f 102 }
41746c4f 103}
5a029ac1 104
6c6c970d 105void
5a029ac1
KB
106setgrfile(file)
107 char *file;
108{
6c6c970d
KB
109 _gr_file = file;
110}
111
112static
113grscan(search, gid, name)
114 register int search, gid;
115 register char *name;
116{
117 register char *cp, **m;
0f38f0b7 118 char *bp;
6c6c970d
KB
119 char *fgets(), *strsep(), *index();
120
121 for (;;) {
122 if (!fgets(line, sizeof(line), _gr_fp))
123 return(0);
0f38f0b7 124 bp = line;
6c6c970d
KB
125 /* skip lines that are too big */
126 if (!index(line, '\n')) {
127 int ch;
128
129 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
130 ;
131 continue;
132 }
0f38f0b7 133 _gr_group.gr_name = strsep(&bp, ":\n");
6c6c970d
KB
134 if (search && name && strcmp(_gr_group.gr_name, name))
135 continue;
0f38f0b7
MT
136 _gr_group.gr_passwd = strsep(&bp, ":\n");
137 if (!(cp = strsep(&bp, ":\n")))
6c6c970d
KB
138 continue;
139 _gr_group.gr_gid = atoi(cp);
140 if (search && gid && _gr_group.gr_gid != gid)
141 continue;
142 for (m = _gr_group.gr_mem = members;; ++m) {
143 if (m == &members[MAXGRP - 1]) {
144 *m = NULL;
145 break;
146 }
0f38f0b7 147 if ((*m = strsep(&bp, ", \n")) == NULL)
6c6c970d
KB
148 break;
149 }
150 return(1);
151 }
152 /* NOTREACHED */
5a029ac1 153}