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 smallint show_color;
121 /* long option entry used only for --color, which has no short option
123 static const char ls_color_opt[] =
124 "color\0" Optional_argument "\xff" /* no short equivalent */
127 enum { show_color = 0 };
131 * a directory entry and its stat info are stored here
133 struct dnode { /* the basic node */
134 const char *name; /* the dir entry name */
135 const char *fullname; /* the dir entry name */
137 struct stat dstat; /* the file stat info */
138 USE_SELINUX(security_context_t sid;)
139 struct dnode *next; /* point at the next node */
141 typedef struct dnode dnode_t;
143 static struct dnode **list_dir(const char *);
144 static struct dnode **dnalloc(int);
145 static int list_single(struct dnode *);
147 static unsigned all_fmt;
149 #if ENABLE_FEATURE_AUTOWIDTH
150 static unsigned tabstops = COLUMN_GAP;
151 static unsigned terminal_width = TERMINAL_WIDTH;
154 tabstops = COLUMN_GAP,
155 terminal_width = TERMINAL_WIDTH,
159 static int status = EXIT_SUCCESS;
161 static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
165 USE_SELINUX(security_context_t sid = NULL;)
167 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
169 if (is_selinux_enabled()) {
170 getfilecon(fullname, &sid);
173 if (stat(fullname, &dstat)) {
174 bb_perror_msg("%s", fullname);
175 status = EXIT_FAILURE;
180 if (is_selinux_enabled()) {
181 lgetfilecon(fullname, &sid);
184 if (lstat(fullname, &dstat)) {
185 bb_perror_msg("%s", fullname);
186 status = EXIT_FAILURE;
191 cur = xmalloc(sizeof(struct dnode));
192 cur->fullname = fullname;
195 USE_SELINUX(cur->sid = sid;)
199 #if ENABLE_FEATURE_LS_COLOR
200 static char fgcolor(mode_t mode)
202 /* Check wheter the file is existing (if so, color it red!) */
205 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
206 return COLOR(0xF000); /* File is executable ... */
210 static char bgcolor(mode_t mode)
212 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
213 return ATTR(0xF000); /* File is executable ... */
218 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
219 static char append_char(mode_t mode)
221 if (!(all_fmt & LIST_FILETYPE))
225 if (!(all_fmt & LIST_EXEC))
227 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
229 return APPCHAR(mode);
233 #define countdirs(A, B) count_dirs((A), (B), 1)
234 #define countsubdirs(A, B) count_dirs((A), (B), 0)
235 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
242 for (i = 0; i < nfiles; i++) {
244 if (!S_ISDIR(dn[i]->dstat.st_mode))
248 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
256 static int countfiles(struct dnode **dnp)
264 for (cur = dnp[0]; cur->next; cur = cur->next)
270 /* get memory to hold an array of pointers */
271 static struct dnode **dnalloc(int num)
276 return xzalloc(num * sizeof(struct dnode *));
279 #if ENABLE_FEATURE_LS_RECURSIVE
280 static void dfree(struct dnode **dnp, int nfiles)
287 for (i = 0; i < nfiles; i++) {
288 struct dnode *cur = dnp[i];
290 free((char*)cur->fullname); /* free the filename */
291 free(cur); /* free the dnode */
293 free(dnp); /* free the array holding the dnode pointers */
296 #define dfree(...) ((void)0)
299 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
304 if (dn == NULL || nfiles < 1)
307 /* count how many dirs and regular files there are */
308 if (which == SPLIT_SUBDIR)
309 dncnt = countsubdirs(dn, nfiles);
311 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
312 if (which == SPLIT_FILE)
313 dncnt = nfiles - dncnt; /* looking for files */
316 /* allocate a file array and a dir array */
317 dnp = dnalloc(dncnt);
319 /* copy the entrys into the file or dir array */
320 for (d = i = 0; i < nfiles; i++) {
321 if (S_ISDIR(dn[i]->dstat.st_mode)) {
323 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
326 if ((which & SPLIT_DIR)
327 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
331 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
338 #if ENABLE_FEATURE_LS_SORTFILES
339 static int sortcmp(const void *a, const void *b)
341 struct dnode *d1 = *(struct dnode **)a;
342 struct dnode *d2 = *(struct dnode **)b;
343 unsigned sort_opts = all_fmt & SORT_MASK;
346 dif = 0; /* assume SORT_NAME */
347 // TODO: use pre-initialized function pointer
348 // instead of branch forest
349 if (sort_opts == SORT_SIZE) {
350 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
351 } else if (sort_opts == SORT_ATIME) {
352 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
353 } else if (sort_opts == SORT_CTIME) {
354 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
355 } else if (sort_opts == SORT_MTIME) {
356 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
357 } else if (sort_opts == SORT_DIR) {
358 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
359 /* } else if (sort_opts == SORT_VERSION) { */
360 /* } else if (sort_opts == SORT_EXT) { */
364 /* sort by name - may be a tie_breaker for time or size cmp */
365 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
366 else dif = strcmp(d1->name, d2->name);
369 if (all_fmt & SORT_REVERSE) {
375 static void dnsort(struct dnode **dn, int size)
377 qsort(dn, size, sizeof(*dn), sortcmp);
380 #define dnsort(dn, size) ((void)0)
384 static void showfiles(struct dnode **dn, int nfiles)
386 int i, ncols, nrows, row, nc;
389 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
391 if (dn == NULL || nfiles < 1)
394 if (all_fmt & STYLE_LONG) {
397 /* find the longest file name, use that as the column width */
398 for (i = 0; i < nfiles; i++) {
399 int len = strlen(dn[i]->name);
400 if (column_width < len)
403 column_width += tabstops +
404 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
405 ((all_fmt & LIST_INO) ? 8 : 0) +
406 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
407 ncols = (int) (terminal_width / column_width);
411 nrows = nfiles / ncols;
412 if (nrows * ncols < nfiles)
413 nrows++; /* round up fractionals */
419 for (row = 0; row < nrows; row++) {
420 for (nc = 0; nc < ncols; nc++) {
421 /* reach into the array based on the column and row */
422 i = (nc * nrows) + row; /* assume display by column */
423 if (all_fmt & DISP_ROWS)
424 i = (row * ncols) + nc; /* display across row */
428 printf("%*s", nexttab, "");
431 nexttab = column + column_width;
432 column += list_single(dn[i]);
441 static void showdirs(struct dnode **dn, int ndirs, int first)
444 struct dnode **subdnp;
448 if (dn == NULL || ndirs < 1)
451 for (i = 0; i < ndirs; i++) {
452 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
456 printf("%s:\n", dn[i]->fullname);
458 subdnp = list_dir(dn[i]->fullname);
459 nfiles = countfiles(subdnp);
461 /* list all files at this level */
462 dnsort(subdnp, nfiles);
463 showfiles(subdnp, nfiles);
464 if (ENABLE_FEATURE_LS_RECURSIVE) {
465 if (all_fmt & DISP_RECURSIVE) {
466 /* recursive- list the sub-dirs */
467 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
468 dndirs = countsubdirs(subdnp, nfiles);
471 showdirs(dnd, dndirs, 0);
472 /* free the array of dnode pointers to the dirs */
476 /* free the dnodes and the fullname mem */
477 dfree(subdnp, nfiles);
484 static struct dnode **list_dir(const char *path)
486 struct dnode *dn, *cur, **dnp;
487 struct dirent *entry;
496 dir = warn_opendir(path);
498 status = EXIT_FAILURE;
499 return NULL; /* could not open the dir */
501 while ((entry = readdir(dir)) != NULL) {
504 /* are we going to list the file- it may be . or .. or a hidden file */
505 if (entry->d_name[0] == '.') {
506 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
507 && !(all_fmt & DISP_DOT)
511 if (!(all_fmt & DISP_HIDDEN))
514 fullname = concat_path_file(path, entry->d_name);
515 cur = my_stat(fullname, bb_basename(fullname), 0);
527 /* now that we know how many files there are
528 * allocate memory for an array to hold dnode pointers
532 dnp = dnalloc(nfiles);
533 for (i = 0, cur = dn; i < nfiles; i++) {
534 dnp[i] = cur; /* save pointer to node in array */
542 #if ENABLE_FEATURE_LS_TIMESTAMPS
543 /* Do time() just once. Saves one syscall per file for "ls -l" */
544 /* Initialized in main() */
545 static time_t current_time_t;
548 static int list_single(struct dnode *dn)
552 #if ENABLE_FEATURE_LS_TIMESTAMPS
556 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
561 if (dn->fullname == NULL)
564 #if ENABLE_FEATURE_LS_TIMESTAMPS
565 ttime = dn->dstat.st_mtime; /* the default time */
566 if (all_fmt & TIME_ACCESS)
567 ttime = dn->dstat.st_atime;
568 if (all_fmt & TIME_CHANGE)
569 ttime = dn->dstat.st_ctime;
570 filetime = ctime(&ttime);
572 #if ENABLE_FEATURE_LS_FILETYPES
573 append = append_char(dn->dstat.st_mode);
576 for (i = 0; i <= 31; i++) {
577 switch (all_fmt & (1 << i)) {
579 column += printf("%7ld ", (long) dn->dstat.st_ino);
582 column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
585 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
588 column += printf("%4ld ", (long) dn->dstat.st_nlink);
591 #if ENABLE_FEATURE_LS_USERNAME
592 printf("%-8.8s %-8.8s",
593 get_cached_username(dn->dstat.st_uid),
594 get_cached_groupname(dn->dstat.st_gid));
598 case LIST_ID_NUMERIC:
599 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
603 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
604 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
605 (int) minor(dn->dstat.st_rdev));
607 if (all_fmt & LS_DISP_HR) {
608 column += printf("%9s ",
609 make_human_readable_str(dn->dstat.st_size, 1, 0));
611 column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
615 #if ENABLE_FEATURE_LS_TIMESTAMPS
617 printf("%24.24s ", filetime);
621 if ((all_fmt & LIST_FULLTIME) == 0) {
622 /* current_time_t ~== time(NULL) */
623 age = current_time_t - ttime;
624 printf("%6.6s ", filetime + 4);
625 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
626 /* hh:mm if less than 6 months old */
627 printf("%5.5s ", filetime + 11);
629 printf(" %4.4s ", filetime + 20);
642 /* I assume sid initilized with NULL */
643 len = strlen(dn->sid) + 1;
644 safe_strncpy(context, dn->sid, len);
647 safe_strncpy(context, "unknown", 8);
649 printf("%-32s ", context);
650 column += MAX(33, len);
656 #if ENABLE_FEATURE_LS_COLOR
657 if (show_color && !lstat(dn->fullname, &info)) {
658 printf("\033[%d;%dm", bgcolor(info.st_mode),
659 fgcolor(info.st_mode));
662 column += printf("%s", dn->name);
668 if (S_ISLNK(dn->dstat.st_mode)) {
669 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
672 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
673 if (!stat(dn->fullname, &info)) {
674 append = append_char(info.st_mode);
677 #if ENABLE_FEATURE_LS_COLOR
680 printf("\033[%d;%dm", bgcolor(info.st_mode),
681 fgcolor(info.st_mode));
684 column += printf("%s", lpath) + 4;
691 #if ENABLE_FEATURE_LS_FILETYPES
705 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
706 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
707 /* "[-]Ak" GNU options, busybox always supports */
708 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
709 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
710 /* "[-]SXvThw", GNU options, busybox optionally supports */
711 /* "[-]K", SELinux mandated options, busybox optionally supports */
712 /* "[-]e", I think we made this one up */
713 static const char ls_options[] = "Cadil1gnsxAk"
714 USE_FEATURE_LS_TIMESTAMPS("cetu")
715 USE_FEATURE_LS_SORTFILES("SXrv")
716 USE_FEATURE_LS_FILETYPES("Fp")
717 USE_FEATURE_LS_FOLLOWLINKS("L")
718 USE_FEATURE_LS_RECURSIVE("R")
719 USE_FEATURE_HUMAN_READABLE("h")
721 USE_FEATURE_AUTOWIDTH("T:w:")
725 LIST_MASK_TRIGGER = 0,
726 STYLE_MASK_TRIGGER = STYLE_MASK,
727 DISP_MASK_TRIGGER = DISP_ROWS,
728 SORT_MASK_TRIGGER = SORT_MASK,
731 static const unsigned opt_flags[] = {
732 LIST_SHORT | STYLE_COLUMNS, /* C */
733 DISP_HIDDEN | DISP_DOT, /* a */
736 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
737 LIST_SHORT | STYLE_SINGLE, /* 1 */
739 LIST_ID_NUMERIC, /* n */
743 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
744 #if ENABLE_FEATURE_LS_TIMESTAMPS
745 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
746 LIST_FULLTIME, /* e */
747 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
748 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
750 #if ENABLE_FEATURE_LS_SORTFILES
753 SORT_REVERSE, /* r */
754 SORT_VERSION, /* v */
756 #if ENABLE_FEATURE_LS_FILETYPES
757 LIST_FILETYPE | LIST_EXEC, /* F */
758 LIST_FILETYPE, /* p */
760 #if ENABLE_FEATURE_LS_FOLLOWLINKS
761 FOLLOW_LINKS, /* L */
763 #if ENABLE_FEATURE_LS_RECURSIVE
764 DISP_RECURSIVE, /* R */
766 #if ENABLE_FEATURE_HUMAN_READABLE
770 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
772 #if ENABLE_FEATURE_AUTOWIDTH
773 0, 0, /* T, w - ignored */
776 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
782 /* THIS IS A "SAFE" APPLET, main() MAY BE CALLED INTERNALLY FROM SHELL */
785 int ls_main(int argc, char **argv);
786 int ls_main(int argc, char **argv)
801 USE_FEATURE_AUTOWIDTH(char *tabstops_str = NULL;)
802 USE_FEATURE_AUTOWIDTH(char *terminal_width_str = NULL;)
803 USE_FEATURE_LS_COLOR(char *color_opt;)
805 #if ENABLE_FEATURE_LS_TIMESTAMPS
806 time(¤t_time_t);
809 all_fmt = LIST_SHORT |
810 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
812 #if ENABLE_FEATURE_AUTOWIDTH
813 /* Obtain the terminal width */
814 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
819 /* process options */
820 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
821 #if ENABLE_FEATURE_AUTOWIDTH
822 opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
823 USE_FEATURE_LS_COLOR(, &color_opt));
825 tabstops = xatou(tabstops_str);
826 if (terminal_width_str)
827 terminal_width = xatou(terminal_width_str);
829 opt = getopt32(argc, argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
831 for (i = 0; opt_flags[i] != (1U<<31); i++) {
832 if (opt & (1 << i)) {
833 unsigned flags = opt_flags[i];
835 if (flags & LIST_MASK_TRIGGER)
836 all_fmt &= ~LIST_MASK;
837 if (flags & STYLE_MASK_TRIGGER)
838 all_fmt &= ~STYLE_MASK;
839 if (flags & SORT_MASK_TRIGGER)
840 all_fmt &= ~SORT_MASK;
841 if (flags & DISP_MASK_TRIGGER)
842 all_fmt &= ~DISP_MASK;
843 if (flags & TIME_MASK)
844 all_fmt &= ~TIME_MASK;
845 if (flags & LIST_CONTEXT)
846 all_fmt |= STYLE_SINGLE;
847 /* huh?? opt cannot be 'l' */
848 //if (LS_DISP_HR && opt == 'l')
849 // all_fmt &= ~LS_DISP_HR;
854 #if ENABLE_FEATURE_LS_COLOR
855 /* find color bit value - last position for short getopt */
856 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
857 char *p = getenv("LS_COLORS");
858 /* LS_COLORS is unset, or (not empty && not "none") ? */
859 if (!p || (p[0] && strcmp(p, "none")))
862 if (opt & (1 << i)) { /* next flag after short options */
863 if (!color_opt || !strcmp("always", color_opt))
865 else if (color_opt && !strcmp("never", color_opt))
867 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
872 /* sort out which command line options take precedence */
873 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
874 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
875 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
876 if (all_fmt & TIME_CHANGE)
877 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
878 if (all_fmt & TIME_ACCESS)
879 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
881 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
882 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
883 if (ENABLE_FEATURE_LS_USERNAME)
884 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
885 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
887 /* choose a display format */
888 if (!(all_fmt & STYLE_MASK))
889 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
892 * when there are no cmd line args we have to supply a default "." arg.
893 * we will create a second argv array, "av" that will hold either
894 * our created "." arg, or the real cmd line args. The av array
895 * just holds the pointers- we don't move the date the pointers
898 ac = argc - optind; /* how many cmd line args are left */
900 static const char *const dotdir[] = { "." };
902 av = (char **) dotdir;
908 /* now, everything is in the av array */
910 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
912 /* stuff the command line file names into a dnode array */
914 for (oi = 0; oi < ac; oi++) {
915 /* ls w/o -l follows links on command line */
916 cur = my_stat(av[oi], av[oi], !(all_fmt & STYLE_LONG));
925 /* now that we know how many files there are
926 * allocate memory for an array to hold dnode pointers
928 dnp = dnalloc(nfiles);
929 for (i = 0, cur = dn; i < nfiles; i++) {
930 dnp[i] = cur; /* save pointer to node in array */
934 if (all_fmt & DISP_NOLIST) {
937 showfiles(dnp, nfiles);
939 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
940 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
941 dndirs = countdirs(dnp, nfiles);
942 dnfiles = nfiles - dndirs;
944 dnsort(dnf, dnfiles);
945 showfiles(dnf, dnfiles);
946 if (ENABLE_FEATURE_CLEAN_UP)
951 showdirs(dnd, dndirs, dnfiles == 0);
952 if (ENABLE_FEATURE_CLEAN_UP)
956 if (ENABLE_FEATURE_CLEAN_UP)