diff options
| author | Paul Fox <pgf@brightstareng.com> | 2005-08-01 19:33:30 +0000 |
|---|---|---|
| committer | Paul Fox <pgf@brightstareng.com> | 2005-08-01 19:33:30 +0000 |
| commit | 156dc41cbc5f6d063ee641d4bb18055f4ae1e3f5 (patch) | |
| tree | 7c84655b2121802fc8e6af14156b606ec041a054 | |
| parent | fc2256a6ca7f943d671edaceac1ed1dfe3d1751c (diff) | |
| download | busybox-w32-156dc41cbc5f6d063ee641d4bb18055f4ae1e3f5.tar.gz busybox-w32-156dc41cbc5f6d063ee641d4bb18055f4ae1e3f5.tar.bz2 busybox-w32-156dc41cbc5f6d063ee641d4bb18055f4ae1e3f5.zip | |
commiting patch from bug 71:
0000071: patch: implement "--color" option for ls coloring control
| -rw-r--r-- | coreutils/Config.in | 16 | ||||
| -rw-r--r-- | coreutils/ls.c | 63 | ||||
| -rw-r--r-- | include/usage.h | 6 |
3 files changed, 81 insertions, 4 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in index 79081e5f7..49b884b70 100644 --- a/coreutils/Config.in +++ b/coreutils/Config.in | |||
| @@ -304,11 +304,23 @@ config CONFIG_FEATURE_LS_USERNAME | |||
| 304 | Allow ls to display username/groupname for files. | 304 | Allow ls to display username/groupname for files. |
| 305 | 305 | ||
| 306 | config CONFIG_FEATURE_LS_COLOR | 306 | config CONFIG_FEATURE_LS_COLOR |
| 307 | bool " Use color to identify file types" | 307 | bool " Allow use of color to identify file types" |
| 308 | default y | 308 | default y |
| 309 | depends on CONFIG_LS | 309 | depends on CONFIG_LS |
| 310 | help | 310 | help |
| 311 | Allow ls to use color when displaying files. | 311 | This enables the --color option to ls. |
| 312 | |||
| 313 | if CONFIG_FEATURE_LS_COLOR | ||
| 314 | config CONFIG_FEATURE_LS_COLOR_IS_DEFAULT | ||
| 315 | bool " Produce colored ls output by default" | ||
| 316 | default n | ||
| 317 | help | ||
| 318 | Saying yes here will turn coloring on by default, | ||
| 319 | even if no "--color" option is given to the ls command. | ||
| 320 | This is not recommended, since the colors are not | ||
| 321 | configurable, and the output may not be legible on | ||
| 322 | many output screens. | ||
| 323 | endif | ||
| 312 | 324 | ||
| 313 | config CONFIG_MD5SUM | 325 | config CONFIG_MD5SUM |
| 314 | bool "md5sum" | 326 | bool "md5sum" |
diff --git a/coreutils/ls.c b/coreutils/ls.c index 92e150966..75d7b1f33 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
| @@ -60,6 +60,7 @@ enum { | |||
| 60 | #include <fcntl.h> | 60 | #include <fcntl.h> |
| 61 | #include <signal.h> | 61 | #include <signal.h> |
| 62 | #include <termios.h> | 62 | #include <termios.h> |
| 63 | #include <getopt.h> | ||
| 63 | #include <sys/ioctl.h> | 64 | #include <sys/ioctl.h> |
| 64 | #include <sys/sysmacros.h> /* major() and minor() */ | 65 | #include <sys/sysmacros.h> /* major() and minor() */ |
| 65 | #include "busybox.h" | 66 | #include "busybox.h" |
| @@ -164,8 +165,18 @@ enum { | |||
| 164 | 165 | ||
| 165 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ | 166 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ |
| 166 | #ifdef CONFIG_FEATURE_LS_COLOR | 167 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 168 | |||
| 167 | static int show_color = 0; | 169 | static int show_color = 0; |
| 168 | 170 | ||
| 171 | /* long option entry used only for --color, which has no short option | ||
| 172 | * equivalent. */ | ||
| 173 | static int got_color_opt; | ||
| 174 | static struct option ls_color_opt[] = | ||
| 175 | { | ||
| 176 | {"color", optional_argument, &got_color_opt, 1}, | ||
| 177 | {NULL, 0, NULL, 0} | ||
| 178 | }; | ||
| 179 | |||
| 169 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ | 180 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ |
| 170 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) | 181 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) |
| 171 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ | 182 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ |
| @@ -984,8 +995,7 @@ extern int ls_main(int argc, char **argv) | |||
| 984 | #endif | 995 | #endif |
| 985 | 996 | ||
| 986 | #ifdef CONFIG_FEATURE_LS_COLOR | 997 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 987 | if (isatty(STDOUT_FILENO)) | 998 | bb_applet_long_options = ls_color_opt; |
| 988 | show_color = 1; | ||
| 989 | #endif | 999 | #endif |
| 990 | 1000 | ||
| 991 | /* process options */ | 1001 | /* process options */ |
| @@ -1034,6 +1044,55 @@ extern int ls_main(int argc, char **argv) | |||
| 1034 | } | 1044 | } |
| 1035 | } | 1045 | } |
| 1036 | 1046 | ||
| 1047 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
| 1048 | if (got_color_opt) { | ||
| 1049 | /* there is no way for bb_getopt_ulflags() to | ||
| 1050 | * return us the argument string for long options | ||
| 1051 | * which don't have a short option equivalent. | ||
| 1052 | * all we can find out is that the option was | ||
| 1053 | * present, and we have to rescan to find the | ||
| 1054 | * argument string. | ||
| 1055 | */ | ||
| 1056 | got_color_opt=0; | ||
| 1057 | optind = 1; | ||
| 1058 | while ((i = getopt_long (argc, argv, ls_options, | ||
| 1059 | ls_color_opt, NULL)) >= 0) { | ||
| 1060 | if (i != 0) continue; | ||
| 1061 | if (got_color_opt) { | ||
| 1062 | if (!optarg || strcmp("always", optarg) == 0) | ||
| 1063 | show_color = 1; | ||
| 1064 | else if (strcmp("never", optarg) == 0) | ||
| 1065 | show_color = 0; | ||
| 1066 | else if (strcmp("auto", optarg) == 0 && | ||
| 1067 | isatty(STDOUT_FILENO)) | ||
| 1068 | show_color = 1; | ||
| 1069 | |||
| 1070 | /* don't break; want to a) pick up repeated | ||
| 1071 | * --color options, and b) leave optind | ||
| 1072 | * set correctly when we're done. | ||
| 1073 | */ | ||
| 1074 | got_color_opt = 0; | ||
| 1075 | } | ||
| 1076 | } | ||
| 1077 | #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT | ||
| 1078 | } else { | ||
| 1079 | /* if no option set by user, then this config option | ||
| 1080 | * forces "auto", which is what busybox 1.00 and previous | ||
| 1081 | * did. however, provide one more "out" for users that | ||
| 1082 | * don't want color: if LS_COLOR is set, and is null or | ||
| 1083 | * "none" -- then default coloring to "off". | ||
| 1084 | */ | ||
| 1085 | char *p; | ||
| 1086 | if ((p = getenv ("LS_COLORS")) != NULL && | ||
| 1087 | (*p == '\0' || (strcmp(p, "none") == 0))) { | ||
| 1088 | show_color = 0; | ||
| 1089 | } else if (isatty(STDOUT_FILENO)) { | ||
| 1090 | show_color = 1; | ||
| 1091 | } | ||
| 1092 | #endif | ||
| 1093 | } | ||
| 1094 | #endif | ||
| 1095 | |||
| 1037 | /* sort out which command line options take precedence */ | 1096 | /* sort out which command line options take precedence */ |
| 1038 | #ifdef CONFIG_FEATURE_LS_RECURSIVE | 1097 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
| 1039 | if (all_fmt & DISP_NOLIST) | 1098 | if (all_fmt & DISP_NOLIST) |
diff --git a/include/usage.h b/include/usage.h index 967ab3f19..dad6078cd 100644 --- a/include/usage.h +++ b/include/usage.h | |||
| @@ -1602,6 +1602,11 @@ | |||
| 1602 | #else | 1602 | #else |
| 1603 | # define USAGE_AUTOWIDTH(a) | 1603 | # define USAGE_AUTOWIDTH(a) |
| 1604 | #endif | 1604 | #endif |
| 1605 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
| 1606 | #define USAGE_LS_COLOR(a) a | ||
| 1607 | #else | ||
| 1608 | #define USAGE_LS_COLOR(a) | ||
| 1609 | #endif | ||
| 1605 | 1610 | ||
| 1606 | #define ls_trivial_usage \ | 1611 | #define ls_trivial_usage \ |
| 1607 | "[-1Aa" USAGE_LS_TIMESTAMPS("c") "Cd" USAGE_LS_TIMESTAMPS("e") USAGE_LS_FILETYPES("F") "iln" USAGE_LS_FILETYPES("p") USAGE_LS_FOLLOWLINKS("L") USAGE_LS_RECURSIVE("R") USAGE_LS_SORTFILES("rS") "s" USAGE_AUTOWIDTH("T") USAGE_LS_TIMESTAMPS("tu") USAGE_LS_SORTFILES("v") USAGE_AUTOWIDTH("w") "x" USAGE_LS_SORTFILES("X") USAGE_HUMAN_READABLE("h") USAGE_NOT_HUMAN_READABLE("") "k" USAGE_SELINUX("K") "] [filenames...]" | 1612 | "[-1Aa" USAGE_LS_TIMESTAMPS("c") "Cd" USAGE_LS_TIMESTAMPS("e") USAGE_LS_FILETYPES("F") "iln" USAGE_LS_FILETYPES("p") USAGE_LS_FOLLOWLINKS("L") USAGE_LS_RECURSIVE("R") USAGE_LS_SORTFILES("rS") "s" USAGE_AUTOWIDTH("T") USAGE_LS_TIMESTAMPS("tu") USAGE_LS_SORTFILES("v") USAGE_AUTOWIDTH("w") "x" USAGE_LS_SORTFILES("X") USAGE_HUMAN_READABLE("h") USAGE_NOT_HUMAN_READABLE("") "k" USAGE_SELINUX("K") "] [filenames...]" |
| @@ -1613,6 +1618,7 @@ | |||
| 1613 | "\t-a\tdo not hide entries starting with .\n" \ | 1618 | "\t-a\tdo not hide entries starting with .\n" \ |
| 1614 | "\t-C\tlist entries by columns\n" \ | 1619 | "\t-C\tlist entries by columns\n" \ |
| 1615 | USAGE_LS_TIMESTAMPS("\t-c\twith -l: show ctime\n") \ | 1620 | USAGE_LS_TIMESTAMPS("\t-c\twith -l: show ctime\n") \ |
| 1621 | USAGE_LS_COLOR("\t--color[={always,never,auto}]\tto control coloring\n") \ | ||
| 1616 | "\t-d\tlist directory entries instead of contents\n" \ | 1622 | "\t-d\tlist directory entries instead of contents\n" \ |
| 1617 | USAGE_LS_TIMESTAMPS("\t-e\tlist both full date and full time\n") \ | 1623 | USAGE_LS_TIMESTAMPS("\t-e\tlist both full date and full time\n") \ |
| 1618 | USAGE_LS_FILETYPES("\t-F\tappend indicator (one of */=@|) to entries\n") \ | 1624 | USAGE_LS_FILETYPES("\t-F\tappend indicator (one of */=@|) to entries\n") \ |
