diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-18 22:39:34 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-18 22:39:34 +0000 |
commit | c1969f69b11dca4fdd8916684daea8b2abf63017 (patch) | |
tree | 7ce2f13aee53d0f69b8632de77d0672ecccdbca9 | |
parent | cbb4e61dd5431a48cf161489f02d46b77c997053 (diff) | |
download | busybox-w32-c1969f69b11dca4fdd8916684daea8b2abf63017.tar.gz busybox-w32-c1969f69b11dca4fdd8916684daea8b2abf63017.tar.bz2 busybox-w32-c1969f69b11dca4fdd8916684daea8b2abf63017.zip |
ls: make color-related code more readable. Fix a case when
it was working non-deterministically.
function old new delta
bold - 34 +34
showfiles 1508 1495 -13
fgcolor 50 34 -16
bgcolor 34 - -34
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/2 up/down: 34/-63) Total: -29 bytes
-rw-r--r-- | coreutils/ls.c | 62 |
1 files changed, 43 insertions, 19 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index a461b154f..8007f2a2e 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
@@ -127,14 +127,6 @@ SPLIT_SUBDIR = 2, | |||
127 | 127 | ||
128 | }; | 128 | }; |
129 | 129 | ||
130 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) | ||
131 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) | ||
132 | #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) | ||
133 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ | ||
134 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) | ||
135 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ | ||
136 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) | ||
137 | |||
138 | /* "[-]Cadil1", POSIX mandated options, busybox always supports */ | 130 | /* "[-]Cadil1", POSIX mandated options, busybox always supports */ |
139 | /* "[-]gnsx", POSIX non-mandated options, busybox always supports */ | 131 | /* "[-]gnsx", POSIX non-mandated options, busybox always supports */ |
140 | /* "[-]Q" GNU option? busybox always supports */ | 132 | /* "[-]Q" GNU option? busybox always supports */ |
@@ -344,18 +336,46 @@ static struct dnode *my_stat(const char *fullname, const char *name, int force_f | |||
344 | return cur; | 336 | return cur; |
345 | } | 337 | } |
346 | 338 | ||
339 | |||
340 | /* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket | ||
341 | * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file | ||
342 | * 3/7:multiplexed char/block device) | ||
343 | * and we use 0 for unknown and 15 for executables (see below) */ | ||
344 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) | ||
345 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) | ||
346 | #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) | ||
347 | /* 036 black foreground 050 black background | ||
348 | 037 red foreground 051 red background | ||
349 | 040 green foreground 052 green background | ||
350 | 041 brown foreground 053 brown background | ||
351 | 042 blue foreground 054 blue background | ||
352 | 043 magenta (purple) foreground 055 magenta background | ||
353 | 044 cyan (light blue) foreground 056 cyan background | ||
354 | 045 gray foreground 057 white background | ||
355 | */ | ||
356 | #define COLOR(mode) ( \ | ||
357 | /*un fi chr dir blk file link sock exe */ \ | ||
358 | "\037\043\043\045\042\045\043\043\000\045\044\045\043\045\045\040" \ | ||
359 | [TYPEINDEX(mode)]) | ||
360 | /* Select normal (0) [actually "reset all"] or bold (1) | ||
361 | * (other attributes are 2:dim 4:underline 5:blink 7:reverse, | ||
362 | * let's use 7 for "impossible" types, just for fun) | ||
363 | * Note: coreutils 6.9 uses inverted red for setuid binaries. | ||
364 | */ | ||
365 | #define ATTR(mode) ( \ | ||
366 | /*un fi chr dir blk file link sock exe */ \ | ||
367 | "\01\00\01\07\01\07\01\07\00\07\01\07\01\07\07\01" \ | ||
368 | [TYPEINDEX(mode)]) | ||
369 | |||
347 | #if ENABLE_FEATURE_LS_COLOR | 370 | #if ENABLE_FEATURE_LS_COLOR |
371 | /* mode of zero is interpreted as "unknown" (stat failed) */ | ||
348 | static char fgcolor(mode_t mode) | 372 | static char fgcolor(mode_t mode) |
349 | { | 373 | { |
350 | /* Check wheter the file is existing (if so, color it red!) */ | ||
351 | if (errno == ENOENT) | ||
352 | return '\037'; | ||
353 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) | 374 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
354 | return COLOR(0xF000); /* File is executable ... */ | 375 | return COLOR(0xF000); /* File is executable ... */ |
355 | return COLOR(mode); | 376 | return COLOR(mode); |
356 | } | 377 | } |
357 | 378 | static char bold(mode_t mode) | |
358 | static char bgcolor(mode_t mode) | ||
359 | { | 379 | { |
360 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) | 380 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
361 | return ATTR(0xF000); /* File is executable ... */ | 381 | return ATTR(0xF000); /* File is executable ... */ |
@@ -378,6 +398,7 @@ static char append_char(mode_t mode) | |||
378 | } | 398 | } |
379 | #endif | 399 | #endif |
380 | 400 | ||
401 | |||
381 | #define countdirs(A, B) count_dirs((A), (B), 1) | 402 | #define countdirs(A, B) count_dirs((A), (B), 1) |
382 | #define countsubdirs(A, B) count_dirs((A), (B), 0) | 403 | #define countsubdirs(A, B) count_dirs((A), (B), 0) |
383 | static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) | 404 | static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) |
@@ -818,10 +839,11 @@ static int list_single(const struct dnode *dn) | |||
818 | break; | 839 | break; |
819 | #endif | 840 | #endif |
820 | case LIST_FILENAME: | 841 | case LIST_FILENAME: |
821 | errno = 0; | ||
822 | #if ENABLE_FEATURE_LS_COLOR | 842 | #if ENABLE_FEATURE_LS_COLOR |
823 | if (show_color && !lstat(dn->fullname, &info)) { | 843 | if (show_color) { |
824 | printf("\033[%u;%um", bgcolor(info.st_mode), | 844 | info.st_mode = 0; /* for fgcolor() */ |
845 | lstat(dn->fullname, &info); | ||
846 | printf("\033[%u;%um", bold(info.st_mode), | ||
825 | fgcolor(info.st_mode)); | 847 | fgcolor(info.st_mode)); |
826 | } | 848 | } |
827 | #endif | 849 | #endif |
@@ -836,14 +858,16 @@ static int list_single(const struct dnode *dn) | |||
836 | if (!lpath) break; | 858 | if (!lpath) break; |
837 | printf(" -> "); | 859 | printf(" -> "); |
838 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR | 860 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
839 | if (!stat(dn->fullname, &info)) { | 861 | #if ENABLE_FEATURE_LS_COLOR |
862 | info.st_mode = 0; /* for fgcolor() */ | ||
863 | #endif | ||
864 | if (stat(dn->fullname, &info) == 0) { | ||
840 | append = append_char(info.st_mode); | 865 | append = append_char(info.st_mode); |
841 | } | 866 | } |
842 | #endif | 867 | #endif |
843 | #if ENABLE_FEATURE_LS_COLOR | 868 | #if ENABLE_FEATURE_LS_COLOR |
844 | if (show_color) { | 869 | if (show_color) { |
845 | errno = 0; | 870 | printf("\033[%u;%um", bold(info.st_mode), |
846 | printf("\033[%u;%um", bgcolor(info.st_mode), | ||
847 | fgcolor(info.st_mode)); | 871 | fgcolor(info.st_mode)); |
848 | } | 872 | } |
849 | #endif | 873 | #endif |