diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-03-31 12:37:43 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-03-31 12:37:43 +0200 |
commit | 17fcd72add2a94c9542dba72124032b54838cb50 (patch) | |
tree | a7731a911e880d94047b4bdfcf50b8adc15e6500 | |
parent | 9e59e27cdfd58897b163ef554ee44bd418118081 (diff) | |
download | busybox-w32-17fcd72add2a94c9542dba72124032b54838cb50.tar.gz busybox-w32-17fcd72add2a94c9542dba72124032b54838cb50.tar.bz2 busybox-w32-17fcd72add2a94c9542dba72124032b54838cb50.zip |
libpwdgrp: style cleanups
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libpwdgrp/pwd_grp.c | 177 |
1 files changed, 91 insertions, 86 deletions
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c index acb1d96bb..26e8ff469 100644 --- a/libpwdgrp/pwd_grp.c +++ b/libpwdgrp/pwd_grp.c | |||
@@ -42,13 +42,17 @@ | |||
42 | /**********************************************************************/ | 42 | /**********************************************************************/ |
43 | /* Prototypes for internal functions. */ | 43 | /* Prototypes for internal functions. */ |
44 | 44 | ||
45 | static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, | 45 | static int bb__pgsreader( |
46 | char *__restrict line_buff, size_t buflen, FILE *f); | 46 | int FAST_FUNC (*parserfunc)(void *d, char *line), |
47 | 47 | void *data, | |
48 | static int bb__parsepwent(void *pw, char *line); | 48 | char *__restrict line_buff, |
49 | static int bb__parsegrent(void *gr, char *line); | 49 | size_t buflen, |
50 | FILE *f); | ||
51 | |||
52 | static int FAST_FUNC bb__parsepwent(void *pw, char *line); | ||
53 | static int FAST_FUNC bb__parsegrent(void *gr, char *line); | ||
50 | #if ENABLE_USE_BB_SHADOW | 54 | #if ENABLE_USE_BB_SHADOW |
51 | static int bb__parsespent(void *sp, char *line); | 55 | static int FAST_FUNC bb__parsespent(void *sp, char *line); |
52 | #endif | 56 | #endif |
53 | 57 | ||
54 | /**********************************************************************/ | 58 | /**********************************************************************/ |
@@ -230,7 +234,7 @@ int sgetspent_r(const char *string, struct spwd *result_buf, | |||
230 | 234 | ||
231 | if (buflen < PWD_BUFFER_SIZE) { | 235 | if (buflen < PWD_BUFFER_SIZE) { |
232 | DO_ERANGE: | 236 | DO_ERANGE: |
233 | errno=rv; | 237 | errno = rv; |
234 | goto DONE; | 238 | goto DONE; |
235 | } | 239 | } |
236 | 240 | ||
@@ -405,14 +409,17 @@ int getpw(uid_t uid, char *buf) | |||
405 | 409 | ||
406 | if (!buf) { | 410 | if (!buf) { |
407 | errno = EINVAL; | 411 | errno = EINVAL; |
408 | } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) { | 412 | return -1; |
413 | } | ||
414 | |||
415 | if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) { | ||
409 | if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n", | 416 | if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n", |
410 | resultbuf.pw_name, resultbuf.pw_passwd, | 417 | resultbuf.pw_name, resultbuf.pw_passwd, |
411 | (unsigned long)(resultbuf.pw_uid), | 418 | (unsigned long)(resultbuf.pw_uid), |
412 | (unsigned long)(resultbuf.pw_gid), | 419 | (unsigned long)(resultbuf.pw_gid), |
413 | resultbuf.pw_gecos, resultbuf.pw_dir, | 420 | resultbuf.pw_gecos, resultbuf.pw_dir, |
414 | resultbuf.pw_shell) >= 0 | 421 | resultbuf.pw_shell) >= 0 |
415 | ) { | 422 | ) { |
416 | return 0; | 423 | return 0; |
417 | } | 424 | } |
418 | } | 425 | } |
@@ -644,7 +651,7 @@ static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gi | |||
644 | for (m = group.gr_mem; *m; m++) { | 651 | for (m = group.gr_mem; *m; m++) { |
645 | if (strcmp(*m, user) != 0) | 652 | if (strcmp(*m, user) != 0) |
646 | continue; | 653 | continue; |
647 | group_list = xrealloc_vector(group_list, 3, ngroups); | 654 | group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups); |
648 | group_list[ngroups++] = group.gr_gid; | 655 | group_list[ngroups++] = group.gr_gid; |
649 | break; | 656 | break; |
650 | } | 657 | } |
@@ -686,16 +693,17 @@ int putpwent(const struct passwd *__restrict p, FILE *__restrict f) | |||
686 | 693 | ||
687 | if (!p || !f) { | 694 | if (!p || !f) { |
688 | errno = EINVAL; | 695 | errno = EINVAL; |
689 | } else { | 696 | return rv; |
690 | /* No extra thread locking is needed above what fprintf does. */ | 697 | } |
691 | if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n", | 698 | |
692 | p->pw_name, p->pw_passwd, | 699 | /* No extra thread locking is needed above what fprintf does. */ |
693 | (unsigned long)(p->pw_uid), | 700 | if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n", |
694 | (unsigned long)(p->pw_gid), | 701 | p->pw_name, p->pw_passwd, |
695 | p->pw_gecos, p->pw_dir, p->pw_shell) >= 0 | 702 | (unsigned long)(p->pw_uid), |
696 | ) { | 703 | (unsigned long)(p->pw_gid), |
697 | rv = 0; | 704 | p->pw_gecos, p->pw_dir, p->pw_shell) >= 0 |
698 | } | 705 | ) { |
706 | rv = 0; | ||
699 | } | 707 | } |
700 | 708 | ||
701 | return rv; | 709 | return rv; |
@@ -703,38 +711,39 @@ int putpwent(const struct passwd *__restrict p, FILE *__restrict f) | |||
703 | 711 | ||
704 | int putgrent(const struct group *__restrict p, FILE *__restrict f) | 712 | int putgrent(const struct group *__restrict p, FILE *__restrict f) |
705 | { | 713 | { |
706 | static const char format[] ALIGN1 = ",%s"; | ||
707 | |||
708 | char **m; | ||
709 | const char *fmt; | ||
710 | int rv = -1; | 714 | int rv = -1; |
711 | 715 | ||
712 | if (!p || !f) { /* Sigh... glibc checks. */ | 716 | if (!p || !f) { /* Sigh... glibc checks. */ |
713 | errno = EINVAL; | 717 | errno = EINVAL; |
714 | } else { | 718 | return rv; |
715 | if (fprintf(f, "%s:%s:%lu:", | 719 | } |
716 | p->gr_name, p->gr_passwd, | ||
717 | (unsigned long)(p->gr_gid)) >= 0 | ||
718 | ) { | ||
719 | 720 | ||
720 | fmt = format + 1; | 721 | if (fprintf(f, "%s:%s:%lu:", |
722 | p->gr_name, p->gr_passwd, | ||
723 | (unsigned long)(p->gr_gid)) >= 0 | ||
724 | ) { | ||
725 | static const char format[] ALIGN1 = ",%s"; | ||
721 | 726 | ||
722 | assert(p->gr_mem); | 727 | char **m; |
723 | m = p->gr_mem; | 728 | const char *fmt; |
724 | 729 | ||
725 | while (1) { | 730 | fmt = format + 1; |
726 | if (!*m) { | 731 | |
727 | if (fputc('\n', f) >= 0) { | 732 | assert(p->gr_mem); |
728 | rv = 0; | 733 | m = p->gr_mem; |
729 | } | 734 | |
730 | break; | 735 | while (1) { |
731 | } | 736 | if (!*m) { |
732 | if (fprintf(f, fmt, *m) < 0) { | 737 | if (fputc('\n', f) >= 0) { |
733 | break; | 738 | rv = 0; |
734 | } | 739 | } |
735 | ++m; | 740 | break; |
736 | fmt = format; | 741 | } |
742 | if (fprintf(f, fmt, *m) < 0) { | ||
743 | break; | ||
737 | } | 744 | } |
745 | m++; | ||
746 | fmt = format; | ||
738 | } | 747 | } |
739 | } | 748 | } |
740 | 749 | ||
@@ -742,7 +751,7 @@ int putgrent(const struct group *__restrict p, FILE *__restrict f) | |||
742 | } | 751 | } |
743 | 752 | ||
744 | #if ENABLE_USE_BB_SHADOW | 753 | #if ENABLE_USE_BB_SHADOW |
745 | static const unsigned char _sp_off[] ALIGN1 = { | 754 | static const unsigned char put_sp_off[] ALIGN1 = { |
746 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ | 755 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
747 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ | 756 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
748 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ | 757 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
@@ -753,9 +762,7 @@ static const unsigned char _sp_off[] ALIGN1 = { | |||
753 | 762 | ||
754 | int putspent(const struct spwd *p, FILE *stream) | 763 | int putspent(const struct spwd *p, FILE *stream) |
755 | { | 764 | { |
756 | static const char ld_format[] ALIGN1 = "%ld:"; | 765 | const char *fmt; |
757 | |||
758 | const char *f; | ||
759 | long x; | 766 | long x; |
760 | int i; | 767 | int i; |
761 | int rv = -1; | 768 | int rv = -1; |
@@ -767,13 +774,13 @@ int putspent(const struct spwd *p, FILE *stream) | |||
767 | goto DO_UNLOCK; | 774 | goto DO_UNLOCK; |
768 | } | 775 | } |
769 | 776 | ||
770 | for (i = 0; i < sizeof(_sp_off); i++) { | 777 | for (i = 0; i < sizeof(put_sp_off); i++) { |
771 | f = ld_format; | 778 | fmt = "%ld:"; |
772 | x = *(const long *)(((const char *) p) + _sp_off[i]); | 779 | x = *(long *)((char *)p + put_sp_off[i]); |
773 | if (x == -1) { | 780 | if (x == -1) { |
774 | f += 3; | 781 | fmt += 3; |
775 | } | 782 | } |
776 | if (fprintf(stream, f, x) < 0) { | 783 | if (fprintf(stream, fmt, x) < 0) { |
777 | goto DO_UNLOCK; | 784 | goto DO_UNLOCK; |
778 | } | 785 | } |
779 | } | 786 | } |
@@ -805,7 +812,7 @@ static const unsigned char pw_off[] ALIGN1 = { | |||
805 | offsetof(struct passwd, pw_shell) /* 6 */ | 812 | offsetof(struct passwd, pw_shell) /* 6 */ |
806 | }; | 813 | }; |
807 | 814 | ||
808 | static int bb__parsepwent(void *data, char *line) | 815 | static int FAST_FUNC bb__parsepwent(void *data, char *line) |
809 | { | 816 | { |
810 | char *endptr; | 817 | char *endptr; |
811 | char *p; | 818 | char *p; |
@@ -815,9 +822,9 @@ static int bb__parsepwent(void *data, char *line) | |||
815 | while (1) { | 822 | while (1) { |
816 | p = (char *) data + pw_off[i]; | 823 | p = (char *) data + pw_off[i]; |
817 | 824 | ||
818 | if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ | 825 | if (i < 2 || i > 3) { |
819 | *((char **) p) = line; | 826 | *((char **) p) = line; |
820 | if (i==6) { | 827 | if (i == 6) { |
821 | return 0; | 828 | return 0; |
822 | } | 829 | } |
823 | /* NOTE: glibc difference - glibc allows omission of | 830 | /* NOTE: glibc difference - glibc allows omission of |
@@ -844,8 +851,8 @@ static int bb__parsepwent(void *data, char *line) | |||
844 | } | 851 | } |
845 | } | 852 | } |
846 | 853 | ||
847 | *line++ = 0; | 854 | *line++ = '\0'; |
848 | ++i; | 855 | i++; |
849 | } /* while (1) */ | 856 | } /* while (1) */ |
850 | 857 | ||
851 | return -1; | 858 | return -1; |
@@ -859,7 +866,7 @@ static const unsigned char gr_off[] ALIGN1 = { | |||
859 | offsetof(struct group, gr_gid) /* 2 - not a char ptr */ | 866 | offsetof(struct group, gr_gid) /* 2 - not a char ptr */ |
860 | }; | 867 | }; |
861 | 868 | ||
862 | static int bb__parsegrent(void *data, char *line) | 869 | static int FAST_FUNC bb__parsegrent(void *data, char *line) |
863 | { | 870 | { |
864 | char *endptr; | 871 | char *endptr; |
865 | char *p; | 872 | char *p; |
@@ -878,8 +885,8 @@ static int bb__parsegrent(void *data, char *line) | |||
878 | if (!line) { | 885 | if (!line) { |
879 | break; | 886 | break; |
880 | } | 887 | } |
881 | *line++ = 0; | 888 | *line++ = '\0'; |
882 | ++i; | 889 | i++; |
883 | } else { | 890 | } else { |
884 | *((gid_t *) p) = strtoul(line, &endptr, 10); | 891 | *((gid_t *) p) = strtoul(line, &endptr, 10); |
885 | 892 | ||
@@ -954,18 +961,18 @@ static int bb__parsegrent(void *data, char *line) | |||
954 | 961 | ||
955 | #if ENABLE_USE_BB_SHADOW | 962 | #if ENABLE_USE_BB_SHADOW |
956 | static const unsigned char sp_off[] ALIGN1 = { | 963 | static const unsigned char sp_off[] ALIGN1 = { |
957 | offsetof(struct spwd, sp_namp), /* 0 */ | 964 | offsetof(struct spwd, sp_namp), /* 0: char* */ |
958 | offsetof(struct spwd, sp_pwdp), /* 1 */ | 965 | offsetof(struct spwd, sp_pwdp), /* 1: char* */ |
959 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ | 966 | offsetof(struct spwd, sp_lstchg), /* 2: long */ |
960 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ | 967 | offsetof(struct spwd, sp_min), /* 3: long */ |
961 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ | 968 | offsetof(struct spwd, sp_max), /* 4: long */ |
962 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ | 969 | offsetof(struct spwd, sp_warn), /* 5: long */ |
963 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ | 970 | offsetof(struct spwd, sp_inact), /* 6: long */ |
964 | offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ | 971 | offsetof(struct spwd, sp_expire), /* 7: long */ |
965 | offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ | 972 | offsetof(struct spwd, sp_flag) /* 8: unsigned long */ |
966 | }; | 973 | }; |
967 | 974 | ||
968 | static int bb__parsespent(void *data, char *line) | 975 | static int FAST_FUNC bb__parsespent(void *data, char *line) |
969 | { | 976 | { |
970 | char *endptr; | 977 | char *endptr; |
971 | char *p; | 978 | char *p; |
@@ -978,31 +985,26 @@ static int bb__parsespent(void *data, char *line) | |||
978 | *((char **) p) = line; | 985 | *((char **) p) = line; |
979 | line = strchr(line, ':'); | 986 | line = strchr(line, ':'); |
980 | if (!line) { | 987 | if (!line) { |
981 | break; | 988 | break; /* error */ |
982 | } | 989 | } |
983 | } else { | 990 | } else { |
984 | *((long *) p) = strtoul(line, &endptr, 10); | 991 | *((long *) p) = strtoul(line, &endptr, 10); |
985 | |||
986 | if (endptr == line) { | 992 | if (endptr == line) { |
987 | *((long *) p) = (i != 8) ? -1L : (long)(~0UL); | 993 | *((long *) p) = -1L; |
988 | } | 994 | } |
989 | |||
990 | line = endptr; | 995 | line = endptr; |
991 | |||
992 | if (i == 8) { | 996 | if (i == 8) { |
993 | if (!*endptr) { | 997 | if (*line != '\0') { |
994 | return 0; | 998 | break; /* error */ |
995 | } | 999 | } |
996 | break; | 1000 | return 0; /* all ok */ |
997 | } | 1001 | } |
998 | 1002 | if (*line != ':') { | |
999 | if (*endptr != ':') { | 1003 | break; /* error */ |
1000 | break; | ||
1001 | } | 1004 | } |
1002 | } | 1005 | } |
1003 | |||
1004 | *line++ = '\0'; | 1006 | *line++ = '\0'; |
1005 | ++i; | 1007 | i++; |
1006 | } | 1008 | } |
1007 | 1009 | ||
1008 | return EINVAL; | 1010 | return EINVAL; |
@@ -1016,9 +1018,12 @@ static int bb__parsespent(void *data, char *line) | |||
1016 | * | 1018 | * |
1017 | * Returns 0 on success and ENOENT for end-of-file (glibc concession). | 1019 | * Returns 0 on success and ENOENT for end-of-file (glibc concession). |
1018 | */ | 1020 | */ |
1019 | 1021 | static int bb__pgsreader( | |
1020 | static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, | 1022 | int FAST_FUNC (*parserfunc)(void *d, char *line), |
1021 | char *__restrict line_buff, size_t buflen, FILE *f) | 1023 | void *data, |
1024 | char *__restrict line_buff, | ||
1025 | size_t buflen, | ||
1026 | FILE *f) | ||
1022 | { | 1027 | { |
1023 | int skip; | 1028 | int skip; |
1024 | int rv = ERANGE; | 1029 | int rv = ERANGE; |