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 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
105 /* which of the three times will be used */
107 #define TIME_CHANGE (1U<<23)
108 #define TIME_ACCESS (1U<<24)
110 #define TIME_MASK (3U<<23)
113 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
114 #define FOLLOW_LINKS (1U<<25)
116 #ifdef CONFIG_FEATURE_HUMAN_READABLE
117 #define LS_DISP_HR (1U<<26)
120 #define LIST_SHORT (LIST_FILENAME)
121 #define LIST_ISHORT (LIST_INO | LIST_FILENAME)
122 #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
123 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
124 #define LIST_ILONG (LIST_INO | LIST_LONG)
128 #define SPLIT_SUBDIR 2
130 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
131 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
133 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
134 # define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
137 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
138 #ifdef CONFIG_FEATURE_LS_COLOR
140 static int show_color = 0;
142 /* long option entry used only for --color, which has no short option
144 static const struct option ls_color_opt[] =
146 {"color", optional_argument, NULL, 1},
150 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
151 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
152 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
153 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
157 * a directory entry and its stat info are stored here
159 struct dnode { /* the basic node */
160 char *name; /* the dir entry name */
161 char *fullname; /* the dir entry name */
163 struct stat dstat; /* the file stat info */
164 #ifdef CONFIG_SELINUX
165 security_context_t sid;
167 struct dnode *next; /* point at the next node */
169 typedef struct dnode dnode_t;
171 static struct dnode **list_dir(const char *);
172 static struct dnode **dnalloc(int);
173 static int list_single(struct dnode *);
175 static unsigned int all_fmt;
177 #ifdef CONFIG_FEATURE_AUTOWIDTH
178 static int terminal_width = TERMINAL_WIDTH;
179 static unsigned short tabstops = COLUMN_GAP;
181 #define tabstops COLUMN_GAP
182 #define terminal_width TERMINAL_WIDTH
185 static int status = EXIT_SUCCESS;
187 static struct dnode *my_stat(char *fullname, char *name)
191 #ifdef CONFIG_SELINUX
192 security_context_t sid=NULL;
195 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
196 if (all_fmt & FOLLOW_LINKS) {
197 #ifdef CONFIG_SELINUX
198 if (is_selinux_enabled()) {
199 getfilecon(fullname,&sid);
202 if (stat(fullname, &dstat)) {
203 bb_perror_msg("%s", fullname);
204 status = EXIT_FAILURE;
210 #ifdef CONFIG_SELINUX
211 if (is_selinux_enabled()) {
212 lgetfilecon(fullname,&sid);
215 if (lstat(fullname, &dstat)) {
216 bb_perror_msg("%s", fullname);
217 status = EXIT_FAILURE;
222 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
223 cur->fullname = fullname;
226 #ifdef CONFIG_SELINUX
232 /*----------------------------------------------------------------------*/
233 #ifdef CONFIG_FEATURE_LS_COLOR
234 static char fgcolor(mode_t mode)
236 /* Check wheter the file is existing (if so, color it red!) */
237 if (errno == ENOENT) {
240 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
241 return COLOR(0xF000); /* File is executable ... */
245 /*----------------------------------------------------------------------*/
246 static char bgcolor(mode_t mode)
248 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
249 return ATTR(0xF000); /* File is executable ... */
254 /*----------------------------------------------------------------------*/
255 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
256 static char append_char(mode_t mode)
258 if (!(all_fmt & LIST_FILETYPE))
262 if (!(all_fmt & LIST_EXEC))
264 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
266 return APPCHAR(mode);
270 /*----------------------------------------------------------------------*/
272 #define countdirs(A,B) count_dirs((A), (B), 1)
273 #define countsubdirs(A,B) count_dirs((A), (B), 0)
275 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
279 if (dn == NULL || nfiles < 1)
282 for (i = 0; i < nfiles; i++) {
283 if (S_ISDIR(dn[i]->dstat.st_mode)
285 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
286 && ((dn[i]->name[1] != '.')
287 || dn[i]->name[2])))))
293 static int countfiles(struct dnode **dnp)
301 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
307 /* get memory to hold an array of pointers */
308 static struct dnode **dnalloc(int num)
315 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
319 #ifdef CONFIG_FEATURE_LS_RECURSIVE
320 static void dfree(struct dnode **dnp, int nfiles)
327 for (i = 0; i < nfiles; i++) {
328 struct dnode *cur = dnp[i];
330 free(cur->fullname); /* free the filename */
331 free(cur); /* free the dnode */
333 free(dnp); /* free the array holding the dnode pointers */
339 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
344 if (dn == NULL || nfiles < 1)
347 /* count how many dirs and regular files there are */
348 if (which == SPLIT_SUBDIR)
349 dncnt = countsubdirs(dn, nfiles);
351 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
352 if (which == SPLIT_FILE)
353 dncnt = nfiles - dncnt; /* looking for files */
356 /* allocate a file array and a dir array */
357 dnp = dnalloc(dncnt);
359 /* copy the entrys into the file or dir array */
360 for (d = i = 0; i < nfiles; i++) {
361 if (S_ISDIR(dn[i]->dstat.st_mode)) {
362 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
363 if ((which & SPLIT_DIR)
364 || ((dn[i]->name[0] != '.')
366 && ((dn[i]->name[1] != '.')
367 || dn[i]->name[2])))) {
371 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
378 /*----------------------------------------------------------------------*/
379 #ifdef CONFIG_FEATURE_LS_SORTFILES
380 static int sortcmp(const void *a, const void *b)
382 struct dnode *d1 = *(struct dnode **)a;
383 struct dnode *d2 = *(struct dnode **)b;
384 unsigned int sort_opts = all_fmt & SORT_MASK;
387 dif = 0; /* assume SORT_NAME */
388 if (sort_opts == SORT_SIZE) {
389 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
390 } else if (sort_opts == SORT_ATIME) {
391 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
392 } else if (sort_opts == SORT_CTIME) {
393 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
394 } else if (sort_opts == SORT_MTIME) {
395 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
396 } else if (sort_opts == SORT_DIR) {
397 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
398 /* } else if (sort_opts == SORT_VERSION) { */
399 /* } else if (sort_opts == SORT_EXT) { */
403 /* sort by name- may be a tie_breaker for time or size cmp */
404 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
405 else dif = strcmp(d1->name, d2->name);
408 if (all_fmt & SORT_ORDER_REVERSE) {
414 /*----------------------------------------------------------------------*/
415 static void dnsort(struct dnode **dn, int size)
417 qsort(dn, size, sizeof *dn, sortcmp);
420 #define sortcmp(a, b) 0
421 #define dnsort(dn, size)
425 /*----------------------------------------------------------------------*/
426 static void showfiles(struct dnode **dn, int nfiles)
428 int i, ncols, nrows, row, nc;
431 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
433 if (dn == NULL || nfiles < 1)
436 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
439 /* find the longest file name- use that as the column width */
440 for (i = 0; i < nfiles; i++) {
441 int len = strlen(dn[i]->name) +
442 #ifdef CONFIG_SELINUX
443 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
445 ((all_fmt & LIST_INO) ? 8 : 0) +
446 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
447 if (column_width < len)
450 column_width += tabstops;
451 ncols = (int) (terminal_width / column_width);
455 nrows = nfiles / ncols;
456 if ((nrows * ncols) < nfiles)
457 nrows++; /* round up fractionals */
463 for (row = 0; row < nrows; row++) {
464 for (nc = 0; nc < ncols; nc++) {
465 /* reach into the array based on the column and row */
466 i = (nc * nrows) + row; /* assume display by column */
467 if (all_fmt & DISP_ROWS)
468 i = (row * ncols) + nc; /* display across row */
477 nexttab = column + column_width;
478 column += list_single(dn[i]);
486 /*----------------------------------------------------------------------*/
487 static void showdirs(struct dnode **dn, int ndirs, int first)
490 struct dnode **subdnp;
494 if (dn == NULL || ndirs < 1)
497 for (i = 0; i < ndirs; i++) {
498 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
502 printf("%s:\n", dn[i]->fullname);
504 subdnp = list_dir(dn[i]->fullname);
505 nfiles = countfiles(subdnp);
507 /* list all files at this level */
508 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(subdnp, nfiles);
509 showfiles(subdnp, nfiles);
510 #ifdef CONFIG_FEATURE_LS_RECURSIVE
511 if (all_fmt & DISP_RECURSIVE) {
512 /* recursive- list the sub-dirs */
513 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
514 dndirs = countsubdirs(subdnp, nfiles);
516 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
517 showdirs(dnd, dndirs, 0);
518 free(dnd); /* free the array of dnode pointers to the dirs */
521 dfree(subdnp, nfiles); /* free the dnodes and the fullname mem */
527 /*----------------------------------------------------------------------*/
528 static struct dnode **list_dir(const char *path)
530 struct dnode *dn, *cur, **dnp;
531 struct dirent *entry;
540 dir = bb_opendir(path);
542 status = EXIT_FAILURE;
543 return (NULL); /* could not open the dir */
545 while ((entry = readdir(dir)) != NULL) {
548 /* are we going to list the file- it may be . or .. or a hidden file */
549 if (entry->d_name[0] == '.') {
550 if ((entry->d_name[1] == 0 || (
551 entry->d_name[1] == '.'
552 && entry->d_name[2] == 0))
553 && !(all_fmt & DISP_DOT))
555 if (!(all_fmt & DISP_HIDDEN))
558 fullname = concat_path_file(path, entry->d_name);
559 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
569 /* now that we know how many files there are
570 ** allocate memory for an array to hold dnode pointers
574 dnp = dnalloc(nfiles);
575 for (i = 0, cur = dn; i < nfiles; i++) {
576 dnp[i] = cur; /* save pointer to node in array */
583 /*----------------------------------------------------------------------*/
584 static int list_single(struct dnode *dn)
588 #ifdef CONFIG_FEATURE_LS_USERNAME
591 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
595 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
600 if (dn->fullname == NULL)
603 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
604 ttime = dn->dstat.st_mtime; /* the default time */
605 if (all_fmt & TIME_ACCESS)
606 ttime = dn->dstat.st_atime;
607 if (all_fmt & TIME_CHANGE)
608 ttime = dn->dstat.st_ctime;
609 filetime = ctime(&ttime);
611 #ifdef CONFIG_FEATURE_LS_FILETYPES
612 append = append_char(dn->dstat.st_mode);
615 for (i = 0; i <= 31; i++) {
616 switch (all_fmt & (1 << i)) {
618 column += printf("%7ld ", (long int) dn->dstat.st_ino);
621 #if _FILE_OFFSET_BITS == 64
622 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
624 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
628 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
631 column += printf("%4ld ", (long) dn->dstat.st_nlink);
634 #ifdef CONFIG_FEATURE_LS_USERNAME
635 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
636 printf("%-8.8s ", scratch);
637 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
638 printf("%-8.8s", scratch);
642 case LIST_ID_NUMERIC:
643 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
647 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
648 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
649 (int) minor(dn->dstat.st_rdev));
651 #ifdef CONFIG_FEATURE_HUMAN_READABLE
652 if (all_fmt & LS_DISP_HR) {
653 column += printf("%9s ",
654 make_human_readable_str(dn->dstat.st_size, 1, 0));
658 #if _FILE_OFFSET_BITS == 64
659 column += printf("%9lld ", (long long) dn->dstat.st_size);
661 column += printf("%9ld ", dn->dstat.st_size);
666 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
668 printf("%24.24s ", filetime);
672 if ((all_fmt & LIST_FULLTIME) == 0) {
673 age = time(NULL) - ttime;
674 printf("%6.6s ", filetime + 4);
675 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
676 /* hh:mm if less than 6 months old */
677 printf("%5.5s ", filetime + 11);
679 printf(" %4.4s ", filetime + 20);
685 #ifdef CONFIG_SELINUX
692 /* I assume sid initilized with NULL */
693 len = strlen(dn->sid)+1;
694 safe_strncpy(context, dn->sid, len);
697 safe_strncpy(context, "unknown", 8);
699 printf("%-32s ", context);
700 column += MAX(33, len);
705 #ifdef CONFIG_FEATURE_LS_COLOR
707 if (show_color && !lstat(dn->fullname, &info)) {
708 printf("\033[%d;%dm", bgcolor(info.st_mode),
709 fgcolor(info.st_mode));
712 column += printf("%s", dn->name);
713 #ifdef CONFIG_FEATURE_LS_COLOR
720 if (S_ISLNK(dn->dstat.st_mode)) {
721 char *lpath = xreadlink(dn->fullname);
725 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
726 if (!stat(dn->fullname, &info)) {
727 append = append_char(info.st_mode);
730 #ifdef CONFIG_FEATURE_LS_COLOR
733 printf("\033[%d;%dm", bgcolor(info.st_mode),
734 fgcolor(info.st_mode));
737 column += printf("%s", lpath) + 4;
738 #ifdef CONFIG_FEATURE_LS_COLOR
747 #ifdef CONFIG_FEATURE_LS_FILETYPES
749 if (append != '\0') {
750 printf("%1c", append);
761 /*----------------------------------------------------------------------*/
763 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
764 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
765 /* "[-]Ak" GNU options, busybox always supports */
766 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
767 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
768 /* "[-]SXvThw", GNU options, busybox optionally supports */
769 /* "[-]K", SELinux mandated options, busybox optionally supports */
770 /* "[-]e", I think we made this one up */
772 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
773 # define LS_STR_TIMESTAMPS "cetu"
775 # define LS_STR_TIMESTAMPS ""
778 #ifdef CONFIG_FEATURE_LS_FILETYPES
779 # define LS_STR_FILETYPES "Fp"
781 # define LS_STR_FILETYPES ""
784 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
785 # define LS_STR_FOLLOW_LINKS "L"
787 # define LS_STR_FOLLOW_LINKS ""
790 #ifdef CONFIG_FEATURE_LS_RECURSIVE
791 # define LS_STR_RECURSIVE "R"
793 # define LS_STR_RECURSIVE ""
796 #ifdef CONFIG_FEATURE_HUMAN_READABLE
797 # define LS_STR_HUMAN_READABLE "h"
799 # define LS_STR_HUMAN_READABLE ""
802 #ifdef CONFIG_SELINUX
803 # define LS_STR_SELINUX "K"
805 # define LS_STR_SELINUX ""
808 #ifdef CONFIG_FEATURE_AUTOWIDTH
809 # define LS_STR_AUTOWIDTH "T:w:"
811 # define LS_STR_AUTOWIDTH ""
814 static const char ls_options[]="Cadil1gnsxAk" \
816 USE_FEATURE_LS_SORTFILES("SXrv") \
818 LS_STR_FOLLOW_LINKS \
820 LS_STR_HUMAN_READABLE \
824 #define LIST_MASK_TRIGGER 0
825 #define STYLE_MASK_TRIGGER STYLE_MASK
826 #define SORT_MASK_TRIGGER SORT_MASK
827 #define DISP_MASK_TRIGGER DISP_ROWS
828 #define TIME_MASK_TRIGGER TIME_MASK
830 static const unsigned opt_flags[] = {
831 LIST_SHORT | STYLE_COLUMNS, /* C */
832 DISP_HIDDEN | DISP_DOT, /* a */
835 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
836 LIST_SHORT | STYLE_SINGLE, /* 1 */
838 LIST_ID_NUMERIC, /* n */
842 #ifdef CONFIG_SELINUX
843 LIST_CONTEXT, /* k */
847 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
848 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
849 LIST_FULLTIME, /* e */
850 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
851 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
853 #ifdef CONFIG_FEATURE_LS_SORTFILES
856 SORT_ORDER_REVERSE, /* r */
857 SORT_VERSION, /* v */
859 #ifdef CONFIG_FEATURE_LS_FILETYPES
860 LIST_FILETYPE | LIST_EXEC, /* F */
861 LIST_FILETYPE, /* p */
863 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
864 FOLLOW_LINKS, /* L */
866 #ifdef CONFIG_FEATURE_LS_RECURSIVE
867 DISP_RECURSIVE, /* R */
869 #ifdef CONFIG_FEATURE_HUMAN_READABLE
872 #ifdef CONFIG_SELINUX
873 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
875 #ifdef CONFIG_FEATURE_AUTOWIDTH
876 0, 0, /* T, w - ignored */
882 /*----------------------------------------------------------------------*/
884 int ls_main(int argc, char **argv)
899 #ifdef CONFIG_FEATURE_AUTOWIDTH
900 char *tabstops_str = NULL;
901 char *terminal_width_str = NULL;
903 #ifdef CONFIG_FEATURE_LS_COLOR
907 all_fmt = LIST_SHORT | (ENABLE_FEATURE_LS_TIMESTAMPS * TIME_MOD) |
908 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
910 #ifdef CONFIG_FEATURE_AUTOWIDTH
911 /* Obtain the terminal width. */
912 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
917 #ifdef CONFIG_FEATURE_LS_COLOR
918 bb_applet_long_options = ls_color_opt;
921 /* process options */
922 #ifdef CONFIG_FEATURE_AUTOWIDTH
923 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
924 #ifdef CONFIG_FEATURE_LS_COLOR
929 tabstops = atoi(tabstops_str);
931 if (terminal_width_str) {
932 terminal_width = atoi(terminal_width_str);
935 opt = bb_getopt_ulflags(argc, argv, ls_options
936 #ifdef CONFIG_FEATURE_LS_COLOR
941 for (i = 0; opt_flags[i] != (1U<<31); i++) {
942 if (opt & (1 << i)) {
943 unsigned int flags = opt_flags[i];
945 if (flags & LIST_MASK_TRIGGER) {
946 all_fmt &= ~LIST_MASK;
948 if (flags & STYLE_MASK_TRIGGER) {
949 all_fmt &= ~STYLE_MASK;
951 if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) {
952 all_fmt &= ~SORT_MASK;
954 if (flags & DISP_MASK_TRIGGER) {
955 all_fmt &= ~DISP_MASK;
957 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
958 if (flags & TIME_MASK_TRIGGER) {
959 all_fmt &= ~TIME_MASK;
962 if (flags & LIST_CONTEXT) {
963 all_fmt |= STYLE_SINGLE;
965 #ifdef CONFIG_FEATURE_HUMAN_READABLE
967 all_fmt &= ~LS_DISP_HR;
974 #ifdef CONFIG_FEATURE_LS_COLOR
976 /* find color bit value - last position for short getopt */
978 #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
981 if ((p = getenv ("LS_COLORS")) != NULL &&
982 (*p == '\0' || (strcmp(p, "none") == 0))) {
984 } else if (isatty(STDOUT_FILENO)) {
989 if((opt & (1 << i))) { /* next flag after short options */
990 if (color_opt == NULL || strcmp("always", color_opt) == 0)
992 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
994 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
1000 /* sort out which command line options take precedence */
1001 #ifdef CONFIG_FEATURE_LS_RECURSIVE
1002 if (all_fmt & DISP_NOLIST)
1003 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
1005 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
1006 if (all_fmt & TIME_CHANGE)
1007 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1008 if (all_fmt & TIME_ACCESS)
1009 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
1011 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1012 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
1013 #ifdef CONFIG_FEATURE_LS_USERNAME
1014 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1015 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
1018 /* choose a display format */
1019 if (!(all_fmt & STYLE_MASK))
1020 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1023 * when there are no cmd line args we have to supply a default "." arg.
1024 * we will create a second argv array, "av" that will hold either
1025 * our created "." arg, or the real cmd line args. The av array
1026 * just holds the pointers- we don't move the date the pointers
1029 ac = argc - optind; /* how many cmd line args are left */
1031 static const char * const dotdir[] = { "." };
1033 av = (char **) dotdir;
1039 /* now, everything is in the av array */
1041 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
1043 /* stuff the command line file names into an dnode array */
1045 for (oi = 0; oi < ac; oi++) {
1046 cur = my_stat(av[oi], av[oi]);
1055 /* now that we know how many files there are
1056 ** allocate memory for an array to hold dnode pointers
1058 dnp = dnalloc(nfiles);
1059 for (i = 0, cur = dn; i < nfiles; i++) {
1060 dnp[i] = cur; /* save pointer to node in array */
1064 if (all_fmt & DISP_NOLIST) {
1065 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
1067 showfiles(dnp, nfiles);
1069 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1070 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1071 dndirs = countdirs(dnp, nfiles);
1072 dnfiles = nfiles - dndirs;
1074 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
1075 showfiles(dnf, dnfiles);
1076 if (ENABLE_FEATURE_CLEAN_UP)
1080 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
1081 showdirs(dnd, dndirs, dnfiles == 0);
1082 if (ENABLE_FEATURE_CLEAN_UP)
1086 if (ENABLE_FEATURE_CLEAN_UP)