diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-05-13 17:27:15 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-05-13 17:27:15 +0200 |
commit | d27ac299eb34fc2be3038d029e8cd581138d0a06 (patch) | |
tree | 39d6e1e43630e5f084ce8170144beb567c53b6fa /coreutils/ls.c | |
parent | 3a240212d9133d7d8a4462e0410380c4eaf91c90 (diff) | |
download | busybox-w32-d27ac299eb34fc2be3038d029e8cd581138d0a06.tar.gz busybox-w32-d27ac299eb34fc2be3038d029e8cd581138d0a06.tar.bz2 busybox-w32-d27ac299eb34fc2be3038d029e8cd581138d0a06.zip |
ls: code shrink
function old new delta
list_single 990 961 -29
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/ls.c')
-rw-r--r-- | coreutils/ls.c | 53 |
1 files changed, 21 insertions, 32 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 145e7781f..0914d752b 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
@@ -659,23 +659,19 @@ static unsigned print_name(const char *name) | |||
659 | static NOINLINE unsigned list_single(const struct dnode *dn) | 659 | static NOINLINE unsigned list_single(const struct dnode *dn) |
660 | { | 660 | { |
661 | unsigned column = 0; | 661 | unsigned column = 0; |
662 | char *lpath = lpath; /* for compiler */ | 662 | char *lpath; |
663 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR | 663 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
664 | struct stat info; | 664 | struct stat info; |
665 | char append; | 665 | char append; |
666 | #endif | 666 | #endif |
667 | 667 | ||
668 | /* Never happens: | ||
669 | if (dn->fullname == NULL) | ||
670 | return 0; | ||
671 | */ | ||
672 | |||
673 | #if ENABLE_FEATURE_LS_FILETYPES | 668 | #if ENABLE_FEATURE_LS_FILETYPES |
674 | append = append_char(dn->dstat.st_mode); | 669 | append = append_char(dn->dstat.st_mode); |
675 | #endif | 670 | #endif |
676 | 671 | ||
677 | /* Do readlink early, so that if it fails, error message | 672 | /* Do readlink early, so that if it fails, error message |
678 | * does not appear *inside* the "ls -l" line */ | 673 | * does not appear *inside* the "ls -l" line */ |
674 | lpath = NULL; | ||
679 | if (all_fmt & LIST_SYMLINK) | 675 | if (all_fmt & LIST_SYMLINK) |
680 | if (S_ISLNK(dn->dstat.st_mode)) | 676 | if (S_ISLNK(dn->dstat.st_mode)) |
681 | lpath = xmalloc_readlink_or_warn(dn->fullname); | 677 | lpath = xmalloc_readlink_or_warn(dn->fullname); |
@@ -763,7 +759,7 @@ static NOINLINE unsigned list_single(const struct dnode *dn) | |||
763 | 759 | ||
764 | #if ENABLE_FEATURE_LS_COLOR | 760 | #if ENABLE_FEATURE_LS_COLOR |
765 | if (show_color) { | 761 | if (show_color) { |
766 | info.st_mode = 0; /* for fgcolor() */ | 762 | info.st_mode = 0; |
767 | lstat(dn->fullname, &info); | 763 | lstat(dn->fullname, &info); |
768 | printf("\033[%u;%um", bold(info.st_mode), | 764 | printf("\033[%u;%um", bold(info.st_mode), |
769 | fgcolor(info.st_mode)); | 765 | fgcolor(info.st_mode)); |
@@ -774,29 +770,26 @@ static NOINLINE unsigned list_single(const struct dnode *dn) | |||
774 | printf("\033[0m"); | 770 | printf("\033[0m"); |
775 | } | 771 | } |
776 | 772 | ||
777 | if (all_fmt & LIST_SYMLINK) { | 773 | if (lpath) { |
778 | if (S_ISLNK(dn->dstat.st_mode) && lpath) { | 774 | printf(" -> "); |
779 | printf(" -> "); | ||
780 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR | 775 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
781 | #if ENABLE_FEATURE_LS_COLOR | 776 | info.st_mode = 0; |
782 | info.st_mode = 0; /* for fgcolor() */ | 777 | stat(dn->fullname, &info); |
783 | #endif | 778 | # if ENABLE_FEATURE_LS_FILETYPES |
784 | if (stat(dn->fullname, &info) == 0) { | 779 | append = append_char(info.st_mode); |
785 | append = append_char(info.st_mode); | 780 | # endif |
786 | } | ||
787 | #endif | 781 | #endif |
788 | #if ENABLE_FEATURE_LS_COLOR | 782 | #if ENABLE_FEATURE_LS_COLOR |
789 | if (show_color) { | 783 | if (show_color) { |
790 | printf("\033[%u;%um", bold(info.st_mode), | 784 | printf("\033[%u;%um", bold(info.st_mode), |
791 | fgcolor(info.st_mode)); | 785 | fgcolor(info.st_mode)); |
792 | } | 786 | } |
793 | #endif | 787 | #endif |
794 | column += print_name(lpath) + 4; | 788 | column += print_name(lpath) + 4; |
795 | if (show_color) { | 789 | if (show_color) { |
796 | printf("\033[0m"); | 790 | printf("\033[0m"); |
797 | } | ||
798 | free(lpath); | ||
799 | } | 791 | } |
792 | free(lpath); | ||
800 | } | 793 | } |
801 | #if ENABLE_FEATURE_LS_FILETYPES | 794 | #if ENABLE_FEATURE_LS_FILETYPES |
802 | if (all_fmt & LIST_FILETYPE) { | 795 | if (all_fmt & LIST_FILETYPE) { |
@@ -902,9 +895,7 @@ static struct dnode **list_dir(const char *, unsigned *); | |||
902 | static void showdirs(struct dnode **dn, int first) | 895 | static void showdirs(struct dnode **dn, int first) |
903 | { | 896 | { |
904 | unsigned nfiles; | 897 | unsigned nfiles; |
905 | unsigned dndirs; | ||
906 | struct dnode **subdnp; | 898 | struct dnode **subdnp; |
907 | struct dnode **dnd; | ||
908 | 899 | ||
909 | for (; *dn; dn++) { | 900 | for (; *dn; dn++) { |
910 | if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) { | 901 | if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) { |
@@ -925,6 +916,8 @@ static void showdirs(struct dnode **dn, int first) | |||
925 | if (ENABLE_FEATURE_LS_RECURSIVE | 916 | if (ENABLE_FEATURE_LS_RECURSIVE |
926 | && (all_fmt & DISP_RECURSIVE) | 917 | && (all_fmt & DISP_RECURSIVE) |
927 | ) { | 918 | ) { |
919 | struct dnode **dnd; | ||
920 | unsigned dndirs; | ||
928 | /* recursive - list the sub-dirs */ | 921 | /* recursive - list the sub-dirs */ |
929 | dnd = splitdnarray(subdnp, SPLIT_SUBDIR); | 922 | dnd = splitdnarray(subdnp, SPLIT_SUBDIR); |
930 | dndirs = count_dirs(subdnp, SPLIT_SUBDIR); | 923 | dndirs = count_dirs(subdnp, SPLIT_SUBDIR); |
@@ -950,11 +943,6 @@ static struct dnode **list_dir(const char *path, unsigned *nfiles_p) | |||
950 | DIR *dir; | 943 | DIR *dir; |
951 | unsigned i, nfiles; | 944 | unsigned i, nfiles; |
952 | 945 | ||
953 | /* Never happens: | ||
954 | if (path == NULL) | ||
955 | return NULL; | ||
956 | */ | ||
957 | |||
958 | *nfiles_p = 0; | 946 | *nfiles_p = 0; |
959 | dir = warn_opendir(path); | 947 | dir = warn_opendir(path); |
960 | if (dir == NULL) { | 948 | if (dir == NULL) { |
@@ -1150,6 +1138,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv) | |||
1150 | ) | 1138 | ) |
1151 | /* ... or if -H: */ | 1139 | /* ... or if -H: */ |
1152 | || (option_mask32 & OPT_H) | 1140 | || (option_mask32 & OPT_H) |
1141 | /* ... or if -L, but my_stat always follows links if -L */ | ||
1153 | ); | 1142 | ); |
1154 | argv++; | 1143 | argv++; |
1155 | if (!cur) | 1144 | if (!cur) |