Initial public busybox upstream commit
[busybox4maemo] / coreutils / ls.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * tiny-ls.c version 0.1.0: A minimalist 'ls'
4  * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7  */
8
9 /*
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.
14  *
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
17  * it more portable.
18  *
19  * KNOWN BUGS:
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
23  *
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
28  * PORTABILITY:
29  * 1. requires lstat (BSD) - how do you do it without?
30  */
31
32 #include <getopt.h>
33 #include "libbb.h"
34
35 /* This is a NOEXEC applet. Be very careful! */
36
37
38 enum {
39
40 TERMINAL_WIDTH  = 80,           /* use 79 if terminal has linefold bug */
41 COLUMN_GAP      = 2,            /* includes the file type char */
42
43 /* what is the overall style of the listing */
44 STYLE_COLUMNS   = 1 << 21,      /* fill columns */
45 STYLE_LONG      = 2 << 21,      /* one record per line, extended info */
46 STYLE_SINGLE    = 3 << 21,      /* one record per line */
47 STYLE_MASK      = STYLE_SINGLE,
48
49 /* 51306 lrwxrwxrwx  1 root     root         2 May 11 01:43 /bin/view -> vi* */
50 /* what file information will be listed */
51 LIST_INO        = 1 << 0,
52 LIST_BLOCKS     = 1 << 1,
53 LIST_MODEBITS   = 1 << 2,
54 LIST_NLINKS     = 1 << 3,
55 LIST_ID_NAME    = 1 << 4,
56 LIST_ID_NUMERIC = 1 << 5,
57 LIST_CONTEXT    = 1 << 6,
58 LIST_SIZE       = 1 << 7,
59 LIST_DEV        = 1 << 8,
60 LIST_DATE_TIME  = 1 << 9,
61 LIST_FULLTIME   = 1 << 10,
62 LIST_FILENAME   = 1 << 11,
63 LIST_SYMLINK    = 1 << 12,
64 LIST_FILETYPE   = 1 << 13,
65 LIST_EXEC       = 1 << 14,
66 LIST_MASK       = (LIST_EXEC << 1) - 1,
67
68 /* what files will be displayed */
69 DISP_DIRNAME    = 1 << 15,      /* 2 or more items? label directories */
70 DISP_HIDDEN     = 1 << 16,      /* show filenames starting with . */
71 DISP_DOT        = 1 << 17,      /* show . and .. */
72 DISP_NOLIST     = 1 << 18,      /* show directory as itself, not contents */
73 DISP_RECURSIVE  = 1 << 19,      /* show directory and everything below it */
74 DISP_ROWS       = 1 << 20,      /* print across rows */
75 DISP_MASK       = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
76
77 /* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
78 SORT_FORWARD    = 0,            /* sort in reverse order */
79 SORT_REVERSE    = 1 << 27,      /* sort in reverse order */
80
81 SORT_NAME       = 0,            /* sort by file name */
82 SORT_SIZE       = 1 << 28,      /* sort by file size */
83 SORT_ATIME      = 2 << 28,      /* sort by last access time */
84 SORT_CTIME      = 3 << 28,      /* sort by last change time */
85 SORT_MTIME      = 4 << 28,      /* sort by last modification time */
86 SORT_VERSION    = 5 << 28,      /* sort by version */
87 SORT_EXT        = 6 << 28,      /* sort by file name extension */
88 SORT_DIR        = 7 << 28,      /* sort by file or directory */
89 SORT_MASK       = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
90
91 /* which of the three times will be used */
92 TIME_CHANGE     = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
93 TIME_ACCESS     = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
94 TIME_MASK       = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
95
96 FOLLOW_LINKS    = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
97
98 LS_DISP_HR      = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
99
100 LIST_SHORT      = LIST_FILENAME,
101 LIST_LONG       = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
102                   LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
103
104 SPLIT_DIR       = 1,
105 SPLIT_FILE      = 0,
106 SPLIT_SUBDIR    = 2,
107
108 };
109
110 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
111 #define TYPECHAR(mode)  ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
112 #define APPCHAR(mode)   ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
113 #define COLOR(mode)     ("\000\043\043\043\042\000\043\043"\
114                          "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
115 #define ATTR(mode)      ("\00\00\01\00\01\00\01\00"\
116                          "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
117
118 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
119 #if ENABLE_FEATURE_LS_COLOR
120 static smallint show_color;
121 /* long option entry used only for --color, which has no short option
122  * equivalent */
123 static const char ls_color_opt[] ALIGN1 =
124         "color\0" Optional_argument "\xff" /* no short equivalent */
125         ;
126 #else
127 enum { show_color = 0 };
128 #endif
129
130 /*
131  * a directory entry and its stat info are stored here
132  */
133 struct dnode {                  /* the basic node */
134         const char *name;             /* the dir entry name */
135         const char *fullname;         /* the dir entry name */
136         int   allocated;
137         struct stat dstat;      /* the file stat info */
138         USE_SELINUX(security_context_t sid;)
139         struct dnode *next;     /* point at the next node */
140 };
141 typedef struct dnode dnode_t;
142
143 static struct dnode **list_dir(const char *);
144 static struct dnode **dnalloc(int);
145 static int list_single(struct dnode *);
146
147 static unsigned all_fmt;
148
149 #if ENABLE_FEATURE_AUTOWIDTH
150 static unsigned tabstops = COLUMN_GAP;
151 static unsigned terminal_width = TERMINAL_WIDTH;
152 #else
153 enum {
154         tabstops = COLUMN_GAP,
155         terminal_width = TERMINAL_WIDTH,
156 };
157 #endif
158
159 static int status = EXIT_SUCCESS;
160
161 static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
162 {
163         struct stat dstat;
164         struct dnode *cur;
165         USE_SELINUX(security_context_t sid = NULL;)
166
167         if ((all_fmt & FOLLOW_LINKS) || force_follow) {
168 #if ENABLE_SELINUX
169                 if (is_selinux_enabled())  {
170                          getfilecon(fullname, &sid);
171                 }
172 #endif
173                 if (stat(fullname, &dstat)) {
174                         bb_simple_perror_msg(fullname);
175                         status = EXIT_FAILURE;
176                         return 0;
177                 }
178         } else {
179 #if ENABLE_SELINUX
180                 if (is_selinux_enabled()) {
181                         lgetfilecon(fullname, &sid);
182                 }
183 #endif
184                 if (lstat(fullname, &dstat)) {
185                         bb_simple_perror_msg(fullname);
186                         status = EXIT_FAILURE;
187                         return 0;
188                 }
189         }
190
191         cur = xmalloc(sizeof(struct dnode));
192         cur->fullname = fullname;
193         cur->name = name;
194         cur->dstat = dstat;
195         USE_SELINUX(cur->sid = sid;)
196         return cur;
197 }
198
199 #if ENABLE_FEATURE_LS_COLOR
200 static char fgcolor(mode_t mode)
201 {
202         /* Check wheter the file is existing (if so, color it red!) */
203         if (errno == ENOENT)
204                 return '\037';
205         if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
206                 return COLOR(0xF000);   /* File is executable ... */
207         return COLOR(mode);
208 }
209
210 static char bgcolor(mode_t mode)
211 {
212         if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
213                 return ATTR(0xF000);    /* File is executable ... */
214         return ATTR(mode);
215 }
216 #endif
217
218 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
219 static char append_char(mode_t mode)
220 {
221         if (!(all_fmt & LIST_FILETYPE))
222                 return '\0';
223         if (S_ISDIR(mode))
224                 return '/';
225         if (!(all_fmt & LIST_EXEC))
226                 return '\0';
227         if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
228                 return '*';
229         return APPCHAR(mode);
230 }
231 #endif
232
233 #define countdirs(A, B) count_dirs((A), (B), 1)
234 #define countsubdirs(A, B) count_dirs((A), (B), 0)
235 static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
236 {
237         int i, dirs;
238
239         if (!dn)
240                 return 0;
241         dirs = 0;
242         for (i = 0; i < nfiles; i++) {
243                 const char *name;
244                 if (!S_ISDIR(dn[i]->dstat.st_mode))
245                         continue;
246                 name = dn[i]->name;
247                 if (notsubdirs
248                  || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
249                 ) {
250                         dirs++;
251                 }
252         }
253         return dirs;
254 }
255
256 static int countfiles(struct dnode **dnp)
257 {
258         int nfiles;
259         struct dnode *cur;
260
261         if (dnp == NULL)
262                 return 0;
263         nfiles = 0;
264         for (cur = dnp[0]; cur->next; cur = cur->next)
265                 nfiles++;
266         nfiles++;
267         return nfiles;
268 }
269
270 /* get memory to hold an array of pointers */
271 static struct dnode **dnalloc(int num)
272 {
273         if (num < 1)
274                 return NULL;
275
276         return xzalloc(num * sizeof(struct dnode *));
277 }
278
279 #if ENABLE_FEATURE_LS_RECURSIVE
280 static void dfree(struct dnode **dnp, int nfiles)
281 {
282         int i;
283
284         if (dnp == NULL)
285                 return;
286
287         for (i = 0; i < nfiles; i++) {
288                 struct dnode *cur = dnp[i];
289                 if (cur->allocated)
290                         free((char*)cur->fullname);     /* free the filename */
291                 free(cur);              /* free the dnode */
292         }
293         free(dnp);                      /* free the array holding the dnode pointers */
294 }
295 #else
296 #define dfree(...) ((void)0)
297 #endif
298
299 static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
300 {
301         int dncnt, i, d;
302         struct dnode **dnp;
303
304         if (dn == NULL || nfiles < 1)
305                 return NULL;
306
307         /* count how many dirs and regular files there are */
308         if (which == SPLIT_SUBDIR)
309                 dncnt = countsubdirs(dn, nfiles);
310         else {
311                 dncnt = countdirs(dn, nfiles);  /* assume we are looking for dirs */
312                 if (which == SPLIT_FILE)
313                         dncnt = nfiles - dncnt; /* looking for files */
314         }
315
316         /* allocate a file array and a dir array */
317         dnp = dnalloc(dncnt);
318
319         /* copy the entrys into the file or dir array */
320         for (d = i = 0; i < nfiles; i++) {
321                 if (S_ISDIR(dn[i]->dstat.st_mode)) {
322                         const char *name;
323                         if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
324                                 continue;
325                         name = dn[i]->name;
326                         if ((which & SPLIT_DIR)
327                          || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
328                         ) {
329                                 dnp[d++] = dn[i];
330                         }
331                 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
332                         dnp[d++] = dn[i];
333                 }
334         }
335         return dnp;
336 }
337
338 #if ENABLE_FEATURE_LS_SORTFILES
339 static int sortcmp(const void *a, const void *b)
340 {
341         struct dnode *d1 = *(struct dnode **)a;
342         struct dnode *d2 = *(struct dnode **)b;
343         unsigned sort_opts = all_fmt & SORT_MASK;
344         int dif;
345
346         dif = 0; /* assume SORT_NAME */
347         // TODO: use pre-initialized function pointer
348         // instead of branch forest
349         if (sort_opts == SORT_SIZE) {
350                 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
351         } else if (sort_opts == SORT_ATIME) {
352                 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
353         } else if (sort_opts == SORT_CTIME) {
354                 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
355         } else if (sort_opts == SORT_MTIME) {
356                 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
357         } else if (sort_opts == SORT_DIR) {
358                 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
359                 /* } else if (sort_opts == SORT_VERSION) { */
360                 /* } else if (sort_opts == SORT_EXT) { */
361         }
362
363         if (dif == 0) {
364                 /* sort by name - may be a tie_breaker for time or size cmp */
365                 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
366                 else dif = strcmp(d1->name, d2->name);
367         }
368
369         if (all_fmt & SORT_REVERSE) {
370                 dif = -dif;
371         }
372         return dif;
373 }
374
375 static void dnsort(struct dnode **dn, int size)
376 {
377         qsort(dn, size, sizeof(*dn), sortcmp);
378 }
379 #else
380 #define dnsort(dn, size) ((void)0)
381 #endif
382
383
384 static void showfiles(struct dnode **dn, int nfiles)
385 {
386         int i, ncols, nrows, row, nc;
387         int column = 0;
388         int nexttab = 0;
389         int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
390
391         if (dn == NULL || nfiles < 1)
392                 return;
393
394         if (all_fmt & STYLE_LONG) {
395                 ncols = 1;
396         } else {
397                 /* find the longest file name, use that as the column width */
398                 for (i = 0; i < nfiles; i++) {
399                         int len = strlen(dn[i]->name);
400                         if (column_width < len)
401                                 column_width = len;
402                 }
403                 column_width += tabstops +
404                         USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
405                                      ((all_fmt & LIST_INO) ? 8 : 0) +
406                                      ((all_fmt & LIST_BLOCKS) ? 5 : 0);
407                 ncols = (int) (terminal_width / column_width);
408         }
409
410         if (ncols > 1) {
411                 nrows = nfiles / ncols;
412                 if (nrows * ncols < nfiles)
413                         nrows++;                /* round up fractionals */
414         } else {
415                 nrows = nfiles;
416                 ncols = 1;
417         }
418
419         for (row = 0; row < nrows; row++) {
420                 for (nc = 0; nc < ncols; nc++) {
421                         /* reach into the array based on the column and row */
422                         i = (nc * nrows) + row; /* assume display by column */
423                         if (all_fmt & DISP_ROWS)
424                                 i = (row * ncols) + nc; /* display across row */
425                         if (i < nfiles) {
426                                 if (column > 0) {
427                                         nexttab -= column;
428                                         printf("%*s", nexttab, "");
429                                         column += nexttab;
430                                 }
431                                 nexttab = column + column_width;
432                                 column += list_single(dn[i]);
433                         }
434                 }
435                 putchar('\n');
436                 column = 0;
437         }
438 }
439
440
441 static void showdirs(struct dnode **dn, int ndirs, int first)
442 {
443         int i, nfiles;
444         struct dnode **subdnp;
445         int dndirs;
446         struct dnode **dnd;
447
448         if (dn == NULL || ndirs < 1)
449                 return;
450
451         for (i = 0; i < ndirs; i++) {
452                 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
453                         if (!first)
454                                 bb_putchar('\n');
455                         first = 0;
456                         printf("%s:\n", dn[i]->fullname);
457                 }
458                 subdnp = list_dir(dn[i]->fullname);
459                 nfiles = countfiles(subdnp);
460                 if (nfiles > 0) {
461                         /* list all files at this level */
462                         dnsort(subdnp, nfiles);
463                         showfiles(subdnp, nfiles);
464                         if (ENABLE_FEATURE_LS_RECURSIVE) {
465                                 if (all_fmt & DISP_RECURSIVE) {
466                                         /* recursive- list the sub-dirs */
467                                         dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
468                                         dndirs = countsubdirs(subdnp, nfiles);
469                                         if (dndirs > 0) {
470                                                 dnsort(dnd, dndirs);
471                                                 showdirs(dnd, dndirs, 0);
472                                                 /* free the array of dnode pointers to the dirs */
473                                                 free(dnd);
474                                         }
475                                 }
476                                 /* free the dnodes and the fullname mem */
477                                 dfree(subdnp, nfiles);
478                         }
479                 }
480         }
481 }
482
483
484 static struct dnode **list_dir(const char *path)
485 {
486         struct dnode *dn, *cur, **dnp;
487         struct dirent *entry;
488         DIR *dir;
489         int i, nfiles;
490
491         if (path == NULL)
492                 return NULL;
493
494         dn = NULL;
495         nfiles = 0;
496         dir = warn_opendir(path);
497         if (dir == NULL) {
498                 status = EXIT_FAILURE;
499                 return NULL;    /* could not open the dir */
500         }
501         while ((entry = readdir(dir)) != NULL) {
502                 char *fullname;
503
504                 /* are we going to list the file- it may be . or .. or a hidden file */
505                 if (entry->d_name[0] == '.') {
506                         if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
507                          && !(all_fmt & DISP_DOT)
508                         ) {
509                                 continue;
510                         }
511                         if (!(all_fmt & DISP_HIDDEN))
512                                 continue;
513                 }
514                 fullname = concat_path_file(path, entry->d_name);
515                 cur = my_stat(fullname, bb_basename(fullname), 0);
516                 if (!cur) {
517                         free(fullname);
518                         continue;
519                 }
520                 cur->allocated = 1;
521                 cur->next = dn;
522                 dn = cur;
523                 nfiles++;
524         }
525         closedir(dir);
526
527         /* now that we know how many files there are
528          * allocate memory for an array to hold dnode pointers
529          */
530         if (dn == NULL)
531                 return NULL;
532         dnp = dnalloc(nfiles);
533         for (i = 0, cur = dn; i < nfiles; i++) {
534                 dnp[i] = cur;   /* save pointer to node in array */
535                 cur = cur->next;
536         }
537
538         return dnp;
539 }
540
541
542 #if ENABLE_FEATURE_LS_TIMESTAMPS
543 /* Do time() just once. Saves one syscall per file for "ls -l" */
544 /* Initialized in main() */
545 static time_t current_time_t;
546 #endif
547
548 static int list_single(struct dnode *dn)
549 {
550         int i, column = 0;
551
552 #if ENABLE_FEATURE_LS_TIMESTAMPS
553         char *filetime;
554         time_t ttime, age;
555 #endif
556 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
557         struct stat info;
558         char append;
559 #endif
560
561         if (dn->fullname == NULL)
562                 return 0;
563
564 #if ENABLE_FEATURE_LS_TIMESTAMPS
565         ttime = dn->dstat.st_mtime;     /* the default time */
566         if (all_fmt & TIME_ACCESS)
567                 ttime = dn->dstat.st_atime;
568         if (all_fmt & TIME_CHANGE)
569                 ttime = dn->dstat.st_ctime;
570         filetime = ctime(&ttime);
571 #endif
572 #if ENABLE_FEATURE_LS_FILETYPES
573         append = append_char(dn->dstat.st_mode);
574 #endif
575
576         for (i = 0; i <= 31; i++) {
577                 switch (all_fmt & (1 << i)) {
578                 case LIST_INO:
579                         column += printf("%7ld ", (long) dn->dstat.st_ino);
580                         break;
581                 case LIST_BLOCKS:
582                         column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
583                         break;
584                 case LIST_MODEBITS:
585                         column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
586                         break;
587                 case LIST_NLINKS:
588                         column += printf("%4ld ", (long) dn->dstat.st_nlink);
589                         break;
590                 case LIST_ID_NAME:
591 #if ENABLE_FEATURE_LS_USERNAME
592                         printf("%-8.8s %-8.8s",
593                                 get_cached_username(dn->dstat.st_uid),
594                                 get_cached_groupname(dn->dstat.st_gid));
595                         column += 17;
596                         break;
597 #endif
598                 case LIST_ID_NUMERIC:
599                         column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
600                         break;
601                 case LIST_SIZE:
602                 case LIST_DEV:
603                         if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
604                                 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
605                                            (int) minor(dn->dstat.st_rdev));
606                         } else {
607                                 if (all_fmt & LS_DISP_HR) {
608                                         column += printf("%9s ",
609                                                 make_human_readable_str(dn->dstat.st_size, 1, 0));
610                                 } else {
611                                         column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
612                                 }
613                         }
614                         break;
615 #if ENABLE_FEATURE_LS_TIMESTAMPS
616                 case LIST_FULLTIME:
617                         printf("%24.24s ", filetime);
618                         column += 25;
619                         break;
620                 case LIST_DATE_TIME:
621                         if ((all_fmt & LIST_FULLTIME) == 0) {
622                                 /* current_time_t ~== time(NULL) */
623                                 age = current_time_t - ttime;
624                                 printf("%6.6s ", filetime + 4);
625                                 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
626                                         /* hh:mm if less than 6 months old */
627                                         printf("%5.5s ", filetime + 11);
628                                 } else {
629                                         printf(" %4.4s ", filetime + 20);
630                                 }
631                                 column += 13;
632                         }
633                         break;
634 #endif
635 #if ENABLE_SELINUX
636                 case LIST_CONTEXT:
637                         {
638                                 char context[80];
639                                 int len = 0;
640
641                                 if (dn->sid) {
642                                         /* I assume sid initilized with NULL */
643                                         len = strlen(dn->sid) + 1;
644                                         safe_strncpy(context, dn->sid, len);
645                                         freecon(dn->sid);
646                                 } else {
647                                         safe_strncpy(context, "unknown", 8);
648                                 }
649                                 printf("%-32s ", context);
650                                 column += MAX(33, len);
651                         }
652                         break;
653 #endif
654                 case LIST_FILENAME:
655                         errno = 0;
656 #if ENABLE_FEATURE_LS_COLOR
657                         if (show_color && !lstat(dn->fullname, &info)) {
658                                 printf("\033[%d;%dm", bgcolor(info.st_mode),
659                                                 fgcolor(info.st_mode));
660                         }
661 #endif
662                         column += printf("%s", dn->name);
663                         if (show_color) {
664                                 printf("\033[0m");
665                         }
666                         break;
667                 case LIST_SYMLINK:
668                         if (S_ISLNK(dn->dstat.st_mode)) {
669                                 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
670                                 if (!lpath) break;
671                                 printf(" -> ");
672 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
673                                 if (!stat(dn->fullname, &info)) {
674                                         append = append_char(info.st_mode);
675                                 }
676 #endif
677 #if ENABLE_FEATURE_LS_COLOR
678                                 if (show_color) {
679                                         errno = 0;
680                                         printf("\033[%d;%dm", bgcolor(info.st_mode),
681                                                    fgcolor(info.st_mode));
682                                 }
683 #endif
684                                 column += printf("%s", lpath) + 4;
685                                 if (show_color) {
686                                         printf("\033[0m");
687                                 }
688                                 free(lpath);
689                         }
690                         break;
691 #if ENABLE_FEATURE_LS_FILETYPES
692                 case LIST_FILETYPE:
693                         if (append) {
694                                 putchar(append);
695                                 column++;
696                         }
697                         break;
698 #endif
699                 }
700         }
701
702         return column;
703 }
704
705 /* "[-]Cadil1", POSIX mandated options, busybox always supports */
706 /* "[-]gnsx", POSIX non-mandated options, busybox always supports */
707 /* "[-]Ak" GNU options, busybox always supports */
708 /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
709 /* "[-]p", POSIX non-mandated options, busybox optionally supports */
710 /* "[-]SXvThw", GNU options, busybox optionally supports */
711 /* "[-]K", SELinux mandated options, busybox optionally supports */
712 /* "[-]e", I think we made this one up */
713 static const char ls_options[] ALIGN1 =
714         "Cadil1gnsxAk"
715         USE_FEATURE_LS_TIMESTAMPS("cetu")
716         USE_FEATURE_LS_SORTFILES("SXrv")
717         USE_FEATURE_LS_FILETYPES("Fp")
718         USE_FEATURE_LS_FOLLOWLINKS("L")
719         USE_FEATURE_LS_RECURSIVE("R")
720         USE_FEATURE_HUMAN_READABLE("h")
721         USE_SELINUX("K")
722         USE_FEATURE_AUTOWIDTH("T:w:")
723         USE_SELINUX("Z");
724
725 enum {
726         LIST_MASK_TRIGGER       = 0,
727         STYLE_MASK_TRIGGER      = STYLE_MASK,
728         DISP_MASK_TRIGGER       = DISP_ROWS,
729         SORT_MASK_TRIGGER       = SORT_MASK,
730 };
731
732 static const unsigned opt_flags[] = {
733         LIST_SHORT | STYLE_COLUMNS, /* C */
734         DISP_HIDDEN | DISP_DOT,     /* a */
735         DISP_NOLIST,                /* d */
736         LIST_INO,                   /* i */
737         LIST_LONG | STYLE_LONG,     /* l - remember LS_DISP_HR in mask! */
738         LIST_SHORT | STYLE_SINGLE,  /* 1 */
739         0,                          /* g - ingored */
740         LIST_ID_NUMERIC,            /* n */
741         LIST_BLOCKS,                /* s */
742         DISP_ROWS,                  /* x */
743         DISP_HIDDEN,                /* A */
744         ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
745 #if ENABLE_FEATURE_LS_TIMESTAMPS
746         TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME),   /* c */
747         LIST_FULLTIME,              /* e */
748         ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME,   /* t */
749         TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME),   /* u */
750 #endif
751 #if ENABLE_FEATURE_LS_SORTFILES
752         SORT_SIZE,                  /* S */
753         SORT_EXT,                   /* X */
754         SORT_REVERSE,               /* r */
755         SORT_VERSION,               /* v */
756 #endif
757 #if ENABLE_FEATURE_LS_FILETYPES
758         LIST_FILETYPE | LIST_EXEC,  /* F */
759         LIST_FILETYPE,              /* p */
760 #endif
761 #if ENABLE_FEATURE_LS_FOLLOWLINKS
762         FOLLOW_LINKS,               /* L */
763 #endif
764 #if ENABLE_FEATURE_LS_RECURSIVE
765         DISP_RECURSIVE,             /* R */
766 #endif
767 #if ENABLE_FEATURE_HUMAN_READABLE
768         LS_DISP_HR,                 /* h */
769 #endif
770 #if ENABLE_SELINUX
771         LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
772 #endif
773 #if ENABLE_FEATURE_AUTOWIDTH
774         0, 0,                       /* T, w - ignored */
775 #endif
776 #if ENABLE_SELINUX
777         LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
778 #endif
779         (1U<<31)
780 };
781
782
783 /* THIS IS A "SAFE" APPLET, main() MAY BE CALLED INTERNALLY FROM SHELL */
784 /* BE CAREFUL! */
785
786 int ls_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
787 int ls_main(int argc, char **argv)
788 {
789         struct dnode **dnd;
790         struct dnode **dnf;
791         struct dnode **dnp;
792         struct dnode *dn;
793         struct dnode *cur;
794         unsigned opt;
795         int nfiles = 0;
796         int dnfiles;
797         int dndirs;
798         int oi;
799         int ac;
800         int i;
801         char **av;
802         USE_FEATURE_LS_COLOR(char *color_opt;)
803
804 #if ENABLE_FEATURE_LS_TIMESTAMPS
805         time(&current_time_t);
806 #endif
807
808         all_fmt = LIST_SHORT |
809                 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
810
811 #if ENABLE_FEATURE_AUTOWIDTH
812         /* Obtain the terminal width */
813         get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
814         /* Go one less... */
815         terminal_width--;
816 #endif
817
818         /* process options */
819         USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
820 #if ENABLE_FEATURE_AUTOWIDTH
821         opt_complementary = "T+:w+"; /* -T N, -w N */
822         opt = getopt32(argv, ls_options, &tabstops, &terminal_width
823                                 USE_FEATURE_LS_COLOR(, &color_opt));
824 #else
825         opt = getopt32(argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
826 #endif
827         for (i = 0; opt_flags[i] != (1U<<31); i++) {
828                 if (opt & (1 << i)) {
829                         unsigned flags = opt_flags[i];
830
831                         if (flags & LIST_MASK_TRIGGER)
832                                 all_fmt &= ~LIST_MASK;
833                         if (flags & STYLE_MASK_TRIGGER)
834                                 all_fmt &= ~STYLE_MASK;
835                         if (flags & SORT_MASK_TRIGGER)
836                                 all_fmt &= ~SORT_MASK;
837                         if (flags & DISP_MASK_TRIGGER)
838                                 all_fmt &= ~DISP_MASK;
839                         if (flags & TIME_MASK)
840                                 all_fmt &= ~TIME_MASK;
841                         if (flags & LIST_CONTEXT)
842                                 all_fmt |= STYLE_SINGLE;
843                         /* huh?? opt cannot be 'l' */
844                         //if (LS_DISP_HR && opt == 'l')
845                         //      all_fmt &= ~LS_DISP_HR;
846                         all_fmt |= flags;
847                 }
848         }
849
850 #if ENABLE_FEATURE_LS_COLOR
851         /* find color bit value - last position for short getopt */
852         if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
853                 char *p = getenv("LS_COLORS");
854                 /* LS_COLORS is unset, or (not empty && not "none") ? */
855                 if (!p || (p[0] && strcmp(p, "none")))
856                         show_color = 1;
857         }
858         if (opt & (1 << i)) {  /* next flag after short options */
859                 if (!color_opt || !strcmp("always", color_opt))
860                         show_color = 1;
861                 else if (color_opt && !strcmp("never", color_opt))
862                         show_color = 0;
863                 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
864                         show_color = 1;
865         }
866 #endif
867
868         /* sort out which command line options take precedence */
869         if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
870                 all_fmt &= ~DISP_RECURSIVE;     /* no recurse if listing only dir */
871         if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
872                 if (all_fmt & TIME_CHANGE)
873                         all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
874                 if (all_fmt & TIME_ACCESS)
875                         all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
876         }
877         if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
878                 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
879         if (ENABLE_FEATURE_LS_USERNAME)
880                 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
881                         all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
882
883         /* choose a display format */
884         if (!(all_fmt & STYLE_MASK))
885                 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
886
887         /*
888          * when there are no cmd line args we have to supply a default "." arg.
889          * we will create a second argv array, "av" that will hold either
890          * our created "." arg, or the real cmd line args.  The av array
891          * just holds the pointers- we don't move the date the pointers
892          * point to.
893          */
894         ac = argc - optind;     /* how many cmd line args are left */
895         if (ac < 1) {
896                 static const char *const dotdir[] = { "." };
897
898                 av = (char **) dotdir;
899                 ac = 1;
900         } else {
901                 av = argv + optind;
902         }
903
904         /* now, everything is in the av array */
905         if (ac > 1)
906                 all_fmt |= DISP_DIRNAME;        /* 2 or more items? label directories */
907
908         /* stuff the command line file names into a dnode array */
909         dn = NULL;
910         for (oi = 0; oi < ac; oi++) {
911                 /* ls w/o -l follows links on command line */
912                 cur = my_stat(av[oi], av[oi], !(all_fmt & STYLE_LONG));
913                 if (!cur)
914                         continue;
915                 cur->allocated = 0;
916                 cur->next = dn;
917                 dn = cur;
918                 nfiles++;
919         }
920
921         /* now that we know how many files there are
922          * allocate memory for an array to hold dnode pointers
923          */
924         dnp = dnalloc(nfiles);
925         for (i = 0, cur = dn; i < nfiles; i++) {
926                 dnp[i] = cur;   /* save pointer to node in array */
927                 cur = cur->next;
928         }
929
930         if (all_fmt & DISP_NOLIST) {
931                 dnsort(dnp, nfiles);
932                 if (nfiles > 0)
933                         showfiles(dnp, nfiles);
934         } else {
935                 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
936                 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
937                 dndirs = countdirs(dnp, nfiles);
938                 dnfiles = nfiles - dndirs;
939                 if (dnfiles > 0) {
940                         dnsort(dnf, dnfiles);
941                         showfiles(dnf, dnfiles);
942                         if (ENABLE_FEATURE_CLEAN_UP)
943                                 free(dnf);
944                 }
945                 if (dndirs > 0) {
946                         dnsort(dnd, dndirs);
947                         showdirs(dnd, dndirs, dnfiles == 0);
948                         if (ENABLE_FEATURE_CLEAN_UP)
949                                 free(dnd);
950                 }
951         }
952         if (ENABLE_FEATURE_CLEAN_UP)
953                 dfree(dnp, nfiles);
954         return status;
955 }