xref: /original-bsd/lib/libc/gen/getgrent.c (revision 33586e34)
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 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)getgrent.c	5.4 (Berkeley) 03/11/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/types.h>
23 #include <stdio.h>
24 #include <grp.h>
25 
26 static FILE *_gr_fp;
27 static struct group _gr_group;
28 static int _gr_stayopen;
29 static char *_gr_file = _PATH_GROUP;
30 
31 #define	MAXGRP		200
32 static char *members[MAXGRP];
33 #define	MAXLINELENGTH	1024
34 static char line[MAXLINELENGTH];
35 
36 struct group *
37 getgrent()
38 {
39 	if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
40 		return((struct group *)NULL);
41 	return(&_gr_group);
42 }
43 
44 struct group *
45 getgrnam(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 
58 struct group *
59 getgrgid(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 
72 static
73 start_gr()
74 {
75 	if (_gr_fp) {
76 		rewind(_gr_fp);
77 		return(1);
78 	}
79 	return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
80 }
81 
82 setgrent()
83 {
84 	return(setgroupent(0));
85 }
86 
87 setgroupent(stayopen)
88 	int stayopen;
89 {
90 	if (!start_gr())
91 		return(0);
92 	_gr_stayopen = stayopen;
93 	return(1);
94 }
95 
96 void
97 endgrent()
98 {
99 	if (_gr_fp) {
100 		(void)fclose(_gr_fp);
101 		_gr_fp = (FILE *)NULL;
102 	}
103 }
104 
105 void
106 setgrfile(file)
107 	char *file;
108 {
109 	_gr_file = file;
110 }
111 
112 static
113 grscan(search, gid, name)
114 	register int search, gid;
115 	register char *name;
116 {
117 	register char *cp, **m;
118 	char *fgets(), *strsep(), *index();
119 
120 	for (;;) {
121 		if (!fgets(line, sizeof(line), _gr_fp))
122 			return(0);
123 		/* skip lines that are too big */
124 		if (!index(line, '\n')) {
125 			int ch;
126 
127 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
128 				;
129 			continue;
130 		}
131 		_gr_group.gr_name = strsep(line, ":\n");
132 		if (search && name && strcmp(_gr_group.gr_name, name))
133 			continue;
134 		_gr_group.gr_passwd = strsep((char *)NULL, ":\n");
135 		if (!(cp = strsep((char *)NULL, ":\n")))
136 			continue;
137 		_gr_group.gr_gid = atoi(cp);
138 		if (search && gid && _gr_group.gr_gid != gid)
139 			continue;
140 		for (m = _gr_group.gr_mem = members;; ++m) {
141 			if (m == &members[MAXGRP - 1]) {
142 				*m = NULL;
143 				break;
144 			}
145 			if ((*m = strsep((char *)NULL, ", \n")) == NULL)
146 				break;
147 		}
148 		return(1);
149 	}
150 	/* NOTREACHED */
151 }
152