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