* [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! */
*/
# undef CONFIG_FEATURE_LS_TIMESTAMPS
# undef ENABLE_FEATURE_LS_TIMESTAMPS
-# undef USE_FEATURE_LS_TIMESTAMPS
-# undef SKIP_FEATURE_LS_TIMESTAMPS
+# undef IF_FEATURE_LS_TIMESTAMPS
+# undef IF_NOT_FEATURE_LS_TIMESTAMPS
# define CONFIG_FEATURE_LS_TIMESTAMPS 1
# define ENABLE_FEATURE_LS_TIMESTAMPS 1
-# define USE_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__
-# define SKIP_FEATURE_LS_TIMESTAMPS(...)
+# define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__
+# define IF_NOT_FEATURE_LS_TIMESTAMPS(...)
#endif
/* "[-]e", I think we made this one up */
static const char ls_options[] ALIGN1 =
"Cadil1gnsxQAk" /* 13 opts, total 13 */
- USE_FEATURE_LS_TIMESTAMPS("cetu") /* 4, 17 */
- USE_FEATURE_LS_SORTFILES("SXrv") /* 4, 21 */
- USE_FEATURE_LS_FILETYPES("Fp") /* 2, 23 */
- USE_FEATURE_LS_FOLLOWLINKS("L") /* 1, 24 */
- USE_FEATURE_LS_RECURSIVE("R") /* 1, 25 */
- USE_FEATURE_HUMAN_READABLE("h") /* 1, 26 */
- USE_SELINUX("K") /* 1, 27 */
- USE_SELINUX("Z") /* 1, 28 */
- USE_FEATURE_AUTOWIDTH("T:w:") /* 2, 30 */
+ IF_FEATURE_LS_TIMESTAMPS("cetu") /* 4, 17 */
+ IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 21 */
+ IF_FEATURE_LS_FILETYPES("Fp") /* 2, 23 */
+ IF_FEATURE_LS_FOLLOWLINKS("L") /* 1, 24 */
+ IF_FEATURE_LS_RECURSIVE("R") /* 1, 25 */
+ IF_FEATURE_HUMAN_READABLE("h") /* 1, 26 */
+ IF_SELINUX("KZ") /* 2, 28 */
+ IF_FEATURE_AUTOWIDTH("T:w:") /* 2, 30 */
;
enum {
//OPT_C = (1 << 0),
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 {
const char *fullname; /* the dir entry name */
int allocated;
struct stat dstat; /* the file stat info */
- USE_SELINUX(security_context_t sid;)
+ IF_SELINUX(security_context_t sid;)
struct dnode *next; /* point at the next node */
};
/* memset: we have to zero it out because of NOEXEC */
#define INIT_G() do { \
memset(&G, 0, sizeof(G)); \
- USE_FEATURE_AUTOWIDTH(tabstops = COLUMN_GAP;) \
- USE_FEATURE_AUTOWIDTH(terminal_width = TERMINAL_WIDTH;) \
- USE_FEATURE_LS_TIMESTAMPS(time(¤t_time_t);) \
+ IF_FEATURE_AUTOWIDTH(tabstops = COLUMN_GAP;) \
+ IF_FEATURE_AUTOWIDTH(terminal_width = TERMINAL_WIDTH;) \
+ IF_FEATURE_LS_TIMESTAMPS(time(¤t_time_t);) \
} 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;
struct dnode *cur;
- USE_SELINUX(security_context_t sid = NULL;)
+ IF_SELINUX(security_context_t sid = NULL;)
if ((all_fmt & FOLLOW_LINKS) || force_follow) {
#if ENABLE_SELINUX
cur->fullname = fullname;
cur->name = name;
cur->dstat = dstat;
- USE_SELINUX(cur->sid = sid;)
+ IF_SELINUX(cur->sid = sid;)
return cur;
}
} 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;
}
column_width += tabstops +
- USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
+ IF_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
((all_fmt & LIST_INO) ? 8 : 0) +
((all_fmt & LIST_BLOCKS) ? 5 : 0);
ncols = (int) (terminal_width / column_width);
{
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
/* No -Q: */
#if ENABLE_FEATURE_ASSUME_UNICODE
fputs(name, stdout);
- return mbstrlen(name);
+ return bb_mbstrlen(name);
#else
return printf("%s", name);
#endif
static int list_single(const struct dnode *dn)
{
int column = 0;
- char *lpath;
+ char *lpath = lpath; /* for compiler */
#if ENABLE_FEATURE_LS_TIMESTAMPS
char *filetime;
time_t ttime, age;
}
-/* 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;
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 */
- USE_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 */
- USE_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
- USE_FEATURE_LS_COLOR(, &color_opt));
+ IF_FEATURE_LS_COLOR(, &color_opt));
#else
- opt = getopt32(argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
+ opt = getopt32(argv, ls_options IF_FEATURE_LS_COLOR(, &color_opt));
#endif
for (i = 0; opt_flags[i] != (1U<<31); i++) {
if (opt & (1 << i)) {
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