sparc: disable cancellable system, as it fails in strange ways right now
[uclibc-ng.git] / libc / pwd_grp / pwd_grp.c
blobf5c22f885a2303c8dc965a9d0c2af5b6cdbabfe6
1 /*
2 * Copyright (C) 2003 Manuel Novoa III <mjn3@uclibc.org>
3 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 */
8 /* Nov 6, 2003 Initial version.
10 * NOTE: This implementation is quite strict about requiring all
11 * field seperators. It also does not allow leading whitespace
12 * except when processing the numeric fields. glibc is more
13 * lenient. See the various glibc difference comments below.
15 * TODO:
16 * Move to dynamic allocation of (currently statically allocated)
17 * buffers; especially for the group-related functions since
18 * large group member lists will cause error returns.
22 #include <features.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <stddef.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <paths.h>
34 #ifdef __UCLIBC_HAS_SHADOW__
35 # include <shadow.h>
36 #endif
37 #include <bits/uClibc_mutex.h>
39 /**********************************************************************/
40 /* Prototypes for internal functions. */
42 extern int __parsepwent(void *pw, char *line) attribute_hidden;
43 extern int __parsegrent(void *gr, char *line) attribute_hidden;
44 extern int __parsespent(void *sp, char *line) attribute_hidden;
46 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
47 char *__restrict line_buff, size_t buflen, FILE *f) attribute_hidden;
49 extern gid_t* __getgrouplist_internal(const char *user, gid_t gid, int *ngroups) attribute_hidden;
51 /**********************************************************************/
52 /* For the various fget??ent_r funcs, return
54 * 0: success
55 * ENOENT: end-of-file encountered
56 * ERANGE: buflen too small
57 * other error values possible. See __pgsreader.
59 * Also, *result == resultbuf on success and NULL on failure.
61 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
62 * We do not, as it really isn't an error if we reach the end-of-file.
63 * Doing so is analogous to having fgetc() set errno on EOF.
65 /**********************************************************************/
66 #ifdef L_fgetpwent_r
68 #ifdef __USE_SVID
69 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
70 char *__restrict buffer, size_t buflen,
71 struct passwd **__restrict result)
73 int rv;
75 *result = NULL;
77 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
78 *result = resultbuf;
81 return rv;
83 libc_hidden_def(fgetpwent_r)
84 #endif
86 #endif
87 /**********************************************************************/
88 #ifdef L_fgetgrent_r
90 #ifdef __USE_SVID
91 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
92 char *__restrict buffer, size_t buflen,
93 struct group **__restrict result)
95 int rv;
97 *result = NULL;
99 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
100 *result = resultbuf;
103 return rv;
105 libc_hidden_def(fgetgrent_r)
106 #endif
108 #endif
109 /**********************************************************************/
110 #ifdef L_fgetspent_r
112 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
113 char *__restrict buffer, size_t buflen,
114 struct spwd **__restrict result)
116 int rv;
118 *result = NULL;
120 if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
121 *result = resultbuf;
124 return rv;
126 libc_hidden_def(fgetspent_r)
128 #endif
129 /**********************************************************************/
130 /* For the various fget??ent funcs, return NULL on failure and a
131 * pointer to the appropriate struct (statically allocated) on success.
133 /**********************************************************************/
134 #ifdef L_fgetpwent
136 #ifdef __USE_SVID
138 struct passwd *fgetpwent(FILE *stream)
140 static char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
141 static struct passwd resultbuf;
142 struct passwd *result;
144 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
145 return result;
147 #endif
149 #endif
150 /**********************************************************************/
151 #ifdef L_fgetgrent
153 #ifdef __USE_SVID
155 struct group *fgetgrent(FILE *stream)
157 static char buffer[__UCLIBC_GRP_BUFFER_SIZE__];
158 static struct group resultbuf;
159 struct group *result;
161 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
162 return result;
164 #endif
166 #endif
167 /**********************************************************************/
168 #ifdef L_fgetspent
171 struct spwd *fgetspent(FILE *stream)
173 static char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
174 static struct spwd resultbuf;
175 struct spwd *result;
177 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
178 return result;
181 #endif
182 /**********************************************************************/
183 #ifdef L_sgetspent_r
185 int sgetspent_r(const char *string, struct spwd *result_buf,
186 char *buffer, size_t buflen, struct spwd **result)
188 int rv = ERANGE;
190 *result = NULL;
192 if (buflen < __UCLIBC_PWD_BUFFER_SIZE__) {
193 DO_ERANGE:
194 __set_errno(rv);
195 goto DONE;
198 if (string != buffer) {
199 if (strlen(string) >= buflen) {
200 goto DO_ERANGE;
202 strcpy(buffer, string);
205 if (!(rv = __parsespent(result_buf, buffer))) {
206 *result = result_buf;
209 DONE:
210 return rv;
212 libc_hidden_def(sgetspent_r)
214 #endif
215 /**********************************************************************/
217 #ifdef GETXXKEY_R_FUNC
218 #error GETXXKEY_R_FUNC is already defined!
219 #endif
221 #ifdef L_getpwnam_r
222 #define GETXXKEY_R_FUNC getpwnam_r
223 #define GETXXKEY_R_PARSER __parsepwent
224 #define GETXXKEY_R_ENTTYPE struct passwd
225 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
226 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
227 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
228 #include "pwd_grp_internal.c"
229 #endif
231 #ifdef L_getgrnam_r
232 #define GETXXKEY_R_FUNC getgrnam_r
233 #define GETXXKEY_R_PARSER __parsegrent
234 #define GETXXKEY_R_ENTTYPE struct group
235 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
236 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
237 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
238 #include "pwd_grp_internal.c"
239 #endif
241 #ifdef L_getspnam_r
242 #define GETXXKEY_R_FUNC getspnam_r
243 #define GETXXKEY_R_PARSER __parsespent
244 #define GETXXKEY_R_ENTTYPE struct spwd
245 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
246 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
247 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
248 #include "pwd_grp_internal.c"
249 #endif
251 #ifdef L_getpwuid_r
252 #define GETXXKEY_R_FUNC getpwuid_r
253 #define GETXXKEY_R_PARSER __parsepwent
254 #define GETXXKEY_R_ENTTYPE struct passwd
255 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
256 #define DO_GETXXKEY_R_KEYTYPE uid_t
257 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
258 #include "pwd_grp_internal.c"
259 #endif
261 #ifdef L_getgrgid_r
262 #define GETXXKEY_R_FUNC getgrgid_r
263 #define GETXXKEY_R_PARSER __parsegrent
264 #define GETXXKEY_R_ENTTYPE struct group
265 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
266 #define DO_GETXXKEY_R_KEYTYPE gid_t
267 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
268 #include "pwd_grp_internal.c"
269 #endif
271 /**********************************************************************/
272 #ifdef L_getpwuid
275 struct passwd *getpwuid(uid_t uid)
277 static char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
278 static struct passwd resultbuf;
279 struct passwd *result;
281 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
282 return result;
285 #endif
286 /**********************************************************************/
287 #ifdef L_getgrgid
290 struct group *getgrgid(gid_t gid)
292 static char buffer[__UCLIBC_GRP_BUFFER_SIZE__];
293 static struct group resultbuf;
294 struct group *result;
296 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
297 return result;
300 #endif
301 /**********************************************************************/
302 #ifdef L_getspuid_r
304 /* This function is non-standard and is currently not built. It seems
305 * to have been created as a reentrant version of the non-standard
306 * functions getspuid. Why getspuid was added, I do not know. */
309 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
310 char *__restrict buffer, size_t buflen,
311 struct spwd **__restrict result)
313 int rv;
314 struct passwd *pp;
315 struct passwd password;
316 char pwd_buff[__UCLIBC_PWD_BUFFER_SIZE__];
318 *result = NULL;
319 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
320 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
323 return rv;
326 #endif
327 /**********************************************************************/
328 #ifdef L_getspuid
330 /* This function is non-standard and is currently not built.
331 * Why it was added, I do not know. */
333 struct spwd *getspuid(uid_t uid)
335 static char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
336 static struct spwd resultbuf;
337 struct spwd *result;
339 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
340 return result;
343 #endif
344 /**********************************************************************/
345 #ifdef L_getpwnam
348 struct passwd *getpwnam(const char *name)
350 static char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
351 static struct passwd resultbuf;
352 struct passwd *result;
354 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
355 return result;
358 #endif
359 /**********************************************************************/
360 #ifdef L_getgrnam
363 struct group *getgrnam(const char *name)
365 static char buffer[__UCLIBC_GRP_BUFFER_SIZE__];
366 static struct group resultbuf;
367 struct group *result;
369 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
370 return result;
373 #endif
374 /**********************************************************************/
375 #ifdef L_getspnam
378 struct spwd *getspnam(const char *name)
380 static char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
381 static struct spwd resultbuf;
382 struct spwd *result;
384 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
385 return result;
388 #endif
389 /**********************************************************************/
390 #ifdef L_getpw
393 int getpw(uid_t uid, char *buf)
395 struct passwd resultbuf;
396 struct passwd *result;
397 char buffer[__UCLIBC_PWD_BUFFER_SIZE__];
399 if (!buf) {
400 __set_errno(EINVAL);
401 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
402 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
403 resultbuf.pw_name, resultbuf.pw_passwd,
404 (unsigned long)(resultbuf.pw_uid),
405 (unsigned long)(resultbuf.pw_gid),
406 resultbuf.pw_gecos, resultbuf.pw_dir,
407 resultbuf.pw_shell) >= 0
409 return 0;
413 return -1;
416 #endif
417 /**********************************************************************/
419 #ifdef L_getpwent_r
420 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
422 static FILE *pwf /*= NULL*/;
424 void setpwent(void)
426 __UCLIBC_MUTEX_LOCK(mylock);
427 if (pwf) {
428 rewind(pwf);
430 __UCLIBC_MUTEX_UNLOCK(mylock);
433 void endpwent(void)
435 __UCLIBC_MUTEX_LOCK(mylock);
436 if (pwf) {
437 fclose(pwf);
438 pwf = NULL;
440 __UCLIBC_MUTEX_UNLOCK(mylock);
444 int getpwent_r(struct passwd *__restrict resultbuf,
445 char *__restrict buffer, size_t buflen,
446 struct passwd **__restrict result)
448 int rv;
450 __UCLIBC_MUTEX_LOCK(mylock);
452 *result = NULL; /* In case of error... */
454 if (!pwf) {
455 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
456 rv = errno;
457 goto ERR;
459 __STDIO_SET_USER_LOCKING(pwf);
462 if (!(rv = __pgsreader(__parsepwent, resultbuf,
463 buffer, buflen, pwf))) {
464 *result = resultbuf;
467 ERR:
468 __UCLIBC_MUTEX_UNLOCK(mylock);
470 return rv;
472 libc_hidden_def(getpwent_r)
474 #endif
475 /**********************************************************************/
476 #ifdef L_getgrent_r
477 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
479 static FILE *grf /*= NULL*/;
481 void setgrent(void)
483 __UCLIBC_MUTEX_LOCK(mylock);
484 if (grf) {
485 rewind(grf);
487 __UCLIBC_MUTEX_UNLOCK(mylock);
490 void endgrent(void)
492 __UCLIBC_MUTEX_LOCK(mylock);
493 if (grf) {
494 fclose(grf);
495 grf = NULL;
497 __UCLIBC_MUTEX_UNLOCK(mylock);
500 int getgrent_r(struct group *__restrict resultbuf,
501 char *__restrict buffer, size_t buflen,
502 struct group **__restrict result)
504 int rv;
506 __UCLIBC_MUTEX_LOCK(mylock);
508 *result = NULL; /* In case of error... */
510 if (!grf) {
511 if (!(grf = fopen(_PATH_GROUP, "r"))) {
512 rv = errno;
513 goto ERR;
515 __STDIO_SET_USER_LOCKING(grf);
518 if (!(rv = __pgsreader(__parsegrent, resultbuf,
519 buffer, buflen, grf))) {
520 *result = resultbuf;
523 ERR:
524 __UCLIBC_MUTEX_UNLOCK(mylock);
526 return rv;
528 libc_hidden_def(getgrent_r)
530 #endif
531 /**********************************************************************/
532 #ifdef L_getspent_r
533 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
535 static FILE *spf /*= NULL*/;
537 void setspent(void)
539 __UCLIBC_MUTEX_LOCK(mylock);
540 if (spf) {
541 rewind(spf);
543 __UCLIBC_MUTEX_UNLOCK(mylock);
546 void endspent(void)
548 __UCLIBC_MUTEX_LOCK(mylock);
549 if (spf) {
550 fclose(spf);
551 spf = NULL;
553 __UCLIBC_MUTEX_UNLOCK(mylock);
556 int getspent_r(struct spwd *resultbuf, char *buffer,
557 size_t buflen, struct spwd **result)
559 int rv;
561 __UCLIBC_MUTEX_LOCK(mylock);
563 *result = NULL; /* In case of error... */
565 if (!spf) {
566 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
567 rv = errno;
568 goto ERR;
570 __STDIO_SET_USER_LOCKING(spf);
573 if (!(rv = __pgsreader(__parsespent, resultbuf,
574 buffer, buflen, spf))) {
575 *result = resultbuf;
578 ERR:
579 __UCLIBC_MUTEX_UNLOCK(mylock);
581 return rv;
583 libc_hidden_def(getspent_r)
585 #endif
586 /**********************************************************************/
587 #ifdef L_getpwent
590 struct passwd *getpwent(void)
592 static char line_buff[__UCLIBC_PWD_BUFFER_SIZE__];
593 static struct passwd pwd;
594 struct passwd *result;
596 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
597 return result;
600 #endif
601 /**********************************************************************/
602 #ifdef L_getgrent
605 struct group *getgrent(void)
607 static char line_buff[__UCLIBC_GRP_BUFFER_SIZE__];
608 static struct group gr;
609 struct group *result;
611 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
612 return result;
615 #endif
616 /**********************************************************************/
617 #ifdef L_getspent
620 struct spwd *getspent(void)
622 static char line_buff[__UCLIBC_PWD_BUFFER_SIZE__];
623 static struct spwd spwd;
624 struct spwd *result;
626 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
627 return result;
630 #endif
631 /**********************************************************************/
632 #ifdef L_sgetspent
635 struct spwd *sgetspent(const char *string)
637 static char line_buff[__UCLIBC_PWD_BUFFER_SIZE__];
638 static struct spwd spwd;
639 struct spwd *result;
641 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
642 return result;
645 #endif
646 /**********************************************************************/
647 #ifdef L___getgrouplist_internal
649 gid_t attribute_hidden *__getgrouplist_internal(const char *user, gid_t gid, int *ngroups)
651 FILE *grfile;
652 gid_t *group_list;
653 int num_groups;
654 struct group group;
655 char buff[__UCLIBC_PWD_BUFFER_SIZE__];
657 *ngroups = num_groups = 1;
659 /* We alloc space for 8 gids at a time. */
660 group_list = malloc(8 * sizeof(group_list[0]));
661 if (!group_list)
662 return NULL;
664 group_list[0] = gid;
665 grfile = fopen(_PATH_GROUP, "r");
666 /* If /etc/group doesn't exist, we still return 1-element vector */
667 if (!grfile)
668 return group_list;
670 __STDIO_SET_USER_LOCKING(grfile);
672 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
673 char **m;
675 assert(group.gr_mem); /* Must have at least a NULL terminator. */
676 if (group.gr_gid == gid)
677 continue;
678 for (m = group.gr_mem; *m; m++) {
679 if (strcmp(*m, user) != 0)
680 continue;
681 if (!(num_groups & 7)) {
682 gid_t *tmp = realloc(group_list, (num_groups+8) * sizeof(group_list[0]));
683 if (!tmp)
684 goto DO_CLOSE;
685 group_list = tmp;
687 group_list[num_groups++] = group.gr_gid;
688 break;
692 DO_CLOSE:
693 fclose(grfile);
694 *ngroups = num_groups;
695 return group_list;
698 #endif
699 /**********************************************************************/
700 #ifdef L_getgrouplist
702 #if defined __USE_BSD || defined __USE_GNU
703 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
705 int sz = *ngroups;
706 gid_t *group_list = __getgrouplist_internal(user, gid, ngroups);
708 if (!group_list) {
709 /* malloc failure - what shall we do?
710 * fail with ENOMEM? I bet users never check for that */
711 /* *ngroups = 1; - already done by __getgrouplist_internal */
712 if (sz) {
713 groups[0] = gid;
714 return 1;
716 return -1;
718 /* *ngroups is non-zero here */
720 if (sz > *ngroups)
721 sz = *ngroups;
722 if (sz)
723 memcpy(groups, group_list, sz * sizeof(group_list[0]));
724 free(group_list);
725 if (sz < *ngroups)
726 return -1;
727 return sz;
729 #endif
731 #endif
732 /**********************************************************************/
733 #ifdef L_initgroups
735 #ifdef __USE_BSD
737 int initgroups(const char *user, gid_t gid)
739 int rv;
740 int num_groups = ((unsigned)~0) >> 1; /* INT_MAX */
741 gid_t *group_list = __getgrouplist_internal(user, gid, &num_groups);
742 if (!group_list)
743 return -1;
744 rv = setgroups(num_groups, group_list);
745 free(group_list);
746 return rv;
748 #endif
750 #endif
751 /**********************************************************************/
752 #ifdef L_putpwent
754 #ifdef __USE_SVID
755 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
757 int rv = -1;
759 if (!p || !f) {
760 __set_errno(EINVAL);
761 } else {
762 /* No extra thread locking is needed above what fprintf does. */
763 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
764 p->pw_name, p->pw_passwd,
765 (unsigned long)(p->pw_uid),
766 (unsigned long)(p->pw_gid),
767 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
769 rv = 0;
773 return rv;
775 #endif
777 #endif
778 /**********************************************************************/
779 #ifdef L_putgrent
781 int putgrent(const struct group *__restrict p, FILE *__restrict f)
783 static const char format[] = ",%s";
784 char **m;
785 const char *fmt;
786 int rv = -1;
787 __STDIO_AUTO_THREADLOCK_VAR;
789 if (!p || !f) { /* Sigh... glibc checks. */
790 __set_errno(EINVAL);
791 } else {
792 __STDIO_AUTO_THREADLOCK(f);
794 if (fprintf(f, "%s:%s:%lu:",
795 p->gr_name, p->gr_passwd,
796 (unsigned long)(p->gr_gid)) >= 0
799 fmt = format + 1;
801 assert(p->gr_mem);
802 m = p->gr_mem;
804 do {
805 if (!*m) {
806 if (__fputc_unlocked('\n', f) >= 0) {
807 rv = 0;
809 break;
811 if (fprintf(f, fmt, *m) < 0) {
812 break;
814 ++m;
815 fmt = format;
816 } while (1);
820 __STDIO_AUTO_THREADUNLOCK(f);
823 return rv;
826 #endif
827 /**********************************************************************/
828 #ifdef L_putspent
830 static const unsigned char _sp_off[] = {
831 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
832 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
833 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
834 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
835 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
836 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
839 int putspent(const struct spwd *p, FILE *stream)
841 static const char ld_format[] = "%ld:";
842 const char *f;
843 long int x;
844 size_t i;
845 int rv = -1;
846 __STDIO_AUTO_THREADLOCK_VAR;
848 /* Unlike putpwent and putgrent, glibc does not check the args. */
850 __STDIO_AUTO_THREADLOCK(stream);
852 if (fprintf(stream, "%s:%s:", p->sp_namp,
853 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
855 goto DO_UNLOCK;
858 for (i=0 ; i < sizeof(_sp_off) ; i++) {
859 f = ld_format;
860 if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
861 f += 3;
863 if (fprintf(stream, f, x) < 0) {
864 goto DO_UNLOCK;
868 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
869 goto DO_UNLOCK;
872 if (__fputc_unlocked('\n', stream) > 0) {
873 rv = 0;
876 DO_UNLOCK:
877 __STDIO_AUTO_THREADUNLOCK(stream);
879 return rv;
882 #endif
883 /**********************************************************************/
884 /* Internal uClibc functions. */
885 /**********************************************************************/
886 #ifdef L___parsepwent
888 static const unsigned char pw_off[] = {
889 offsetof(struct passwd, pw_name), /* 0 */
890 offsetof(struct passwd, pw_passwd), /* 1 */
891 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
892 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
893 offsetof(struct passwd, pw_gecos), /* 4 */
894 offsetof(struct passwd, pw_dir), /* 5 */
895 offsetof(struct passwd, pw_shell) /* 6 */
898 int attribute_hidden __parsepwent(void *data, char *line)
900 char *endptr;
901 char *p;
902 int i;
904 i = 0;
905 do {
906 p = ((char *) ((struct passwd *) data)) + pw_off[i];
908 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
909 *((char **) p) = line;
910 if (i==6) {
911 return 0;
913 /* NOTE: glibc difference - glibc allows omission of
914 * ':' seperators after the gid field if all remaining
915 * entries are empty. We require all separators. */
916 if (!(line = strchr(line, ':'))) {
917 break;
919 } else {
920 unsigned long t = strtoul(line, &endptr, 10);
921 /* Make sure we had at least one digit, and that the
922 * failing char is the next field seperator ':'. See
923 * glibc difference note above. */
924 /* TODO: Also check for leading whitespace? */
925 if ((endptr == line) || (*endptr != ':')) {
926 break;
928 line = endptr;
929 if (i & 1) { /* i == 3 -- gid */
930 *((gid_t *) p) = t;
931 } else { /* i == 2 -- uid */
932 *((uid_t *) p) = t;
936 *line++ = 0;
937 ++i;
938 } while (1);
940 return -1;
943 #endif
944 /**********************************************************************/
945 #ifdef L___parsegrent
947 static const unsigned char gr_off[] = {
948 offsetof(struct group, gr_name), /* 0 */
949 offsetof(struct group, gr_passwd), /* 1 */
950 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
953 int attribute_hidden __parsegrent(void *data, char *line)
955 char *endptr;
956 char *p;
957 int i;
958 char **members;
959 char *end_of_buf;
961 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
962 i = 0;
963 do {
964 p = ((char *) ((struct group *) data)) + gr_off[i];
966 if (i < 2) {
967 *((char **) p) = line;
968 if (!(line = strchr(line, ':'))) {
969 break;
971 *line++ = 0;
972 ++i;
973 } else {
974 *((gid_t *) p) = strtoul(line, &endptr, 10);
976 /* NOTE: glibc difference - glibc allows omission of the
977 * trailing colon when there is no member list. We treat
978 * this as an error. */
980 /* Make sure we had at least one digit, and that the
981 * failing char is the next field seperator ':'. See
982 * glibc difference note above. */
983 if ((endptr == line) || (*endptr != ':')) {
984 break;
987 i = 1; /* Count terminating NULL ptr. */
988 p = endptr;
990 if (p[1]) { /* We have a member list to process. */
991 /* Overwrite the last ':' with a ',' before counting.
992 * This allows us to test for initial ',' and adds
993 * one ',' so that the ',' count equals the member
994 * count. */
995 *p = ',';
996 do {
997 /* NOTE: glibc difference - glibc allows and trims leading
998 * (but not trailing) space. We treat this as an error. */
999 /* NOTE: glibc difference - glibc allows consecutive and
1000 * trailing commas, and ignores "empty string" users. We
1001 * treat this as an error. */
1002 if (*p == ',') {
1003 ++i;
1004 *p = 0; /* nul-terminate each member string. */
1005 if (!*++p || (*p == ',') || isspace(*p)) {
1006 goto ERR;
1009 } while (*++p);
1012 /* Now align (p+1), rounding up. */
1013 /* Assumes sizeof(char **) is a power of 2. */
1014 members = (char **)( (((intptr_t) p) + sizeof(char **))
1015 & ~((intptr_t)(sizeof(char **) - 1)) );
1017 if (((char *)(members + i)) > end_of_buf) { /* No space. */
1018 break;
1021 ((struct group *) data)->gr_mem = members;
1023 if (--i) {
1024 p = endptr; /* Pointing to char prior to first member. */
1025 do {
1026 *members++ = ++p;
1027 if (!--i) break;
1028 while (*++p) {}
1029 } while (1);
1031 *members = NULL;
1033 return 0;
1035 } while (1);
1037 ERR:
1038 return -1;
1041 #endif
1042 /**********************************************************************/
1043 #ifdef L___parsespent
1045 static const unsigned char sp_off[] = {
1046 offsetof(struct spwd, sp_namp), /* 0 */
1047 offsetof(struct spwd, sp_pwdp), /* 1 */
1048 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
1049 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
1050 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
1051 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
1052 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
1053 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
1054 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
1057 int attribute_hidden __parsespent(void *data, char * line)
1059 char *endptr;
1060 char *p;
1061 int i;
1063 i = 0;
1064 do {
1065 p = ((char *) ((struct spwd *) data)) + sp_off[i];
1066 if (i < 2) {
1067 *((char **) p) = line;
1068 if (!(line = strchr(line, ':'))) {
1069 break;
1071 } else {
1072 #if 0
1073 if (i==5) { /* Support for old format. */
1074 while (isspace(*line)) ++line; /* glibc eats space here. */
1075 if (!*line) {
1076 ((struct spwd *) data)->sp_warn = -1;
1077 ((struct spwd *) data)->sp_inact = -1;
1078 ((struct spwd *) data)->sp_expire = -1;
1079 ((struct spwd *) data)->sp_flag = ~0UL;
1080 return 0;
1083 #endif
1085 *((long *) p) = (long) strtoul(line, &endptr, 10);
1087 if (endptr == line) {
1088 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
1091 line = endptr;
1093 if (i == 8) {
1094 if (!*endptr) {
1095 return 0;
1097 break;
1100 if (*endptr != ':') {
1101 break;
1106 *line++ = 0;
1107 ++i;
1108 } while (1);
1110 return EINVAL;
1113 #endif
1114 /**********************************************************************/
1115 #ifdef L___pgsreader
1117 /* Reads until if EOF, or until if finds a line which fits in the buffer
1118 * and for which the parser function succeeds.
1120 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1123 int attribute_hidden __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
1124 char *__restrict line_buff, size_t buflen, FILE *f)
1126 size_t line_len;
1127 int skip;
1128 int rv = ERANGE;
1129 __STDIO_AUTO_THREADLOCK_VAR;
1131 if (buflen < __UCLIBC_PWD_BUFFER_SIZE__) {
1132 __set_errno(rv);
1133 } else {
1134 __STDIO_AUTO_THREADLOCK(f);
1136 skip = 0;
1137 do {
1138 if (!fgets_unlocked(line_buff, buflen, f)) {
1139 if (feof_unlocked(f)) {
1140 rv = ENOENT;
1142 break;
1145 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1146 if (line_buff[line_len] == '\n') {
1147 line_buff[line_len] = 0;
1148 } else if (line_len + 2 == buflen) { /* line too long */
1149 ++skip;
1150 continue;
1153 if (skip) {
1154 --skip;
1155 continue;
1158 /* NOTE: glibc difference - glibc strips leading whitespace from
1159 * records. We do not allow leading whitespace. */
1161 /* Skip empty lines, comment lines, and lines with leading
1162 * whitespace. */
1163 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1164 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
1165 /* The group entry parsing function needs to know where
1166 * the end of the buffer is so that it can construct the
1167 * group member ptr table. */
1168 ((struct group *) data)->gr_name = line_buff + buflen;
1171 if (!__parserfunc(data, line_buff)) {
1172 rv = 0;
1173 break;
1176 } while (1);
1178 __STDIO_AUTO_THREADUNLOCK(f);
1181 return rv;
1184 #endif
1185 /**********************************************************************/