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 /************************************************************************/
45 #include <getopt.h> /* struct option */
46 #include <sys/ioctl.h>
47 #include <sys/sysmacros.h> /* major() and minor() */
50 /* what is the overall style of the listing */
51 #define STYLE_COLUMNS (1U<<21) /* fill columns */
52 #define STYLE_LONG (2U<<21) /* one record per line, extended info */
53 #define STYLE_SINGLE (3U<<21) /* one record per line */
55 #define STYLE_MASK STYLE_SINGLE
56 #define STYLE_ONE_RECORD_FLAG STYLE_LONG
58 /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
59 /* what file information will be listed */
60 #define LIST_INO (1U<<0)
61 #define LIST_BLOCKS (1U<<1)
62 #define LIST_MODEBITS (1U<<2)
63 #define LIST_NLINKS (1U<<3)
64 #define LIST_ID_NAME (1U<<4)
65 #define LIST_ID_NUMERIC (1U<<5)
66 #define LIST_CONTEXT (1U<<6)
67 #define LIST_SIZE (1U<<7)
68 #define LIST_DEV (1U<<8)
69 #define LIST_DATE_TIME (1U<<9)
70 #define LIST_FULLTIME (1U<<10)
71 #define LIST_FILENAME (1U<<11)
72 #define LIST_SYMLINK (1U<<12)
73 #define LIST_FILETYPE (1U<<13)
74 #define LIST_EXEC (1U<<14)
76 #define LIST_MASK ((LIST_EXEC << 1) - 1)
78 /* what files will be displayed */
79 #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
80 #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
81 #define DISP_DOT (1U<<17) /* show . and .. */
82 #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
83 #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
84 #define DISP_ROWS (1U<<20) /* print across rows */
86 #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1))
88 // CONFIG_FEATURE_LS_SORTFILES
89 /* how will the files be sorted */
90 #define SORT_ORDER_FORWARD 0 /* sort in reverse order */
91 #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
93 #define SORT_NAME 0 /* sort by file name */
94 #define SORT_SIZE (1U<<28) /* sort by file size */
95 #define SORT_ATIME (2U<<28) /* sort by last access time */
96 #define SORT_CTIME (3U<<28) /* sort by last change time */
97 #define SORT_MTIME (4U<<28) /* sort by last modification time */
98 #define SORT_VERSION (5U<<28) /* sort by version */
99 #define SORT_EXT (6U<<28) /* sort by file name extension */
100 #define SORT_DIR (7U<<28) /* sort by file or directory */
102 #define SORT_MASK (7U<<28)
104 /* which of the three times will be used */
105 #define TIME_CHANGE ((1U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
106 #define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS)
107 #define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
109 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
110 #define FOLLOW_LINKS (1U<<25)
112 #ifdef CONFIG_FEATURE_HUMAN_READABLE
113 #define LS_DISP_HR (1U<<26)
116 #define LIST_SHORT (LIST_FILENAME)
117 #define LIST_ISHORT (LIST_INO | LIST_FILENAME)
118 #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
119 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
120 #define LIST_ILONG (LIST_INO | LIST_LONG)
124 #define SPLIT_SUBDIR 2
126 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
127 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
129 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
130 # define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
133 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
134 #ifdef CONFIG_FEATURE_LS_COLOR
136 static int show_color = 0;
138 /* long option entry used only for --color, which has no short option
140 static const struct option ls_color_opt[] =
142 {"color", optional_argument, NULL, 1},
146 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
147 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
148 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
149 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
153 * a directory entry and its stat info are stored here
155 struct dnode { /* the basic node */
156 char *name; /* the dir entry name */
157 char *fullname; /* the dir entry name */
159 struct stat dstat; /* the file stat info */
160 #ifdef CONFIG_SELINUX
161 security_context_t sid;
163 struct dnode *next; /* point at the next node */
165 typedef struct dnode dnode_t;
167 static struct dnode **list_dir(const char *);
168 static struct dnode **dnalloc(int);
169 static int list_single(struct dnode *);
171 static unsigned int all_fmt;
173 #ifdef CONFIG_FEATURE_AUTOWIDTH
174 static int terminal_width = TERMINAL_WIDTH;
175 static unsigned short tabstops = COLUMN_GAP;
177 #define tabstops COLUMN_GAP
178 #define terminal_width TERMINAL_WIDTH
181 static int status = EXIT_SUCCESS;
183 static struct dnode *my_stat(char *fullname, char *name)
187 #ifdef CONFIG_SELINUX
188 security_context_t sid=NULL;
191 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
192 if (all_fmt & FOLLOW_LINKS) {
193 #ifdef CONFIG_SELINUX
194 if (is_selinux_enabled()) {
195 getfilecon(fullname,&sid);
198 if (stat(fullname, &dstat)) {
199 bb_perror_msg("%s", fullname);
200 status = EXIT_FAILURE;
206 #ifdef CONFIG_SELINUX
207 if (is_selinux_enabled()) {
208 lgetfilecon(fullname,&sid);
211 if (lstat(fullname, &dstat)) {
212 bb_perror_msg("%s", fullname);
213 status = EXIT_FAILURE;
218 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
219 cur->fullname = fullname;
222 #ifdef CONFIG_SELINUX
228 /*----------------------------------------------------------------------*/
229 #ifdef CONFIG_FEATURE_LS_COLOR
230 static char fgcolor(mode_t mode)
232 /* Check wheter the file is existing (if so, color it red!) */
233 if (errno == ENOENT) {
236 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
237 return COLOR(0xF000); /* File is executable ... */
241 /*----------------------------------------------------------------------*/
242 static char bgcolor(mode_t mode)
244 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
245 return ATTR(0xF000); /* File is executable ... */
250 /*----------------------------------------------------------------------*/
251 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
252 static char append_char(mode_t mode)
254 if (!(all_fmt & LIST_FILETYPE))
258 if (!(all_fmt & LIST_EXEC))
260 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
262 return APPCHAR(mode);
266 /*----------------------------------------------------------------------*/
268 #define countdirs(A,B) count_dirs((A), (B), 1)
269 #define countsubdirs(A,B) count_dirs((A), (B), 0)
271 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
275 if (dn == NULL || nfiles < 1)
278 for (i = 0; i < nfiles; i++) {
279 if (S_ISDIR(dn[i]->dstat.st_mode)
281 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
282 && ((dn[i]->name[1] != '.')
283 || dn[i]->name[2])))))
289 static int countfiles(struct dnode **dnp)
297 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
303 /* get memory to hold an array of pointers */
304 static struct dnode **dnalloc(int num)
311 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
315 #ifdef CONFIG_FEATURE_LS_RECURSIVE
316 static void dfree(struct dnode **dnp, int nfiles)
323 for (i = 0; i < nfiles; i++) {
324 struct dnode *cur = dnp[i];
326 free(cur->fullname); /* free the filename */
327 free(cur); /* free the dnode */
329 free(dnp); /* free the array holding the dnode pointers */
335 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
340 if (dn == NULL || nfiles < 1)
343 /* count how many dirs and regular files there are */
344 if (which == SPLIT_SUBDIR)
345 dncnt = countsubdirs(dn, nfiles);
347 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
348 if (which == SPLIT_FILE)
349 dncnt = nfiles - dncnt; /* looking for files */
352 /* allocate a file array and a dir array */
353 dnp = dnalloc(dncnt);
355 /* copy the entrys into the file or dir array */
356 for (d = i = 0; i < nfiles; i++) {
357 if (S_ISDIR(dn[i]->dstat.st_mode)) {
358 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
359 if ((which & SPLIT_DIR)
360 || ((dn[i]->name[0] != '.')
362 && ((dn[i]->name[1] != '.')
363 || dn[i]->name[2])))) {
367 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
374 /*----------------------------------------------------------------------*/
375 #ifdef CONFIG_FEATURE_LS_SORTFILES
376 static int sortcmp(const void *a, const void *b)
378 struct dnode *d1 = *(struct dnode **)a;
379 struct dnode *d2 = *(struct dnode **)b;
380 unsigned int sort_opts = all_fmt & SORT_MASK;
383 dif = 0; /* assume SORT_NAME */
384 if (sort_opts == SORT_SIZE) {
385 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
386 } else if (sort_opts == SORT_ATIME) {
387 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
388 } else if (sort_opts == SORT_CTIME) {
389 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
390 } else if (sort_opts == SORT_MTIME) {
391 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
392 } else if (sort_opts == SORT_DIR) {
393 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
394 /* } else if (sort_opts == SORT_VERSION) { */
395 /* } else if (sort_opts == SORT_EXT) { */
399 /* sort by name- may be a tie_breaker for time or size cmp */
400 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
401 else dif = strcmp(d1->name, d2->name);
404 if (all_fmt & SORT_ORDER_REVERSE) {
410 /*----------------------------------------------------------------------*/
411 static void dnsort(struct dnode **dn, int size)
413 qsort(dn, size, sizeof *dn, sortcmp);
416 #define sortcmp(a, b) 0
417 #define dnsort(dn, size)
421 /*----------------------------------------------------------------------*/
422 static void showfiles(struct dnode **dn, int nfiles)
424 int i, ncols, nrows, row, nc;
427 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
429 if (dn == NULL || nfiles < 1)
432 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
435 /* find the longest file name- use that as the column width */
436 for (i = 0; i < nfiles; i++) {
437 int len = strlen(dn[i]->name) +
438 #ifdef CONFIG_SELINUX
439 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
441 ((all_fmt & LIST_INO) ? 8 : 0) +
442 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
443 if (column_width < len)
446 column_width += tabstops;
447 ncols = (int) (terminal_width / column_width);
451 nrows = nfiles / ncols;
452 if ((nrows * ncols) < nfiles)
453 nrows++; /* round up fractionals */
459 for (row = 0; row < nrows; row++) {
460 for (nc = 0; nc < ncols; nc++) {
461 /* reach into the array based on the column and row */
462 i = (nc * nrows) + row; /* assume display by column */
463 if (all_fmt & DISP_ROWS)
464 i = (row * ncols) + nc; /* display across row */
473 nexttab = column + column_width;
474 column += list_single(dn[i]);
482 /*----------------------------------------------------------------------*/
483 static void showdirs(struct dnode **dn, int ndirs, int first)
486 struct dnode **subdnp;
490 if (dn == NULL || ndirs < 1)
493 for (i = 0; i < ndirs; i++) {
494 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
498 printf("%s:\n", dn[i]->fullname);
500 subdnp = list_dir(dn[i]->fullname);
501 nfiles = countfiles(subdnp);
503 /* list all files at this level */
504 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(subdnp, nfiles);
505 showfiles(subdnp, nfiles);
506 if (ENABLE_FEATURE_LS_RECURSIVE) {
507 if (all_fmt & DISP_RECURSIVE) {
508 /* recursive- list the sub-dirs */
509 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
510 dndirs = countsubdirs(subdnp, nfiles);
512 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
513 showdirs(dnd, dndirs, 0);
514 /* free the array of dnode pointers to the dirs */
518 /* free the dnodes and the fullname mem */
519 dfree(subdnp, nfiles);
525 /*----------------------------------------------------------------------*/
526 static struct dnode **list_dir(const char *path)
528 struct dnode *dn, *cur, **dnp;
529 struct dirent *entry;
538 dir = bb_opendir(path);
540 status = EXIT_FAILURE;
541 return (NULL); /* could not open the dir */
543 while ((entry = readdir(dir)) != NULL) {
546 /* are we going to list the file- it may be . or .. or a hidden file */
547 if (entry->d_name[0] == '.') {
548 if ((entry->d_name[1] == 0 || (
549 entry->d_name[1] == '.'
550 && entry->d_name[2] == 0))
551 && !(all_fmt & DISP_DOT))
553 if (!(all_fmt & DISP_HIDDEN))
556 fullname = concat_path_file(path, entry->d_name);
557 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
567 /* now that we know how many files there are
568 ** allocate memory for an array to hold dnode pointers
572 dnp = dnalloc(nfiles);
573 for (i = 0, cur = dn; i < nfiles; i++) {
574 dnp[i] = cur; /* save pointer to node in array */
581 /*----------------------------------------------------------------------*/
582 static int list_single(struct dnode *dn)
586 #ifdef CONFIG_FEATURE_LS_USERNAME
589 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
593 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
598 if (dn->fullname == NULL)
601 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
602 ttime = dn->dstat.st_mtime; /* the default time */
603 if (all_fmt & TIME_ACCESS)
604 ttime = dn->dstat.st_atime;
605 if (all_fmt & TIME_CHANGE)
606 ttime = dn->dstat.st_ctime;
607 filetime = ctime(&ttime);
609 #ifdef CONFIG_FEATURE_LS_FILETYPES
610 append = append_char(dn->dstat.st_mode);
613 for (i = 0; i <= 31; i++) {
614 switch (all_fmt & (1 << i)) {
616 column += printf("%7ld ", (long int) dn->dstat.st_ino);
619 #if _FILE_OFFSET_BITS == 64
620 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
622 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
626 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
629 column += printf("%4ld ", (long) dn->dstat.st_nlink);
632 #ifdef CONFIG_FEATURE_LS_USERNAME
633 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
634 printf("%-8.8s ", scratch);
635 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
636 printf("%-8.8s", scratch);
640 case LIST_ID_NUMERIC:
641 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
645 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
646 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
647 (int) minor(dn->dstat.st_rdev));
649 #ifdef CONFIG_FEATURE_HUMAN_READABLE
650 if (all_fmt & LS_DISP_HR) {
651 column += printf("%9s ",
652 make_human_readable_str(dn->dstat.st_size, 1, 0));
656 #if _FILE_OFFSET_BITS == 64
657 column += printf("%9lld ", (long long) dn->dstat.st_size);
659 column += printf("%9ld ", dn->dstat.st_size);
664 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
666 printf("%24.24s ", filetime);
670 if ((all_fmt & LIST_FULLTIME) == 0) {
671 age = time(NULL) - ttime;
672 printf("%6.6s ", filetime + 4);
673 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
674 /* hh:mm if less than 6 months old */
675 printf("%5.5s ", filetime + 11);
677 printf(" %4.4s ", filetime + 20);
683 #ifdef CONFIG_SELINUX
690 /* I assume sid initilized with NULL */
691 len = strlen(dn->sid)+1;
692 safe_strncpy(context, dn->sid, len);
695 safe_strncpy(context, "unknown", 8);
697 printf("%-32s ", context);
698 column += MAX(33, len);
703 #ifdef CONFIG_FEATURE_LS_COLOR
705 if (show_color && !lstat(dn->fullname, &info)) {
706 printf("\033[%d;%dm", bgcolor(info.st_mode),
707 fgcolor(info.st_mode));
710 column += printf("%s", dn->name);
711 #ifdef CONFIG_FEATURE_LS_COLOR
718 if (S_ISLNK(dn->dstat.st_mode)) {
719 char *lpath = xreadlink(dn->fullname);
723 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
724 if (!stat(dn->fullname, &info)) {
725 append = append_char(info.st_mode);
728 #ifdef CONFIG_FEATURE_LS_COLOR
731 printf("\033[%d;%dm", bgcolor(info.st_mode),
732 fgcolor(info.st_mode));
735 column += printf("%s", lpath) + 4;
736 #ifdef CONFIG_FEATURE_LS_COLOR
745 #ifdef CONFIG_FEATURE_LS_FILETYPES
747 if (append != '\0') {
748 printf("%1c", append);
759 /*----------------------------------------------------------------------*/
761 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
762 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
763 /* "[-]Ak" GNU options, busybox always supports */
764 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
765 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
766 /* "[-]SXvThw", GNU options, busybox optionally supports */
767 /* "[-]K", SELinux mandated options, busybox optionally supports */
768 /* "[-]e", I think we made this one up */
770 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
771 # define LS_STR_TIMESTAMPS "cetu"
773 # define LS_STR_TIMESTAMPS ""
776 #ifdef CONFIG_FEATURE_LS_FILETYPES
777 # define LS_STR_FILETYPES "Fp"
779 # define LS_STR_FILETYPES ""
782 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
783 # define LS_STR_FOLLOW_LINKS "L"
785 # define LS_STR_FOLLOW_LINKS ""
788 #ifdef CONFIG_FEATURE_LS_RECURSIVE
789 # define LS_STR_RECURSIVE "R"
791 # define LS_STR_RECURSIVE ""
794 #ifdef CONFIG_FEATURE_HUMAN_READABLE
795 # define LS_STR_HUMAN_READABLE "h"
797 # define LS_STR_HUMAN_READABLE ""
800 #ifdef CONFIG_SELINUX
801 # define LS_STR_SELINUX "K"
803 # define LS_STR_SELINUX ""
806 #ifdef CONFIG_FEATURE_AUTOWIDTH
807 # define LS_STR_AUTOWIDTH "T:w:"
809 # define LS_STR_AUTOWIDTH ""
812 static const char ls_options[]="Cadil1gnsxAk" \
814 USE_FEATURE_LS_SORTFILES("SXrv") \
816 LS_STR_FOLLOW_LINKS \
818 LS_STR_HUMAN_READABLE \
822 #define LIST_MASK_TRIGGER 0
823 #define STYLE_MASK_TRIGGER STYLE_MASK
824 #define SORT_MASK_TRIGGER SORT_MASK
825 #define DISP_MASK_TRIGGER DISP_ROWS
827 static const unsigned opt_flags[] = {
828 LIST_SHORT | STYLE_COLUMNS, /* C */
829 DISP_HIDDEN | DISP_DOT, /* a */
832 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
833 LIST_SHORT | STYLE_SINGLE, /* 1 */
835 LIST_ID_NUMERIC, /* n */
839 #ifdef CONFIG_SELINUX
840 LIST_CONTEXT, /* k */
844 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
845 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
846 LIST_FULLTIME, /* e */
847 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
848 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
850 #ifdef CONFIG_FEATURE_LS_SORTFILES
853 SORT_ORDER_REVERSE, /* r */
854 SORT_VERSION, /* v */
856 #ifdef CONFIG_FEATURE_LS_FILETYPES
857 LIST_FILETYPE | LIST_EXEC, /* F */
858 LIST_FILETYPE, /* p */
860 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
861 FOLLOW_LINKS, /* L */
863 #ifdef CONFIG_FEATURE_LS_RECURSIVE
864 DISP_RECURSIVE, /* R */
866 #ifdef CONFIG_FEATURE_HUMAN_READABLE
869 #ifdef CONFIG_SELINUX
870 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
872 #ifdef CONFIG_FEATURE_AUTOWIDTH
873 0, 0, /* T, w - ignored */
879 /*----------------------------------------------------------------------*/
881 int ls_main(int argc, char **argv)
896 #ifdef CONFIG_FEATURE_AUTOWIDTH
897 char *tabstops_str = NULL;
898 char *terminal_width_str = NULL;
900 #ifdef CONFIG_FEATURE_LS_COLOR
904 all_fmt = LIST_SHORT |
905 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
907 #ifdef CONFIG_FEATURE_AUTOWIDTH
908 /* Obtain the terminal width. */
909 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
914 #ifdef CONFIG_FEATURE_LS_COLOR
915 bb_applet_long_options = ls_color_opt;
918 /* process options */
919 #ifdef CONFIG_FEATURE_AUTOWIDTH
920 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
921 #ifdef CONFIG_FEATURE_LS_COLOR
926 tabstops = atoi(tabstops_str);
928 if (terminal_width_str) {
929 terminal_width = atoi(terminal_width_str);
932 opt = bb_getopt_ulflags(argc, argv, ls_options
933 #ifdef CONFIG_FEATURE_LS_COLOR
938 for (i = 0; opt_flags[i] != (1U<<31); i++) {
939 if (opt & (1 << i)) {
940 unsigned int flags = opt_flags[i];
942 if (flags & LIST_MASK_TRIGGER) {
943 all_fmt &= ~LIST_MASK;
945 if (flags & STYLE_MASK_TRIGGER) {
946 all_fmt &= ~STYLE_MASK;
948 if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) {
949 all_fmt &= ~SORT_MASK;
951 if (flags & DISP_MASK_TRIGGER) {
952 all_fmt &= ~DISP_MASK;
954 if (flags & TIME_MASK) {
955 all_fmt &= ~TIME_MASK;
957 if (flags & LIST_CONTEXT) {
958 all_fmt |= STYLE_SINGLE;
960 #ifdef CONFIG_FEATURE_HUMAN_READABLE
962 all_fmt &= ~LS_DISP_HR;
969 #ifdef CONFIG_FEATURE_LS_COLOR
971 /* find color bit value - last position for short getopt */
973 #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
976 if ((p = getenv ("LS_COLORS")) != NULL &&
977 (*p == '\0' || (strcmp(p, "none") == 0))) {
979 } else if (isatty(STDOUT_FILENO)) {
984 if((opt & (1 << i))) { /* next flag after short options */
985 if (color_opt == NULL || strcmp("always", color_opt) == 0)
987 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
989 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
995 /* sort out which command line options take precedence */
996 #ifdef CONFIG_FEATURE_LS_RECURSIVE
997 if (all_fmt & DISP_NOLIST)
998 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
1000 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
1001 if (all_fmt & TIME_CHANGE)
1002 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1003 if (all_fmt & TIME_ACCESS)
1004 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
1006 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1007 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
1008 #ifdef CONFIG_FEATURE_LS_USERNAME
1009 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1010 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
1013 /* choose a display format */
1014 if (!(all_fmt & STYLE_MASK))
1015 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1018 * when there are no cmd line args we have to supply a default "." arg.
1019 * we will create a second argv array, "av" that will hold either
1020 * our created "." arg, or the real cmd line args. The av array
1021 * just holds the pointers- we don't move the date the pointers
1024 ac = argc - optind; /* how many cmd line args are left */
1026 static const char * const dotdir[] = { "." };
1028 av = (char **) dotdir;
1034 /* now, everything is in the av array */
1036 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
1038 /* stuff the command line file names into an dnode array */
1040 for (oi = 0; oi < ac; oi++) {
1041 cur = my_stat(av[oi], av[oi]);
1050 /* now that we know how many files there are
1051 ** allocate memory for an array to hold dnode pointers
1053 dnp = dnalloc(nfiles);
1054 for (i = 0, cur = dn; i < nfiles; i++) {
1055 dnp[i] = cur; /* save pointer to node in array */
1059 if (all_fmt & DISP_NOLIST) {
1060 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
1062 showfiles(dnp, nfiles);
1064 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1065 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1066 dndirs = countdirs(dnp, nfiles);
1067 dnfiles = nfiles - dndirs;
1069 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
1070 showfiles(dnf, dnfiles);
1071 if (ENABLE_FEATURE_CLEAN_UP)
1075 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
1076 showdirs(dnd, dndirs, dnfiles == 0);
1077 if (ENABLE_FEATURE_CLEAN_UP)
1081 if (ENABLE_FEATURE_CLEAN_UP)