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?
33 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
34 COLUMN_GAP = 2, /* includes the file type char */
37 /************************************************************************/
42 /* what is the overall style of the listing */
43 #define STYLE_COLUMNS (1U<<21) /* fill columns */
44 #define STYLE_LONG (2U<<21) /* one record per line, extended info */
45 #define STYLE_SINGLE (3U<<21) /* one record per line */
47 #define STYLE_MASK STYLE_SINGLE
48 #define STYLE_ONE_RECORD_FLAG STYLE_LONG
50 /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
51 /* what file information will be listed */
52 #define LIST_INO (1U<<0)
53 #define LIST_BLOCKS (1U<<1)
54 #define LIST_MODEBITS (1U<<2)
55 #define LIST_NLINKS (1U<<3)
56 #define LIST_ID_NAME (1U<<4)
57 #define LIST_ID_NUMERIC (1U<<5)
58 #define LIST_CONTEXT (1U<<6)
59 #define LIST_SIZE (1U<<7)
60 #define LIST_DEV (1U<<8)
61 #define LIST_DATE_TIME (1U<<9)
62 #define LIST_FULLTIME (1U<<10)
63 #define LIST_FILENAME (1U<<11)
64 #define LIST_SYMLINK (1U<<12)
65 #define LIST_FILETYPE (1U<<13)
66 #define LIST_EXEC (1U<<14)
68 #define LIST_MASK ((LIST_EXEC << 1) - 1)
70 /* what files will be displayed */
71 #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
72 #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
73 #define DISP_DOT (1U<<17) /* show . and .. */
74 #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
75 #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
76 #define DISP_ROWS (1U<<20) /* print across rows */
78 #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1))
80 // CONFIG_FEATURE_LS_SORTFILES
81 /* how will the files be sorted */
82 #define SORT_ORDER_FORWARD 0 /* sort in reverse order */
83 #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
85 #define SORT_NAME 0 /* sort by file name */
86 #define SORT_SIZE (1U<<28) /* sort by file size */
87 #define SORT_ATIME (2U<<28) /* sort by last access time */
88 #define SORT_CTIME (3U<<28) /* sort by last change time */
89 #define SORT_MTIME (4U<<28) /* sort by last modification time */
90 #define SORT_VERSION (5U<<28) /* sort by version */
91 #define SORT_EXT (6U<<28) /* sort by file name extension */
92 #define SORT_DIR (7U<<28) /* sort by file or directory */
94 #define SORT_MASK (7U<<28)
96 /* which of the three times will be used */
97 #define TIME_CHANGE ((1U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
98 #define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS)
99 #define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
101 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
102 #define FOLLOW_LINKS (1U<<25)
104 #ifdef CONFIG_FEATURE_HUMAN_READABLE
105 #define LS_DISP_HR (1U<<26)
108 #define LIST_SHORT (LIST_FILENAME)
109 #define LIST_ISHORT (LIST_INO | LIST_FILENAME)
110 #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
111 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
112 #define LIST_ILONG (LIST_INO | LIST_LONG)
116 #define SPLIT_SUBDIR 2
118 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
119 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
121 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
122 # define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
125 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
126 #ifdef CONFIG_FEATURE_LS_COLOR
128 static int show_color = 0;
130 /* long option entry used only for --color, which has no short option
132 static const struct option ls_color_opt[] =
134 {"color", optional_argument, NULL, 1},
138 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
139 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
140 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
141 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
145 * a directory entry and its stat info are stored here
147 struct dnode { /* the basic node */
148 char *name; /* the dir entry name */
149 char *fullname; /* the dir entry name */
151 struct stat dstat; /* the file stat info */
152 #ifdef CONFIG_SELINUX
153 security_context_t sid;
155 struct dnode *next; /* point at the next node */
157 typedef struct dnode dnode_t;
159 static struct dnode **list_dir(const char *);
160 static struct dnode **dnalloc(int);
161 static int list_single(struct dnode *);
163 static unsigned int all_fmt;
165 #ifdef CONFIG_FEATURE_AUTOWIDTH
166 static unsigned terminal_width = TERMINAL_WIDTH;
167 static unsigned tabstops = COLUMN_GAP;
169 #define tabstops COLUMN_GAP
170 #define terminal_width TERMINAL_WIDTH
173 static int status = EXIT_SUCCESS;
175 static struct dnode *my_stat(char *fullname, char *name)
179 #ifdef CONFIG_SELINUX
180 security_context_t sid=NULL;
183 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
184 if (all_fmt & FOLLOW_LINKS) {
185 #ifdef CONFIG_SELINUX
186 if (is_selinux_enabled()) {
187 getfilecon(fullname,&sid);
190 if (stat(fullname, &dstat)) {
191 bb_perror_msg("%s", fullname);
192 status = EXIT_FAILURE;
198 #ifdef CONFIG_SELINUX
199 if (is_selinux_enabled()) {
200 lgetfilecon(fullname,&sid);
203 if (lstat(fullname, &dstat)) {
204 bb_perror_msg("%s", fullname);
205 status = EXIT_FAILURE;
210 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
211 cur->fullname = fullname;
214 #ifdef CONFIG_SELINUX
220 /*----------------------------------------------------------------------*/
221 #ifdef CONFIG_FEATURE_LS_COLOR
222 static char fgcolor(mode_t mode)
224 /* Check wheter the file is existing (if so, color it red!) */
225 if (errno == ENOENT) {
228 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
229 return COLOR(0xF000); /* File is executable ... */
233 /*----------------------------------------------------------------------*/
234 static char bgcolor(mode_t mode)
236 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
237 return ATTR(0xF000); /* File is executable ... */
242 /*----------------------------------------------------------------------*/
243 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
244 static char append_char(mode_t mode)
246 if (!(all_fmt & LIST_FILETYPE))
250 if (!(all_fmt & LIST_EXEC))
252 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
254 return APPCHAR(mode);
258 /*----------------------------------------------------------------------*/
260 #define countdirs(A,B) count_dirs((A), (B), 1)
261 #define countsubdirs(A,B) count_dirs((A), (B), 0)
263 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
267 if (dn == NULL || nfiles < 1)
270 for (i = 0; i < nfiles; i++) {
271 if (S_ISDIR(dn[i]->dstat.st_mode)
273 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
274 && ((dn[i]->name[1] != '.')
275 || dn[i]->name[2])))))
281 static int countfiles(struct dnode **dnp)
289 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
295 /* get memory to hold an array of pointers */
296 static struct dnode **dnalloc(int num)
303 p = (struct dnode **) xzalloc(num * sizeof(struct dnode *));
307 #ifdef CONFIG_FEATURE_LS_RECURSIVE
308 static void dfree(struct dnode **dnp, int nfiles)
315 for (i = 0; i < nfiles; i++) {
316 struct dnode *cur = dnp[i];
318 free(cur->fullname); /* free the filename */
319 free(cur); /* free the dnode */
321 free(dnp); /* free the array holding the dnode pointers */
327 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
332 if (dn == NULL || nfiles < 1)
335 /* count how many dirs and regular files there are */
336 if (which == SPLIT_SUBDIR)
337 dncnt = countsubdirs(dn, nfiles);
339 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
340 if (which == SPLIT_FILE)
341 dncnt = nfiles - dncnt; /* looking for files */
344 /* allocate a file array and a dir array */
345 dnp = dnalloc(dncnt);
347 /* copy the entrys into the file or dir array */
348 for (d = i = 0; i < nfiles; i++) {
349 if (S_ISDIR(dn[i]->dstat.st_mode)) {
350 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
351 if ((which & SPLIT_DIR)
352 || ((dn[i]->name[0] != '.')
354 && ((dn[i]->name[1] != '.')
355 || dn[i]->name[2])))) {
359 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
366 /*----------------------------------------------------------------------*/
367 #ifdef CONFIG_FEATURE_LS_SORTFILES
368 static int sortcmp(const void *a, const void *b)
370 struct dnode *d1 = *(struct dnode **)a;
371 struct dnode *d2 = *(struct dnode **)b;
372 unsigned int sort_opts = all_fmt & SORT_MASK;
375 dif = 0; /* assume SORT_NAME */
376 if (sort_opts == SORT_SIZE) {
377 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
378 } else if (sort_opts == SORT_ATIME) {
379 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
380 } else if (sort_opts == SORT_CTIME) {
381 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
382 } else if (sort_opts == SORT_MTIME) {
383 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
384 } else if (sort_opts == SORT_DIR) {
385 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
386 /* } else if (sort_opts == SORT_VERSION) { */
387 /* } else if (sort_opts == SORT_EXT) { */
391 /* sort by name- may be a tie_breaker for time or size cmp */
392 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
393 else dif = strcmp(d1->name, d2->name);
396 if (all_fmt & SORT_ORDER_REVERSE) {
402 /*----------------------------------------------------------------------*/
403 static void dnsort(struct dnode **dn, int size)
405 qsort(dn, size, sizeof *dn, sortcmp);
408 #define sortcmp(a, b) 0
409 #define dnsort(dn, size)
413 /*----------------------------------------------------------------------*/
414 static void showfiles(struct dnode **dn, int nfiles)
416 int i, ncols, nrows, row, nc;
419 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
421 if (dn == NULL || nfiles < 1)
424 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
427 /* find the longest file name- use that as the column width */
428 for (i = 0; i < nfiles; i++) {
429 int len = strlen(dn[i]->name) +
430 #ifdef CONFIG_SELINUX
431 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
433 ((all_fmt & LIST_INO) ? 8 : 0) +
434 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
435 if (column_width < len)
438 column_width += tabstops;
439 ncols = (int) (terminal_width / column_width);
443 nrows = nfiles / ncols;
444 if ((nrows * ncols) < nfiles)
445 nrows++; /* round up fractionals */
451 for (row = 0; row < nrows; row++) {
452 for (nc = 0; nc < ncols; nc++) {
453 /* reach into the array based on the column and row */
454 i = (nc * nrows) + row; /* assume display by column */
455 if (all_fmt & DISP_ROWS)
456 i = (row * ncols) + nc; /* display across row */
465 nexttab = column + column_width;
466 column += list_single(dn[i]);
474 /*----------------------------------------------------------------------*/
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 if (ENABLE_FEATURE_LS_SORTFILES) 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);
504 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
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);
517 /*----------------------------------------------------------------------*/
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 status = 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] == 0 || (
541 entry->d_name[1] == '.'
542 && entry->d_name[2] == 0))
543 && !(all_fmt & DISP_DOT))
545 if (!(all_fmt & DISP_HIDDEN))
548 fullname = concat_path_file(path, entry->d_name);
549 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
559 /* now that we know how many files there are
560 ** allocate memory for an array to hold dnode pointers
564 dnp = dnalloc(nfiles);
565 for (i = 0, cur = dn; i < nfiles; i++) {
566 dnp[i] = cur; /* save pointer to node in array */
573 /*----------------------------------------------------------------------*/
574 static int list_single(struct dnode *dn)
578 #ifdef CONFIG_FEATURE_LS_USERNAME
581 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
585 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
590 if (dn->fullname == NULL)
593 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
594 ttime = dn->dstat.st_mtime; /* the default time */
595 if (all_fmt & TIME_ACCESS)
596 ttime = dn->dstat.st_atime;
597 if (all_fmt & TIME_CHANGE)
598 ttime = dn->dstat.st_ctime;
599 filetime = ctime(&ttime);
601 #ifdef CONFIG_FEATURE_LS_FILETYPES
602 append = append_char(dn->dstat.st_mode);
605 for (i = 0; i <= 31; i++) {
606 switch (all_fmt & (1 << i)) {
608 column += printf("%7ld ", (long int) dn->dstat.st_ino);
611 #if _FILE_OFFSET_BITS == 64
612 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
614 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
618 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
621 column += printf("%4ld ", (long) dn->dstat.st_nlink);
624 #ifdef CONFIG_FEATURE_LS_USERNAME
625 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
626 printf("%-8.8s ", scratch);
627 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
628 printf("%-8.8s", scratch);
632 case LIST_ID_NUMERIC:
633 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
637 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
638 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
639 (int) minor(dn->dstat.st_rdev));
641 #ifdef CONFIG_FEATURE_HUMAN_READABLE
642 if (all_fmt & LS_DISP_HR) {
643 column += printf("%9s ",
644 make_human_readable_str(dn->dstat.st_size, 1, 0));
648 #if _FILE_OFFSET_BITS == 64
649 column += printf("%9lld ", (long long) dn->dstat.st_size);
651 column += printf("%9ld ", dn->dstat.st_size);
656 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
658 printf("%24.24s ", filetime);
662 if ((all_fmt & LIST_FULLTIME) == 0) {
663 age = time(NULL) - ttime;
664 printf("%6.6s ", filetime + 4);
665 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
666 /* hh:mm if less than 6 months old */
667 printf("%5.5s ", filetime + 11);
669 printf(" %4.4s ", filetime + 20);
675 #ifdef CONFIG_SELINUX
682 /* I assume sid initilized with NULL */
683 len = strlen(dn->sid)+1;
684 safe_strncpy(context, dn->sid, len);
687 safe_strncpy(context, "unknown", 8);
689 printf("%-32s ", context);
690 column += MAX(33, len);
695 #ifdef CONFIG_FEATURE_LS_COLOR
697 if (show_color && !lstat(dn->fullname, &info)) {
698 printf("\033[%d;%dm", bgcolor(info.st_mode),
699 fgcolor(info.st_mode));
702 column += printf("%s", dn->name);
703 #ifdef CONFIG_FEATURE_LS_COLOR
710 if (S_ISLNK(dn->dstat.st_mode)) {
711 char *lpath = xreadlink(dn->fullname);
715 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
716 if (!stat(dn->fullname, &info)) {
717 append = append_char(info.st_mode);
720 #ifdef CONFIG_FEATURE_LS_COLOR
723 printf("\033[%d;%dm", bgcolor(info.st_mode),
724 fgcolor(info.st_mode));
727 column += printf("%s", lpath) + 4;
728 #ifdef CONFIG_FEATURE_LS_COLOR
737 #ifdef CONFIG_FEATURE_LS_FILETYPES
739 if (append != '\0') {
740 printf("%1c", append);
751 /*----------------------------------------------------------------------*/
753 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
754 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
755 /* "[-]Ak" GNU options, busybox always supports */
756 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
757 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
758 /* "[-]SXvThw", GNU options, busybox optionally supports */
759 /* "[-]K", SELinux mandated options, busybox optionally supports */
760 /* "[-]e", I think we made this one up */
762 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
763 # define LS_STR_TIMESTAMPS "cetu"
765 # define LS_STR_TIMESTAMPS ""
768 #ifdef CONFIG_FEATURE_LS_FILETYPES
769 # define LS_STR_FILETYPES "Fp"
771 # define LS_STR_FILETYPES ""
774 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
775 # define LS_STR_FOLLOW_LINKS "L"
777 # define LS_STR_FOLLOW_LINKS ""
780 #ifdef CONFIG_FEATURE_LS_RECURSIVE
781 # define LS_STR_RECURSIVE "R"
783 # define LS_STR_RECURSIVE ""
786 #ifdef CONFIG_FEATURE_HUMAN_READABLE
787 # define LS_STR_HUMAN_READABLE "h"
789 # define LS_STR_HUMAN_READABLE ""
792 #ifdef CONFIG_SELINUX
793 # define LS_STR_SELINUX "K"
795 # define LS_STR_SELINUX ""
798 #ifdef CONFIG_FEATURE_AUTOWIDTH
799 # define LS_STR_AUTOWIDTH "T:w:"
801 # define LS_STR_AUTOWIDTH ""
804 static const char ls_options[]="Cadil1gnsxAk" \
806 USE_FEATURE_LS_SORTFILES("SXrv") \
808 LS_STR_FOLLOW_LINKS \
810 LS_STR_HUMAN_READABLE \
814 #define LIST_MASK_TRIGGER 0
815 #define STYLE_MASK_TRIGGER STYLE_MASK
816 #define SORT_MASK_TRIGGER SORT_MASK
817 #define DISP_MASK_TRIGGER DISP_ROWS
819 static const unsigned opt_flags[] = {
820 LIST_SHORT | STYLE_COLUMNS, /* C */
821 DISP_HIDDEN | DISP_DOT, /* a */
824 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
825 LIST_SHORT | STYLE_SINGLE, /* 1 */
827 LIST_ID_NUMERIC, /* n */
831 #ifdef CONFIG_SELINUX
832 LIST_CONTEXT, /* k */
836 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
837 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
838 LIST_FULLTIME, /* e */
839 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
840 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
842 #ifdef CONFIG_FEATURE_LS_SORTFILES
845 SORT_ORDER_REVERSE, /* r */
846 SORT_VERSION, /* v */
848 #ifdef CONFIG_FEATURE_LS_FILETYPES
849 LIST_FILETYPE | LIST_EXEC, /* F */
850 LIST_FILETYPE, /* p */
852 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
853 FOLLOW_LINKS, /* L */
855 #ifdef CONFIG_FEATURE_LS_RECURSIVE
856 DISP_RECURSIVE, /* R */
858 #ifdef CONFIG_FEATURE_HUMAN_READABLE
861 #ifdef CONFIG_SELINUX
862 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
864 #ifdef CONFIG_FEATURE_AUTOWIDTH
865 0, 0, /* T, w - ignored */
871 /*----------------------------------------------------------------------*/
873 int ls_main(int argc, char **argv)
888 #ifdef CONFIG_FEATURE_AUTOWIDTH
889 char *tabstops_str = NULL;
890 char *terminal_width_str = NULL;
892 #ifdef CONFIG_FEATURE_LS_COLOR
896 all_fmt = LIST_SHORT |
897 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
899 #ifdef CONFIG_FEATURE_AUTOWIDTH
900 /* Obtain the terminal width. */
901 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
906 #ifdef CONFIG_FEATURE_LS_COLOR
907 applet_long_options = ls_color_opt;
910 /* process options */
911 #ifdef CONFIG_FEATURE_AUTOWIDTH
912 opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
913 #ifdef CONFIG_FEATURE_LS_COLOR
918 tabstops = xatou(tabstops_str);
920 if (terminal_width_str) {
921 terminal_width = xatou(terminal_width_str);
924 opt = getopt32(argc, argv, ls_options
925 #ifdef CONFIG_FEATURE_LS_COLOR
930 for (i = 0; opt_flags[i] != (1U<<31); i++) {
931 if (opt & (1 << i)) {
932 unsigned int flags = opt_flags[i];
934 if (flags & LIST_MASK_TRIGGER) {
935 all_fmt &= ~LIST_MASK;
937 if (flags & STYLE_MASK_TRIGGER) {
938 all_fmt &= ~STYLE_MASK;
940 if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) {
941 all_fmt &= ~SORT_MASK;
943 if (flags & DISP_MASK_TRIGGER) {
944 all_fmt &= ~DISP_MASK;
946 if (flags & TIME_MASK) {
947 all_fmt &= ~TIME_MASK;
949 if (flags & LIST_CONTEXT) {
950 all_fmt |= STYLE_SINGLE;
952 #ifdef CONFIG_FEATURE_HUMAN_READABLE
954 all_fmt &= ~LS_DISP_HR;
961 #ifdef CONFIG_FEATURE_LS_COLOR
963 /* find color bit value - last position for short getopt */
965 #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
968 if ((p = getenv ("LS_COLORS")) != NULL &&
969 (*p == '\0' || (strcmp(p, "none") == 0))) {
971 } else if (isatty(STDOUT_FILENO)) {
976 if((opt & (1 << i))) { /* next flag after short options */
977 if (color_opt == NULL || strcmp("always", color_opt) == 0)
979 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
981 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
987 /* sort out which command line options take precedence */
988 #ifdef CONFIG_FEATURE_LS_RECURSIVE
989 if (all_fmt & DISP_NOLIST)
990 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
992 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
993 if (all_fmt & TIME_CHANGE)
994 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
995 if (all_fmt & TIME_ACCESS)
996 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
998 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
999 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
1000 #ifdef CONFIG_FEATURE_LS_USERNAME
1001 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1002 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
1005 /* choose a display format */
1006 if (!(all_fmt & STYLE_MASK))
1007 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1010 * when there are no cmd line args we have to supply a default "." arg.
1011 * we will create a second argv array, "av" that will hold either
1012 * our created "." arg, or the real cmd line args. The av array
1013 * just holds the pointers- we don't move the date the pointers
1016 ac = argc - optind; /* how many cmd line args are left */
1018 static const char * const dotdir[] = { "." };
1020 av = (char **) dotdir;
1026 /* now, everything is in the av array */
1028 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
1030 /* stuff the command line file names into an dnode array */
1032 for (oi = 0; oi < ac; oi++) {
1033 cur = my_stat(av[oi], av[oi]);
1042 /* now that we know how many files there are
1043 ** allocate memory for an array to hold dnode pointers
1045 dnp = dnalloc(nfiles);
1046 for (i = 0, cur = dn; i < nfiles; i++) {
1047 dnp[i] = cur; /* save pointer to node in array */
1051 if (all_fmt & DISP_NOLIST) {
1052 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
1054 showfiles(dnp, nfiles);
1056 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1057 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1058 dndirs = countdirs(dnp, nfiles);
1059 dnfiles = nfiles - dndirs;
1061 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
1062 showfiles(dnf, dnfiles);
1063 if (ENABLE_FEATURE_CLEAN_UP)
1067 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
1068 showdirs(dnd, dndirs, dnfiles == 0);
1069 if (ENABLE_FEATURE_CLEAN_UP)
1073 if (ENABLE_FEATURE_CLEAN_UP)