Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / misc / getgrent.c
1 /*
2  * getgrent - get entry form group file
3  *
4  * Author: Patrick van Kleef
5  */
6 /* $Id: getgrent.c,v 1.6 1994/06/24 11:45:12 ceriel Exp $ */
7
8 #include        <stdlib.h>
9 #include        <string.h>
10 #include        <grp.h>
11
12 #define O_RDONLY        0
13
14 int open(const char *path, int flags);
15
16 #if     defined(__BSD4_2)
17 typedef int off_t;                              /* see lseek(2) */
18 #else
19 typedef long off_t;
20 #endif
21
22 off_t _lseek(int d, off_t offset, int whence);
23 int _read(int d, char *buf, int nbytes);
24 int _close(int d);
25
26 #define RBUFSIZE        1024
27 static char _gr_file[] = "/etc/group";
28 static char _grbuf[256];
29 static char _buffer[RBUFSIZE];
30 static char *_pnt;
31 static char *_buf;
32 static int  _gfd = -1;
33 static int  _bufcnt;
34 static struct group grp;
35
36 int
37 setgrent(void)
38 {
39         if (_gfd >= 0)
40                 _lseek(_gfd, 0L, 0);
41         else
42                 _gfd = open(_gr_file, O_RDONLY);
43
44         _bufcnt = 0;
45         return _gfd;
46 }
47
48 void
49 endgrent(void) 
50 {
51         if (_gfd >= 0)
52                 _close(_gfd);
53
54         _gfd = -1;
55         _bufcnt = 0;
56 }
57
58
59 static int
60 getline(void) 
61 {
62         if (_gfd < 0 && setgrent() < 0)
63                 return 0;
64
65         _buf = _grbuf;
66         do {
67                 if (--_bufcnt <= 0){
68                         if ((_bufcnt = _read(_gfd, _buffer, RBUFSIZE)) <= 0)
69                                 return 0;
70                         else
71                                 _pnt = _buffer;
72                 }
73                 *_buf++ = *_pnt++;
74         } while (*_pnt != '\n');
75         _pnt++;
76         _bufcnt--;
77         *_buf = 0;
78         _buf = _grbuf;
79         return 1;
80 }
81
82 static void
83 skip_period(void) 
84 {
85         while (*_buf && *_buf != ':')
86                 _buf++;
87         *_buf++ = '\0';
88 }
89
90 struct group *
91 getgrent(void) 
92 {
93         if (getline() == 0)
94                return 0;
95
96         grp.gr_name = _buf;
97         skip_period();
98         grp.gr_passwd = _buf;
99         skip_period();
100         grp.gr_gid = atoi(_buf);
101         skip_period();
102         return &grp;
103 }
104
105 struct group *
106 getgrnam(const char *name)
107 {
108         struct group *g;
109
110         setgrent();
111         while ((g = getgrent()) != 0)
112                 if (!strcmp(g -> gr_name, name))
113                         break;
114         endgrent();
115         if (g != 0)
116                 return g;
117         else
118                 return 0;
119 }
120
121 struct group *
122 getgrgid(int gid)
123 {
124         struct group   *g;
125
126         setgrent();
127         while ((g = getgrent()) != 0)
128                 if (g -> gr_gid == gid)
129                         break;
130         endgrent();
131         if (g != 0)
132                 return g;
133         else
134                 return 0;
135 }