File:  [MW Coherent from dump] / coherent / d / lib / libc / gen / getpwent.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed May 29 04:56:37 2019 UTC (7 years, 2 months ago) by root
Branches: MarkWilliams, MAIN
CVS tags: relic, HEAD
coherent

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

#include <stdio.h>
#include <pwd.h>

#define field(x)	{ x=cp; while (*cp++); }
#define	NPWLINE	120
#define	PWFILE	"/etc/passwd"

static	char	pwline[NPWLINE];
static	struct	passwd pw;
static	FILE	*pwfile	= { NULL };

struct passwd *
getpwnam(name)
char *name;
{
	register struct passwd *pwp;

	setpwent();
	while ((pwp = getpwent()) != NULL)
		if (streq(name, pwp->pw_name))
			return (pwp);
	return (NULL);
}

struct	passwd *
getpwuid(uid)
{
	register struct passwd *pwp;

	setpwent();
	while ((pwp = getpwent()) != NULL)
		if (uid == pwp->pw_uid)
			return (pwp);
	return (NULL);
}

struct passwd *
getpwent()
{
	register char *cp, *xp;
	register c;

	if (pwfile == NULL)
		if ((pwfile = fopen(PWFILE, "r")) == NULL)
			return (NULL);
	cp = pwline;
	while ((c = getc(pwfile))!=EOF && c!='\n') {
		if (c == ':')
			c = '\0';
		if (cp < &pwline[NPWLINE-1])
			*cp++ = c;
	}
	if (c == EOF)
		return (NULL);
	*cp = '\0';
	cp = pwline;
	field(pw.pw_name);
	field(pw.pw_passwd);
	field(xp);
	pw.pw_uid = atoi(xp);
	field(xp);
	pw.pw_gid = atoi(xp);
	field(pw.pw_gecos);
	field(pw.pw_dir);
	field(pw.pw_shell);
	return (&pw);
}

setpwent()
{
	if (pwfile != NULL)
		rewind(pwfile);
}

endpwent()
{
	if (pwfile != NULL) {
		fclose(pwfile);
		pwfile = NULL;
	}
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.