1*37131Sbostic /* 2*37131Sbostic * Copyright (c) 1989 The Regents of the University of California. 3*37131Sbostic * All rights reserved. 4*37131Sbostic * 5*37131Sbostic * Redistribution and use in source and binary forms are permitted 6*37131Sbostic * provided that the above copyright notice and this paragraph are 7*37131Sbostic * duplicated in all such forms and that any documentation, 8*37131Sbostic * advertising materials, and other materials related to such 9*37131Sbostic * distribution and use acknowledge that the software was developed 10*37131Sbostic * by the University of California, Berkeley. The name of the 11*37131Sbostic * University may not be used to endorse or promote products derived 12*37131Sbostic * from this software without specific prior written permission. 13*37131Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*37131Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*37131Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16*37131Sbostic */ 17*37131Sbostic 1826555Sdonn #if defined(LIBC_SCCS) && !defined(lint) 19*37131Sbostic static char sccsid[] = "@(#)getgrent.c 5.4 (Berkeley) 03/11/89"; 20*37131Sbostic #endif /* LIBC_SCCS and not lint */ 216349Swnj 22*37131Sbostic #include <sys/types.h> 232014Swnj #include <stdio.h> 242014Swnj #include <grp.h> 252014Swnj 26*37131Sbostic static FILE *_gr_fp; 27*37131Sbostic static struct group _gr_group; 28*37131Sbostic static int _gr_stayopen; 29*37131Sbostic static char *_gr_file = _PATH_GROUP; 302014Swnj 31*37131Sbostic #define MAXGRP 200 32*37131Sbostic static char *members[MAXGRP]; 33*37131Sbostic #define MAXLINELENGTH 1024 34*37131Sbostic static char line[MAXLINELENGTH]; 352014Swnj 36*37131Sbostic struct group * 37*37131Sbostic getgrent() 382014Swnj { 39*37131Sbostic if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL)) 40*37131Sbostic return((struct group *)NULL); 41*37131Sbostic return(&_gr_group); 422014Swnj } 432014Swnj 44*37131Sbostic struct group * 45*37131Sbostic getgrnam(name) 46*37131Sbostic char *name; 472014Swnj { 48*37131Sbostic int rval; 49*37131Sbostic 50*37131Sbostic if (!start_gr()) 51*37131Sbostic return((struct group *)NULL); 52*37131Sbostic rval = grscan(1, 0, name); 53*37131Sbostic if (!_gr_stayopen) 54*37131Sbostic endgrent(); 55*37131Sbostic return(rval ? &_gr_group : (struct group *)NULL); 56*37131Sbostic } 57*37131Sbostic 58*37131Sbostic struct group * 59*37131Sbostic getgrgid(gid) 60*37131Sbostic int gid; 61*37131Sbostic { 62*37131Sbostic int rval; 63*37131Sbostic 64*37131Sbostic if (!start_gr()) 65*37131Sbostic return((struct group *)NULL); 66*37131Sbostic rval = grscan(1, gid, (char *)NULL); 67*37131Sbostic if (!_gr_stayopen) 68*37131Sbostic endgrent(); 69*37131Sbostic return(rval ? &_gr_group : (struct group *)NULL); 70*37131Sbostic } 71*37131Sbostic 72*37131Sbostic static 73*37131Sbostic start_gr() 74*37131Sbostic { 75*37131Sbostic if (_gr_fp) { 76*37131Sbostic rewind(_gr_fp); 77*37131Sbostic return(1); 782014Swnj } 79*37131Sbostic return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0); 802014Swnj } 812014Swnj 82*37131Sbostic setgrent() 832014Swnj { 84*37131Sbostic return(setgroupent(0)); 852014Swnj } 862014Swnj 87*37131Sbostic setgroupent(stayopen) 88*37131Sbostic int stayopen; 892014Swnj { 90*37131Sbostic if (!start_gr()) 91*37131Sbostic return(0); 92*37131Sbostic _gr_stayopen = stayopen; 93*37131Sbostic return(1); 94*37131Sbostic } 952014Swnj 96*37131Sbostic void 97*37131Sbostic endgrent() 98*37131Sbostic { 99*37131Sbostic if (_gr_fp) { 100*37131Sbostic (void)fclose(_gr_fp); 101*37131Sbostic _gr_fp = (FILE *)NULL; 1022014Swnj } 1032014Swnj } 10432592Sbostic 105*37131Sbostic void 10632592Sbostic setgrfile(file) 10732592Sbostic char *file; 10832592Sbostic { 109*37131Sbostic _gr_file = file; 11032592Sbostic } 111*37131Sbostic 112*37131Sbostic static 113*37131Sbostic grscan(search, gid, name) 114*37131Sbostic register int search, gid; 115*37131Sbostic register char *name; 116*37131Sbostic { 117*37131Sbostic register char *cp, **m; 118*37131Sbostic char *fgets(), *strsep(), *index(); 119*37131Sbostic 120*37131Sbostic for (;;) { 121*37131Sbostic if (!fgets(line, sizeof(line), _gr_fp)) 122*37131Sbostic return(0); 123*37131Sbostic /* skip lines that are too big */ 124*37131Sbostic if (!index(line, '\n')) { 125*37131Sbostic int ch; 126*37131Sbostic 127*37131Sbostic while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 128*37131Sbostic ; 129*37131Sbostic continue; 130*37131Sbostic } 131*37131Sbostic _gr_group.gr_name = strsep(line, ":\n"); 132*37131Sbostic if (search && name && strcmp(_gr_group.gr_name, name)) 133*37131Sbostic continue; 134*37131Sbostic _gr_group.gr_passwd = strsep((char *)NULL, ":\n"); 135*37131Sbostic if (!(cp = strsep((char *)NULL, ":\n"))) 136*37131Sbostic continue; 137*37131Sbostic _gr_group.gr_gid = atoi(cp); 138*37131Sbostic if (search && gid && _gr_group.gr_gid != gid) 139*37131Sbostic continue; 140*37131Sbostic for (m = _gr_group.gr_mem = members;; ++m) { 141*37131Sbostic if (m == &members[MAXGRP - 1]) { 142*37131Sbostic *m = NULL; 143*37131Sbostic break; 144*37131Sbostic } 145*37131Sbostic if ((*m = strsep((char *)NULL, ", \n")) == NULL) 146*37131Sbostic break; 147*37131Sbostic } 148*37131Sbostic return(1); 149*37131Sbostic } 150*37131Sbostic /* NOTREACHED */ 151*37131Sbostic } 152