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 /************************************************************************/
39 #include <sys/types.h>
51 #include <getopt.h> /* struct option */
52 #include <sys/ioctl.h>
53 #include <sys/sysmacros.h> /* major() and minor() */
56 #include <selinux/selinux.h> /* for is_selinux_enabled() */
59 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
63 /* what is the overall style of the listing */
64 #define STYLE_AUTO (0)
65 #define STYLE_COLUMNS (1U<<21) /* fill columns */
66 #define STYLE_LONG (2U<<21) /* one record per line, extended info */
67 #define STYLE_SINGLE (3U<<21) /* one record per line */
69 #define STYLE_MASK STYLE_SINGLE
70 #define STYLE_ONE_RECORD_FLAG STYLE_LONG
72 /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
73 /* what file information will be listed */
74 #define LIST_INO (1U<<0)
75 #define LIST_BLOCKS (1U<<1)
76 #define LIST_MODEBITS (1U<<2)
77 #define LIST_NLINKS (1U<<3)
78 #define LIST_ID_NAME (1U<<4)
79 #define LIST_ID_NUMERIC (1U<<5)
80 #define LIST_CONTEXT (1U<<6)
81 #define LIST_SIZE (1U<<7)
82 #define LIST_DEV (1U<<8)
83 #define LIST_DATE_TIME (1U<<9)
84 #define LIST_FULLTIME (1U<<10)
85 #define LIST_FILENAME (1U<<11)
86 #define LIST_SYMLINK (1U<<12)
87 #define LIST_FILETYPE (1U<<13)
88 #define LIST_EXEC (1U<<14)
90 #define LIST_MASK ((LIST_EXEC << 1) - 1)
92 /* what files will be displayed */
93 /* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */
94 #define DISP_NORMAL (1U<<14) /* show normal filenames */
95 #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
96 #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
97 #define DISP_DOT (1U<<17) /* show . and .. */
98 #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
99 #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
100 #define DISP_ROWS (1U<<20) /* print across rows */
102 #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1))
104 #ifdef CONFIG_FEATURE_LS_SORTFILES
105 /* how will the files be sorted */
106 #define SORT_ORDER_FORWARD 0 /* sort in reverse order */
107 #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
109 #define SORT_NAME 0 /* sort by file name */
110 #define SORT_SIZE (1U<<28) /* sort by file size */
111 #define SORT_ATIME (2U<<28) /* sort by last access time */
112 #define SORT_CTIME (3U<<28) /* sort by last change time */
113 #define SORT_MTIME (4U<<28) /* sort by last modification time */
114 #define SORT_VERSION (5U<<28) /* sort by version */
115 #define SORT_EXT (6U<<28) /* sort by file name extension */
116 #define SORT_DIR (7U<<28) /* sort by file or directory */
118 #define SORT_MASK (7U<<28)
121 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
122 /* which of the three times will be used */
124 #define TIME_CHANGE (1U<<23)
125 #define TIME_ACCESS (1U<<24)
127 #define TIME_MASK (3U<<23)
130 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
131 #define FOLLOW_LINKS (1U<<25)
133 #ifdef CONFIG_FEATURE_HUMAN_READABLE
134 #define LS_DISP_HR (1U<<26)
137 #define LIST_SHORT (LIST_FILENAME)
138 #define LIST_ISHORT (LIST_INO | LIST_FILENAME)
139 #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
140 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
141 #define LIST_ILONG (LIST_INO | LIST_LONG)
145 #define SPLIT_SUBDIR 2
147 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
148 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
150 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
151 # define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
154 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
155 #ifdef CONFIG_FEATURE_LS_COLOR
157 static int show_color = 0;
159 /* long option entry used only for --color, which has no short option
161 static const struct option ls_color_opt[] =
163 {"color", optional_argument, NULL, 1},
167 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
168 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
169 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
170 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
174 * a directory entry and its stat info are stored here
176 struct dnode { /* the basic node */
177 char *name; /* the dir entry name */
178 char *fullname; /* the dir entry name */
180 struct stat dstat; /* the file stat info */
181 #ifdef CONFIG_SELINUX
182 security_context_t sid;
184 struct dnode *next; /* point at the next node */
186 typedef struct dnode dnode_t;
188 static struct dnode **list_dir(const char *);
189 static struct dnode **dnalloc(int);
190 static int list_single(struct dnode *);
192 static unsigned int all_fmt;
194 #ifdef CONFIG_FEATURE_AUTOWIDTH
195 static int terminal_width = TERMINAL_WIDTH;
196 static unsigned short tabstops = COLUMN_GAP;
198 #define tabstops COLUMN_GAP
199 #define terminal_width TERMINAL_WIDTH
202 static int status = EXIT_SUCCESS;
204 static struct dnode *my_stat(char *fullname, char *name)
208 #ifdef CONFIG_SELINUX
209 security_context_t sid=NULL;
213 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
214 if (all_fmt & FOLLOW_LINKS) {
215 #ifdef CONFIG_SELINUX
216 if (is_selinux_enabled()) {
217 rc=0; /* Set the number which means success before hand. */
218 rc = getfilecon(fullname,&sid);
221 rc = stat(fullname, &dstat);
223 bb_perror_msg("%s", fullname);
224 status = EXIT_FAILURE;
230 #ifdef CONFIG_SELINUX
231 if (is_selinux_enabled()) {
232 rc=0; /* Set the number which means success before hand. */
233 rc = lgetfilecon(fullname,&sid);
236 rc = lstat(fullname, &dstat);
238 bb_perror_msg("%s", fullname);
239 status = EXIT_FAILURE;
244 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
245 cur->fullname = fullname;
248 #ifdef CONFIG_SELINUX
254 /*----------------------------------------------------------------------*/
255 #ifdef CONFIG_FEATURE_LS_COLOR
256 static char fgcolor(mode_t mode)
258 /* Check wheter the file is existing (if so, color it red!) */
259 if (errno == ENOENT) {
262 if (LIST_EXEC && S_ISREG(mode)
263 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
264 return COLOR(0xF000); /* File is executable ... */
268 /*----------------------------------------------------------------------*/
269 static char bgcolor(mode_t mode)
271 if (LIST_EXEC && S_ISREG(mode)
272 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
273 return ATTR(0xF000); /* File is executable ... */
278 /*----------------------------------------------------------------------*/
279 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
280 static char append_char(mode_t mode)
282 if (!(all_fmt & LIST_FILETYPE))
284 if ((all_fmt & LIST_EXEC) && S_ISREG(mode)
285 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
287 return APPCHAR(mode);
291 /*----------------------------------------------------------------------*/
293 #define countdirs(A,B) count_dirs((A), (B), 1)
294 #define countsubdirs(A,B) count_dirs((A), (B), 0)
296 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
300 if (dn == NULL || nfiles < 1)
303 for (i = 0; i < nfiles; i++) {
304 if (S_ISDIR(dn[i]->dstat.st_mode)
306 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
307 && ((dn[i]->name[1] != '.')
308 || dn[i]->name[2])))))
314 static int countfiles(struct dnode **dnp)
322 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
328 /* get memory to hold an array of pointers */
329 static struct dnode **dnalloc(int num)
336 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
340 #ifdef CONFIG_FEATURE_LS_RECURSIVE
341 static void dfree(struct dnode **dnp, int nfiles)
348 for (i = 0; i < nfiles; i++) {
349 struct dnode *cur = dnp[i];
351 free(cur->fullname); /* free the filename */
352 free(cur); /* free the dnode */
354 free(dnp); /* free the array holding the dnode pointers */
360 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
365 if (dn == NULL || nfiles < 1)
368 /* count how many dirs and regular files there are */
369 if (which == SPLIT_SUBDIR)
370 dncnt = countsubdirs(dn, nfiles);
372 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
373 if (which == SPLIT_FILE)
374 dncnt = nfiles - dncnt; /* looking for files */
377 /* allocate a file array and a dir array */
378 dnp = dnalloc(dncnt);
380 /* copy the entrys into the file or dir array */
381 for (d = i = 0; i < nfiles; i++) {
382 if (S_ISDIR(dn[i]->dstat.st_mode)) {
383 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
384 if ((which & SPLIT_DIR)
385 || ((dn[i]->name[0] != '.')
387 && ((dn[i]->name[1] != '.')
388 || dn[i]->name[2])))) {
392 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
399 /*----------------------------------------------------------------------*/
400 #ifdef CONFIG_FEATURE_LS_SORTFILES
401 static int sortcmp(const void *a, const void *b)
403 struct dnode *d1 = *(struct dnode **)a;
404 struct dnode *d2 = *(struct dnode **)b;
405 unsigned int sort_opts = all_fmt & SORT_MASK;
408 dif = 0; /* assume SORT_NAME */
409 if (sort_opts == SORT_SIZE) {
410 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
411 } else if (sort_opts == SORT_ATIME) {
412 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
413 } else if (sort_opts == SORT_CTIME) {
414 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
415 } else if (sort_opts == SORT_MTIME) {
416 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
417 } else if (sort_opts == SORT_DIR) {
418 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
419 /* } else if (sort_opts == SORT_VERSION) { */
420 /* } else if (sort_opts == SORT_EXT) { */
424 /* sort by name- may be a tie_breaker for time or size cmp */
425 #ifdef CONFIG_LOCALE_SUPPORT
426 dif = strcoll(d1->name, d2->name);
428 dif = strcmp(d1->name, d2->name);
432 if (all_fmt & SORT_ORDER_REVERSE) {
438 /*----------------------------------------------------------------------*/
439 static void dnsort(struct dnode **dn, int size)
441 qsort(dn, size, sizeof *dn, sortcmp);
445 /*----------------------------------------------------------------------*/
446 static void showfiles(struct dnode **dn, int nfiles)
448 int i, ncols, nrows, row, nc;
451 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
453 if (dn == NULL || nfiles < 1)
456 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
459 /* find the longest file name- use that as the column width */
460 for (i = 0; i < nfiles; i++) {
461 int len = strlen(dn[i]->name) +
462 #ifdef CONFIG_SELINUX
463 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
465 ((all_fmt & LIST_INO) ? 8 : 0) +
466 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
467 if (column_width < len)
470 column_width += tabstops;
471 ncols = (int) (terminal_width / column_width);
475 nrows = nfiles / ncols;
476 if ((nrows * ncols) < nfiles)
477 nrows++; /* round up fractionals */
483 for (row = 0; row < nrows; row++) {
484 for (nc = 0; nc < ncols; nc++) {
485 /* reach into the array based on the column and row */
486 i = (nc * nrows) + row; /* assume display by column */
487 if (all_fmt & DISP_ROWS)
488 i = (row * ncols) + nc; /* display across row */
497 nexttab = column + column_width;
498 column += list_single(dn[i]);
506 /*----------------------------------------------------------------------*/
507 static void showdirs(struct dnode **dn, int ndirs, int first)
510 struct dnode **subdnp;
512 #ifdef CONFIG_FEATURE_LS_RECURSIVE
517 if (dn == NULL || ndirs < 1)
520 for (i = 0; i < ndirs; i++) {
521 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
525 printf("%s:\n", dn[i]->fullname);
527 subdnp = list_dir(dn[i]->fullname);
528 nfiles = countfiles(subdnp);
530 /* list all files at this level */
531 #ifdef CONFIG_FEATURE_LS_SORTFILES
532 dnsort(subdnp, nfiles);
534 showfiles(subdnp, nfiles);
535 #ifdef CONFIG_FEATURE_LS_RECURSIVE
536 if (all_fmt & DISP_RECURSIVE) {
537 /* recursive- list the sub-dirs */
538 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
539 dndirs = countsubdirs(subdnp, nfiles);
541 #ifdef CONFIG_FEATURE_LS_SORTFILES
544 showdirs(dnd, dndirs, 0);
545 free(dnd); /* free the array of dnode pointers to the dirs */
548 dfree(subdnp, nfiles); /* free the dnodes and the fullname mem */
554 /*----------------------------------------------------------------------*/
555 static struct dnode **list_dir(const char *path)
557 struct dnode *dn, *cur, **dnp;
558 struct dirent *entry;
567 dir = bb_opendir(path);
569 status = EXIT_FAILURE;
570 return (NULL); /* could not open the dir */
572 while ((entry = readdir(dir)) != NULL) {
575 /* are we going to list the file- it may be . or .. or a hidden file */
576 if (entry->d_name[0] == '.') {
577 if ((entry->d_name[1] == 0 || (
578 entry->d_name[1] == '.'
579 && entry->d_name[2] == 0))
580 && !(all_fmt & DISP_DOT))
582 if (!(all_fmt & DISP_HIDDEN))
585 fullname = concat_path_file(path, entry->d_name);
586 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
596 /* now that we know how many files there are
597 ** allocate memory for an array to hold dnode pointers
601 dnp = dnalloc(nfiles);
602 for (i = 0, cur = dn; i < nfiles; i++) {
603 dnp[i] = cur; /* save pointer to node in array */
610 /*----------------------------------------------------------------------*/
611 static int list_single(struct dnode *dn)
615 #ifdef CONFIG_FEATURE_LS_USERNAME
618 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
622 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
627 if (dn->fullname == NULL)
630 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
631 ttime = dn->dstat.st_mtime; /* the default time */
632 if (all_fmt & TIME_ACCESS)
633 ttime = dn->dstat.st_atime;
634 if (all_fmt & TIME_CHANGE)
635 ttime = dn->dstat.st_ctime;
636 filetime = ctime(&ttime);
638 #ifdef CONFIG_FEATURE_LS_FILETYPES
639 append = append_char(dn->dstat.st_mode);
642 for (i = 0; i <= 31; i++) {
643 switch (all_fmt & (1 << i)) {
645 column += printf("%7ld ", (long int) dn->dstat.st_ino);
648 #if _FILE_OFFSET_BITS == 64
649 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
651 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
655 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
658 column += printf("%4ld ", (long) dn->dstat.st_nlink);
661 #ifdef CONFIG_FEATURE_LS_USERNAME
662 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
663 printf("%-8.8s ", scratch);
664 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
665 printf("%-8.8s", scratch);
669 case LIST_ID_NUMERIC:
670 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
674 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
675 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
676 (int) minor(dn->dstat.st_rdev));
678 #ifdef CONFIG_FEATURE_HUMAN_READABLE
679 if (all_fmt & LS_DISP_HR) {
680 column += printf("%9s ",
681 make_human_readable_str(dn->dstat.st_size, 1, 0));
685 #if _FILE_OFFSET_BITS == 64
686 column += printf("%9lld ", (long long) dn->dstat.st_size);
688 column += printf("%9ld ", dn->dstat.st_size);
693 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
695 printf("%24.24s ", filetime);
699 if ((all_fmt & LIST_FULLTIME) == 0) {
700 age = time(NULL) - ttime;
701 printf("%6.6s ", filetime + 4);
702 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
703 /* hh:mm if less than 6 months old */
704 printf("%5.5s ", filetime + 11);
706 printf(" %4.4s ", filetime + 20);
712 #ifdef CONFIG_SELINUX
719 /* I assume sid initilized with NULL */
720 len = strlen(dn->sid)+1;
721 safe_strncpy(context, dn->sid, len);
724 safe_strncpy(context, "unknown", 8);
726 printf("%-32s ", context);
727 column += MAX(33, len);
732 #ifdef CONFIG_FEATURE_LS_COLOR
734 if (show_color && !lstat(dn->fullname, &info)) {
735 printf("\033[%d;%dm", bgcolor(info.st_mode),
736 fgcolor(info.st_mode));
739 column += printf("%s", dn->name);
740 #ifdef CONFIG_FEATURE_LS_COLOR
747 if (S_ISLNK(dn->dstat.st_mode)) {
748 char *lpath = xreadlink(dn->fullname);
752 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
753 if (!stat(dn->fullname, &info)) {
754 append = append_char(info.st_mode);
757 #ifdef CONFIG_FEATURE_LS_COLOR
760 printf("\033[%d;%dm", bgcolor(info.st_mode),
761 fgcolor(info.st_mode));
764 column += printf("%s", lpath) + 4;
765 #ifdef CONFIG_FEATURE_LS_COLOR
774 #ifdef CONFIG_FEATURE_LS_FILETYPES
776 if (append != '\0') {
777 printf("%1c", append);
788 /*----------------------------------------------------------------------*/
790 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
791 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
792 /* "[-]Ak" GNU options, busybox always supports */
793 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
794 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
795 /* "[-]SXvThw", GNU options, busybox optionally supports */
796 /* "[-]K", SELinux mandated options, busybox optionally supports */
797 /* "[-]e", I think we made this one up */
799 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
800 # define LS_STR_TIMESTAMPS "cetu"
802 # define LS_STR_TIMESTAMPS ""
805 #ifdef CONFIG_FEATURE_LS_SORTFILES
806 # define LS_STR_SORTFILES "SXrv"
808 # define LS_STR_SORTFILES ""
811 #ifdef CONFIG_FEATURE_LS_FILETYPES
812 # define LS_STR_FILETYPES "Fp"
814 # define LS_STR_FILETYPES ""
817 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
818 # define LS_STR_FOLLOW_LINKS "L"
820 # define LS_STR_FOLLOW_LINKS ""
823 #ifdef CONFIG_FEATURE_LS_RECURSIVE
824 # define LS_STR_RECURSIVE "R"
826 # define LS_STR_RECURSIVE ""
829 #ifdef CONFIG_FEATURE_HUMAN_READABLE
830 # define LS_STR_HUMAN_READABLE "h"
832 # define LS_STR_HUMAN_READABLE ""
835 #ifdef CONFIG_SELINUX
836 # define LS_STR_SELINUX "K"
838 # define LS_STR_SELINUX ""
841 #ifdef CONFIG_FEATURE_AUTOWIDTH
842 # define LS_STR_AUTOWIDTH "T:w:"
844 # define LS_STR_AUTOWIDTH ""
847 static const char ls_options[]="Cadil1gnsxAk" \
851 LS_STR_FOLLOW_LINKS \
853 LS_STR_HUMAN_READABLE \
857 #define LIST_MASK_TRIGGER 0
858 #define STYLE_MASK_TRIGGER STYLE_MASK
859 #define SORT_MASK_TRIGGER SORT_MASK
860 #define DISP_MASK_TRIGGER DISP_ROWS
861 #define TIME_MASK_TRIGGER TIME_MASK
863 static const unsigned opt_flags[] = {
864 LIST_SHORT | STYLE_COLUMNS, /* C */
865 DISP_HIDDEN | DISP_DOT, /* a */
868 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
869 LIST_SHORT | STYLE_SINGLE, /* 1 */
871 LIST_ID_NUMERIC, /* n */
875 #ifdef CONFIG_SELINUX
876 LIST_CONTEXT, /* k */
880 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
881 # ifdef CONFIG_FEATURE_LS_SORTFILES
882 TIME_CHANGE | SORT_CTIME, /* c */
886 LIST_FULLTIME, /* e */
887 # ifdef CONFIG_FEATURE_LS_SORTFILES
890 0, /* t - ignored -- is this correct? */
892 # ifdef CONFIG_FEATURE_LS_SORTFILES
893 TIME_ACCESS | SORT_ATIME, /* u */
898 #ifdef CONFIG_FEATURE_LS_SORTFILES
901 SORT_ORDER_REVERSE, /* r */
902 SORT_VERSION, /* v */
904 #ifdef CONFIG_FEATURE_LS_FILETYPES
905 LIST_FILETYPE | LIST_EXEC, /* F */
906 LIST_FILETYPE, /* p */
908 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
909 FOLLOW_LINKS, /* L */
911 #ifdef CONFIG_FEATURE_LS_RECURSIVE
912 DISP_RECURSIVE, /* R */
914 #ifdef CONFIG_FEATURE_HUMAN_READABLE
917 #ifdef CONFIG_SELINUX
918 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
920 #ifdef CONFIG_FEATURE_AUTOWIDTH
921 0, 0, /* T, w - ignored */
927 /*----------------------------------------------------------------------*/
929 int ls_main(int argc, char **argv)
944 #ifdef CONFIG_FEATURE_AUTOWIDTH
945 char *tabstops_str = NULL;
946 char *terminal_width_str = NULL;
948 #ifdef CONFIG_FEATURE_LS_COLOR
952 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
953 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
956 #ifdef CONFIG_FEATURE_LS_SORTFILES
957 | SORT_NAME | SORT_ORDER_FORWARD
961 #ifdef CONFIG_FEATURE_AUTOWIDTH
962 /* Obtain the terminal width. */
963 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
968 #ifdef CONFIG_FEATURE_LS_COLOR
969 bb_applet_long_options = ls_color_opt;
972 /* process options */
973 #ifdef CONFIG_FEATURE_AUTOWIDTH
974 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
975 #ifdef CONFIG_FEATURE_LS_COLOR
980 tabstops = atoi(tabstops_str);
982 if (terminal_width_str) {
983 terminal_width = atoi(terminal_width_str);
986 opt = bb_getopt_ulflags(argc, argv, ls_options
987 #ifdef CONFIG_FEATURE_LS_COLOR
992 for (i = 0; opt_flags[i] != (1U<<31); i++) {
993 if (opt & (1 << i)) {
994 unsigned int flags = opt_flags[i];
996 if (flags & LIST_MASK_TRIGGER) {
997 all_fmt &= ~LIST_MASK;
999 if (flags & STYLE_MASK_TRIGGER) {
1000 all_fmt &= ~STYLE_MASK;
1002 #ifdef CONFIG_FEATURE_LS_SORTFILES
1003 if (flags & SORT_MASK_TRIGGER) {
1004 all_fmt &= ~SORT_MASK;
1007 if (flags & DISP_MASK_TRIGGER) {
1008 all_fmt &= ~DISP_MASK;
1010 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
1011 if (flags & TIME_MASK_TRIGGER) {
1012 all_fmt &= ~TIME_MASK;
1015 if (flags & LIST_CONTEXT) {
1016 all_fmt |= STYLE_SINGLE;
1018 #ifdef CONFIG_FEATURE_HUMAN_READABLE
1020 all_fmt &= ~LS_DISP_HR;
1027 #ifdef CONFIG_FEATURE_LS_COLOR
1029 /* find color bit value - last position for short getopt */
1031 #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
1034 if ((p = getenv ("LS_COLORS")) != NULL &&
1035 (*p == '\0' || (strcmp(p, "none") == 0))) {
1037 } else if (isatty(STDOUT_FILENO)) {
1042 if((opt & (1 << i))) { /* next flag after short options */
1043 if (color_opt == NULL || strcmp("always", color_opt) == 0)
1045 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
1047 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
1053 /* sort out which command line options take precedence */
1054 #ifdef CONFIG_FEATURE_LS_RECURSIVE
1055 if (all_fmt & DISP_NOLIST)
1056 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
1058 #if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
1059 if (all_fmt & TIME_CHANGE)
1060 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1061 if (all_fmt & TIME_ACCESS)
1062 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
1064 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1065 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
1066 #ifdef CONFIG_FEATURE_LS_USERNAME
1067 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1068 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
1071 /* choose a display format */
1072 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
1074 all_fmt = (all_fmt & ~STYLE_MASK)
1075 | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1077 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1081 * when there are no cmd line args we have to supply a default "." arg.
1082 * we will create a second argv array, "av" that will hold either
1083 * our created "." arg, or the real cmd line args. The av array
1084 * just holds the pointers- we don't move the date the pointers
1087 ac = argc - optind; /* how many cmd line args are left */
1089 static const char * const dotdir[] = { "." };
1091 av = (char **) dotdir;
1097 /* now, everything is in the av array */
1099 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
1101 /* stuff the command line file names into an dnode array */
1103 for (oi = 0; oi < ac; oi++) {
1104 cur = my_stat(av[oi], av[oi]);
1113 /* now that we know how many files there are
1114 ** allocate memory for an array to hold dnode pointers
1116 dnp = dnalloc(nfiles);
1117 for (i = 0, cur = dn; i < nfiles; i++) {
1118 dnp[i] = cur; /* save pointer to node in array */
1122 if (all_fmt & DISP_NOLIST) {
1123 #ifdef CONFIG_FEATURE_LS_SORTFILES
1124 dnsort(dnp, nfiles);
1127 showfiles(dnp, nfiles);
1129 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1130 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1131 dndirs = countdirs(dnp, nfiles);
1132 dnfiles = nfiles - dndirs;
1134 #ifdef CONFIG_FEATURE_LS_SORTFILES
1135 dnsort(dnf, dnfiles);
1137 showfiles(dnf, dnfiles);
1138 if (ENABLE_FEATURE_CLEAN_UP)
1142 #ifdef CONFIG_FEATURE_LS_SORTFILES
1143 dnsort(dnd, dndirs);
1145 showdirs(dnd, dndirs, dnfiles == 0);
1146 if (ENABLE_FEATURE_CLEAN_UP)
1150 if (ENABLE_FEATURE_CLEAN_UP)