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