137131Sbostic /* 237131Sbostic * Copyright (c) 1989 The Regents of the University of California. 337131Sbostic * All rights reserved. 437131Sbostic * 542624Sbostic * %sccs.include.redist.c% 637131Sbostic */ 737131Sbostic 826555Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46597Sdonn static char sccsid[] = "@(#)getgrent.c 5.8 (Berkeley) 02/23/91"; 1037131Sbostic #endif /* LIBC_SCCS and not lint */ 116349Swnj 1237131Sbostic #include <sys/types.h> 132014Swnj #include <stdio.h> 14*46597Sdonn #include <stdlib.h> 152014Swnj #include <grp.h> 162014Swnj 1737131Sbostic static FILE *_gr_fp; 1837131Sbostic static struct group _gr_group; 1937131Sbostic static int _gr_stayopen; 20*46597Sdonn static const char *_gr_file = _PATH_GROUP; 21*46597Sdonn static start_gr(); 22*46597Sdonn static grscan(); 232014Swnj 2437131Sbostic #define MAXGRP 200 2537131Sbostic static char *members[MAXGRP]; 2637131Sbostic #define MAXLINELENGTH 1024 2737131Sbostic static char line[MAXLINELENGTH]; 282014Swnj 2937131Sbostic struct group * 3037131Sbostic getgrent() 312014Swnj { 3237131Sbostic if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL)) 3337131Sbostic return((struct group *)NULL); 3437131Sbostic return(&_gr_group); 352014Swnj } 362014Swnj 3737131Sbostic struct group * 3837131Sbostic getgrnam(name) 39*46597Sdonn const char *name; 402014Swnj { 4137131Sbostic int rval; 4237131Sbostic 4337131Sbostic if (!start_gr()) 4437131Sbostic return((struct group *)NULL); 4537131Sbostic rval = grscan(1, 0, name); 4637131Sbostic if (!_gr_stayopen) 4737131Sbostic endgrent(); 4837131Sbostic return(rval ? &_gr_group : (struct group *)NULL); 4937131Sbostic } 5037131Sbostic 5137131Sbostic struct group * 52*46597Sdonn #ifdef __STDC__ 53*46597Sdonn getgrgid(gid_t gid) 54*46597Sdonn #else 5537131Sbostic getgrgid(gid) 56*46597Sdonn gid_t gid; 57*46597Sdonn #endif 5837131Sbostic { 5937131Sbostic int rval; 6037131Sbostic 6137131Sbostic if (!start_gr()) 6237131Sbostic return((struct group *)NULL); 6337131Sbostic rval = grscan(1, gid, (char *)NULL); 6437131Sbostic if (!_gr_stayopen) 6537131Sbostic endgrent(); 6637131Sbostic return(rval ? &_gr_group : (struct group *)NULL); 6737131Sbostic } 6837131Sbostic 6937131Sbostic static 7037131Sbostic start_gr() 7137131Sbostic { 7237131Sbostic if (_gr_fp) { 7337131Sbostic rewind(_gr_fp); 7437131Sbostic return(1); 752014Swnj } 7637131Sbostic return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0); 772014Swnj } 782014Swnj 79*46597Sdonn int 8037131Sbostic setgrent() 812014Swnj { 8237131Sbostic return(setgroupent(0)); 832014Swnj } 842014Swnj 85*46597Sdonn int 8637131Sbostic setgroupent(stayopen) 8737131Sbostic int stayopen; 882014Swnj { 8937131Sbostic if (!start_gr()) 9037131Sbostic return(0); 9137131Sbostic _gr_stayopen = stayopen; 9237131Sbostic return(1); 9337131Sbostic } 942014Swnj 9537131Sbostic void 9637131Sbostic endgrent() 9737131Sbostic { 9837131Sbostic if (_gr_fp) { 9937131Sbostic (void)fclose(_gr_fp); 10037131Sbostic _gr_fp = (FILE *)NULL; 1012014Swnj } 1022014Swnj } 10332592Sbostic 10437131Sbostic void 10532592Sbostic setgrfile(file) 106*46597Sdonn const char *file; 10732592Sbostic { 10837131Sbostic _gr_file = file; 10932592Sbostic } 11037131Sbostic 11137131Sbostic static 11237131Sbostic grscan(search, gid, name) 11337131Sbostic register int search, gid; 11437131Sbostic register char *name; 11537131Sbostic { 11637131Sbostic register char *cp, **m; 11741756Smarc char *bp; 11837131Sbostic char *fgets(), *strsep(), *index(); 11937131Sbostic 12037131Sbostic for (;;) { 12137131Sbostic if (!fgets(line, sizeof(line), _gr_fp)) 12237131Sbostic return(0); 12341756Smarc bp = line; 12437131Sbostic /* skip lines that are too big */ 12537131Sbostic if (!index(line, '\n')) { 12637131Sbostic int ch; 12737131Sbostic 12837131Sbostic while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 12937131Sbostic ; 13037131Sbostic continue; 13137131Sbostic } 13241756Smarc _gr_group.gr_name = strsep(&bp, ":\n"); 13337131Sbostic if (search && name && strcmp(_gr_group.gr_name, name)) 13437131Sbostic continue; 13541756Smarc _gr_group.gr_passwd = strsep(&bp, ":\n"); 13641756Smarc if (!(cp = strsep(&bp, ":\n"))) 13737131Sbostic continue; 13837131Sbostic _gr_group.gr_gid = atoi(cp); 13943783Sbostic if (search && name == NULL && _gr_group.gr_gid != gid) 14037131Sbostic continue; 14137131Sbostic for (m = _gr_group.gr_mem = members;; ++m) { 14237131Sbostic if (m == &members[MAXGRP - 1]) { 14337131Sbostic *m = NULL; 14437131Sbostic break; 14537131Sbostic } 14641756Smarc if ((*m = strsep(&bp, ", \n")) == NULL) 14737131Sbostic break; 14837131Sbostic } 14937131Sbostic return(1); 15037131Sbostic } 15137131Sbostic /* NOTREACHED */ 15237131Sbostic } 153