Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!mcvax!botter!ast
From: ast@cs.vu.nl (Andy Tanenbaum)
Newsgroups: comp.os.minix
Subject: pwd.h/getpwent.c
Message-ID: <1536@botter.cs.vu.nl>
Date: Thu, 23-Jul-87 07:04:25 EDT
Article-I.D.: botter.1536
Posted: Thu Jul 23 07:04:25 1987
Date-Received: Sat, 25-Jul-87 05:33:46 EDT
Reply-To: ast@cs.vu.nl (Andy Tanenbaum)
Distribution: world
Organization: VU Informatica, Amsterdam
Lines: 143


I thought that getpwent.c and pwd.h were in the 1.1 distribution (in libsrc.a)
but there have been several comments about them recently.  Here they are again.
If login.c or uudecode really don't compile, let me know.

Andy Tanenbaum (ast@cs.vu.nl)

---------------------- pwd.h -------------------------------------
struct passwd {
	char *pw_name;
	char *pw_passwd;
	int pw_uid;
	int pw_gid;
	char *pw_gecos;
	char *pw_dir;
	char *pw_shell;
};
------------------------ getpwent.c -------------------------
/*
 * get entry from password file
 *
 * By Patrick van Kleef
 *
 */


#include "../include/pwd.h"

#define PRIVATE static


PRIVATE char  _pw_file[] = "/etc/passwd";
PRIVATE char  _pwbuf[256];
PRIVATE char  _buffer[1024];
PRIVATE char *_pnt;
PRIVATE char *_buf;
PRIVATE int   _pw = -1;
PRIVATE int   _bufcnt;
PRIVATE struct passwd    pwd;

setpwent() 
{
	if (_pw >= 0)
		lseek (_pw, 0L, 0);
	else
		_pw = open (_pw_file, 0);

	_bufcnt = 0;
	return (_pw);
}


endpwent () 
{
	if (_pw >= 0)
		close (_pw);

	_pw = -1;
	_bufcnt = 0;
}

static getline () 
{
	if (_pw < 0 && setpwent () < 0)
		return (0);
	_buf = _pwbuf;
	do {
		if (--_bufcnt <= 0){
			if ((_bufcnt = read (_pw, _buffer, 1024)) <= 0)
				return (0);
			else
				_pnt = _buffer;
		}
		*_buf++ = *_pnt++;
	} while (*_pnt != '\n');
	_pnt++;
	_bufcnt--;
	*_buf = 0;
	_buf = _pwbuf;
	return (1);
}

static skip_period () 
{
	while (*_buf != ':')
		_buf++;

	*_buf++ = '\0';
}

struct passwd  *getpwent () 
{
	if (getline () == 0)
		return (0);

	pwd.name = _buf;
	skip_period ();
	pwd.passwd = _buf;
	skip_period ();
	pwd.uid = atoi (_buf);
	skip_period ();
	pwd.gid = atoi (_buf);
	skip_period ();
	pwd.gecos = _buf;
	skip_period ();
	pwd.dir = _buf;
	skip_period ();
	pwd.shell = _buf;

	return (&pwd);
}

struct passwd  *getpwnam (name)
char   *name;
{
	struct passwd  *pwd;

	setpwent ();
	while ((pwd = getpwent ()) != 0)
		if (!strcmp (pwd -> name, name))
			break;
	endpwent ();
	if (pwd != 0)
		return (pwd);
	else
		return (0);
}

struct passwd  *getpwuid (uid)
int     uid;
{
	struct passwd  *pwd;

	setpwent ();
	while ((pwd = getpwent ()) != 0)
		if (pwd -> uid == uid)
			break;
	endpwent ();
	if (pwd != 0)
		return (pwd);
	else
		return (0);
}