• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ls.c - list files
2  *
3  * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4  * Copyright 2012 Rob Landley <rob@landley.net>
5  *
6  * See http://opengroup.org/onlinepubs/9699919799/utilities/ls.html
7 
8 USE_LS(NEWTOY(ls, USE_LS_COLOR("(color):;")"(full-time)(show-control-chars)ZgoACFHLRSabcdfhikl@mnpqrstux1[-Cxm1][-Cxml][-Cxmo][-Cxmg][-cu][-ftS][-HL][!qb]", TOYFLAG_BIN|TOYFLAG_LOCALE))
9 
10 config LS
11   bool "ls"
12   default y
13   help
14     usage: ls [-ACFHLRSZacdfhiklmnpqrstux1] [directory...]
15 
16     list files
17 
18     what to show:
19     -a  all files including .hidden    -b  escape nongraphic chars
20     -c  use ctime for timestamps       -d  directory, not contents
21     -i  inode number                   -p  put a '/' after dir names
22     -q  unprintable chars as '?'       -s  storage used (1024 byte units)
23     -u  use access time for timestamps -A  list all files but . and ..
24     -H  follow command line symlinks   -L  follow symlinks
25     -R  recursively list in subdirs    -F  append /dir *exe @sym |FIFO
26     -Z  security context
27 
28     output formats:
29     -1  list one file per line         -C  columns (sorted vertically)
30     -g  like -l but no owner           -h  human readable sizes
31     -l  long (show full details)       -m  comma separated
32     -n  like -l but numeric uid/gid    -o  like -l but no group
33     -x  columns (horizontal sort)      -ll long with nanoseconds (--full-time)
34 
35     sorting (default is alphabetical):
36     -f  unsorted    -r  reverse    -t  timestamp    -S  size
37 
38 config LS_COLOR
39   bool "ls --color"
40   default y
41   depends on LS
42   help
43     usage: ls --color[=auto]
44 
45     --color  device=yellow  symlink=turquoise/red  dir=blue  socket=purple
46              files: exe=green  suid=red  suidfile=redback  stickydir=greenback
47              =auto means detect if output is a tty.
48 */
49 
50 #define FOR_ls
51 #include "toys.h"
52 
53 // test sst output (suid/sticky in ls flaglist)
54 
55 // ls -lR starts .: then ./subdir:
56 
57 GLOBALS(
58   long ll;
59   char *color;
60 
61   struct dirtree *files, *singledir;
62 
63   unsigned screen_width;
64   int nl_title;
65   char *escmore;
66 )
67 
68 // Callback from crunch_str to represent unprintable chars
crunch_qb(FILE * out,int cols,int wc)69 static int crunch_qb(FILE *out, int cols, int wc)
70 {
71   unsigned len = 1;
72   char buf[32];
73 
74   if (toys.optflags&FLAG_q) *buf = '?';
75   else {
76     if (wc<256) *buf = wc;
77     // scrute the inscrutable, eff the ineffable, print the unprintable
78     else len = wcrtomb(buf, wc, 0);
79     if (toys.optflags&FLAG_b) {
80       char *to = buf, *from = buf+24;
81       int i, j;
82 
83       memcpy(from, to, 8);
84       for (i = 0; i<len; i++) {
85         *to++ = '\\';
86         if (strchr(TT.escmore, from[i])) *to++ = from[i];
87         else if (-1 != (j = stridx("\\\a\b\033\f\n\r\t\v", from[i])))
88           *to++ = "\\abefnrtv"[j];
89         else to += sprintf(to, "%03o", from[i]);
90       }
91       len = to-buf;
92     }
93   }
94 
95   if (cols<len) len = cols;
96   if (out) fwrite(buf, len, 1, out);
97 
98   return len;
99 }
100 
101 // Returns wcwidth(utf8) version of strlen with -qb escapes
strwidth(char * s)102 static int strwidth(char *s)
103 {
104   return crunch_str(&s, INT_MAX, 0, TT.escmore, crunch_qb);
105 }
106 
endtype(struct stat * st)107 static char endtype(struct stat *st)
108 {
109   mode_t mode = st->st_mode;
110   if ((toys.optflags&(FLAG_F|FLAG_p)) && S_ISDIR(mode)) return '/';
111   if (toys.optflags & FLAG_F) {
112     if (S_ISLNK(mode)) return '@';
113     if (S_ISREG(mode) && (mode&0111)) return '*';
114     if (S_ISFIFO(mode)) return '|';
115     if (S_ISSOCK(mode)) return '=';
116   }
117   return 0;
118 }
119 
numlen(long long ll)120 static int numlen(long long ll)
121 {
122   return snprintf(0, 0, "%llu", ll);
123 }
124 
print_with_h(char * s,long long value,int units)125 static int print_with_h(char *s, long long value, int units)
126 {
127   if (toys.optflags&FLAG_h) return human_readable(s, value*units, 0);
128   else return sprintf(s, "%lld", value);
129 }
130 
131 // Figure out size of printable entry fields for display indent/wrap
132 
entrylen(struct dirtree * dt,unsigned * len)133 static void entrylen(struct dirtree *dt, unsigned *len)
134 {
135   struct stat *st = &(dt->st);
136   unsigned flags = toys.optflags;
137   char tmp[64];
138 
139   *len = strwidth(dt->name);
140   if (endtype(st)) ++*len;
141   if (flags & FLAG_m) ++*len;
142 
143   len[1] = (flags & FLAG_i) ? numlen(st->st_ino) : 0;
144   if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
145     unsigned fn = flags & FLAG_n;
146 
147     len[2] = numlen(st->st_nlink);
148     len[3] = fn ? numlen(st->st_uid) : strwidth(getusername(st->st_uid));
149     len[4] = fn ? numlen(st->st_gid) : strwidth(getgroupname(st->st_gid));
150     if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
151       // cheating slightly here: assuming minor is always 3 digits to avoid
152       // tracking another column
153       len[5] = numlen(dev_major(st->st_rdev))+5;
154     } else len[5] = print_with_h(tmp, st->st_size, 1);
155   }
156 
157   len[6] = (flags & FLAG_s) ? print_with_h(tmp, st->st_blocks, 512) : 0;
158   len[7] = (flags & FLAG_Z) ? strwidth((char *)dt->extra) : 0;
159 }
160 
compare(void * a,void * b)161 static int compare(void *a, void *b)
162 {
163   struct dirtree *dta = *(struct dirtree **)a;
164   struct dirtree *dtb = *(struct dirtree **)b;
165   int ret = 0, reverse = (toys.optflags & FLAG_r) ? -1 : 1;
166 
167   if (toys.optflags & FLAG_S) {
168     if (dta->st.st_size > dtb->st.st_size) ret = -1;
169     else if (dta->st.st_size < dtb->st.st_size) ret = 1;
170   }
171   if (toys.optflags & FLAG_t) {
172     if (dta->st.st_mtime > dtb->st.st_mtime) ret = -1;
173     else if (dta->st.st_mtime < dtb->st.st_mtime) ret = 1;
174   }
175   if (!ret) ret = strcmp(dta->name, dtb->name);
176   return ret * reverse;
177 }
178 
179 // callback from dirtree_recurse() determining how to handle this entry.
180 
filter(struct dirtree * new)181 static int filter(struct dirtree *new)
182 {
183   int flags = toys.optflags;
184 
185   // Special case to handle enormous dirs without running out of memory.
186   if (flags == (FLAG_1|FLAG_f)) {
187     xprintf("%s\n", new->name);
188     return 0;
189   }
190 
191   if (flags & FLAG_Z) {
192     if (!CFG_TOYBOX_LSM_NONE) {
193 
194       // (Wouldn't it be nice if the lsm functions worked like openat(),
195       // fchmodat(), mknodat(), readlinkat() so we could do this without
196       // even O_PATH? But no, this is 1990's tech.)
197       int fd = openat(dirtree_parentfd(new), new->name,
198         O_PATH|(O_NOFOLLOW*!(toys.optflags&FLAG_L)));
199 
200       if (fd != -1) {
201         if (-1 == lsm_fget_context(fd, (char **)&new->extra) && errno == EBADF)
202         {
203           char hack[32];
204 
205           // Work around kernel bug that won't let us read this "metadata" from
206           // the filehandle unless we have permission to read the data. (We can
207           // query the same data in by path, but can't do it through an O_PATH
208           // filehandle, because reasons. But for some reason, THIS is ok? If
209           // they ever fix the kernel, this should stop triggering.)
210 
211           sprintf(hack, "/proc/self/fd/%d", fd);
212           lsm_lget_context(hack, (char **)&new->extra);
213         }
214         close(fd);
215       }
216     }
217     if (CFG_TOYBOX_LSM_NONE || !new->extra) new->extra = (long)xstrdup("?");
218   }
219 
220   if (flags & FLAG_u) new->st.st_mtime = new->st.st_atime;
221   if (flags & FLAG_c) new->st.st_mtime = new->st.st_ctime;
222   new->st.st_blocks >>= 1;
223 
224   if (flags & (FLAG_a|FLAG_f)) return DIRTREE_SAVE;
225   if (!(flags & FLAG_A) && new->name[0]=='.') return 0;
226 
227   return dirtree_notdotdot(new) & DIRTREE_SAVE;
228 }
229 
230 // For column view, calculate horizontal position (for padding) and return
231 // index of next entry to display.
232 
next_column(unsigned long ul,unsigned long dtlen,unsigned columns,unsigned * xpos)233 static unsigned long next_column(unsigned long ul, unsigned long dtlen,
234   unsigned columns, unsigned *xpos)
235 {
236   unsigned long transition;
237   unsigned height, widecols;
238 
239   // Horizontal sort is easy
240   if (!(toys.optflags & FLAG_C)) {
241     *xpos = ul % columns;
242     return ul;
243   }
244 
245   // vertical sort
246 
247   // For -x, calculate height of display, rounded up
248   height = (dtlen+columns-1)/columns;
249 
250   // Sanity check: does wrapping render this column count impossible
251   // due to the right edge wrapping eating a whole row?
252   if (height*columns - dtlen >= height) {
253     *xpos = columns;
254     return 0;
255   }
256 
257   // Uneven rounding goes along right edge
258   widecols = dtlen % height;
259   if (!widecols) widecols = height;
260   transition = widecols * columns;
261   if (ul < transition) {
262     *xpos =  ul % columns;
263     return (*xpos*height) + (ul/columns);
264   }
265 
266   ul -= transition;
267   *xpos = ul % (columns-1);
268 
269   return (*xpos*height) + widecols + (ul/(columns-1));
270 }
271 
color_from_mode(mode_t mode)272 static int color_from_mode(mode_t mode)
273 {
274   int color = 0;
275 
276   if (S_ISDIR(mode)) color = 256+34;
277   else if (S_ISLNK(mode)) color = 256+36;
278   else if (S_ISBLK(mode) || S_ISCHR(mode)) color = 256+33;
279   else if (S_ISREG(mode) && (mode&0111)) color = 256+32;
280   else if (S_ISFIFO(mode)) color = 33;
281   else if (S_ISSOCK(mode)) color = 256+35;
282 
283   return color;
284 }
285 
286 // Display a list of dirtree entries, according to current format
287 // Output types -1, -l, -C, or stream
288 
listfiles(int dirfd,struct dirtree * indir)289 static void listfiles(int dirfd, struct dirtree *indir)
290 {
291   struct dirtree *dt, **sort;
292   unsigned long dtlen, ul = 0;
293   unsigned width, flags = toys.optflags, totals[8], len[8], totpad = 0,
294     *colsizes = (unsigned *)toybuf, columns = sizeof(toybuf)/4;
295   char tmp[64];
296 
297   if (-1 == dirfd) {
298     perror_msg_raw(indir->name);
299 
300     return;
301   }
302 
303   memset(totals, 0, sizeof(totals));
304   if (CFG_TOYBOX_ON_ANDROID || CFG_TOYBOX_DEBUG) memset(len, 0, sizeof(len));
305 
306   // Top level directory was already populated by main()
307   if (!indir->parent) {
308     // Silently descend into single directory listed by itself on command line.
309     // In this case only show dirname/total header when given -R.
310     dt = indir->child;
311     if (dt && S_ISDIR(dt->st.st_mode) && !dt->next && !(flags&(FLAG_d|FLAG_R)))
312     {
313       listfiles(open(dt->name, 0), TT.singledir = dt);
314 
315       return;
316     }
317 
318     // Do preprocessing (Dirtree didn't populate, so callback wasn't called.)
319     for (;dt; dt = dt->next) filter(dt);
320     if (flags == (FLAG_1|FLAG_f)) return;
321   // Read directory contents. We dup() the fd because this will close it.
322   // This reads/saves contents to display later, except for in "ls -1f" mode.
323   } else dirtree_recurse(indir, filter, dup(dirfd),
324       DIRTREE_SYMFOLLOW*!!(flags&FLAG_L));
325 
326   // Copy linked list to array and sort it. Directories go in array because
327   // we visit them in sorted order too. (The nested loops let us measure and
328   // fill with the same inner loop.)
329   for (sort = 0;;sort = xmalloc(dtlen*sizeof(void *))) {
330     for (dtlen = 0, dt = indir->child; dt; dt = dt->next, dtlen++)
331       if (sort) sort[dtlen] = dt;
332     if (sort || !dtlen) break;
333   }
334 
335   // Label directory if not top of tree, or if -R
336   if (indir->parent && (TT.singledir!=indir || (flags&FLAG_R)))
337   {
338     char *path = dirtree_path(indir, 0);
339 
340     if (TT.nl_title++) xputc('\n');
341     xprintf("%s:\n", path);
342     free(path);
343   }
344 
345   // Measure each entry to work out whitespace padding and total blocks
346   if (!(flags & FLAG_f)) {
347     unsigned long long blocks = 0;
348 
349     qsort(sort, dtlen, sizeof(void *), (void *)compare);
350     for (ul = 0; ul<dtlen; ul++) {
351       entrylen(sort[ul], len);
352       for (width = 0; width<8; width++)
353         if (len[width]>totals[width]) totals[width] = len[width];
354       blocks += sort[ul]->st.st_blocks;
355     }
356     totpad = totals[1]+!!totals[1]+totals[6]+!!totals[6]+totals[7]+!!totals[7];
357     if ((flags&(FLAG_h|FLAG_l|FLAG_o|FLAG_n|FLAG_g|FLAG_s)) && indir->parent) {
358       print_with_h(tmp, blocks, 512);
359       xprintf("total %s\n", tmp);
360     }
361   }
362 
363   // Find largest entry in each field for display alignment
364   if (flags & (FLAG_C|FLAG_x)) {
365 
366     // columns can't be more than toybuf can hold, or more than files,
367     // or > 1/2 screen width (one char filename, one space).
368     if (columns > TT.screen_width/2) columns = TT.screen_width/2;
369     if (columns > dtlen) columns = dtlen;
370 
371     // Try to fit as many columns as we can, dropping down by one each time
372     for (;columns > 1; columns--) {
373       unsigned c, totlen = columns;
374 
375       memset(colsizes, 0, columns*sizeof(unsigned));
376       for (ul=0; ul<dtlen; ul++) {
377         entrylen(sort[next_column(ul, dtlen, columns, &c)], len);
378         *len += totpad+1;
379         if (c == columns) break;
380         // Expand this column if necessary, break if that puts us over budget
381         if (*len > colsizes[c]) {
382           totlen += (*len)-colsizes[c];
383           colsizes[c] = *len;
384           if (totlen > TT.screen_width) break;
385         }
386       }
387       // If everything fit, stop here
388       if (ul == dtlen) break;
389     }
390   }
391 
392   // Loop through again to produce output.
393   width = 0;
394   for (ul = 0; ul<dtlen; ul++) {
395     int ii;
396     unsigned curcol, color = 0;
397     unsigned long next = next_column(ul, dtlen, columns, &curcol);
398     struct stat *st = &(sort[next]->st);
399     mode_t mode = st->st_mode;
400     char et = endtype(st), *ss;
401 
402     // Skip directories at the top of the tree when -d isn't set
403     if (S_ISDIR(mode) && !indir->parent && !(flags & FLAG_d)) continue;
404     TT.nl_title=1;
405 
406     // Handle padding and wrapping for display purposes
407     entrylen(sort[next], len);
408     if (ul) {
409       int mm = !!(flags & FLAG_m);
410 
411       if (mm) xputc(',');
412       if (flags & (FLAG_C|FLAG_x)) {
413         if (!curcol) xputc('\n');
414       } else if ((flags & FLAG_1) || width+1+*len > TT.screen_width) {
415         xputc('\n');
416         width = 0;
417       } else {
418         printf("  "+mm, 0); // shut up the stupid compiler
419         width += 2-mm;
420       }
421     }
422     width += *len;
423 
424     if (flags & FLAG_i) printf("%*lu ", totals[1], (unsigned long)st->st_ino);
425 
426     if (flags & FLAG_s) {
427       print_with_h(tmp, st->st_blocks, 512);
428       printf("%*s ", totals[6], tmp);
429     }
430 
431     if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
432       struct tm *tm;
433 
434       // (long) is to coerce the st types into something we know we can print.
435       mode_to_string(mode, tmp);
436       printf("%s% *ld", tmp, totals[2]+1, (long)st->st_nlink);
437 
438       // print user
439       if (!(flags&FLAG_g)) {
440         putchar(' ');
441         ii = -totals[3];
442         if (flags&FLAG_n) printf("%*u", ii, (unsigned)st->st_uid);
443         else draw_trim_esc(getusername(st->st_uid), ii, abs(ii), TT.escmore,
444                            crunch_qb);
445       }
446 
447       // print group
448       if (!(flags&FLAG_o)) {
449         putchar(' ');
450         ii = -totals[4];
451         if (flags&FLAG_n) printf("%*u", ii, (unsigned)st->st_gid);
452         else draw_trim_esc(getgroupname(st->st_gid), ii, abs(ii), TT.escmore,
453                            crunch_qb);
454       }
455 
456       if (flags & FLAG_Z)
457         printf(" %-*s", -(int)totals[7], (char *)sort[next]->extra);
458 
459       // print major/minor, or size
460       if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
461         printf("% *d,% 4d", totals[5]-4, dev_major(st->st_rdev),
462           dev_minor(st->st_rdev));
463       else {
464         print_with_h(tmp, st->st_size, 1);
465         printf("%*s", totals[5]+1, tmp);
466       }
467 
468       // print time, always in --time-style=long-iso
469       tm = localtime(&(st->st_mtime));
470       strftime(tmp, sizeof(tmp), "%F %H:%M", tm);
471       if (TT.ll>1) {
472         char *s = tmp+strlen(tmp);
473 
474         s += sprintf(s, ":%02d.%09d ", tm->tm_sec, (int)st->st_mtim.tv_nsec);
475         strftime(s, sizeof(tmp)-(s-tmp), "%z", tm);
476       }
477       printf(" %s ", tmp);
478     } else if (flags & FLAG_Z)
479       printf("%-*s ", (int)totals[7], (char *)sort[next]->extra);
480 
481     if (flags & FLAG_color) {
482       color = color_from_mode(st->st_mode);
483       if (color) printf("\033[%d;%dm", color>>8, color&255);
484     }
485 
486     ss = sort[next]->name;
487     crunch_str(&ss, INT_MAX, stdout, TT.escmore, crunch_qb);
488     if (color) printf("\033[0m");
489 
490     if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode)) {
491       printf(" -> ");
492       if (flags & FLAG_color) {
493         struct stat st2;
494 
495         if (fstatat(dirfd, sort[next]->symlink, &st2, 0)) color = 256+31;
496         else color = color_from_mode(st2.st_mode);
497 
498         if (color) printf("\033[%d;%dm", color>>8, color&255);
499       }
500 
501       printf("%s", sort[next]->symlink);
502       if (color) printf("\033[0m");
503     }
504 
505     if (et) xputc(et);
506 
507     // Pad columns
508     if (flags & (FLAG_C|FLAG_x)) {
509       curcol = colsizes[curcol]-(*len)-totpad;
510       if (curcol < 255) printf("%*c", curcol, ' ');
511     }
512   }
513 
514   if (width) xputc('\n');
515 
516   // Free directory entries, recursing first if necessary.
517 
518   for (ul = 0; ul<dtlen; free(sort[ul++])) {
519     if ((flags & FLAG_d) || !S_ISDIR(sort[ul]->st.st_mode)) continue;
520 
521     // Recurse into dirs if at top of the tree or given -R
522     if (!indir->parent || ((flags&FLAG_R) && dirtree_notdotdot(sort[ul])))
523       listfiles(openat(dirfd, sort[ul]->name, 0), sort[ul]);
524     free((void *)sort[ul]->extra);
525   }
526   free(sort);
527   if (dirfd != AT_FDCWD) close(dirfd);
528 }
529 
ls_main(void)530 void ls_main(void)
531 {
532   char **s, *noargs[] = {".", 0};
533   struct dirtree *dt;
534 
535   if (toys.optflags&FLAG_full_time) {
536     toys.optflags |= FLAG_l;
537     TT.ll = 2;
538   }
539 
540   // Do we have an implied -1
541   if (isatty(1)) {
542     if (!(toys.optflags&FLAG_show_control_chars)) toys.optflags |= FLAG_b;
543     if (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)) toys.optflags |= FLAG_1;
544     else if (!(toys.optflags&(FLAG_1|FLAG_x|FLAG_m))) toys.optflags |= FLAG_C;
545   } else {
546     if (!(toys.optflags & FLAG_m)) toys.optflags |= FLAG_1;
547     if (TT.color) toys.optflags ^= FLAG_color;
548   }
549 
550   TT.screen_width = 80;
551   terminal_size(&TT.screen_width, NULL);
552   if (TT.screen_width<2) TT.screen_width = 2;
553   if (toys.optflags&FLAG_b) TT.escmore = " \\";
554 
555   // The optflags parsing infrastructure should really do this for us,
556   // but currently it has "switch off when this is set", so "-dR" and "-Rd"
557   // behave differently
558   if (toys.optflags & FLAG_d) toys.optflags &= ~FLAG_R;
559 
560   // Iterate through command line arguments, collecting directories and files.
561   // Non-absolute paths are relative to current directory. Top of tree is
562   // a dummy node to collect command line arguments into pseudo-directory.
563   TT.files = dirtree_add_node(0, 0, 0);
564   TT.files->dirfd = AT_FDCWD;
565   for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
566     int sym = !(toys.optflags&(FLAG_l|FLAG_d|FLAG_F))
567       || (toys.optflags&(FLAG_L|FLAG_H));
568 
569     dt = dirtree_add_node(0, *s, DIRTREE_SYMFOLLOW*sym);
570 
571     // note: double_list->prev temporarirly goes in dirtree->parent
572     if (dt) dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
573     else toys.exitval = 1;
574   }
575 
576   // Convert double_list into dirtree.
577   dlist_terminate(TT.files->child);
578   for (dt = TT.files->child; dt; dt = dt->next) dt->parent = TT.files;
579 
580   // Display the files we collected
581   listfiles(AT_FDCWD, TT.files);
582 
583   if (CFG_TOYBOX_FREE) free(TT.files);
584 }
585