1989-12-18 14:40:54 +00:00
|
|
|
/*
|
|
|
|
* getgrent - get entry form group file
|
|
|
|
*
|
|
|
|
* Author: Patrick van Kleef
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
|
|
|
#define O_RDONLY 0
|
|
|
|
|
|
|
|
int open(const char *path, int flags);
|
|
|
|
|
1989-12-19 11:20:16 +00:00
|
|
|
#if defined(__BSD4_2)
|
1989-12-18 14:40:54 +00:00
|
|
|
typedef int off_t; /* see lseek(2) */
|
|
|
|
#else
|
|
|
|
typedef long off_t;
|
|
|
|
#endif
|
|
|
|
|
1990-01-22 11:44:21 +00:00
|
|
|
off_t _lseek(int d, off_t offset, int whence);
|
|
|
|
int _read(int d, char *buf, int nbytes);
|
|
|
|
int _close(int d);
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
#define RBUFSIZE 1024
|
|
|
|
static char _gr_file[] = "/etc/group";
|
|
|
|
static char _grbuf[256];
|
|
|
|
static char _buffer[RBUFSIZE];
|
|
|
|
static char *_pnt;
|
|
|
|
static char *_buf;
|
|
|
|
static int _gfd = -1;
|
|
|
|
static int _bufcnt;
|
|
|
|
static struct group grp;
|
|
|
|
|
|
|
|
int
|
|
|
|
setgrent(void)
|
|
|
|
{
|
|
|
|
if (_gfd >= 0)
|
1990-01-22 11:44:21 +00:00
|
|
|
_lseek(_gfd, 0L, 0);
|
1989-12-18 14:40:54 +00:00
|
|
|
else
|
|
|
|
_gfd = open(_gr_file, O_RDONLY);
|
|
|
|
|
|
|
|
_bufcnt = 0;
|
|
|
|
return _gfd;
|
|
|
|
}
|
|
|
|
|
1990-09-11 10:24:15 +00:00
|
|
|
void
|
1989-12-18 14:40:54 +00:00
|
|
|
endgrent(void)
|
|
|
|
{
|
|
|
|
if (_gfd >= 0)
|
1990-01-22 11:44:21 +00:00
|
|
|
_close(_gfd);
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
_gfd = -1;
|
|
|
|
_bufcnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
getline(void)
|
|
|
|
{
|
|
|
|
if (_gfd < 0 && setgrent() < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
_buf = _grbuf;
|
|
|
|
do {
|
|
|
|
if (--_bufcnt <= 0){
|
1990-01-22 11:44:21 +00:00
|
|
|
if ((_bufcnt = _read(_gfd, _buffer, RBUFSIZE)) <= 0)
|
1989-12-18 14:40:54 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
_pnt = _buffer;
|
|
|
|
}
|
|
|
|
*_buf++ = *_pnt++;
|
|
|
|
} while (*_pnt != '\n');
|
|
|
|
_pnt++;
|
|
|
|
_bufcnt--;
|
|
|
|
*_buf = 0;
|
|
|
|
_buf = _grbuf;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1990-09-11 10:24:15 +00:00
|
|
|
static void
|
1989-12-18 14:40:54 +00:00
|
|
|
skip_period(void)
|
|
|
|
{
|
|
|
|
while (*_buf && *_buf != ':')
|
|
|
|
_buf++;
|
|
|
|
*_buf++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
struct group *
|
|
|
|
getgrent(void)
|
|
|
|
{
|
|
|
|
if (getline() == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
grp.gr_name = _buf;
|
|
|
|
skip_period();
|
|
|
|
grp.gr_passwd = _buf;
|
|
|
|
skip_period();
|
|
|
|
grp.gr_gid = atoi(_buf);
|
|
|
|
skip_period();
|
|
|
|
return &grp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct group *
|
|
|
|
getgrnam(const char *name)
|
|
|
|
{
|
1991-09-30 16:24:45 +00:00
|
|
|
struct group *g;
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
setgrent();
|
1991-09-30 16:24:45 +00:00
|
|
|
while ((g = getgrent()) != 0)
|
|
|
|
if (!strcmp(g -> gr_name, name))
|
1989-12-18 14:40:54 +00:00
|
|
|
break;
|
|
|
|
endgrent();
|
1991-09-30 16:24:45 +00:00
|
|
|
if (g != 0)
|
|
|
|
return g;
|
1989-12-18 14:40:54 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct group *
|
|
|
|
getgrgid(int gid)
|
|
|
|
{
|
1991-09-30 16:24:45 +00:00
|
|
|
struct group *g;
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
setgrent();
|
1991-09-30 16:24:45 +00:00
|
|
|
while ((g = getgrent()) != 0)
|
|
|
|
if (g -> gr_gid == gid)
|
1989-12-18 14:40:54 +00:00
|
|
|
break;
|
|
|
|
endgrent();
|
1991-09-30 16:24:45 +00:00
|
|
|
if (g != 0)
|
|
|
|
return g;
|
1989-12-18 14:40:54 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|