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?
34 #if ENABLE_FEATURE_ASSUME_UNICODE
38 /* This is a NOEXEC applet. Be very careful! */
43 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
44 COLUMN_GAP = 2, /* includes the file type char */
46 /* what is the overall style of the listing */
47 STYLE_COLUMNS = 1 << 21, /* fill columns */
48 STYLE_LONG = 2 << 21, /* one record per line, extended info */
49 STYLE_SINGLE = 3 << 21, /* one record per line */
50 STYLE_MASK = STYLE_SINGLE,
52 /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
53 /* what file information will be listed */
56 LIST_MODEBITS = 1 << 2,
58 LIST_ID_NAME = 1 << 4,
59 LIST_ID_NUMERIC = 1 << 5,
60 LIST_CONTEXT = 1 << 6,
63 LIST_DATE_TIME = 1 << 9,
64 LIST_FULLTIME = 1 << 10,
65 LIST_FILENAME = 1 << 11,
66 LIST_SYMLINK = 1 << 12,
67 LIST_FILETYPE = 1 << 13,
69 LIST_MASK = (LIST_EXEC << 1) - 1,
71 /* what files will be displayed */
72 DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
73 DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
74 DISP_DOT = 1 << 17, /* show . and .. */
75 DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
76 DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
77 DISP_ROWS = 1 << 20, /* print across rows */
78 DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
80 /* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
81 SORT_FORWARD = 0, /* sort in reverse order */
82 SORT_REVERSE = 1 << 27, /* sort in reverse order */
84 SORT_NAME = 0, /* sort by file name */
85 SORT_SIZE = 1 << 28, /* sort by file size */
86 SORT_ATIME = 2 << 28, /* sort by last access time */
87 SORT_CTIME = 3 << 28, /* sort by last change time */
88 SORT_MTIME = 4 << 28, /* sort by last modification time */
89 SORT_VERSION = 5 << 28, /* sort by version */
90 SORT_EXT = 6 << 28, /* sort by file name extension */
91 SORT_DIR = 7 << 28, /* sort by file or directory */
92 SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
94 /* which of the three times will be used */
95 TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
96 TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
97 TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
99 FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
101 LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
103 LIST_SHORT = LIST_FILENAME,
104 LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
105 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
113 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
114 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
115 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
116 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
117 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
118 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
119 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
122 * a directory entry and its stat info are stored here
124 struct dnode { /* the basic node */
125 const char *name; /* the dir entry name */
126 const char *fullname; /* the dir entry name */
128 struct stat dstat; /* the file stat info */
129 USE_SELINUX(security_context_t sid;)
130 struct dnode *next; /* point at the next node */
133 static struct dnode **list_dir(const char *);
134 static struct dnode **dnalloc(int);
135 static int list_single(const struct dnode *);
139 #if ENABLE_FEATURE_LS_COLOR
144 #if ENABLE_FEATURE_AUTOWIDTH
145 unsigned tabstops; // = COLUMN_GAP;
146 unsigned terminal_width; // = TERMINAL_WIDTH;
148 #if ENABLE_FEATURE_LS_TIMESTAMPS
149 /* Do time() just once. Saves one syscall per file for "ls -l" */
150 time_t current_time_t;
153 #define G (*(struct globals*)&bb_common_bufsiz1)
154 #if ENABLE_FEATURE_LS_COLOR
155 #define show_color (G.show_color )
157 enum { show_color = 0 };
159 #define exit_code (G.exit_code )
160 #define all_fmt (G.all_fmt )
161 #if ENABLE_FEATURE_AUTOWIDTH
162 #define tabstops (G.tabstops )
163 #define terminal_width (G.terminal_width)
166 tabstops = COLUMN_GAP,
167 terminal_width = TERMINAL_WIDTH,
170 #define current_time_t (G.current_time_t)
171 /* memset: we have to zero it out because of NOEXEC */
172 #define INIT_G() do { \
173 memset(&G, 0, sizeof(G)); \
174 USE_FEATURE_AUTOWIDTH(tabstops = COLUMN_GAP;) \
175 USE_FEATURE_AUTOWIDTH(terminal_width = TERMINAL_WIDTH;) \
176 USE_FEATURE_LS_TIMESTAMPS(time(¤t_time_t);) \
180 #if ENABLE_FEATURE_ASSUME_UNICODE
181 /* libbb candidate */
182 static size_t mbstrlen(const char *string)
184 size_t width = mbsrtowcs(NULL /*dest*/, &string,
185 MAXINT(size_t) /*len*/, NULL /*state*/);
186 if (width == (size_t)-1)
187 return strlen(string);
191 #define mbstrlen(string) strlen(string)
195 static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
199 USE_SELINUX(security_context_t sid = NULL;)
201 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
203 if (is_selinux_enabled()) {
204 getfilecon(fullname, &sid);
207 if (stat(fullname, &dstat)) {
208 bb_simple_perror_msg(fullname);
209 exit_code = EXIT_FAILURE;
214 if (is_selinux_enabled()) {
215 lgetfilecon(fullname, &sid);
218 if (lstat(fullname, &dstat)) {
219 bb_simple_perror_msg(fullname);
220 exit_code = EXIT_FAILURE;
225 cur = xmalloc(sizeof(struct dnode));
226 cur->fullname = fullname;
229 USE_SELINUX(cur->sid = sid;)
233 #if ENABLE_FEATURE_LS_COLOR
234 static char fgcolor(mode_t mode)
236 /* Check wheter the file is existing (if so, color it red!) */
239 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
240 return COLOR(0xF000); /* File is executable ... */
244 static char bgcolor(mode_t mode)
246 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
247 return ATTR(0xF000); /* File is executable ... */
252 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
253 static char append_char(mode_t mode)
255 if (!(all_fmt & LIST_FILETYPE))
259 if (!(all_fmt & LIST_EXEC))
261 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
263 return APPCHAR(mode);
267 #define countdirs(A, B) count_dirs((A), (B), 1)
268 #define countsubdirs(A, B) count_dirs((A), (B), 0)
269 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
276 for (i = 0; i < nfiles; i++) {
278 if (!S_ISDIR(dn[i]->dstat.st_mode))
282 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
290 static int countfiles(struct dnode **dnp)
298 for (cur = dnp[0]; cur->next; cur = cur->next)
304 /* get memory to hold an array of pointers */
305 static struct dnode **dnalloc(int num)
310 return xzalloc(num * sizeof(struct dnode *));
313 #if ENABLE_FEATURE_LS_RECURSIVE
314 static void dfree(struct dnode **dnp, int nfiles)
321 for (i = 0; i < nfiles; i++) {
322 struct dnode *cur = dnp[i];
324 free((char*)cur->fullname); /* free the filename */
325 free(cur); /* free the dnode */
327 free(dnp); /* free the array holding the dnode pointers */
330 #define dfree(...) ((void)0)
333 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
338 if (dn == NULL || nfiles < 1)
341 /* count how many dirs and regular files there are */
342 if (which == SPLIT_SUBDIR)
343 dncnt = countsubdirs(dn, nfiles);
345 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
346 if (which == SPLIT_FILE)
347 dncnt = nfiles - dncnt; /* looking for files */
350 /* allocate a file array and a dir array */
351 dnp = dnalloc(dncnt);
353 /* copy the entrys into the file or dir array */
354 for (d = i = 0; i < nfiles; i++) {
355 if (S_ISDIR(dn[i]->dstat.st_mode)) {
357 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
360 if ((which & SPLIT_DIR)
361 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
365 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
372 #if ENABLE_FEATURE_LS_SORTFILES
373 static int sortcmp(const void *a, const void *b)
375 struct dnode *d1 = *(struct dnode **)a;
376 struct dnode *d2 = *(struct dnode **)b;
377 unsigned sort_opts = all_fmt & SORT_MASK;
380 dif = 0; /* assume SORT_NAME */
381 // TODO: use pre-initialized function pointer
382 // instead of branch forest
383 if (sort_opts == SORT_SIZE) {
384 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
385 } else if (sort_opts == SORT_ATIME) {
386 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
387 } else if (sort_opts == SORT_CTIME) {
388 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
389 } else if (sort_opts == SORT_MTIME) {
390 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
391 } else if (sort_opts == SORT_DIR) {
392 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
393 /* } else if (sort_opts == SORT_VERSION) { */
394 /* } else if (sort_opts == SORT_EXT) { */
398 /* sort by name - may be a tie_breaker for time or size cmp */
399 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
400 else dif = strcmp(d1->name, d2->name);
403 if (all_fmt & SORT_REVERSE) {
409 static void dnsort(struct dnode **dn, int size)
411 qsort(dn, size, sizeof(*dn), sortcmp);
414 #define dnsort(dn, size) ((void)0)
418 static void showfiles(struct dnode **dn, int nfiles)
420 int i, ncols, nrows, row, nc;
423 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
425 if (dn == NULL || nfiles < 1)
428 if (all_fmt & STYLE_LONG) {
431 /* find the longest file name, use that as the column width */
432 for (i = 0; i < nfiles; i++) {
433 int len = mbstrlen(dn[i]->name);
434 if (column_width < len)
437 column_width += tabstops +
438 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
439 ((all_fmt & LIST_INO) ? 8 : 0) +
440 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
441 ncols = (int) (terminal_width / column_width);
445 nrows = nfiles / ncols;
446 if (nrows * ncols < nfiles)
447 nrows++; /* round up fractionals */
453 for (row = 0; row < nrows; row++) {
454 for (nc = 0; nc < ncols; nc++) {
455 /* reach into the array based on the column and row */
456 i = (nc * nrows) + row; /* assume display by column */
457 if (all_fmt & DISP_ROWS)
458 i = (row * ncols) + nc; /* display across row */
462 printf("%*s", nexttab, "");
465 nexttab = column + column_width;
466 column += list_single(dn[i]);
475 static void showdirs(struct dnode **dn, int ndirs, int first)
478 struct dnode **subdnp;
482 if (dn == NULL || ndirs < 1)
485 for (i = 0; i < ndirs; i++) {
486 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
490 printf("%s:\n", dn[i]->fullname);
492 subdnp = list_dir(dn[i]->fullname);
493 nfiles = countfiles(subdnp);
495 /* list all files at this level */
496 dnsort(subdnp, nfiles);
497 showfiles(subdnp, nfiles);
498 if (ENABLE_FEATURE_LS_RECURSIVE) {
499 if (all_fmt & DISP_RECURSIVE) {
500 /* recursive- list the sub-dirs */
501 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
502 dndirs = countsubdirs(subdnp, nfiles);
505 showdirs(dnd, dndirs, 0);
506 /* free the array of dnode pointers to the dirs */
510 /* free the dnodes and the fullname mem */
511 dfree(subdnp, nfiles);
518 static struct dnode **list_dir(const char *path)
520 struct dnode *dn, *cur, **dnp;
521 struct dirent *entry;
530 dir = warn_opendir(path);
532 exit_code = EXIT_FAILURE;
533 return NULL; /* could not open the dir */
535 while ((entry = readdir(dir)) != NULL) {
538 /* are we going to list the file- it may be . or .. or a hidden file */
539 if (entry->d_name[0] == '.') {
540 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
541 && !(all_fmt & DISP_DOT)
545 if (!(all_fmt & DISP_HIDDEN))
548 fullname = concat_path_file(path, entry->d_name);
549 cur = my_stat(fullname, bb_basename(fullname), 0);
561 /* now that we know how many files there are
562 * allocate memory for an array to hold dnode pointers
566 dnp = dnalloc(nfiles);
567 for (i = 0, cur = dn; i < nfiles; i++) {
568 dnp[i] = cur; /* save pointer to node in array */
576 static int list_single(const struct dnode *dn)
580 #if ENABLE_FEATURE_LS_TIMESTAMPS
584 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
589 if (dn->fullname == NULL)
592 #if ENABLE_FEATURE_LS_TIMESTAMPS
593 ttime = dn->dstat.st_mtime; /* the default time */
594 if (all_fmt & TIME_ACCESS)
595 ttime = dn->dstat.st_atime;
596 if (all_fmt & TIME_CHANGE)
597 ttime = dn->dstat.st_ctime;
598 filetime = ctime(&ttime);
600 #if ENABLE_FEATURE_LS_FILETYPES
601 append = append_char(dn->dstat.st_mode);
604 for (i = 0; i <= 31; i++) {
605 switch (all_fmt & (1 << i)) {
607 column += printf("%7ld ", (long) dn->dstat.st_ino);
610 column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
613 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
616 column += printf("%4ld ", (long) dn->dstat.st_nlink);
619 #if ENABLE_FEATURE_LS_USERNAME
620 printf("%-8.8s %-8.8s",
621 get_cached_username(dn->dstat.st_uid),
622 get_cached_groupname(dn->dstat.st_gid));
626 case LIST_ID_NUMERIC:
627 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
631 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
632 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
633 (int) minor(dn->dstat.st_rdev));
635 if (all_fmt & LS_DISP_HR) {
636 column += printf("%9s ",
637 make_human_readable_str(dn->dstat.st_size, 1, 0));
639 column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
643 #if ENABLE_FEATURE_LS_TIMESTAMPS
645 printf("%24.24s ", filetime);
649 if ((all_fmt & LIST_FULLTIME) == 0) {
650 /* current_time_t ~== time(NULL) */
651 age = current_time_t - ttime;
652 printf("%6.6s ", filetime + 4);
653 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
654 /* hh:mm if less than 6 months old */
655 printf("%5.5s ", filetime + 11);
657 printf(" %4.4s ", filetime + 20);
670 /* I assume sid initilized with NULL */
671 len = strlen(dn->sid) + 1;
672 safe_strncpy(context, dn->sid, len);
675 safe_strncpy(context, "unknown", 8);
677 printf("%-32s ", context);
678 column += MAX(33, len);
684 #if ENABLE_FEATURE_LS_COLOR
685 if (show_color && !lstat(dn->fullname, &info)) {
686 printf("\033[%d;%dm", bgcolor(info.st_mode),
687 fgcolor(info.st_mode));
690 #if ENABLE_FEATURE_ASSUME_UNICODE
691 printf("%s", dn->name);
692 column += mbstrlen(dn->name);
694 column += printf("%s", dn->name);
701 if (S_ISLNK(dn->dstat.st_mode)) {
702 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
705 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
706 if (!stat(dn->fullname, &info)) {
707 append = append_char(info.st_mode);
710 #if ENABLE_FEATURE_LS_COLOR
713 printf("\033[%d;%dm", bgcolor(info.st_mode),
714 fgcolor(info.st_mode));
717 column += printf("%s", lpath) + 4;
724 #if ENABLE_FEATURE_LS_FILETYPES
739 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
740 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
741 /* "[-]Ak" GNU options, busybox always supports */
742 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
743 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
744 /* "[-]SXvThw", GNU options, busybox optionally supports */
745 /* "[-]K", SELinux mandated options, busybox optionally supports */
746 /* "[-]e", I think we made this one up */
747 static const char ls_options[] ALIGN1 =
749 USE_FEATURE_LS_TIMESTAMPS("cetu")
750 USE_FEATURE_LS_SORTFILES("SXrv")
751 USE_FEATURE_LS_FILETYPES("Fp")
752 USE_FEATURE_LS_FOLLOWLINKS("L")
753 USE_FEATURE_LS_RECURSIVE("R")
754 USE_FEATURE_HUMAN_READABLE("h")
756 USE_FEATURE_AUTOWIDTH("T:w:")
760 LIST_MASK_TRIGGER = 0,
761 STYLE_MASK_TRIGGER = STYLE_MASK,
762 DISP_MASK_TRIGGER = DISP_ROWS,
763 SORT_MASK_TRIGGER = SORT_MASK,
766 static const unsigned opt_flags[] = {
767 LIST_SHORT | STYLE_COLUMNS, /* C */
768 DISP_HIDDEN | DISP_DOT, /* a */
771 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
772 LIST_SHORT | STYLE_SINGLE, /* 1 */
774 LIST_ID_NUMERIC, /* n */
778 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
779 #if ENABLE_FEATURE_LS_TIMESTAMPS
780 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
781 LIST_FULLTIME, /* e */
782 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
783 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
785 #if ENABLE_FEATURE_LS_SORTFILES
788 SORT_REVERSE, /* r */
789 SORT_VERSION, /* v */
791 #if ENABLE_FEATURE_LS_FILETYPES
792 LIST_FILETYPE | LIST_EXEC, /* F */
793 LIST_FILETYPE, /* p */
795 #if ENABLE_FEATURE_LS_FOLLOWLINKS
796 FOLLOW_LINKS, /* L */
798 #if ENABLE_FEATURE_LS_RECURSIVE
799 DISP_RECURSIVE, /* R */
801 #if ENABLE_FEATURE_HUMAN_READABLE
805 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
807 #if ENABLE_FEATURE_AUTOWIDTH
808 0, 0, /* T, w - ignored */
811 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
817 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
818 #if ENABLE_FEATURE_LS_COLOR
819 /* long option entry used only for --color, which has no short option
821 static const char ls_color_opt[] ALIGN1 =
822 "color\0" Optional_argument "\xff" /* no short equivalent */
827 int ls_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
828 int ls_main(int argc UNUSED_PARAM, char **argv)
840 /* need to initialize since --color has _an optional_ argument */
841 USE_FEATURE_LS_COLOR(const char *color_opt = "always";)
845 all_fmt = LIST_SHORT |
846 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
848 #if ENABLE_FEATURE_AUTOWIDTH
849 /* Obtain the terminal width */
850 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
855 /* process options */
856 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
857 #if ENABLE_FEATURE_AUTOWIDTH
858 opt_complementary = "T+:w+"; /* -T N, -w N */
859 opt = getopt32(argv, ls_options, &tabstops, &terminal_width
860 USE_FEATURE_LS_COLOR(, &color_opt));
862 opt = getopt32(argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
864 for (i = 0; opt_flags[i] != (1U<<31); i++) {
865 if (opt & (1 << i)) {
866 unsigned flags = opt_flags[i];
868 if (flags & LIST_MASK_TRIGGER)
869 all_fmt &= ~LIST_MASK;
870 if (flags & STYLE_MASK_TRIGGER)
871 all_fmt &= ~STYLE_MASK;
872 if (flags & SORT_MASK_TRIGGER)
873 all_fmt &= ~SORT_MASK;
874 if (flags & DISP_MASK_TRIGGER)
875 all_fmt &= ~DISP_MASK;
876 if (flags & TIME_MASK)
877 all_fmt &= ~TIME_MASK;
878 if (flags & LIST_CONTEXT)
879 all_fmt |= STYLE_SINGLE;
880 /* huh?? opt cannot be 'l' */
881 //if (LS_DISP_HR && opt == 'l')
882 // all_fmt &= ~LS_DISP_HR;
887 #if ENABLE_FEATURE_LS_COLOR
888 /* find color bit value - last position for short getopt */
889 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
890 char *p = getenv("LS_COLORS");
891 /* LS_COLORS is unset, or (not empty && not "none") ? */
892 if (!p || (p[0] && strcmp(p, "none") != 0))
895 if (opt & (1 << i)) { /* next flag after short options */
896 if (strcmp("always", color_opt) == 0)
898 else if (strcmp("never", color_opt) == 0)
900 else if (strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
905 /* sort out which command line options take precedence */
906 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
907 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
908 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
909 if (all_fmt & TIME_CHANGE)
910 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
911 if (all_fmt & TIME_ACCESS)
912 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
914 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
915 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
916 if (ENABLE_FEATURE_LS_USERNAME)
917 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
918 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
920 /* choose a display format */
921 if (!(all_fmt & STYLE_MASK))
922 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
926 *--argv = (char*)".";
929 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
931 /* stuff the command line file names into a dnode array */
935 /* ls w/o -l follows links on command line */
936 cur = my_stat(*argv, *argv, !(all_fmt & STYLE_LONG));
946 /* now that we know how many files there are
947 * allocate memory for an array to hold dnode pointers
949 dnp = dnalloc(nfiles);
950 for (i = 0, cur = dn; i < nfiles; i++) {
951 dnp[i] = cur; /* save pointer to node in array */
955 if (all_fmt & DISP_NOLIST) {
958 showfiles(dnp, nfiles);
960 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
961 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
962 dndirs = countdirs(dnp, nfiles);
963 dnfiles = nfiles - dndirs;
965 dnsort(dnf, dnfiles);
966 showfiles(dnf, dnfiles);
967 if (ENABLE_FEATURE_CLEAN_UP)
972 showdirs(dnd, dndirs, dnfiles == 0);
973 if (ENABLE_FEATURE_CLEAN_UP)
977 if (ENABLE_FEATURE_CLEAN_UP)