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