1 /* vi: set sw=4 ts=4: */
3 * tiny-ls.c version 0.1.0: A minimalist 'ls'
4 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * To achieve a small memory footprint, this version of 'ls' doesn't do any
11 * file sorting, and only has the most essential command line switches
12 * (i.e., the ones I couldn't live without :-) All features which involve
13 * linking in substantial chunks of libc can be disabled.
15 * Although I don't really want to add new features to this program to
16 * keep it small, I *am* interested to receive bug fixes and ways to make
20 * 1. ls -l of a directory doesn't give "total <blocks>" header
21 * 2. ls of a symlink to a directory doesn't list directory contents
22 * 3. hidden files can make column width too large
24 * NON-OPTIMAL BEHAVIOUR:
25 * 1. autowidth reads directories twice
26 * 2. if you do a short directory listing without filetype characters
27 * appended, there's no need to stat each one
29 * 1. requires lstat (BSD) - how do you do it without?
35 /* This is a NOEXEC applet. Be very careful! */
40 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
41 COLUMN_GAP = 2, /* includes the file type char */
43 /* what is the overall style of the listing */
44 STYLE_COLUMNS = 1 << 21, /* fill columns */
45 STYLE_LONG = 2 << 21, /* one record per line, extended info */
46 STYLE_SINGLE = 3 << 21, /* one record per line */
47 STYLE_MASK = STYLE_SINGLE,
49 /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
50 /* what file information will be listed */
53 LIST_MODEBITS = 1 << 2,
55 LIST_ID_NAME = 1 << 4,
56 LIST_ID_NUMERIC = 1 << 5,
57 LIST_CONTEXT = 1 << 6,
60 LIST_DATE_TIME = 1 << 9,
61 LIST_FULLTIME = 1 << 10,
62 LIST_FILENAME = 1 << 11,
63 LIST_SYMLINK = 1 << 12,
64 LIST_FILETYPE = 1 << 13,
66 LIST_MASK = (LIST_EXEC << 1) - 1,
68 /* what files will be displayed */
69 DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
70 DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
71 DISP_DOT = 1 << 17, /* show . and .. */
72 DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
73 DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
74 DISP_ROWS = 1 << 20, /* print across rows */
75 DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
77 /* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
78 SORT_FORWARD = 0, /* sort in reverse order */
79 SORT_REVERSE = 1 << 27, /* sort in reverse order */
81 SORT_NAME = 0, /* sort by file name */
82 SORT_SIZE = 1 << 28, /* sort by file size */
83 SORT_ATIME = 2 << 28, /* sort by last access time */
84 SORT_CTIME = 3 << 28, /* sort by last change time */
85 SORT_MTIME = 4 << 28, /* sort by last modification time */
86 SORT_VERSION = 5 << 28, /* sort by version */
87 SORT_EXT = 6 << 28, /* sort by file name extension */
88 SORT_DIR = 7 << 28, /* sort by file or directory */
89 SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
91 /* which of the three times will be used */
92 TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
93 TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
94 TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
96 FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
98 LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
100 LIST_SHORT = LIST_FILENAME,
101 LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
102 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
110 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
111 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
112 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
113 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
114 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
115 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
116 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
118 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
119 #if ENABLE_FEATURE_LS_COLOR
120 static int show_color;
121 /* long option entry used only for --color, which has no short option
123 static const struct option ls_color_opt[] = {
124 { "color", optional_argument, NULL, 1 },
128 enum { show_color = 0 };
132 * a directory entry and its stat info are stored here
134 struct dnode { /* the basic node */
135 char *name; /* the dir entry name */
136 char *fullname; /* the dir entry name */
138 struct stat dstat; /* the file stat info */
139 USE_SELINUX(security_context_t sid;)
140 struct dnode *next; /* point at the next node */
142 typedef struct dnode dnode_t;
144 static struct dnode **list_dir(const char *);
145 static struct dnode **dnalloc(int);
146 static int list_single(struct dnode *);
148 static unsigned all_fmt;
150 #if ENABLE_FEATURE_AUTOWIDTH
151 static unsigned tabstops = COLUMN_GAP;
152 static unsigned terminal_width = TERMINAL_WIDTH;
155 tabstops = COLUMN_GAP,
156 terminal_width = TERMINAL_WIDTH,
160 static int status = EXIT_SUCCESS;
162 static struct dnode *my_stat(char *fullname, char *name, int force_follow)
166 USE_SELINUX(security_context_t sid = NULL;)
168 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
170 if (is_selinux_enabled()) {
171 getfilecon(fullname, &sid);
174 if (stat(fullname, &dstat)) {
175 bb_perror_msg("%s", fullname);
176 status = EXIT_FAILURE;
181 if (is_selinux_enabled()) {
182 lgetfilecon(fullname, &sid);
185 if (lstat(fullname, &dstat)) {
186 bb_perror_msg("%s", fullname);
187 status = EXIT_FAILURE;
192 cur = xmalloc(sizeof(struct dnode));
193 cur->fullname = fullname;
196 USE_SELINUX(cur->sid = sid;)
200 #if ENABLE_FEATURE_LS_COLOR
201 static char fgcolor(mode_t mode)
203 /* Check wheter the file is existing (if so, color it red!) */
206 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
207 return COLOR(0xF000); /* File is executable ... */
211 static char bgcolor(mode_t mode)
213 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
214 return ATTR(0xF000); /* File is executable ... */
219 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
220 static char append_char(mode_t mode)
222 if (!(all_fmt & LIST_FILETYPE))
226 if (!(all_fmt & LIST_EXEC))
228 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
230 return APPCHAR(mode);
234 #define countdirs(A, B) count_dirs((A), (B), 1)
235 #define countsubdirs(A, B) count_dirs((A), (B), 0)
236 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
243 for (i = 0; i < nfiles; i++) {
245 if (!S_ISDIR(dn[i]->dstat.st_mode))
249 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
257 static int countfiles(struct dnode **dnp)
265 for (cur = dnp[0]; cur->next; cur = cur->next)
271 /* get memory to hold an array of pointers */
272 static struct dnode **dnalloc(int num)
277 return xzalloc(num * sizeof(struct dnode *));
280 #if ENABLE_FEATURE_LS_RECURSIVE
281 static void dfree(struct dnode **dnp, int nfiles)
288 for (i = 0; i < nfiles; i++) {
289 struct dnode *cur = dnp[i];
291 free(cur->fullname); /* free the filename */
292 free(cur); /* free the dnode */
294 free(dnp); /* free the array holding the dnode pointers */
297 #define dfree(...) ((void)0)
300 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
305 if (dn == NULL || nfiles < 1)
308 /* count how many dirs and regular files there are */
309 if (which == SPLIT_SUBDIR)
310 dncnt = countsubdirs(dn, nfiles);
312 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
313 if (which == SPLIT_FILE)
314 dncnt = nfiles - dncnt; /* looking for files */
317 /* allocate a file array and a dir array */
318 dnp = dnalloc(dncnt);
320 /* copy the entrys into the file or dir array */
321 for (d = i = 0; i < nfiles; i++) {
322 if (S_ISDIR(dn[i]->dstat.st_mode)) {
324 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
327 if ((which & SPLIT_DIR)
328 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
332 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
339 #if ENABLE_FEATURE_LS_SORTFILES
340 static int sortcmp(const void *a, const void *b)
342 struct dnode *d1 = *(struct dnode **)a;
343 struct dnode *d2 = *(struct dnode **)b;
344 unsigned sort_opts = all_fmt & SORT_MASK;
347 dif = 0; /* assume SORT_NAME */
348 // TODO: use pre-initialized function pointer
349 // instead of branch forest
350 if (sort_opts == SORT_SIZE) {
351 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
352 } else if (sort_opts == SORT_ATIME) {
353 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
354 } else if (sort_opts == SORT_CTIME) {
355 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
356 } else if (sort_opts == SORT_MTIME) {
357 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
358 } else if (sort_opts == SORT_DIR) {
359 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
360 /* } else if (sort_opts == SORT_VERSION) { */
361 /* } else if (sort_opts == SORT_EXT) { */
365 /* sort by name - may be a tie_breaker for time or size cmp */
366 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
367 else dif = strcmp(d1->name, d2->name);
370 if (all_fmt & SORT_REVERSE) {
376 static void dnsort(struct dnode **dn, int size)
378 qsort(dn, size, sizeof(*dn), sortcmp);
381 #define dnsort(dn, size) ((void)0)
385 static void showfiles(struct dnode **dn, int nfiles)
387 int i, ncols, nrows, row, nc;
390 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
392 if (dn == NULL || nfiles < 1)
395 if (all_fmt & STYLE_LONG) {
398 /* find the longest file name, use that as the column width */
399 for (i = 0; i < nfiles; i++) {
400 int len = strlen(dn[i]->name);
401 if (column_width < len)
404 column_width += tabstops +
405 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
406 ((all_fmt & LIST_INO) ? 8 : 0) +
407 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
408 ncols = (int) (terminal_width / column_width);
412 nrows = nfiles / ncols;
413 if (nrows * ncols < nfiles)
414 nrows++; /* round up fractionals */
420 for (row = 0; row < nrows; row++) {
421 for (nc = 0; nc < ncols; nc++) {
422 /* reach into the array based on the column and row */
423 i = (nc * nrows) + row; /* assume display by column */
424 if (all_fmt & DISP_ROWS)
425 i = (row * ncols) + nc; /* display across row */
429 printf("%*s", nexttab, "");
432 nexttab = column + column_width;
433 column += list_single(dn[i]);
442 static void showdirs(struct dnode **dn, int ndirs, int first)
445 struct dnode **subdnp;
449 if (dn == NULL || ndirs < 1)
452 for (i = 0; i < ndirs; i++) {
453 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
457 printf("%s:\n", dn[i]->fullname);
459 subdnp = list_dir(dn[i]->fullname);
460 nfiles = countfiles(subdnp);
462 /* list all files at this level */
463 dnsort(subdnp, nfiles);
464 showfiles(subdnp, nfiles);
465 if (ENABLE_FEATURE_LS_RECURSIVE) {
466 if (all_fmt & DISP_RECURSIVE) {
467 /* recursive- list the sub-dirs */
468 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
469 dndirs = countsubdirs(subdnp, nfiles);
472 showdirs(dnd, dndirs, 0);
473 /* free the array of dnode pointers to the dirs */
477 /* free the dnodes and the fullname mem */
478 dfree(subdnp, nfiles);
485 static struct dnode **list_dir(const char *path)
487 struct dnode *dn, *cur, **dnp;
488 struct dirent *entry;
497 dir = warn_opendir(path);
499 status = EXIT_FAILURE;
500 return NULL; /* could not open the dir */
502 while ((entry = readdir(dir)) != NULL) {
505 /* are we going to list the file- it may be . or .. or a hidden file */
506 if (entry->d_name[0] == '.') {
507 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
508 && !(all_fmt & DISP_DOT)
512 if (!(all_fmt & DISP_HIDDEN))
515 fullname = concat_path_file(path, entry->d_name);
516 cur = my_stat(fullname, strrchr(fullname, '/') + 1, 0);
528 /* now that we know how many files there are
529 * allocate memory for an array to hold dnode pointers
533 dnp = dnalloc(nfiles);
534 for (i = 0, cur = dn; i < nfiles; i++) {
535 dnp[i] = cur; /* save pointer to node in array */
543 #if ENABLE_FEATURE_LS_TIMESTAMPS
544 /* Do time() just once. Saves one syscall per file for "ls -l" */
545 /* Initialized in main() */
546 static time_t current_time_t;
549 static int list_single(struct dnode *dn)
553 #if ENABLE_FEATURE_LS_TIMESTAMPS
557 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
562 if (dn->fullname == NULL)
565 #if ENABLE_FEATURE_LS_TIMESTAMPS
566 ttime = dn->dstat.st_mtime; /* the default time */
567 if (all_fmt & TIME_ACCESS)
568 ttime = dn->dstat.st_atime;
569 if (all_fmt & TIME_CHANGE)
570 ttime = dn->dstat.st_ctime;
571 filetime = ctime(&ttime);
573 #if ENABLE_FEATURE_LS_FILETYPES
574 append = append_char(dn->dstat.st_mode);
577 for (i = 0; i <= 31; i++) {
578 switch (all_fmt & (1 << i)) {
580 column += printf("%7ld ", (long) dn->dstat.st_ino);
583 column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
586 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
589 column += printf("%4ld ", (long) dn->dstat.st_nlink);
592 #if ENABLE_FEATURE_LS_USERNAME
593 printf("%-8.8s %-8.8s",
594 get_cached_username(dn->dstat.st_uid),
595 get_cached_groupname(dn->dstat.st_gid));
599 case LIST_ID_NUMERIC:
600 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
604 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
605 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
606 (int) minor(dn->dstat.st_rdev));
608 if (all_fmt & LS_DISP_HR) {
609 column += printf("%9s ",
610 make_human_readable_str(dn->dstat.st_size, 1, 0));
612 column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
616 #if ENABLE_FEATURE_LS_TIMESTAMPS
618 printf("%24.24s ", filetime);
622 if ((all_fmt & LIST_FULLTIME) == 0) {
623 /* current_time_t ~== time(NULL) */
624 age = current_time_t - ttime;
625 printf("%6.6s ", filetime + 4);
626 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
627 /* hh:mm if less than 6 months old */
628 printf("%5.5s ", filetime + 11);
630 printf(" %4.4s ", filetime + 20);
643 /* I assume sid initilized with NULL */
644 len = strlen(dn->sid) + 1;
645 safe_strncpy(context, dn->sid, len);
648 safe_strncpy(context, "unknown", 8);
650 printf("%-32s ", context);
651 column += MAX(33, len);
657 #if ENABLE_FEATURE_LS_COLOR
658 if (show_color && !lstat(dn->fullname, &info)) {
659 printf("\033[%d;%dm", bgcolor(info.st_mode),
660 fgcolor(info.st_mode));
663 column += printf("%s", dn->name);
669 if (S_ISLNK(dn->dstat.st_mode)) {
670 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
673 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
674 if (!stat(dn->fullname, &info)) {
675 append = append_char(info.st_mode);
678 #if ENABLE_FEATURE_LS_COLOR
681 printf("\033[%d;%dm", bgcolor(info.st_mode),
682 fgcolor(info.st_mode));
685 column += printf("%s", lpath) + 4;
692 #if ENABLE_FEATURE_LS_FILETYPES
706 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
707 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
708 /* "[-]Ak" GNU options, busybox always supports */
709 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
710 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
711 /* "[-]SXvThw", GNU options, busybox optionally supports */
712 /* "[-]K", SELinux mandated options, busybox optionally supports */
713 /* "[-]e", I think we made this one up */
714 static const char ls_options[] = "Cadil1gnsxAk"
715 USE_FEATURE_LS_TIMESTAMPS("cetu")
716 USE_FEATURE_LS_SORTFILES("SXrv")
717 USE_FEATURE_LS_FILETYPES("Fp")
718 USE_FEATURE_LS_FOLLOWLINKS("L")
719 USE_FEATURE_LS_RECURSIVE("R")
720 USE_FEATURE_HUMAN_READABLE("h")
722 USE_FEATURE_AUTOWIDTH("T:w:")
726 LIST_MASK_TRIGGER = 0,
727 STYLE_MASK_TRIGGER = STYLE_MASK,
728 DISP_MASK_TRIGGER = DISP_ROWS,
729 SORT_MASK_TRIGGER = SORT_MASK,
732 static const unsigned opt_flags[] = {
733 LIST_SHORT | STYLE_COLUMNS, /* C */
734 DISP_HIDDEN | DISP_DOT, /* a */
737 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
738 LIST_SHORT | STYLE_SINGLE, /* 1 */
740 LIST_ID_NUMERIC, /* n */
744 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
745 #if ENABLE_FEATURE_LS_TIMESTAMPS
746 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
747 LIST_FULLTIME, /* e */
748 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
749 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
751 #if ENABLE_FEATURE_LS_SORTFILES
754 SORT_REVERSE, /* r */
755 SORT_VERSION, /* v */
757 #if ENABLE_FEATURE_LS_FILETYPES
758 LIST_FILETYPE | LIST_EXEC, /* F */
759 LIST_FILETYPE, /* p */
761 #if ENABLE_FEATURE_LS_FOLLOWLINKS
762 FOLLOW_LINKS, /* L */
764 #if ENABLE_FEATURE_LS_RECURSIVE
765 DISP_RECURSIVE, /* R */
767 #if ENABLE_FEATURE_HUMAN_READABLE
771 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
773 #if ENABLE_FEATURE_AUTOWIDTH
774 0, 0, /* T, w - ignored */
777 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
783 /* THIS IS A "SAFE" APPLET, main() MAY BE CALLED INTERNALLY FROM SHELL */
786 int ls_main(int argc, char **argv);
787 int ls_main(int argc, char **argv)
802 USE_FEATURE_AUTOWIDTH(char *tabstops_str = NULL;)
803 USE_FEATURE_AUTOWIDTH(char *terminal_width_str = NULL;)
804 USE_FEATURE_LS_COLOR(char *color_opt;)
806 #if ENABLE_FEATURE_LS_TIMESTAMPS
807 time(¤t_time_t);
810 all_fmt = LIST_SHORT |
811 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
813 #if ENABLE_FEATURE_AUTOWIDTH
814 /* Obtain the terminal width */
815 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
820 /* process options */
821 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
822 #if ENABLE_FEATURE_AUTOWIDTH
823 opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
824 USE_FEATURE_LS_COLOR(, &color_opt));
826 tabstops = xatou(tabstops_str);
827 if (terminal_width_str)
828 terminal_width = xatou(terminal_width_str);
830 opt = getopt32(argc, argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
832 for (i = 0; opt_flags[i] != (1U<<31); i++) {
833 if (opt & (1 << i)) {
834 unsigned flags = opt_flags[i];
836 if (flags & LIST_MASK_TRIGGER)
837 all_fmt &= ~LIST_MASK;
838 if (flags & STYLE_MASK_TRIGGER)
839 all_fmt &= ~STYLE_MASK;
840 if (flags & SORT_MASK_TRIGGER)
841 all_fmt &= ~SORT_MASK;
842 if (flags & DISP_MASK_TRIGGER)
843 all_fmt &= ~DISP_MASK;
844 if (flags & TIME_MASK)
845 all_fmt &= ~TIME_MASK;
846 if (flags & LIST_CONTEXT)
847 all_fmt |= STYLE_SINGLE;
848 /* huh?? opt cannot be 'l' */
849 //if (LS_DISP_HR && opt == 'l')
850 // all_fmt &= ~LS_DISP_HR;
855 #if ENABLE_FEATURE_LS_COLOR
856 /* find color bit value - last position for short getopt */
857 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
858 char *p = getenv("LS_COLORS");
859 /* LS_COLORS is unset, or (not empty && not "none") ? */
860 if (!p || (p[0] && strcmp(p, "none")))
863 if (opt & (1 << i)) { /* next flag after short options */
864 if (!color_opt || !strcmp("always", color_opt))
866 else if (color_opt && !strcmp("never", color_opt))
868 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
873 /* sort out which command line options take precedence */
874 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
875 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
876 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
877 if (all_fmt & TIME_CHANGE)
878 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
879 if (all_fmt & TIME_ACCESS)
880 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
882 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
883 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
884 if (ENABLE_FEATURE_LS_USERNAME)
885 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
886 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
888 /* choose a display format */
889 if (!(all_fmt & STYLE_MASK))
890 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
893 * when there are no cmd line args we have to supply a default "." arg.
894 * we will create a second argv array, "av" that will hold either
895 * our created "." arg, or the real cmd line args. The av array
896 * just holds the pointers- we don't move the date the pointers
899 ac = argc - optind; /* how many cmd line args are left */
901 static const char *const dotdir[] = { "." };
903 av = (char **) dotdir;
909 /* now, everything is in the av array */
911 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
913 /* stuff the command line file names into a dnode array */
915 for (oi = 0; oi < ac; oi++) {
916 /* ls w/o -l follows links on command line */
917 cur = my_stat(av[oi], av[oi], !(all_fmt & STYLE_LONG));
926 /* now that we know how many files there are
927 * allocate memory for an array to hold dnode pointers
929 dnp = dnalloc(nfiles);
930 for (i = 0, cur = dn; i < nfiles; i++) {
931 dnp[i] = cur; /* save pointer to node in array */
935 if (all_fmt & DISP_NOLIST) {
938 showfiles(dnp, nfiles);
940 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
941 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
942 dndirs = countdirs(dnp, nfiles);
943 dnfiles = nfiles - dndirs;
945 dnsort(dnf, dnfiles);
946 showfiles(dnf, dnfiles);
947 if (ENABLE_FEATURE_CLEAN_UP)
952 showdirs(dnd, dndirs, dnfiles == 0);
953 if (ENABLE_FEATURE_CLEAN_UP)
957 if (ENABLE_FEATURE_CLEAN_UP)