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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * To achieve a small memory footprint, this version of 'ls' doesn't do any
23 * file sorting, and only has the most essential command line switches
24 * (i.e., the ones I couldn't live without :-) All features which involve
25 * linking in substantial chunks of libc can be disabled.
27 * Although I don't really want to add new features to this program to
28 * keep it small, I *am* interested to receive bug fixes and ways to make
32 * 1. ls -l of a directory doesn't give "total <blocks>" header
33 * 2. ls of a symlink to a directory doesn't list directory contents
34 * 3. hidden files can make column width too large
36 * NON-OPTIMAL BEHAVIOUR:
37 * 1. autowidth reads directories twice
38 * 2. if you do a short directory listing without filetype characters
39 * appended, there's no need to stat each one
41 * 1. requires lstat (BSD) - how do you do it without?
45 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
46 COLUMN_GAP = 2, /* includes the file type char */
49 /************************************************************************/
51 #include <sys/types.h>
63 #include <getopt.h> /* struct option */
64 #include <sys/ioctl.h>
65 #include <sys/sysmacros.h> /* major() and minor() */
68 #include <selinux/selinux.h> /* for is_selinux_enabled() */
71 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
75 /* what is the overall style of the listing */
76 #define STYLE_AUTO (0)
77 #define STYLE_COLUMNS (1U<<21) /* fill columns */
78 #define STYLE_LONG (2U<<21) /* one record per line, extended info */
79 #define STYLE_SINGLE (3U<<21) /* one record per line */
81 #define STYLE_MASK STYLE_SINGLE
82 #define STYLE_ONE_RECORD_FLAG STYLE_LONG
84 /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
85 /* what file information will be listed */
86 #define LIST_INO (1U<<0)
87 #define LIST_BLOCKS (1U<<1)
88 #define LIST_MODEBITS (1U<<2)
89 #define LIST_NLINKS (1U<<3)
90 #define LIST_ID_NAME (1U<<4)
91 #define LIST_ID_NUMERIC (1U<<5)
92 #define LIST_CONTEXT (1U<<6)
93 #define LIST_SIZE (1U<<7)
94 #define LIST_DEV (1U<<8)
95 #define LIST_DATE_TIME (1U<<9)
96 #define LIST_FULLTIME (1U<<10)
97 #define LIST_FILENAME (1U<<11)
98 #define LIST_SYMLINK (1U<<12)
99 #define LIST_FILETYPE (1U<<13)
100 #define LIST_EXEC (1U<<14)
102 #define LIST_MASK ((LIST_EXEC << 1) - 1)
104 /* what files will be displayed */
105 /* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */
106 #define DISP_NORMAL (1U<<14) /* show normal filenames */
107 #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
108 #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
109 #define DISP_DOT (1U<<17) /* show . and .. */
110 #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
111 #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
112 #define DISP_ROWS (1U<<20) /* print across rows */
114 #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1))
116 #ifdef CONFIG_FEATURE_LS_SORTFILES
117 /* how will the files be sorted */
118 #define SORT_ORDER_FORWARD 0 /* sort in reverse order */
119 #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
121 #define SORT_NAME 0 /* sort by file name */
122 #define SORT_SIZE (1U<<28) /* sort by file size */
123 #define SORT_ATIME (2U<<28) /* sort by last access time */
124 #define SORT_CTIME (3U<<28) /* sort by last change time */
125 #define SORT_MTIME (4U<<28) /* sort by last modification time */
126 #define SORT_VERSION (5U<<28) /* sort by version */
127 #define SORT_EXT (6U<<28) /* sort by file name extension */
128 #define SORT_DIR (7U<<28) /* sort by file or directory */
130 #define SORT_MASK (7U<<28)
133 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
134 /* which of the three times will be used */
136 #define TIME_CHANGE (1U<<23)
137 #define TIME_ACCESS (1U<<24)
139 #define TIME_MASK (3U<<23)
142 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
143 #define FOLLOW_LINKS (1U<<25)
145 #ifdef CONFIG_FEATURE_HUMAN_READABLE
146 #define LS_DISP_HR (1U<<26)
149 #define LIST_SHORT (LIST_FILENAME)
150 #define LIST_ISHORT (LIST_INO | LIST_FILENAME)
151 #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
152 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
153 #define LIST_ILONG (LIST_INO | LIST_LONG)
157 #define SPLIT_SUBDIR 2
159 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
160 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
162 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
163 # define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
166 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
167 #ifdef CONFIG_FEATURE_LS_COLOR
169 static int show_color = 0;
171 /* long option entry used only for --color, which has no short option
173 static const struct option ls_color_opt[] =
175 {"color", optional_argument, NULL, 1},
179 #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
180 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
181 #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
182 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
186 * a directory entry and its stat info are stored here
188 struct dnode { /* the basic node */
189 char *name; /* the dir entry name */
190 char *fullname; /* the dir entry name */
192 struct stat dstat; /* the file stat info */
193 #ifdef CONFIG_SELINUX
194 security_context_t sid;
196 struct dnode *next; /* point at the next node */
198 typedef struct dnode dnode_t;
200 static struct dnode **list_dir(const char *);
201 static struct dnode **dnalloc(int);
202 static int list_single(struct dnode *);
204 static unsigned int all_fmt;
206 #ifdef CONFIG_FEATURE_AUTOWIDTH
207 static int terminal_width = TERMINAL_WIDTH;
208 static unsigned short tabstops = COLUMN_GAP;
210 #define tabstops COLUMN_GAP
211 #define terminal_width TERMINAL_WIDTH
214 static int status = EXIT_SUCCESS;
216 static struct dnode *my_stat(char *fullname, char *name)
220 #ifdef CONFIG_SELINUX
221 security_context_t sid=NULL;
225 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
226 if (all_fmt & FOLLOW_LINKS) {
227 #ifdef CONFIG_SELINUX
228 if (is_selinux_enabled()) {
229 rc=0; /* Set the number which means success before hand. */
230 rc = getfilecon(fullname,&sid);
233 rc = stat(fullname, &dstat);
235 bb_perror_msg("%s", fullname);
236 status = EXIT_FAILURE;
242 #ifdef CONFIG_SELINUX
243 if (is_selinux_enabled()) {
244 rc=0; /* Set the number which means success before hand. */
245 rc = lgetfilecon(fullname,&sid);
248 rc = lstat(fullname, &dstat);
250 bb_perror_msg("%s", fullname);
251 status = EXIT_FAILURE;
256 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
257 cur->fullname = fullname;
260 #ifdef CONFIG_SELINUX
266 /*----------------------------------------------------------------------*/
267 #ifdef CONFIG_FEATURE_LS_COLOR
268 static char fgcolor(mode_t mode)
270 /* Check wheter the file is existing (if so, color it red!) */
271 if (errno == ENOENT) {
274 if (LIST_EXEC && S_ISREG(mode)
275 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
276 return COLOR(0xF000); /* File is executable ... */
280 /*----------------------------------------------------------------------*/
281 static char bgcolor(mode_t mode)
283 if (LIST_EXEC && S_ISREG(mode)
284 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
285 return ATTR(0xF000); /* File is executable ... */
290 /*----------------------------------------------------------------------*/
291 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
292 static char append_char(mode_t mode)
294 if (!(all_fmt & LIST_FILETYPE))
296 if ((all_fmt & LIST_EXEC) && S_ISREG(mode)
297 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
299 return APPCHAR(mode);
303 /*----------------------------------------------------------------------*/
305 #define countdirs(A,B) count_dirs((A), (B), 1)
306 #define countsubdirs(A,B) count_dirs((A), (B), 0)
308 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
312 if (dn == NULL || nfiles < 1)
315 for (i = 0; i < nfiles; i++) {
316 if (S_ISDIR(dn[i]->dstat.st_mode)
318 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
319 && ((dn[i]->name[1] != '.')
320 || dn[i]->name[2])))))
326 static int countfiles(struct dnode **dnp)
334 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
340 /* get memory to hold an array of pointers */
341 static struct dnode **dnalloc(int num)
348 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
352 #ifdef CONFIG_FEATURE_LS_RECURSIVE
353 static void dfree(struct dnode **dnp)
355 struct dnode *cur, *next;
361 while (cur != NULL) {
363 free(cur->fullname); /* free the filename */
365 free(cur); /* free the dnode */
368 free(dnp); /* free the array holding the dnode pointers */
372 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
377 if (dn == NULL || nfiles < 1)
380 /* count how many dirs and regular files there are */
381 if (which == SPLIT_SUBDIR)
382 dncnt = countsubdirs(dn, nfiles);
384 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
385 if (which == SPLIT_FILE)
386 dncnt = nfiles - dncnt; /* looking for files */
389 /* allocate a file array and a dir array */
390 dnp = dnalloc(dncnt);
392 /* copy the entrys into the file or dir array */
393 for (d = i = 0; i < nfiles; i++) {
394 if (S_ISDIR(dn[i]->dstat.st_mode)) {
395 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
396 if ((which & SPLIT_DIR)
397 || ((dn[i]->name[0] != '.')
399 && ((dn[i]->name[1] != '.')
400 || dn[i]->name[2])))) {
404 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
411 /*----------------------------------------------------------------------*/
412 #ifdef CONFIG_FEATURE_LS_SORTFILES
413 static int sortcmp(struct dnode *d1, struct dnode *d2)
415 unsigned int sort_opts = all_fmt & SORT_MASK;
418 dif = 0; /* assume SORT_NAME */
419 if (sort_opts == SORT_SIZE) {
420 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
421 } else if (sort_opts == SORT_ATIME) {
422 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
423 } else if (sort_opts == SORT_CTIME) {
424 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
425 } else if (sort_opts == SORT_MTIME) {
426 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
427 } else if (sort_opts == SORT_DIR) {
428 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
429 /* } else if (sort_opts == SORT_VERSION) { */
430 /* } else if (sort_opts == SORT_EXT) { */
434 /* sort by name- may be a tie_breaker for time or size cmp */
435 #ifdef CONFIG_LOCALE_SUPPORT
436 dif = strcoll(d1->name, d2->name);
438 dif = strcmp(d1->name, d2->name);
442 if (all_fmt & SORT_ORDER_REVERSE) {
448 /*----------------------------------------------------------------------*/
449 static void shellsort(struct dnode **dn, int size)
454 /* shell short the array */
455 if (dn == NULL || size < 2)
458 for (gap = size / 2; gap > 0; gap /= 2) {
459 for (i = gap; i < size; i++) {
460 for (j = i - gap; j >= 0; j -= gap) {
461 if (sortcmp(dn[j], dn[j + gap]) <= 0)
463 /* they are out of order, swap them */
473 /*----------------------------------------------------------------------*/
474 static void showfiles(struct dnode **dn, int nfiles)
476 int i, ncols, nrows, row, nc;
479 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
481 if (dn == NULL || nfiles < 1)
484 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
487 /* find the longest file name- use that as the column width */
488 for (i = 0; i < nfiles; i++) {
489 int len = strlen(dn[i]->name) +
490 #ifdef CONFIG_SELINUX
491 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
493 ((all_fmt & LIST_INO) ? 8 : 0) +
494 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
495 if (column_width < len)
498 column_width += tabstops;
499 ncols = (int) (terminal_width / column_width);
503 nrows = nfiles / ncols;
504 if ((nrows * ncols) < nfiles)
505 nrows++; /* round up fractionals */
511 for (row = 0; row < nrows; row++) {
512 for (nc = 0; nc < ncols; nc++) {
513 /* reach into the array based on the column and row */
514 i = (nc * nrows) + row; /* assume display by column */
515 if (all_fmt & DISP_ROWS)
516 i = (row * ncols) + nc; /* display across row */
525 nexttab = column + column_width;
526 column += list_single(dn[i]);
534 /*----------------------------------------------------------------------*/
535 static void showdirs(struct dnode **dn, int ndirs, int first)
538 struct dnode **subdnp;
540 #ifdef CONFIG_FEATURE_LS_RECURSIVE
545 if (dn == NULL || ndirs < 1)
548 for (i = 0; i < ndirs; i++) {
549 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
553 printf("%s:\n", dn[i]->fullname);
555 subdnp = list_dir(dn[i]->fullname);
556 nfiles = countfiles(subdnp);
558 /* list all files at this level */
559 #ifdef CONFIG_FEATURE_LS_SORTFILES
560 shellsort(subdnp, nfiles);
562 showfiles(subdnp, nfiles);
563 #ifdef CONFIG_FEATURE_LS_RECURSIVE
564 if (all_fmt & DISP_RECURSIVE) {
565 /* recursive- list the sub-dirs */
566 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
567 dndirs = countsubdirs(subdnp, nfiles);
569 #ifdef CONFIG_FEATURE_LS_SORTFILES
570 shellsort(dnd, dndirs);
572 showdirs(dnd, dndirs, 0);
573 free(dnd); /* free the array of dnode pointers to the dirs */
576 dfree(subdnp); /* free the dnodes and the fullname mem */
582 /*----------------------------------------------------------------------*/
583 static struct dnode **list_dir(const char *path)
585 struct dnode *dn, *cur, **dnp;
586 struct dirent *entry;
597 bb_perror_msg("%s", path);
598 status = EXIT_FAILURE;
599 return (NULL); /* could not open the dir */
601 while ((entry = readdir(dir)) != NULL) {
604 /* are we going to list the file- it may be . or .. or a hidden file */
605 if (entry->d_name[0] == '.') {
606 if ((entry->d_name[1] == 0 || (
607 entry->d_name[1] == '.'
608 && entry->d_name[2] == 0))
609 && !(all_fmt & DISP_DOT))
611 if (!(all_fmt & DISP_HIDDEN))
614 fullname = concat_path_file(path, entry->d_name);
615 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
625 /* now that we know how many files there are
626 ** allocate memory for an array to hold dnode pointers
630 dnp = dnalloc(nfiles);
631 for (i = 0, cur = dn; i < nfiles; i++) {
632 dnp[i] = cur; /* save pointer to node in array */
639 /*----------------------------------------------------------------------*/
640 static int list_single(struct dnode *dn)
644 #ifdef CONFIG_FEATURE_LS_USERNAME
647 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
651 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
656 if (dn->fullname == NULL)
659 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
660 ttime = dn->dstat.st_mtime; /* the default time */
661 if (all_fmt & TIME_ACCESS)
662 ttime = dn->dstat.st_atime;
663 if (all_fmt & TIME_CHANGE)
664 ttime = dn->dstat.st_ctime;
665 filetime = ctime(&ttime);
667 #ifdef CONFIG_FEATURE_LS_FILETYPES
668 append = append_char(dn->dstat.st_mode);
671 for (i = 0; i <= 31; i++) {
672 switch (all_fmt & (1 << i)) {
674 column += printf("%7ld ", (long int) dn->dstat.st_ino);
677 #if _FILE_OFFSET_BITS == 64
678 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
680 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
684 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
687 column += printf("%4ld ", (long) dn->dstat.st_nlink);
690 #ifdef CONFIG_FEATURE_LS_USERNAME
691 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
692 printf("%-8.8s ", scratch);
693 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
694 printf("%-8.8s", scratch);
698 case LIST_ID_NUMERIC:
699 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
703 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
704 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
705 (int) minor(dn->dstat.st_rdev));
707 #ifdef CONFIG_FEATURE_HUMAN_READABLE
708 if (all_fmt & LS_DISP_HR) {
709 column += printf("%9s ",
710 make_human_readable_str(dn->dstat.st_size, 1, 0));
714 #if _FILE_OFFSET_BITS == 64
715 column += printf("%9lld ", (long long) dn->dstat.st_size);
717 column += printf("%9ld ", dn->dstat.st_size);
722 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
724 printf("%24.24s ", filetime);
728 if ((all_fmt & LIST_FULLTIME) == 0) {
729 age = time(NULL) - ttime;
730 printf("%6.6s ", filetime + 4);
731 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
732 /* hh:mm if less than 6 months old */
733 printf("%5.5s ", filetime + 11);
735 printf(" %4.4s ", filetime + 20);
741 #ifdef CONFIG_SELINUX
748 /* I assume sid initilized with NULL */
749 len = strlen(dn->sid)+1;
750 safe_strncpy(context, dn->sid, len);
753 safe_strncpy(context, "unknown", 8);
755 printf("%-32s ", context);
756 column += MAX(33, len);
761 #ifdef CONFIG_FEATURE_LS_COLOR
763 if (show_color && !lstat(dn->fullname, &info)) {
764 printf("\033[%d;%dm", bgcolor(info.st_mode),
765 fgcolor(info.st_mode));
768 column += printf("%s", dn->name);
769 #ifdef CONFIG_FEATURE_LS_COLOR
776 if (S_ISLNK(dn->dstat.st_mode)) {
777 char *lpath = xreadlink(dn->fullname);
781 #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
782 if (!stat(dn->fullname, &info)) {
783 append = append_char(info.st_mode);
786 #ifdef CONFIG_FEATURE_LS_COLOR
789 printf("\033[%d;%dm", bgcolor(info.st_mode),
790 fgcolor(info.st_mode));
793 column += printf("%s", lpath) + 4;
794 #ifdef CONFIG_FEATURE_LS_COLOR
803 #ifdef CONFIG_FEATURE_LS_FILETYPES
805 if (append != '\0') {
806 printf("%1c", append);
817 /*----------------------------------------------------------------------*/
819 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
820 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
821 /* "[-]Ak" GNU options, busybox always supports */
822 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
823 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
824 /* "[-]SXvThw", GNU options, busybox optionally supports */
825 /* "[-]K", SELinux mandated options, busybox optionally supports */
826 /* "[-]e", I think we made this one up */
828 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
829 # define LS_STR_TIMESTAMPS "cetu"
831 # define LS_STR_TIMESTAMPS ""
834 #ifdef CONFIG_FEATURE_LS_SORTFILES
835 # define LS_STR_SORTFILES "SXrv"
837 # define LS_STR_SORTFILES ""
840 #ifdef CONFIG_FEATURE_LS_FILETYPES
841 # define LS_STR_FILETYPES "Fp"
843 # define LS_STR_FILETYPES ""
846 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
847 # define LS_STR_FOLLOW_LINKS "L"
849 # define LS_STR_FOLLOW_LINKS ""
852 #ifdef CONFIG_FEATURE_LS_RECURSIVE
853 # define LS_STR_RECURSIVE "R"
855 # define LS_STR_RECURSIVE ""
858 #ifdef CONFIG_FEATURE_HUMAN_READABLE
859 # define LS_STR_HUMAN_READABLE "h"
861 # define LS_STR_HUMAN_READABLE ""
864 #ifdef CONFIG_SELINUX
865 # define LS_STR_SELINUX "K"
867 # define LS_STR_SELINUX ""
870 #ifdef CONFIG_FEATURE_AUTOWIDTH
871 # define LS_STR_AUTOWIDTH "T:w:"
873 # define LS_STR_AUTOWIDTH ""
876 static const char ls_options[]="Cadil1gnsxAk" \
880 LS_STR_FOLLOW_LINKS \
882 LS_STR_HUMAN_READABLE \
886 #define LIST_MASK_TRIGGER 0
887 #define STYLE_MASK_TRIGGER STYLE_MASK
888 #define SORT_MASK_TRIGGER SORT_MASK
889 #define DISP_MASK_TRIGGER DISP_ROWS
890 #define TIME_MASK_TRIGGER TIME_MASK
892 static const unsigned opt_flags[] = {
893 LIST_SHORT | STYLE_COLUMNS, /* C */
894 DISP_HIDDEN | DISP_DOT, /* a */
897 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
898 LIST_SHORT | STYLE_SINGLE, /* 1 */
900 LIST_ID_NUMERIC, /* n */
904 #ifdef CONFIG_SELINUX
905 LIST_CONTEXT, /* k */
909 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
910 # ifdef CONFIG_FEATURE_LS_SORTFILES
911 TIME_CHANGE | SORT_CTIME, /* c */
915 LIST_FULLTIME, /* e */
916 # ifdef CONFIG_FEATURE_LS_SORTFILES
919 0, /* t - ignored -- is this correct? */
921 # ifdef CONFIG_FEATURE_LS_SORTFILES
922 TIME_ACCESS | SORT_ATIME, /* u */
927 #ifdef CONFIG_FEATURE_LS_SORTFILES
930 SORT_ORDER_REVERSE, /* r */
931 SORT_VERSION, /* v */
933 #ifdef CONFIG_FEATURE_LS_FILETYPES
934 LIST_FILETYPE | LIST_EXEC, /* F */
935 LIST_FILETYPE, /* p */
937 #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
938 FOLLOW_LINKS, /* L */
940 #ifdef CONFIG_FEATURE_LS_RECURSIVE
941 DISP_RECURSIVE, /* R */
943 #ifdef CONFIG_FEATURE_HUMAN_READABLE
946 #ifdef CONFIG_SELINUX
947 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
949 #ifdef CONFIG_FEATURE_AUTOWIDTH
950 0, 0, /* T, w - ignored */
956 /*----------------------------------------------------------------------*/
958 int ls_main(int argc, char **argv)
973 #ifdef CONFIG_FEATURE_AUTOWIDTH
974 char *tabstops_str = NULL;
975 char *terminal_width_str = NULL;
977 #ifdef CONFIG_FEATURE_LS_COLOR
981 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
982 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
985 #ifdef CONFIG_FEATURE_LS_SORTFILES
986 | SORT_NAME | SORT_ORDER_FORWARD
990 #ifdef CONFIG_FEATURE_AUTOWIDTH
991 /* Obtain the terminal width. */
992 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
997 #ifdef CONFIG_FEATURE_LS_COLOR
998 bb_applet_long_options = ls_color_opt;
1001 /* process options */
1002 #ifdef CONFIG_FEATURE_AUTOWIDTH
1003 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
1004 #ifdef CONFIG_FEATURE_LS_COLOR
1009 tabstops = atoi(tabstops_str);
1011 if (terminal_width_str) {
1012 terminal_width = atoi(terminal_width_str);
1015 opt = bb_getopt_ulflags(argc, argv, ls_options
1016 #ifdef CONFIG_FEATURE_LS_COLOR
1021 for (i = 0; opt_flags[i] != (1U<<31); i++) {
1022 if (opt & (1 << i)) {
1023 unsigned int flags = opt_flags[i];
1025 if (flags & LIST_MASK_TRIGGER) {
1026 all_fmt &= ~LIST_MASK;
1028 if (flags & STYLE_MASK_TRIGGER) {
1029 all_fmt &= ~STYLE_MASK;
1031 #ifdef CONFIG_FEATURE_LS_SORTFILES
1032 if (flags & SORT_MASK_TRIGGER) {
1033 all_fmt &= ~SORT_MASK;
1036 if (flags & DISP_MASK_TRIGGER) {
1037 all_fmt &= ~DISP_MASK;
1039 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
1040 if (flags & TIME_MASK_TRIGGER) {
1041 all_fmt &= ~TIME_MASK;
1044 if (flags & LIST_CONTEXT) {
1045 all_fmt |= STYLE_SINGLE;
1047 #ifdef CONFIG_FEATURE_HUMAN_READABLE
1049 all_fmt &= ~LS_DISP_HR;
1056 #ifdef CONFIG_FEATURE_LS_COLOR
1058 /* find color bit value - last position for short getopt */
1060 #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
1063 if ((p = getenv ("LS_COLORS")) != NULL &&
1064 (*p == '\0' || (strcmp(p, "none") == 0))) {
1066 } else if (isatty(STDOUT_FILENO)) {
1071 if((opt & (1 << i))) { /* next flag after short options */
1072 if (color_opt == NULL || strcmp("always", color_opt) == 0)
1074 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
1076 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
1082 /* sort out which command line options take precedence */
1083 #ifdef CONFIG_FEATURE_LS_RECURSIVE
1084 if (all_fmt & DISP_NOLIST)
1085 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
1087 #if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
1088 if (all_fmt & TIME_CHANGE)
1089 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1090 if (all_fmt & TIME_ACCESS)
1091 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
1093 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1094 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
1095 #ifdef CONFIG_FEATURE_LS_USERNAME
1096 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1097 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
1100 /* choose a display format */
1101 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
1103 all_fmt = (all_fmt & ~STYLE_MASK)
1104 | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1106 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
1110 * when there are no cmd line args we have to supply a default "." arg.
1111 * we will create a second argv array, "av" that will hold either
1112 * our created "." arg, or the real cmd line args. The av array
1113 * just holds the pointers- we don't move the date the pointers
1116 ac = argc - optind; /* how many cmd line args are left */
1118 static const char * const dotdir[] = { "." };
1120 av = (char **) dotdir;
1126 /* now, everything is in the av array */
1128 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
1130 /* stuff the command line file names into an dnode array */
1132 for (oi = 0; oi < ac; oi++) {
1133 cur = my_stat(av[oi], av[oi]);
1142 /* now that we know how many files there are
1143 ** allocate memory for an array to hold dnode pointers
1145 dnp = dnalloc(nfiles);
1146 for (i = 0, cur = dn; i < nfiles; i++) {
1147 dnp[i] = cur; /* save pointer to node in array */
1151 if (all_fmt & DISP_NOLIST) {
1152 #ifdef CONFIG_FEATURE_LS_SORTFILES
1153 shellsort(dnp, nfiles);
1156 showfiles(dnp, nfiles);
1158 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1159 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1160 dndirs = countdirs(dnp, nfiles);
1161 dnfiles = nfiles - dndirs;
1163 #ifdef CONFIG_FEATURE_LS_SORTFILES
1164 shellsort(dnf, dnfiles);
1166 showfiles(dnf, dnfiles);
1169 #ifdef CONFIG_FEATURE_LS_SORTFILES
1170 shellsort(dnd, dndirs);
1172 showdirs(dnd, dndirs, dnfiles == 0);