DataMuseum.dk

Presents historical artifacts from the history of:

Commodore CBM-900

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about Commodore CBM-900

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦7ed5128d6⟧ TextFile

    Length: 1603 (0x643)
    Types: TextFile
    Notes: UNIX file
    Names: »getgrent.c«

Derivation

└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code
    └─⟦f4b8d8c84⟧ UNIX V7 Filesystem
        └─ ⟦this⟧ »libc/gen/getgrent.c« 

TextFile

/*
 * Coherent I/O Library.
 * Routines to get the group file entry.
 * (searches by next entry, name or numerical id).
 */

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

#define field(x)	{ x=cp; while (*cp++); }
#define	NGRPLINE	256
#define	NGRMEM		64		/* Maximum no. of members in a group */
#define	GRFILE	"/etc/group"

static	char	grline[NGRPLINE];
static	char	*grmems[NGRMEM+1];
static	struct	group gr;
static	FILE	*grfile	= { NULL };

struct group *
getgrnam(name)
char *name;
{
	register struct group *grp;

	setgrent();
	while ((grp = getgrent()) != NULL)
		if (streq(name, grp->gr_name))
			return (grp);
	return (NULL);
}

struct	group *
getgrgid(gid)
{
	register struct group *grp;

	setgrent();
	while ((grp = getgrent()) != NULL)
		if (gid == grp->gr_gid)
			return (grp);
	return (NULL);
}

struct group *
getgrent()
{
	register char *cp, *xp;

	if (grfile == NULL)
		if ((grfile = fopen(GRFILE, "r")) == NULL)
			return (NULL);
	cp = grline;
	{
		register int c;

		while ((c = getc(grfile))!=EOF && c!='\n') {
			if (c == ':')
				c = '\0';
			if (cp < &grline[NGRPLINE-1])
				*cp++ = c;
		}
		if (c == EOF)
			return (NULL);
	}
	*cp = '\0';
	cp = grline;
	field(gr.gr_name);
	field(gr.gr_passwd);
	field(xp);
	gr.gr_gid = atoi(xp);
	{
		register char **mp;

		gr.gr_mem = mp = grmems;
		for (;;) {
			if (*cp == '\0')
				break;
			*mp++ = cp;
			while (*cp!=',' && *cp!='\0')
				cp++;
			if (*cp == ',')
				*cp++ = '\0';
		}
		*mp = NULL;
	}
	return (&gr);
}

setgrent()
{
	if (grfile != NULL)
		rewind(grfile);
}

endgrent()
{
	if (grfile != NULL) {
		fclose(grfile);
		grfile = NULL;
	}
}