1 /* vi: set sw=4 ts=4: */
2 /* Copyright (C) 2003 Manuel Novoa III
4 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 /* Nov 6, 2003 Initial version.
9 * NOTE: This implementation is quite strict about requiring all
10 * field seperators. It also does not allow leading whitespace
11 * except when processing the numeric fields. glibc is more
12 * lenient. See the various glibc difference comments below.
15 * Move to dynamic allocation of (currently statically allocated)
16 * buffers; especially for the group-related functions since
17 * large group member lists will cause error returns.
24 #define _PATH_SHADOW "/etc/shadow"
27 #define _PATH_PASSWD "/etc/passwd"
30 #define _PATH_GROUP "/etc/group"
33 /**********************************************************************/
34 /* Sizes for statically allocated buffers. */
36 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
37 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
38 #define PWD_BUFFER_SIZE 256
39 #define GRP_BUFFER_SIZE 256
41 /**********************************************************************/
42 /* Prototypes for internal functions. */
44 static int bb__pgsreader(
45 int FAST_FUNC (*parserfunc
)(void *d
, char *line
),
47 char *__restrict line_buff
,
51 static int FAST_FUNC
bb__parsepwent(void *pw
, char *line
);
52 static int FAST_FUNC
bb__parsegrent(void *gr
, char *line
);
53 #if ENABLE_USE_BB_SHADOW
54 static int FAST_FUNC
bb__parsespent(void *sp
, char *line
);
57 /**********************************************************************/
58 /* We avoid having big global data. */
61 /* Smaller things first */
62 struct passwd getpwuid_resultbuf
;
63 struct group getgrgid_resultbuf
;
64 struct passwd getpwnam_resultbuf
;
65 struct group getgrnam_resultbuf
;
67 char getpwuid_buffer
[PWD_BUFFER_SIZE
];
68 char getgrgid_buffer
[GRP_BUFFER_SIZE
];
69 char getpwnam_buffer
[PWD_BUFFER_SIZE
];
70 char getgrnam_buffer
[GRP_BUFFER_SIZE
];
72 struct passwd fgetpwent_resultbuf
;
73 struct group fgetgrent_resultbuf
;
74 struct spwd fgetspent_resultbuf
;
75 char fgetpwent_buffer
[PWD_BUFFER_SIZE
];
76 char fgetgrent_buffer
[GRP_BUFFER_SIZE
];
77 char fgetspent_buffer
[PWD_BUFFER_SIZE
];
79 #if 0 //ENABLE_USE_BB_SHADOW
80 struct spwd getspuid_resultbuf
;
81 struct spwd getspnam_resultbuf
;
82 char getspuid_buffer
[PWD_BUFFER_SIZE
];
83 char getspnam_buffer
[PWD_BUFFER_SIZE
];
85 // Not converted - too small to bother
86 //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
87 //FILE *pwf /*= NULL*/;
88 //FILE *grf /*= NULL*/;
89 //FILE *spf /*= NULL*/;
91 struct passwd getpwent_pwd
;
92 struct group getgrent_gr
;
93 char getpwent_line_buff
[PWD_BUFFER_SIZE
];
94 char getgrent_line_buff
[GRP_BUFFER_SIZE
];
96 #if 0 //ENABLE_USE_BB_SHADOW
97 struct spwd getspent_spwd
;
98 struct spwd sgetspent_spwd
;
99 char getspent_line_buff
[PWD_BUFFER_SIZE
];
100 char sgetspent_line_buff
[PWD_BUFFER_SIZE
];
104 static struct statics
*ptr_to_statics
;
106 static struct statics
*get_S(void)
109 ptr_to_statics
= xzalloc(sizeof(*ptr_to_statics
));
110 return ptr_to_statics
;
113 /* Always use in this order, get_S() must be called first */
114 #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
115 #define BUFFER(name) (S->name##_buffer)
117 /**********************************************************************/
118 /* For the various fget??ent_r funcs, return
121 * ENOENT: end-of-file encountered
122 * ERANGE: buflen too small
123 * other error values possible. See bb__pgsreader.
125 * Also, *result == resultbuf on success and NULL on failure.
127 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
128 * We do not, as it really isn't an error if we reach the end-of-file.
129 * Doing so is analogous to having fgetc() set errno on EOF.
131 /**********************************************************************/
133 int fgetpwent_r(FILE *__restrict stream
, struct passwd
*__restrict resultbuf
,
134 char *__restrict buffer
, size_t buflen
,
135 struct passwd
**__restrict result
)
141 rv
= bb__pgsreader(bb__parsepwent
, resultbuf
, buffer
, buflen
, stream
);
149 int fgetgrent_r(FILE *__restrict stream
, struct group
*__restrict resultbuf
,
150 char *__restrict buffer
, size_t buflen
,
151 struct group
**__restrict result
)
157 rv
= bb__pgsreader(bb__parsegrent
, resultbuf
, buffer
, buflen
, stream
);
165 #if ENABLE_USE_BB_SHADOW
166 #ifdef UNUSED_FOR_NOW
167 int fgetspent_r(FILE *__restrict stream
, struct spwd
*__restrict resultbuf
,
168 char *__restrict buffer
, size_t buflen
,
169 struct spwd
**__restrict result
)
175 rv
= bb__pgsreader(bb__parsespent
, resultbuf
, buffer
, buflen
, stream
);
185 /**********************************************************************/
186 /* For the various fget??ent funcs, return NULL on failure and a
187 * pointer to the appropriate struct (statically allocated) on success.
188 * TODO: audit & stop using these in bbox, they pull in static buffers */
189 /**********************************************************************/
191 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
192 struct passwd
*fgetpwent(FILE *stream
)
195 struct passwd
*resultbuf
= RESULTBUF(fgetpwent
);
196 char *buffer
= BUFFER(fgetpwent
);
197 struct passwd
*result
;
199 fgetpwent_r(stream
, resultbuf
, buffer
, sizeof(BUFFER(fgetpwent
)), &result
);
203 struct group
*fgetgrent(FILE *stream
)
206 struct group
*resultbuf
= RESULTBUF(fgetgrent
);
207 char *buffer
= BUFFER(fgetgrent
);
208 struct group
*result
;
210 fgetgrent_r(stream
, resultbuf
, buffer
, sizeof(BUFFER(fgetgrent
)), &result
);
215 #if ENABLE_USE_BB_SHADOW
216 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
217 struct spwd
*fgetspent(FILE *stream
)
220 struct spwd
*resultbuf
= RESULTBUF(fgetspent
);
221 char *buffer
= BUFFER(fgetspent
);
224 fgetspent_r(stream
, resultbuf
, buffer
, sizeof(BUFFER(fgetspent
)), &result
);
229 #ifdef UNUSED_FOR_NOW
230 int sgetspent_r(const char *string
, struct spwd
*result_buf
,
231 char *buffer
, size_t buflen
, struct spwd
**result
)
237 if (buflen
< PWD_BUFFER_SIZE
) {
243 if (string
!= buffer
) {
244 if (strlen(string
) >= buflen
) {
247 strcpy(buffer
, string
);
250 rv
= bb__parsespent(result_buf
, buffer
);
252 *result
= result_buf
;
259 #endif /* ENABLE_USE_BB_SHADOW */
261 /**********************************************************************/
263 #define GETXXKEY_R_FUNC getpwnam_r
264 #define GETXXKEY_R_PARSER bb__parsepwent
265 #define GETXXKEY_R_ENTTYPE struct passwd
266 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
267 #define GETXXKEY_R_KEYTYPE const char *__restrict
268 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
269 #include "pwd_grp_internal.c"
271 #define GETXXKEY_R_FUNC getgrnam_r
272 #define GETXXKEY_R_PARSER bb__parsegrent
273 #define GETXXKEY_R_ENTTYPE struct group
274 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
275 #define GETXXKEY_R_KEYTYPE const char *__restrict
276 #define GETXXKEY_R_PATHNAME _PATH_GROUP
277 #include "pwd_grp_internal.c"
279 #if ENABLE_USE_BB_SHADOW
280 #define GETXXKEY_R_FUNC getspnam_r
281 #define GETXXKEY_R_PARSER bb__parsespent
282 #define GETXXKEY_R_ENTTYPE struct spwd
283 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
284 #define GETXXKEY_R_KEYTYPE const char *__restrict
285 #define GETXXKEY_R_PATHNAME _PATH_SHADOW
286 #include "pwd_grp_internal.c"
289 #define GETXXKEY_R_FUNC getpwuid_r
290 #define GETXXKEY_R_PARSER bb__parsepwent
291 #define GETXXKEY_R_ENTTYPE struct passwd
292 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
293 #define GETXXKEY_R_KEYTYPE uid_t
294 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
295 #include "pwd_grp_internal.c"
297 #define GETXXKEY_R_FUNC getgrgid_r
298 #define GETXXKEY_R_PARSER bb__parsegrent
299 #define GETXXKEY_R_ENTTYPE struct group
300 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
301 #define GETXXKEY_R_KEYTYPE gid_t
302 #define GETXXKEY_R_PATHNAME _PATH_GROUP
303 #include "pwd_grp_internal.c"
305 /**********************************************************************/
306 /* TODO: audit & stop using these in bbox, they pull in static buffers */
308 /* This one has many users */
309 struct passwd
*getpwuid(uid_t uid
)
312 struct passwd
*resultbuf
= RESULTBUF(getpwuid
);
313 char *buffer
= BUFFER(getpwuid
);
314 struct passwd
*result
;
316 getpwuid_r(uid
, resultbuf
, buffer
, sizeof(BUFFER(getpwuid
)), &result
);
320 /* This one has many users */
321 struct group
*getgrgid(gid_t gid
)
324 struct group
*resultbuf
= RESULTBUF(getgrgid
);
325 char *buffer
= BUFFER(getgrgid
);
326 struct group
*result
;
328 getgrgid_r(gid
, resultbuf
, buffer
, sizeof(BUFFER(getgrgid
)), &result
);
332 #if 0 //ENABLE_USE_BB_SHADOW
333 /* This function is non-standard and is currently not built. It seems
334 * to have been created as a reentrant version of the non-standard
335 * functions getspuid. Why getspuid was added, I do not know. */
336 int getspuid_r(uid_t uid
, struct spwd
*__restrict resultbuf
,
337 char *__restrict buffer
, size_t buflen
,
338 struct spwd
**__restrict result
)
342 struct passwd password
;
343 char pwd_buff
[PWD_BUFFER_SIZE
];
346 rv
= getpwuid_r(uid
, &password
, pwd_buff
, sizeof(pwd_buff
), &pp
);
348 rv
= getspnam_r(password
.pw_name
, resultbuf
, buffer
, buflen
, result
);
354 /* This function is non-standard and is currently not built.
355 * Why it was added, I do not know. */
356 struct spwd
*getspuid(uid_t uid
)
359 struct spwd
*resultbuf
= RESULTBUF(getspuid
);
360 char *buffer
= BUFFER(getspuid
);
363 getspuid_r(uid
, resultbuf
, buffer
, sizeof(BUFFER(getspuid
)), &result
);
368 /* This one has many users */
369 struct passwd
*getpwnam(const char *name
)
372 struct passwd
*resultbuf
= RESULTBUF(getpwnam
);
373 char *buffer
= BUFFER(getpwnam
);
374 struct passwd
*result
;
376 getpwnam_r(name
, resultbuf
, buffer
, sizeof(BUFFER(getpwnam
)), &result
);
380 /* This one has many users */
381 struct group
*getgrnam(const char *name
)
384 struct group
*resultbuf
= RESULTBUF(getgrnam
);
385 char *buffer
= BUFFER(getgrnam
);
386 struct group
*result
;
388 getgrnam_r(name
, resultbuf
, buffer
, sizeof(BUFFER(getgrnam
)), &result
);
392 #if 0 //ENABLE_USE_BB_SHADOW
393 struct spwd
*getspnam(const char *name
)
396 struct spwd
*resultbuf
= RESULTBUF(getspnam
);
397 char *buffer
= BUFFER(getspnam
);
400 getspnam_r(name
, resultbuf
, buffer
, sizeof(BUFFER(getspnam
)), &result
);
405 /**********************************************************************/
407 /* FIXME: we don't have such CONFIG_xx - ?! */
409 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
410 static pthread_mutex_t mylock
= PTHREAD_MUTEX_INITIALIZER
;
411 # define LOCK pthread_mutex_lock(&mylock)
412 # define UNLOCK pthread_mutex_unlock(&mylock);
414 # define LOCK ((void) 0)
415 # define UNLOCK ((void) 0)
418 static FILE *pwf
/*= NULL*/;
439 int getpwent_r(struct passwd
*__restrict resultbuf
,
440 char *__restrict buffer
, size_t buflen
,
441 struct passwd
**__restrict result
)
446 *result
= NULL
; /* In case of error... */
449 pwf
= fopen_for_read(_PATH_PASSWD
);
456 rv
= bb__pgsreader(bb__parsepwent
, resultbuf
, buffer
, buflen
, pwf
);
466 static FILE *grf
/*= NULL*/;
486 int getgrent_r(struct group
*__restrict resultbuf
,
487 char *__restrict buffer
, size_t buflen
,
488 struct group
**__restrict result
)
493 *result
= NULL
; /* In case of error... */
496 grf
= fopen_for_read(_PATH_GROUP
);
503 rv
= bb__pgsreader(bb__parsegrent
, resultbuf
, buffer
, buflen
, grf
);
513 #ifdef UNUSED_FOR_NOW
514 #if ENABLE_USE_BB_SHADOW
515 static FILE *spf
/*= NULL*/;
535 int getspent_r(struct spwd
*resultbuf
, char *buffer
,
536 size_t buflen
, struct spwd
**result
)
541 *result
= NULL
; /* In case of error... */
544 spf
= fopen_for_read(_PATH_SHADOW
);
551 rv
= bb__pgsreader(bb__parsespent
, resultbuf
, buffer
, buflen
, spf
);
561 #endif /* UNUSED_FOR_NOW */
563 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
564 struct passwd
*getpwent(void)
566 static char line_buff
[PWD_BUFFER_SIZE
];
567 static struct passwd pwd
;
568 struct passwd
*result
;
570 getpwent_r(&pwd
, line_buff
, sizeof(line_buff
), &result
);
574 struct group
*getgrent(void)
576 static char line_buff
[GRP_BUFFER_SIZE
];
577 static struct group gr
;
578 struct group
*result
;
580 getgrent_r(&gr
, line_buff
, sizeof(line_buff
), &result
);
584 #if ENABLE_USE_BB_SHADOW
585 struct spwd
*getspent(void)
587 static char line_buff
[PWD_BUFFER_SIZE
];
588 static struct spwd spwd
;
591 getspent_r(&spwd
, line_buff
, sizeof(line_buff
), &result
);
595 struct spwd
*sgetspent(const char *string
)
597 static char line_buff
[PWD_BUFFER_SIZE
];
598 static struct spwd spwd
;
601 sgetspent_r(string
, &spwd
, line_buff
, sizeof(line_buff
), &result
);
605 #endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
607 static gid_t
*getgrouplist_internal(int *ngroups_ptr
, const char *user
, gid_t gid
)
613 char buff
[PWD_BUFFER_SIZE
];
615 /* We alloc space for 8 gids at a time. */
616 group_list
= xmalloc(8 * sizeof(group_list
[0]));
620 grfile
= fopen_for_read(_PATH_GROUP
);
622 while (!bb__pgsreader(bb__parsegrent
, &group
, buff
, sizeof(buff
), grfile
)) {
624 assert(group
.gr_mem
); /* Must have at least a NULL terminator. */
625 if (group
.gr_gid
== gid
)
627 for (m
= group
.gr_mem
; *m
; m
++) {
628 if (strcmp(*m
, user
) != 0)
630 group_list
= xrealloc_vector(group_list
, /*8=2^3:*/ 3, ngroups
);
631 group_list
[ngroups
++] = group
.gr_gid
;
637 *ngroups_ptr
= ngroups
;
641 int initgroups(const char *user
, gid_t gid
)
644 gid_t
*group_list
= getgrouplist_internal(&ngroups
, user
, gid
);
646 ngroups
= setgroups(ngroups
, group_list
);
651 int getgrouplist(const char *user
, gid_t gid
, gid_t
*groups
, int *ngroups
)
653 int ngroups_old
= *ngroups
;
654 gid_t
*group_list
= getgrouplist_internal(ngroups
, user
, gid
);
656 if (*ngroups
<= ngroups_old
) {
657 ngroups_old
= *ngroups
;
658 memcpy(groups
, group_list
, ngroups_old
* sizeof(groups
[0]));
666 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
667 int putpwent(const struct passwd
*__restrict p
, FILE *__restrict f
)
672 /* glibc does this check */
679 /* No extra thread locking is needed above what fprintf does. */
680 if (fprintf(f
, "%s:%s:%lu:%lu:%s:%s:%s\n",
681 p
->pw_name
, p
->pw_passwd
,
682 (unsigned long)(p
->pw_uid
),
683 (unsigned long)(p
->pw_gid
),
684 p
->pw_gecos
, p
->pw_dir
, p
->pw_shell
) >= 0
692 int putgrent(const struct group
*__restrict p
, FILE *__restrict f
)
697 /* glibc does this check */
704 if (fprintf(f
, "%s:%s:%lu:",
705 p
->gr_name
, p
->gr_passwd
,
706 (unsigned long)(p
->gr_gid
)) >= 0
708 static const char format
[] ALIGN1
= ",%s";
720 if (fputc('\n', f
) >= 0) {
725 if (fprintf(f
, fmt
, *m
) < 0) {
737 #if ENABLE_USE_BB_SHADOW
738 #ifdef UNUSED_FOR_NOW
739 static const unsigned char put_sp_off
[] ALIGN1
= {
740 offsetof(struct spwd
, sp_lstchg
), /* 2 - not a char ptr */
741 offsetof(struct spwd
, sp_min
), /* 3 - not a char ptr */
742 offsetof(struct spwd
, sp_max
), /* 4 - not a char ptr */
743 offsetof(struct spwd
, sp_warn
), /* 5 - not a char ptr */
744 offsetof(struct spwd
, sp_inact
), /* 6 - not a char ptr */
745 offsetof(struct spwd
, sp_expire
) /* 7 - not a char ptr */
748 int putspent(const struct spwd
*p
, FILE *stream
)
755 /* Unlike putpwent and putgrent, glibc does not check the args. */
756 if (fprintf(stream
, "%s:%s:", p
->sp_namp
,
757 (p
->sp_pwdp
? p
->sp_pwdp
: "")) < 0
762 for (i
= 0; i
< sizeof(put_sp_off
); i
++) {
764 x
= *(long *)((char *)p
+ put_sp_off
[i
]);
768 if (fprintf(stream
, fmt
, x
) < 0) {
773 if ((p
->sp_flag
!= ~0UL) && (fprintf(stream
, "%lu", p
->sp_flag
) < 0)) {
777 if (fputc('\n', stream
) > 0) {
785 #endif /* USE_BB_SHADOW */
787 /**********************************************************************/
788 /* Internal functions */
789 /**********************************************************************/
791 static const unsigned char pw_off
[] ALIGN1
= {
792 offsetof(struct passwd
, pw_name
), /* 0 */
793 offsetof(struct passwd
, pw_passwd
), /* 1 */
794 offsetof(struct passwd
, pw_uid
), /* 2 - not a char ptr */
795 offsetof(struct passwd
, pw_gid
), /* 3 - not a char ptr */
796 offsetof(struct passwd
, pw_gecos
), /* 4 */
797 offsetof(struct passwd
, pw_dir
), /* 5 */
798 offsetof(struct passwd
, pw_shell
) /* 6 */
801 static int FAST_FUNC
bb__parsepwent(void *data
, char *line
)
809 p
= (char *) data
+ pw_off
[i
];
811 if (i
< 2 || i
> 3) {
812 *((char **) p
) = line
;
816 /* NOTE: glibc difference - glibc allows omission of
817 * ':' seperators after the gid field if all remaining
818 * entries are empty. We require all separators. */
819 line
= strchr(line
, ':');
824 unsigned long t
= strtoul(line
, &endptr
, 10);
825 /* Make sure we had at least one digit, and that the
826 * failing char is the next field seperator ':'. See
827 * glibc difference note above. */
828 /* TODO: Also check for leading whitespace? */
829 if ((endptr
== line
) || (*endptr
!= ':')) {
833 if (i
& 1) { /* i == 3 -- gid */
835 } else { /* i == 2 -- uid */
847 /**********************************************************************/
849 static const unsigned char gr_off
[] ALIGN1
= {
850 offsetof(struct group
, gr_name
), /* 0 */
851 offsetof(struct group
, gr_passwd
), /* 1 */
852 offsetof(struct group
, gr_gid
) /* 2 - not a char ptr */
855 static int FAST_FUNC
bb__parsegrent(void *data
, char *line
)
863 end_of_buf
= ((struct group
*) data
)->gr_name
; /* Evil hack! */
866 p
= (char *) data
+ gr_off
[i
];
869 *((char **) p
) = line
;
870 line
= strchr(line
, ':');
877 *((gid_t
*) p
) = strtoul(line
, &endptr
, 10);
879 /* NOTE: glibc difference - glibc allows omission of the
880 * trailing colon when there is no member list. We treat
881 * this as an error. */
883 /* Make sure we had at least one digit, and that the
884 * failing char is the next field seperator ':'. See
885 * glibc difference note above. */
886 if ((endptr
== line
) || (*endptr
!= ':')) {
890 i
= 1; /* Count terminating NULL ptr. */
893 if (p
[1]) { /* We have a member list to process. */
894 /* Overwrite the last ':' with a ',' before counting.
895 * This allows us to (1) test for initial ','
896 * and (2) adds one ',' so that the number of commas
897 * equals the member count. */
900 /* NOTE: glibc difference - glibc allows and trims leading
901 * (but not trailing) space. We treat this as an error. */
902 /* NOTE: glibc difference - glibc allows consecutive and
903 * trailing commas, and ignores "empty string" users. We
904 * treat this as an error. */
907 *p
= 0; /* nul-terminate each member string. */
908 if (!*++p
|| (*p
== ',') || isspace(*p
)) {
915 /* Now align (p+1), rounding up. */
916 /* Assumes sizeof(char **) is a power of 2. */
917 members
= (char **)( (((intptr_t) p
) + sizeof(char **))
918 & ~((intptr_t)(sizeof(char **) - 1)) );
920 if (((char *)(members
+ i
)) > end_of_buf
) { /* No space. */
924 ((struct group
*) data
)->gr_mem
= members
;
927 p
= endptr
; /* Pointing to char prior to first member. */
946 /**********************************************************************/
948 #if ENABLE_USE_BB_SHADOW
949 static const unsigned char sp_off
[] ALIGN1
= {
950 offsetof(struct spwd
, sp_namp
), /* 0: char* */
951 offsetof(struct spwd
, sp_pwdp
), /* 1: char* */
952 offsetof(struct spwd
, sp_lstchg
), /* 2: long */
953 offsetof(struct spwd
, sp_min
), /* 3: long */
954 offsetof(struct spwd
, sp_max
), /* 4: long */
955 offsetof(struct spwd
, sp_warn
), /* 5: long */
956 offsetof(struct spwd
, sp_inact
), /* 6: long */
957 offsetof(struct spwd
, sp_expire
), /* 7: long */
958 offsetof(struct spwd
, sp_flag
) /* 8: unsigned long */
961 static int FAST_FUNC
bb__parsespent(void *data
, char *line
)
969 p
= (char *) data
+ sp_off
[i
];
971 *((char **) p
) = line
;
972 line
= strchr(line
, ':');
977 *((long *) p
) = strtoul(line
, &endptr
, 10);
978 if (endptr
== line
) {
986 return 0; /* all ok */
1000 /**********************************************************************/
1002 /* Reads until EOF, or until it finds a line which fits in the buffer
1003 * and for which the parser function succeeds.
1005 * Returns 0 on success and ENOENT for end-of-file (glibc convention).
1007 static int bb__pgsreader(
1008 int FAST_FUNC (*parserfunc
)(void *d
, char *line
),
1010 char *__restrict line_buff
,
1017 if (buflen
< PWD_BUFFER_SIZE
) {
1024 if (!fgets(line_buff
, buflen
, f
)) {
1032 int line_len
= strlen(line_buff
) - 1;
1033 if (line_len
>= 0 && line_buff
[line_len
] == '\n') {
1034 line_buff
[line_len
] = '\0';
1036 if (line_len
+ 2 == buflen
) {
1037 /* A start (or continuation) of overlong line */
1040 } /* else: a last line in the file, and it has no '\n' */
1044 /* This "line" is a remainder of overlong line, ignore */
1049 /* NOTE: glibc difference - glibc strips leading whitespace from
1050 * records. We do not allow leading whitespace. */
1052 /* Skip empty lines, comment lines, and lines with leading
1054 if (line_buff
[0] != '\0' && line_buff
[0] != '#' && !isspace(line_buff
[0])) {
1055 if (parserfunc
== bb__parsegrent
) {
1056 /* Do evil group hack:
1057 * The group entry parsing function needs to know where
1058 * the end of the buffer is so that it can construct the
1059 * group member ptr table. */
1060 ((struct group
*) data
)->gr_name
= line_buff
+ buflen
;
1062 if (parserfunc(data
, line_buff
) == 0) {