fgetpwent.c
4.77 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2001, 2007, 2010-2012, 2014 Free Software
Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <pwd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
/* Assuming FP refers to an open file in format of /etc/passwd, read the next
line and store a pointer to a structure containing the broken-out
fields of the record in the location pointed to by RESULT. Use *BUFP
(of size *BUFS) to store the data, reallocating it if necessary. If
*BUFP is NULL or *BUFS is 0, allocate new memory. The caller should
free *BUFP when no longer needed. Do not try to free *RESULT!
Input lines not conforming to the /etc/passwd format are silently
ignored.
Return value:
0 - success
ENOMEM - not enough memory
ENOENT - no more entries.
*/
#define SPWDSIZ (sizeof (struct passwd))
int
mu_fgetpwent_r (FILE *fp, char **bufp, size_t *bufs, struct passwd **result)
{
char *buffer = *bufp;
size_t buflen = *bufs;
char *nb;
size_t ns;
struct passwd *pwbuf;
size_t pos = SPWDSIZ;
int c;
size_t off[6];
int i = 0;
if (!buffer)
buflen = 0;
while ((c = fgetc (fp)) != EOF)
{
if (pos >= buflen)
{
if (buflen == 0)
ns = SPWDSIZ + 128;
else
{
ns = ns * 2;
if (ns < buflen)
return ENOMEM;
}
nb = realloc (buffer, ns);
if (!nb)
return ENOMEM;
buffer = nb;
buflen = ns;
}
if (c == '\n')
{
buffer[pos++] = 0;
if (i != sizeof (off) / sizeof (off[0]))
{
pos = SPWDSIZ;
continue;
}
break;
}
if (c == ':')
{
buffer[pos++] = 0;
if (i < sizeof (off) / sizeof (off[0]))
off[i++] = pos;
}
else
buffer[pos++] = c;
}
if (pos == SPWDSIZ)
return ENOENT;
if (c == EOF)
{
if (i != sizeof (off) / sizeof (off[0]))
return ENOENT;
if (pos == buflen)
{
nb = realloc (buffer, buflen + 1);
if (!nb)
return ENOMEM;
buffer = nb;
buflen = ns;
}
buffer[pos++] = 0;
}
pwbuf = (struct passwd*) buffer;
pwbuf->pw_name = buffer + SPWDSIZ;
pwbuf->pw_passwd = buffer + off[0];
pwbuf->pw_uid = strtoul (buffer + off[1], NULL, 10);
pwbuf->pw_gid = strtoul (buffer + off[2], NULL, 10);
pwbuf->pw_gecos = buffer + off[3];
pwbuf->pw_dir = buffer + off[4];
pwbuf->pw_shell = buffer + off[5];
*bufp = buffer;
*bufs = buflen;
*result = pwbuf;
return 0;
}
/* A simple replacement for fgetpwent().
Note:
- it is not thread safe (neither is the original fgetpwent)
- uses dynamically allocated buffer that is __never__ freed.
- no support for shadow nor BSD-style pwddb (neither has the original
fgetpwent).
- no support for NIS(+).
Initial implementation by Alain Magloire.
Rewritten from scratch by Sergey Poznyakoff.
*/
struct passwd *
mu_fgetpwent (FILE *fp)
{
static char *buffer;
static size_t bufsize;
static struct passwd *pwbuf;
int rc = mu_fgetpwent_r (fp, &buffer, &bufsize, &pwbuf);
if (rc)
{
errno = rc;
return NULL;
}
return pwbuf;
}
#ifdef STANDALONE
# include <assert.h>
int
main (int argc, char **argv)
{
FILE *fp;
if (argc == 1)
{
char *a[3];
a[0] = argv[0];
a[1] = "/etc/passwd";
a[2] = NULL;
argv = a;
argc = 2;
}
while (--argc)
{
struct passwd *pwd;
char *file = *++argv;
fp = fopen (file, "r");
if (!fp)
{
perror (file);
continue;
}
printf ("Reading %s\n", file);
while ((pwd = fgetpwent (fp)))
{
printf ("--------------------------------------\n");
printf ("name %s\n", pwd->pw_name);
printf ("passwd %s\n", pwd->pw_passwd);
printf ("uid %d\n", pwd->pw_uid);
printf ("gid %d\n", pwd->pw_gid);
printf ("gecos %s\n", pwd->pw_gecos);
printf ("dir %s\n", pwd->pw_dir);
printf ("shell %s\n", pwd->pw_shell);
}
printf ("======================================\n");
printf ("End of %s\n", file);
fclose (fp);
}
return 0;
}
#endif