make error messages a little better, use constants in unistd.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)
7d9b2097 9static char sccsid[] = "@(#)getgrent.c 5.7 (Berkeley) %G%";
6c6c970d 10#endif /* LIBC_SCCS and not lint */
bb22887e 11
6c6c970d 12#include <sys/types.h>
41746c4f
BJ
13#include <stdio.h>
14#include <grp.h>
15
6c6c970d
KB
16static FILE *_gr_fp;
17static struct group _gr_group;
18static int _gr_stayopen;
19static char *_gr_file = _PATH_GROUP;
41746c4f 20
6c6c970d
KB
21#define MAXGRP 200
22static char *members[MAXGRP];
23#define MAXLINELENGTH 1024
24static char line[MAXLINELENGTH];
41746c4f 25
6c6c970d
KB
26struct group *
27getgrent()
41746c4f 28{
6c6c970d
KB
29 if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
30 return((struct group *)NULL);
31 return(&_gr_group);
41746c4f
BJ
32}
33
6c6c970d
KB
34struct group *
35getgrnam(name)
36 char *name;
37{
38 int rval;
39
40 if (!start_gr())
41 return((struct group *)NULL);
42 rval = grscan(1, 0, name);
43 if (!_gr_stayopen)
44 endgrent();
45 return(rval ? &_gr_group : (struct group *)NULL);
46}
47
48struct group *
49getgrgid(gid)
50 int gid;
51{
52 int rval;
53
54 if (!start_gr())
55 return((struct group *)NULL);
56 rval = grscan(1, gid, (char *)NULL);
57 if (!_gr_stayopen)
58 endgrent();
59 return(rval ? &_gr_group : (struct group *)NULL);
60}
61
62static
63start_gr()
41746c4f 64{
6c6c970d
KB
65 if (_gr_fp) {
66 rewind(_gr_fp);
67 return(1);
41746c4f 68 }
6c6c970d 69 return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
41746c4f
BJ
70}
71
6c6c970d 72setgrent()
41746c4f 73{
6c6c970d 74 return(setgroupent(0));
41746c4f
BJ
75}
76
6c6c970d
KB
77setgroupent(stayopen)
78 int stayopen;
79{
80 if (!start_gr())
81 return(0);
82 _gr_stayopen = stayopen;
83 return(1);
84}
85
86void
87endgrent()
41746c4f 88{
6c6c970d
KB
89 if (_gr_fp) {
90 (void)fclose(_gr_fp);
91 _gr_fp = (FILE *)NULL;
41746c4f 92 }
41746c4f 93}
5a029ac1 94
6c6c970d 95void
5a029ac1
KB
96setgrfile(file)
97 char *file;
98{
6c6c970d
KB
99 _gr_file = file;
100}
101
102static
103grscan(search, gid, name)
104 register int search, gid;
105 register char *name;
106{
107 register char *cp, **m;
0f38f0b7 108 char *bp;
6c6c970d
KB
109 char *fgets(), *strsep(), *index();
110
111 for (;;) {
112 if (!fgets(line, sizeof(line), _gr_fp))
113 return(0);
0f38f0b7 114 bp = line;
6c6c970d
KB
115 /* skip lines that are too big */
116 if (!index(line, '\n')) {
117 int ch;
118
119 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
120 ;
121 continue;
122 }
0f38f0b7 123 _gr_group.gr_name = strsep(&bp, ":\n");
6c6c970d
KB
124 if (search && name && strcmp(_gr_group.gr_name, name))
125 continue;
0f38f0b7
MT
126 _gr_group.gr_passwd = strsep(&bp, ":\n");
127 if (!(cp = strsep(&bp, ":\n")))
6c6c970d
KB
128 continue;
129 _gr_group.gr_gid = atoi(cp);
7d9b2097 130 if (search && name == NULL && _gr_group.gr_gid != gid)
6c6c970d
KB
131 continue;
132 for (m = _gr_group.gr_mem = members;; ++m) {
133 if (m == &members[MAXGRP - 1]) {
134 *m = NULL;
135 break;
136 }
0f38f0b7 137 if ((*m = strsep(&bp, ", \n")) == NULL)
6c6c970d
KB
138 break;
139 }
140 return(1);
141 }
142 /* NOTREACHED */
5a029ac1 143}