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