Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.1.1.1
      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, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 static char sccsid[] = "@(#)getgrent.c	5.9 (Berkeley) 4/1/91";
     36 #endif /* LIBC_SCCS and not lint */
     37 
     38 #include <sys/types.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <grp.h>
     42 
     43 static FILE *_gr_fp;
     44 static struct group _gr_group;
     45 static int _gr_stayopen;
     46 static int grscan(), start_gr();
     47 
     48 #define	MAXGRP		200
     49 static char *members[MAXGRP];
     50 #define	MAXLINELENGTH	1024
     51 static char line[MAXLINELENGTH];
     52 
     53 struct group *
     54 getgrent()
     55 {
     56 	if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
     57 		return(NULL);
     58 	return(&_gr_group);
     59 }
     60 
     61 struct group *
     62 getgrnam(name)
     63 	const char *name;
     64 {
     65 	int rval;
     66 
     67 	if (!start_gr())
     68 		return(NULL);
     69 	rval = grscan(1, 0, name);
     70 	if (!_gr_stayopen)
     71 		endgrent();
     72 	return(rval ? &_gr_group : NULL);
     73 }
     74 
     75 struct group *
     76 #ifdef __STDC__
     77 getgrgid(gid_t gid)
     78 #else
     79 getgrgid(gid)
     80 	gid_t gid;
     81 #endif
     82 {
     83 	int rval;
     84 
     85 	if (!start_gr())
     86 		return(NULL);
     87 	rval = grscan(1, gid, NULL);
     88 	if (!_gr_stayopen)
     89 		endgrent();
     90 	return(rval ? &_gr_group : NULL);
     91 }
     92 
     93 static
     94 start_gr()
     95 {
     96 	if (_gr_fp) {
     97 		rewind(_gr_fp);
     98 		return(1);
     99 	}
    100 	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
    101 }
    102 
    103 int
    104 setgrent()
    105 {
    106 	return(setgroupent(0));
    107 }
    108 
    109 int
    110 setgroupent(stayopen)
    111 	int stayopen;
    112 {
    113 	if (!start_gr())
    114 		return(0);
    115 	_gr_stayopen = stayopen;
    116 	return(1);
    117 }
    118 
    119 void
    120 endgrent()
    121 {
    122 	if (_gr_fp) {
    123 		(void)fclose(_gr_fp);
    124 		_gr_fp = NULL;
    125 	}
    126 }
    127 
    128 static
    129 grscan(search, gid, name)
    130 	register int search, gid;
    131 	register char *name;
    132 {
    133 	register char *cp, **m;
    134 	char *bp;
    135 	char *fgets(), *strsep(), *index();
    136 
    137 	for (;;) {
    138 		if (!fgets(line, sizeof(line), _gr_fp))
    139 			return(0);
    140 		bp = line;
    141 		/* skip lines that are too big */
    142 		if (!index(line, '\n')) {
    143 			int ch;
    144 
    145 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    146 				;
    147 			continue;
    148 		}
    149 		_gr_group.gr_name = strsep(&bp, ":\n");
    150 		if (search && name && strcmp(_gr_group.gr_name, name))
    151 			continue;
    152 		_gr_group.gr_passwd = strsep(&bp, ":\n");
    153 		if (!(cp = strsep(&bp, ":\n")))
    154 			continue;
    155 		_gr_group.gr_gid = atoi(cp);
    156 		if (search && name == NULL && _gr_group.gr_gid != gid)
    157 			continue;
    158 		for (m = _gr_group.gr_mem = members;; ++m) {
    159 			if (m == &members[MAXGRP - 1]) {
    160 				*m = NULL;
    161 				break;
    162 			}
    163 			if ((*m = strsep(&bp, ", \n")) == NULL)
    164 				break;
    165 		}
    166 		return(1);
    167 	}
    168 	/* NOTREACHED */
    169 }
    170