1 #include <stdio.h>
2 #include <grp.h>
3 #include <stdlib.h>
4
5 #define CL ':'
6 #define CM ','
7 #define NL '\n'
8 #define MAXGRP 100
9
10 static char GROUP[] = "/etc/group";
11 static FILE *grf = NULL;
12 static char line[BUFSIZ+1];
13 static struct group group;
14 static char *gr_mem[MAXGRP];
15
16 void
setgrent(void)17 setgrent(void)
18 {
19 if( !grf )
20 grf = fopen( GROUP, "r" );
21 else
22 rewind( grf );
23 }
24
25 void
endgrent(void)26 endgrent(void)
27 {
28 if( grf ){
29 fclose( grf );
30 grf = NULL;
31 }
32 }
33
34 static char *
grskip(register char * p,register c)35 grskip(register char *p, register c)
36 {
37 while( *p && *p != c ) ++p;
38 if( *p ) *p++ = 0;
39 return( p );
40 }
41
42 struct group *
getgrent()43 getgrent()
44 {
45 register char *p, **q;
46
47 if( !grf && !(grf = fopen( GROUP, "r" )) )
48 return(NULL);
49 if( !(p = fgets( line, BUFSIZ, grf )) )
50 return(NULL);
51 group.gr_name = p;
52 p = grskip(p,CL); /* passwd */
53 group.gr_gid = atoi(p = grskip(p,CL));
54 group.gr_mem = gr_mem;
55 p = grskip(p,CL);
56 grskip(p,NL);
57 q = gr_mem;
58 while( *p ){
59 *q++ = p;
60 p = grskip(p,CM);
61 }
62 *q = NULL;
63 return( &group );
64 }
65