2 * __getpwent.c - This file is part of the libc-8086/pwd package for ELKS,
3 * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define PWD_BUFFER_SIZE 256
31 /* This isn't as flash as my previous version -- it doesn't dynamically
32 scale down the gecos on too-long lines, but it also makes fewer syscalls,
33 so it's probably nicer. Write me if you want the old version. Maybe I
34 should include it as a build-time option... ?
35 -Nat <ndf@linux.mit.edu> */
37 struct passwd *__getpwent(int pwd_fd)
39 static char line_buff[PWD_BUFFER_SIZE];
40 static struct passwd passwd;
48 /* We use the restart label to handle malformatted lines */
50 /* Read the passwd line into the static buffer using a minimal of
52 if ((line_len = read(pwd_fd, line_buff, PWD_BUFFER_SIZE)) <= 0)
54 field_begin = strchr(line_buff, '\n');
55 if (field_begin != NULL)
56 lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)),
58 else { /* The line is too long - skip it. :-\ */
61 if ((line_len = read(pwd_fd, line_buff, PWD_BUFFER_SIZE)) <= 0)
63 } while (!(field_begin = strchr(line_buff, '\n')));
64 lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1,
68 if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
73 /* We've read the line; now parse it. */
74 field_begin = line_buff;
75 for (i = 0; i < 7; i++) {
78 passwd.pw_name = field_begin;
81 passwd.pw_passwd = field_begin;
84 uid_ptr = field_begin;
87 gid_ptr = field_begin;
90 passwd.pw_gecos = field_begin;
93 passwd.pw_dir = field_begin;
96 passwd.pw_shell = field_begin;
100 field_begin = strchr(field_begin, ':');
101 if (field_begin == NULL)
103 *field_begin++ = '\0';
106 passwd.pw_gid = (gid_t) strtoul(gid_ptr, &endptr, 10);
110 passwd.pw_uid = (uid_t) strtoul(uid_ptr, &endptr, 10);