Plan 9 from Bell Labs’s /usr/web/sources/patch/ape-erik/getgrent.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <stdio.h>
#include <grp.h>
#include <stdlib.h>

#define	CL	':'
#define	CM	','
#define	NL	'\n'
#define	MAXGRP	100

static char GROUP[] = "/etc/group";
static FILE *grf = NULL;
static char line[BUFSIZ+1];
static struct group group;
static char *gr_mem[MAXGRP];

void
setgrent(void)
{
	if( !grf )
		grf = fopen( GROUP, "r" );
	else
		rewind( grf );
}

void
endgrent(void)
{
	if( grf ){
		fclose( grf );
		grf = NULL;
	}
}

static char *
grskip(char *p, int c)
{
	while( *p && *p != c ) ++p;
	if( *p ) *p++ = 0;
	return( p );
}

struct group *
getgrent(void)
{
	char *p, **q;

	if( !grf && !(grf = fopen( GROUP, "r" )) )
		return(NULL);
	if( !(p = fgets( line, BUFSIZ, grf )) )
		return(NULL);
	group.gr_name = p;
	p = grskip(p,CL); /* passwd */
	group.gr_gid = atoi(p = grskip(p,CL));
	group.gr_mem = gr_mem;
	p = grskip(p,CL);
	grskip(p,NL);
	q = gr_mem;
	while( *p ){
		*q++ = p;
		p = grskip(p,CM);
	}
	*q = NULL;
	return( &group );
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.