aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-07-03 12:22:19 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-07-03 12:22:19 +0200
commitae05dd42ca809d81f7025d29701d4641f1a996bf (patch)
treec1755f25015c5a9aeaf99c7d9a412f0d07109fbd
parent5508363fd05ecf71bc1db887256da1c0ae960c8d (diff)
downloadbusybox-w32-ae05dd42ca809d81f7025d29701d4641f1a996bf.tar.gz
busybox-w32-ae05dd42ca809d81f7025d29701d4641f1a996bf.tar.bz2
busybox-w32-ae05dd42ca809d81f7025d29701d4641f1a996bf.zip
ls: make --color more compatible with coreutils
function old new delta static.color_str - 34 +34 static.ls_longopts - 9 +9 ls_color_opt 9 - -9 ls_main 865 843 -22 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 0/1 up/down: 43/-31) Total: 12 bytes text data bss dec hex filename 820145 450 7692 828287 ca37f busybox_old 820144 450 7692 828286 ca37e busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/ls.c54
1 files changed, 34 insertions, 20 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index db42601e2..8a6faf23f 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -898,16 +898,6 @@ static int list_single(const struct dnode *dn)
898} 898}
899 899
900 900
901/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
902#if ENABLE_FEATURE_LS_COLOR
903/* long option entry used only for --color, which has no short option
904 * equivalent */
905static const char ls_color_opt[] ALIGN1 =
906 "color\0" Optional_argument "\xff" /* no short equivalent */
907 ;
908#endif
909
910
911int ls_main(int argc UNUSED_PARAM, char **argv) 901int ls_main(int argc UNUSED_PARAM, char **argv)
912{ 902{
913 struct dnode **dnd; 903 struct dnode **dnd;
@@ -920,8 +910,25 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
920 int dnfiles; 910 int dnfiles;
921 int dndirs; 911 int dndirs;
922 int i; 912 int i;
913#if ENABLE_FEATURE_LS_COLOR
914 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
915 /* coreutils 6.10:
916 * # ls --color=BOGUS
917 * ls: invalid argument 'BOGUS' for '--color'
918 * Valid arguments are:
919 * 'always', 'yes', 'force'
920 * 'never', 'no', 'none'
921 * 'auto', 'tty', 'if-tty'
922 * (and substrings: "--color=alwa" work too)
923 */
924 static const char ls_longopts[] ALIGN1 =
925 "color\0" Optional_argument "\xff"; /* no short equivalent */
926 static const char color_str[] ALIGN1 =
927 "always\0""yes\0""force\0"
928 "auto\0""tty\0""if-tty\0";
923 /* need to initialize since --color has _an optional_ argument */ 929 /* need to initialize since --color has _an optional_ argument */
924 IF_FEATURE_LS_COLOR(const char *color_opt = "always";) 930 const char *color_opt = color_str; /* "always" */
931#endif
925 932
926 INIT_G(); 933 INIT_G();
927 934
@@ -929,14 +936,14 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
929 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD)); 936 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
930 937
931#if ENABLE_FEATURE_AUTOWIDTH 938#if ENABLE_FEATURE_AUTOWIDTH
932 /* Obtain the terminal width */ 939 /* obtain the terminal width */
933 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL); 940 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
934 /* Go one less... */ 941 /* go one less... */
935 terminal_width--; 942 terminal_width--;
936#endif 943#endif
937 944
938 /* process options */ 945 /* process options */
939 IF_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;) 946 IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;)
940#if ENABLE_FEATURE_AUTOWIDTH 947#if ENABLE_FEATURE_AUTOWIDTH
941 opt_complementary = "T+:w+"; /* -T N, -w N */ 948 opt_complementary = "T+:w+"; /* -T N, -w N */
942 opt = getopt32(argv, ls_options, &tabstops, &terminal_width 949 opt = getopt32(argv, ls_options, &tabstops, &terminal_width
@@ -975,13 +982,20 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
975 if (!p || (p[0] && strcmp(p, "none") != 0)) 982 if (!p || (p[0] && strcmp(p, "none") != 0))
976 show_color = 1; 983 show_color = 1;
977 } 984 }
978 if (opt & OPT_color) { /* next flag after short options */ 985 if (opt & OPT_color) {
979 if (strcmp("always", color_opt) == 0) 986 if (color_opt[0] == 'n')
980 show_color = 1;
981 else if (strcmp("never", color_opt) == 0)
982 show_color = 0; 987 show_color = 0;
983 else if (strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO)) 988 else switch (index_in_substrings(color_str, color_opt)) {
984 show_color = 1; 989 case 3:
990 case 4:
991 case 5:
992 if (isatty(STDOUT_FILENO)) {
993 case 0:
994 case 1:
995 case 2:
996 show_color = 1;
997 }
998 }
985 } 999 }
986#endif 1000#endif
987 1001