[comp.os.minix] pwd.h/getpwent.c

ast@cs.vu.nl (Andy Tanenbaum) (07/23/87)

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);
}

dale@oakhill.UUCP (Dale Stevens) (07/23/87)

In article <1536@botter.cs.vu.nl> ast@cs.vu.nl (Andy Tanenbaum) writes:
>
>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.
pwd.h and getpwent.c were included in my copy of 1.1.  Getpwent.c is in libsrc.a
but it has some bugs in it.  The following diff file shows the edits I made to
it.  After the edit it must be compiled with the -c and -LIB options and added
to libc.a with ar.

I missed the copy of login.c that was posted.  Will someone please send it to
me or maybe there are enough people that missed it that it should be reposted.

Andy, I think that minix is a great learning tool.  Best software I ever bought!

----------- getpwent.diff - cut here ---------------------------------
78c78
< 	pwd.name = _buf;
---
> 	pwd.pw_name = _buf;
80c80
< 	pwd.passwd = _buf;
---
> 	pwd.pw_passwd = _buf;
82c82
< 	pwd.uid = atoi (_buf);
---
> 	pwd.pw_uid = atoi (_buf);
84c84
< 	pwd.gid = atoi (_buf);
---
> 	pwd.pw_gid = atoi (_buf);
86c86
< 	pwd.gecos = _buf;
---
> 	pwd.pw_gecos = _buf;
88c88
< 	pwd.dir = _buf;
---
> 	pwd.pw_dir = _buf;
90c90
< 	pwd.shell = _buf;
---
> 	pwd.pw_shell = _buf;
102c102
< 		if (!strcmp (pwd -> name, name))
---
> 		if (!strcmp (pwd -> pw_name, name))
118c118
< 		if (pwd -> uid == uid)
---
> 		if (pwd -> pw_uid == uid)
-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Dale Stevens
      Digital Signal Processors Group,
      Motorola Inc.  Austin, Texas

 {ihnp4,seismo,ctvax,gatech}!ut-sally!oakhill!dale
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=