Home | History | Annotate | Line # | Download | only in libhack
getgrent.c revision 1.3
      1  1.3  sommerfe /*	$NetBSD: getgrent.c,v 1.3 1999/03/13 19:08:44 sommerfe 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.1       gwr #include <sys/types.h>
     43  1.1       gwr #include <stdio.h>
     44  1.1       gwr #include <stdlib.h>
     45  1.1       gwr #include <string.h>
     46  1.1       gwr #include <grp.h>
     47  1.1       gwr 
     48  1.1       gwr static FILE *_gr_fp;
     49  1.1       gwr static struct group _gr_group;
     50  1.1       gwr static int _gr_stayopen;
     51  1.3  sommerfe static int grscan __P((int, int, const char *));
     52  1.3  sommerfe static int start_gr __P((void));
     53  1.1       gwr 
     54  1.1       gwr #define	MAXGRP		200
     55  1.1       gwr static char *members[MAXGRP];
     56  1.1       gwr #define	MAXLINELENGTH	1024
     57  1.1       gwr static char line[MAXLINELENGTH];
     58  1.1       gwr 
     59  1.1       gwr struct group *
     60  1.1       gwr getgrent()
     61  1.1       gwr {
     62  1.3  sommerfe 	if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
     63  1.1       gwr 		return(NULL);
     64  1.1       gwr 	return(&_gr_group);
     65  1.1       gwr }
     66  1.1       gwr 
     67  1.1       gwr struct group *
     68  1.1       gwr getgrnam(name)
     69  1.1       gwr 	const char *name;
     70  1.1       gwr {
     71  1.1       gwr 	int rval;
     72  1.1       gwr 
     73  1.1       gwr 	if (!start_gr())
     74  1.1       gwr 		return(NULL);
     75  1.1       gwr 	rval = grscan(1, 0, name);
     76  1.1       gwr 	if (!_gr_stayopen)
     77  1.1       gwr 		endgrent();
     78  1.1       gwr 	return(rval ? &_gr_group : NULL);
     79  1.1       gwr }
     80  1.1       gwr 
     81  1.1       gwr struct group *
     82  1.1       gwr #ifdef __STDC__
     83  1.1       gwr getgrgid(gid_t gid)
     84  1.1       gwr #else
     85  1.1       gwr getgrgid(gid)
     86  1.1       gwr 	gid_t gid;
     87  1.1       gwr #endif
     88  1.1       gwr {
     89  1.1       gwr 	int rval;
     90  1.1       gwr 
     91  1.1       gwr 	if (!start_gr())
     92  1.1       gwr 		return(NULL);
     93  1.1       gwr 	rval = grscan(1, gid, NULL);
     94  1.1       gwr 	if (!_gr_stayopen)
     95  1.1       gwr 		endgrent();
     96  1.1       gwr 	return(rval ? &_gr_group : NULL);
     97  1.1       gwr }
     98  1.1       gwr 
     99  1.1       gwr static int
    100  1.1       gwr start_gr()
    101  1.1       gwr {
    102  1.1       gwr 	if (_gr_fp) {
    103  1.1       gwr 		rewind(_gr_fp);
    104  1.1       gwr 		return(1);
    105  1.1       gwr 	}
    106  1.1       gwr 	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
    107  1.1       gwr }
    108  1.1       gwr 
    109  1.1       gwr void
    110  1.1       gwr setgrent()
    111  1.1       gwr {
    112  1.1       gwr 	(void) setgroupent(0);
    113  1.1       gwr }
    114  1.1       gwr 
    115  1.1       gwr int
    116  1.1       gwr setgroupent(stayopen)
    117  1.1       gwr 	int stayopen;
    118  1.1       gwr {
    119  1.1       gwr 	if (!start_gr())
    120  1.1       gwr 		return(0);
    121  1.1       gwr 	_gr_stayopen = stayopen;
    122  1.1       gwr 	return(1);
    123  1.1       gwr }
    124  1.1       gwr 
    125  1.1       gwr void
    126  1.1       gwr endgrent()
    127  1.1       gwr {
    128  1.1       gwr 	if (_gr_fp) {
    129  1.1       gwr 		(void)fclose(_gr_fp);
    130  1.1       gwr 		_gr_fp = NULL;
    131  1.1       gwr 	}
    132  1.1       gwr }
    133  1.1       gwr 
    134  1.1       gwr static int
    135  1.1       gwr grscan(search, gid, name)
    136  1.1       gwr 	register int search, gid;
    137  1.3  sommerfe 	register const char *name;
    138  1.1       gwr {
    139  1.1       gwr 	register char *cp, **m;
    140  1.1       gwr 	char *bp;
    141  1.1       gwr 
    142  1.1       gwr 	for (;;) {
    143  1.1       gwr 		if (!fgets(line, sizeof(line), _gr_fp))
    144  1.1       gwr 			return(0);
    145  1.1       gwr 		bp = line;
    146  1.1       gwr 		/* skip lines that are too big */
    147  1.1       gwr 		if (!strchr(line, '\n')) {
    148  1.1       gwr 			int ch;
    149  1.1       gwr 
    150  1.1       gwr 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    151  1.1       gwr 				;
    152  1.1       gwr 			continue;
    153  1.1       gwr 		}
    154  1.1       gwr 		_gr_group.gr_name = strsep(&bp, ":\n");
    155  1.1       gwr 		if (search && name && strcmp(_gr_group.gr_name, name))
    156  1.1       gwr 			continue;
    157  1.1       gwr 		_gr_group.gr_passwd = strsep(&bp, ":\n");
    158  1.1       gwr 		if (!(cp = strsep(&bp, ":\n")))
    159  1.1       gwr 			continue;
    160  1.1       gwr 		_gr_group.gr_gid = atoi(cp);
    161  1.1       gwr 		if (search && name == NULL && _gr_group.gr_gid != gid)
    162  1.1       gwr 			continue;
    163  1.1       gwr 		cp = NULL;
    164  1.1       gwr 		if (bp == NULL)
    165  1.1       gwr 			continue;
    166  1.1       gwr 		for (m = _gr_group.gr_mem = members;; bp++) {
    167  1.1       gwr 			if (m == &members[MAXGRP - 1])
    168  1.1       gwr 				break;
    169  1.1       gwr 			if (*bp == ',') {
    170  1.1       gwr 				if (cp) {
    171  1.1       gwr 					*bp = '\0';
    172  1.1       gwr 					*m++ = cp;
    173  1.1       gwr 					cp = NULL;
    174  1.1       gwr 				}
    175  1.1       gwr 			} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    176  1.1       gwr 				if (cp) {
    177  1.1       gwr 					*bp = '\0';
    178  1.1       gwr 					*m++ = cp;
    179  1.1       gwr 				}
    180  1.1       gwr 				break;
    181  1.1       gwr 			} else if (cp == NULL)
    182  1.1       gwr 				cp = bp;
    183  1.1       gwr 		}
    184  1.1       gwr 		*m = NULL;
    185  1.1       gwr 		return(1);
    186  1.1       gwr 	}
    187  1.1       gwr 	/* NOTREACHED */
    188  1.1       gwr }
    189