summaryrefslogtreecommitdiff
path: root/mit-pthreads/gen/getpwent.c
blob: 7bcb2cbd610c94b3711995fd4eaa7abeca664479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (c) 1984 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getpwent.c	5.2 (Berkeley) 3/9/86";
#endif

#include <pthread.h>
#include <stdio.h>
#include <pwd.h>
#include "pwd_internal.h"

void
setpwent()
{
    pwf_context_t *_data;

    _data = _pw_get_data();

	if (_data) {
	  if (_data->pwf == NULL)
		_data->pwf = fopen(_data->pw_file, "r");
	  else
		rewind(_data->pwf);
	}
}

void
endpwent()
{
    pwf_context_t *_data;

	_data = _pw_get_data();

	if (_data) {
	  if (_data->pwf != NULL) {
		fclose(_data->pwf);
		_data->pwf = NULL;
	  }
#ifdef DBM_PWD_SUPPORT
	  if (_data->pw_db != (DBM *)0) {
		dbm_close(_data->pw_db);
		_data->pw_db = (DBM *)0;
		_data->pw_stayopen = 0;
	  }
#endif /* DBM_PWD_SUPPORT */
    }
}

static char *
pwskip(p)
	 char *p;
{
	while (*p && *p != ':' && *p != '\n')
		++p;
	if (*p)
		*p++ = 0;
	return(p);
}

struct passwd *
getpwent()
{
    pwf_context_t *_data;
	char *p;

	_data = _pw_get_data();
	if (!_data)
	    return 0;

	if (_data->pwf == NULL) {
		if ((_data->pwf = fopen(_data->pw_file, "r" )) == NULL)
			return(0);
	}
	p = fgets(_data->line, BUFSIZ, _data->pwf);
	if (p == NULL)
		return(0);
	_data->passwd.pw_name = p;
	p = pwskip(p);
	_data->passwd.pw_passwd = p;
	p = pwskip(p);
	_data->passwd.pw_uid = atoi(p);
	p = pwskip(p);
	_data->passwd.pw_gid = atoi(p);
	p = pwskip(p);
	_data->passwd.pw_gecos = p;
	p = pwskip(p);
	_data->passwd.pw_dir = p;
	p = pwskip(p);
	_data->passwd.pw_shell = p;
	while (*p && *p != '\n')
		p++;
	*p = '\0';
	return(&_data->passwd);
}

void
setpwfile(file)
	char *file;
{
    pwf_context_t *_data;

	_data = _pw_get_data();
	if (_data)
	    _data->pw_file = file;
}