inetd: comment tweak. no code changes
[oweals/busybox.git] / coreutils / ls.c
index 379b0f94ffc9ab3aa737c30612332bf54698fe75..827b3508930f0366f20ec4db072764088a7ad2d0 100644 (file)
  * [2009-03]
  * ls sorts listing now, and supports almost all options.
  */
-
 #include "libbb.h"
+#include "unicode.h"
 
-#if ENABLE_FEATURE_ASSUME_UNICODE
-#include <wchar.h>
-#endif
 
 /* This is a NOEXEC applet. Be very careful! */
 
@@ -144,8 +141,7 @@ static const char ls_options[] ALIGN1 =
        IF_FEATURE_LS_FOLLOWLINKS("L")   /* 1, 24 */
        IF_FEATURE_LS_RECURSIVE("R")     /* 1, 25 */
        IF_FEATURE_HUMAN_READABLE("h")   /* 1, 26 */
-       IF_SELINUX("K") /* 1, 27 */
-       IF_SELINUX("Z") /* 1, 28 */
+       IF_SELINUX("KZ") /* 2, 28 */
        IF_FEATURE_AUTOWIDTH("T:w:") /* 2, 30 */
        ;
 enum {
@@ -162,6 +158,16 @@ enum {
        OPT_Q = (1 << 10),
        //OPT_A = (1 << 11),
        //OPT_k = (1 << 12),
+       OPTBIT_color = 13
+               + 4 * ENABLE_FEATURE_LS_TIMESTAMPS
+               + 4 * ENABLE_FEATURE_LS_SORTFILES
+               + 2 * ENABLE_FEATURE_LS_FILETYPES
+               + 1 * ENABLE_FEATURE_LS_FOLLOWLINKS
+               + 1 * ENABLE_FEATURE_LS_RECURSIVE
+               + 1 * ENABLE_FEATURE_HUMAN_READABLE
+               + 2 * ENABLE_SELINUX
+               + 2 * ENABLE_FEATURE_AUTOWIDTH,
+       OPT_color = 1 << OPTBIT_color,
 };
 
 enum {
@@ -283,21 +289,6 @@ enum {
 } while (0)
 
 
-#if ENABLE_FEATURE_ASSUME_UNICODE
-/* libbb candidate */
-static size_t mbstrlen(const char *string)
-{
-       size_t width = mbsrtowcs(NULL /*dest*/, &string,
-                               MAXINT(size_t) /*len*/, NULL /*state*/);
-       if (width == (size_t)-1)
-               return strlen(string);
-       return width;
-}
-#else
-#define mbstrlen(string) strlen(string)
-#endif
-
-
 static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
 {
        struct stat dstat;
@@ -565,7 +556,7 @@ static void showfiles(struct dnode **dn, int nfiles)
        } else {
                /* find the longest file name, use that as the column width */
                for (i = 0; i < nfiles; i++) {
-                       int len = mbstrlen(dn[i]->name);
+                       int len = bb_mbstrlen(dn[i]->name);
                        if (column_width < len)
                                column_width = len;
                }
@@ -712,7 +703,7 @@ static int print_name(const char *name)
 {
        if (option_mask32 & OPT_Q) {
 #if ENABLE_FEATURE_ASSUME_UNICODE
-               int len = 2 + mbstrlen(name);
+               int len = 2 + bb_mbstrlen(name);
 #else
                int len = 2;
 #endif
@@ -732,7 +723,7 @@ static int print_name(const char *name)
        /* No -Q: */
 #if ENABLE_FEATURE_ASSUME_UNICODE
        fputs(name, stdout);
-       return mbstrlen(name);
+       return bb_mbstrlen(name);
 #else
        return printf("%s", name);
 #endif
@@ -889,16 +880,6 @@ static int list_single(const struct dnode *dn)
 }
 
 
-/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
-#if ENABLE_FEATURE_LS_COLOR
-/* long option entry used only for --color, which has no short option
- * equivalent */
-static const char ls_color_opt[] ALIGN1 =
-       "color\0" Optional_argument "\xff" /* no short equivalent */
-       ;
-#endif
-
-
 int ls_main(int argc UNUSED_PARAM, char **argv)
 {
        struct dnode **dnd;
@@ -911,23 +892,42 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
        int dnfiles;
        int dndirs;
        int i;
+#if ENABLE_FEATURE_LS_COLOR
+       /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
+       /* coreutils 6.10:
+        * # ls --color=BOGUS
+        * ls: invalid argument 'BOGUS' for '--color'
+        * Valid arguments are:
+        * 'always', 'yes', 'force'
+        * 'never', 'no', 'none'
+        * 'auto', 'tty', 'if-tty'
+        * (and substrings: "--color=alwa" work too)
+        */
+       static const char ls_longopts[] ALIGN1 =
+               "color\0" Optional_argument "\xff"; /* no short equivalent */
+       static const char color_str[] ALIGN1 =
+               "always\0""yes\0""force\0"
+               "auto\0""tty\0""if-tty\0";
        /* need to initialize since --color has _an optional_ argument */
-       IF_FEATURE_LS_COLOR(const char *color_opt = "always";)
+       const char *color_opt = color_str; /* "always" */
+#endif
 
        INIT_G();
 
+       check_unicode_in_env();
+
        all_fmt = LIST_SHORT |
                (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
 
 #if ENABLE_FEATURE_AUTOWIDTH
-       /* Obtain the terminal width */
+       /* obtain the terminal width */
        get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
-       /* Go one less... */
+       /* go one less... */
        terminal_width--;
 #endif
 
        /* process options */
-       IF_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
+       IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;)
 #if ENABLE_FEATURE_AUTOWIDTH
        opt_complementary = "T+:w+"; /* -T N, -w N */
        opt = getopt32(argv, ls_options, &tabstops, &terminal_width
@@ -966,13 +966,20 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
                if (!p || (p[0] && strcmp(p, "none") != 0))
                        show_color = 1;
        }
-       if (opt & (1 << i)) {  /* next flag after short options */
-               if (strcmp("always", color_opt) == 0)
-                       show_color = 1;
-               else if (strcmp("never", color_opt) == 0)
+       if (opt & OPT_color) {
+               if (color_opt[0] == 'n')
                        show_color = 0;
-               else if (strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
-                       show_color = 1;
+               else switch (index_in_substrings(color_str, color_opt)) {
+               case 3:
+               case 4:
+               case 5:
+                       if (isatty(STDOUT_FILENO)) {
+               case 0:
+               case 1:
+               case 2:
+                               show_color = 1;
+                       }
+               }
        }
 #endif