summaryrefslogtreecommitdiff
path: root/libc/pwd_grp/__getpwent_r.c
blob: 7614b7edf2e048c0b0da2648a164523569b9feae (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
110
111
112
113
114
115
116
117
118
/* vi: set sw=4 ts=4: */
/*
 * __getpwent_r.c - This file is part of the libc-8086/pwd package for ELKS,
 * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
 * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org>
 * 
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * March 7, 2001 -- Reworked to be reentrant by Erik Andersen 
 * Oct   9, 2003 -- Reworked again by Erik Andersen to be fully reentrant
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "config.h"


/* This isn't as flash as my previous version -- it doesn't dynamically
  scale down the gecos on too-long lines, but it also makes fewer syscalls,
  so it's probably nicer.  Write me if you want the old version.  Maybe I
  should include it as a build-time option... ?
  -Nat <ndf@linux.mit.edu> */
	
int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd)
{
	char *endptr, *field_begin;
	char *gid_ptr=NULL;
	char *uid_ptr=NULL;
	int i, line_len;

	if (buflen<PWD_BUFFER_SIZE) {
		return ERANGE;
	}

	if (pw_fd == -1)
		setpwent();

	/* We use the restart label to handle malformatted lines */
restart:
	/* Read the passwd line into the buffer for processing */
	if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
		return EIO;
	}
	field_begin = strchr(line_buff, '\n');
	if (field_begin != NULL)
		lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR);
	else {
		/* The line is too long - skip it. :-\ */
		do {
			if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
				return EIO;
			}
		} while (!(field_begin = strchr(line_buff, '\n')));
		lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR);
		goto restart;
	}
	if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t')
		goto restart;
	*field_begin = '\0';

	/* We've read the line; now parse it. */
	field_begin = line_buff;
	for (i = 0; i < 7; i++) {
		switch (i) {
			case 0:
				passwd->pw_name = field_begin;
				break;
			case 1:
				passwd->pw_passwd = field_begin;
				break;
			case 2:
				uid_ptr = field_begin;
				break;
			case 3:
				gid_ptr = field_begin;
				break;
			case 4:
				passwd->pw_gecos = field_begin;
				break;
			case 5:
				passwd->pw_dir = field_begin;
				break;
			case 6:
				passwd->pw_shell = field_begin;
				break;
		}
		if (i < 6) {
			field_begin = strchr(field_begin, ':');
			if (field_begin == NULL)
				goto restart;
			*field_begin++ = '\0';
		}
	}
	passwd->pw_gid = (gid_t) strtoul(gid_ptr, &endptr, 10);
	if (*endptr != '\0')
		goto restart;

	passwd->pw_uid = (uid_t) strtoul(uid_ptr, &endptr, 10);
	if (*endptr != '\0')
		goto restart;

	return 0;
}