commiting patch from bug 71:
authorPaul Fox <pgf@brightstareng.com>
Mon, 1 Aug 2005 19:33:30 +0000 (19:33 -0000)
committerPaul Fox <pgf@brightstareng.com>
Mon, 1 Aug 2005 19:33:30 +0000 (19:33 -0000)
    0000071: patch: implement "--color" option for ls coloring control

coreutils/Config.in
coreutils/ls.c
include/usage.h

index 79081e5f786468a50f32083a37466b76c0eae6da..49b884b7059539adcc54d06b8b58201cf6297df4 100644 (file)
@@ -304,11 +304,23 @@ config CONFIG_FEATURE_LS_USERNAME
          Allow ls to display username/groupname for files.
 
 config CONFIG_FEATURE_LS_COLOR
-       bool "  Use color to identify file types"
+       bool "  Allow use of color to identify file types"
        default y
        depends on CONFIG_LS
        help
-         Allow ls to use color when displaying files.
+         This enables the --color option to ls.
+
+if CONFIG_FEATURE_LS_COLOR
+    config CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
+           bool "  Produce colored ls output by default"
+           default n
+           help
+             Saying yes here will turn coloring on by default,
+             even if no "--color" option is given to the ls command.
+             This is not recommended, since the colors are not
+             configurable, and the output may not be legible on
+             many output screens.
+endif
 
 config CONFIG_MD5SUM
        bool "md5sum"
index 92e150966548c1d16ac7a4404af42b03ec13c6b5..75d7b1f33c8e496564d8917181a33499742579e2 100644 (file)
@@ -60,6 +60,7 @@ enum {
 #include <fcntl.h>
 #include <signal.h>
 #include <termios.h>
+#include <getopt.h>
 #include <sys/ioctl.h>
 #include <sys/sysmacros.h>     /* major() and minor() */
 #include "busybox.h"
@@ -164,8 +165,18 @@ enum {
 
 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
 #ifdef CONFIG_FEATURE_LS_COLOR
+
 static int show_color = 0;
 
+/* long option entry used only for --color, which has no short option
+ * equivalent.  */
+static int got_color_opt;
+static struct option ls_color_opt[] =
+{
+        {"color", optional_argument, &got_color_opt, 1},
+        {NULL, 0, NULL, 0}
+};
+
 #define COLOR(mode)    ("\000\043\043\043\042\000\043\043"\
                                        "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
 #define ATTR(mode)     ("\00\00\01\00\01\00\01\00"\
@@ -984,8 +995,7 @@ extern int ls_main(int argc, char **argv)
 #endif
 
 #ifdef CONFIG_FEATURE_LS_COLOR
-       if (isatty(STDOUT_FILENO))
-               show_color = 1;
+       bb_applet_long_options = ls_color_opt;
 #endif
 
        /* process options */
@@ -1034,6 +1044,55 @@ extern int ls_main(int argc, char **argv)
                }
        }
 
+#ifdef CONFIG_FEATURE_LS_COLOR
+       if (got_color_opt) {
+               /* there is no way for bb_getopt_ulflags() to
+                * return us the argument string for long options
+                * which don't have a short option equivalent.
+                * all we can find out is that the option was
+                * present, and we have to rescan to find the
+                * argument string.
+                */
+               got_color_opt=0;
+               optind = 1;
+               while ((i = getopt_long (argc, argv, ls_options,
+                           ls_color_opt, NULL)) >= 0) {
+                       if (i != 0) continue;
+                       if (got_color_opt) {
+                               if (!optarg || strcmp("always", optarg) == 0)
+                                       show_color = 1;
+                               else if (strcmp("never", optarg) == 0)
+                                       show_color = 0;
+                               else if (strcmp("auto", optarg) == 0 &&
+                                               isatty(STDOUT_FILENO))
+                                       show_color = 1;
+
+                               /* don't break; want to a) pick up repeated
+                                * --color options, and b) leave optind
+                                * set correctly when we're done.
+                                */
+                               got_color_opt = 0;
+                       }
+               }
+#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
+       } else {
+               /* if no option set by user, then this config option
+                * forces "auto", which is what busybox 1.00 and previous
+                * did.  however, provide one more "out" for users that
+                * don't want color:  if LS_COLOR is set, and is null or
+                * "none" -- then default coloring to "off".
+                */
+               char *p;
+               if ((p = getenv ("LS_COLORS")) != NULL && 
+                       (*p == '\0' || (strcmp(p, "none") == 0))) {
+                       show_color = 0;
+               } else if (isatty(STDOUT_FILENO)) {
+                       show_color = 1;
+               }
+#endif
+       }
+#endif
+
        /* sort out which command line options take precedence */
 #ifdef CONFIG_FEATURE_LS_RECURSIVE
        if (all_fmt & DISP_NOLIST)
index 967ab3f191ff276eea3fa2e2db88a8dcf4cfc936..dad6078cd5a041d6df6df2d8cab81c104e1d9e5e 100644 (file)
 #else
 #  define USAGE_AUTOWIDTH(a)
 #endif
+#ifdef CONFIG_FEATURE_LS_COLOR
+  #define USAGE_LS_COLOR(a) a
+#else
+  #define USAGE_LS_COLOR(a)
+#endif
 
 #define ls_trivial_usage \
        "[-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...]"
        "\t-a\tdo not hide entries starting with .\n" \
        "\t-C\tlist entries by columns\n" \
        USAGE_LS_TIMESTAMPS("\t-c\twith -l: show ctime\n") \
+       USAGE_LS_COLOR("\t--color[={always,never,auto}]\tto control coloring\n") \
        "\t-d\tlist directory entries instead of contents\n" \
        USAGE_LS_TIMESTAMPS("\t-e\tlist both full date and full time\n") \
        USAGE_LS_FILETYPES("\t-F\tappend indicator (one of */=@|) to entries\n") \