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 * Deviations from posix:
9 * add -b (and default to it instead of -q for an unambiguous representation
10 * that doesn't cause collisions)
11 * add -Z -ll --color
12 * Posix says the -l date format should vary based on how recent it is
13 * and we do --time-style=long-iso instead
14 * ignore -k because we default to 1024 byte blocks
15
16 USE_LS(NEWTOY(ls, "(sort):(color):;(full-time)(show-control-chars)\241(group-directories-first)\376ZgoACFHLNRSUXabcdfhikl@mnpqrstuw#=80<0x1[-Cxm1][-Cxml][-Cxmo][-Cxmg][-cu][-ftS][-HL][-Nqb]", TOYFLAG_BIN|TOYFLAG_LOCALE))
17
18 config LS
19 bool "ls"
20 default y
21 help
22 usage: ls [-1ACFHLNRSUXZabcdfghilmnopqrstuwx] [--color[=auto]] [FILE...]
23
24 List files
25
26 what to show:
27 -A all files except . and .. -a all files including .hidden
28 -b escape nongraphic chars -d directory, not contents
29 -F append /dir *exe @sym |FIFO -f files (no sort/filter/format)
30 -H follow command line symlinks -i inode number
31 -L follow symlinks -N no escaping, even on tty
32 -p put '/' after dir names -q unprintable chars as '?'
33 -R recursively list in subdirs -s storage used (1024 byte units)
34 -Z security context
35
36 output formats:
37 -1 list one file per line -C columns (sorted vertically)
38 -g like -l but no owner -h human readable sizes
39 -l long (show full details) -ll long with nanoseconds (--full-time)
40 -m comma separated -n long with numeric uid/gid
41 -o long without group column -r reverse order
42 -w set column width -x columns (horizontal sort)
43
44 sort by: (also --sort=longname,longname... ends with alphabetical)
45 -c ctime -r reverse -S size -t time -u atime -U none
46 -X extension -! dirfirst -~ nocase
47
48 --color =always (default) =auto (when stdout is tty) =never
49 exe=green suid=red suidfile=redback stickydir=greenback
50 device=yellow symlink=turquoise/red dir=blue socket=purple
51
52 Long output uses -cu for display, use -ltc/-ltu to also sort by ctime/atime.
53 */
54
55 #define FOR_ls
56 #include "toys.h"
57
58 // test sst output (suid/sticky in ls flaglist)
59
60 // ls -lR starts .: then ./subdir:
61
62 GLOBALS(
63 long w, l;
64 char *color, *sort;
65
66 struct dirtree *files, *singledir;
67 unsigned screen_width;
68 int nl_title;
69 char *escmore;
70 )
71
72 // Callback from crunch_str to represent unprintable chars
crunch_qb(FILE * out,int cols,int wc)73 static int crunch_qb(FILE *out, int cols, int wc)
74 {
75 int len = 1;
76 char buf[32];
77
78 if (FLAG(q)) *buf = '?';
79 else {
80 if (wc<256) *buf = wc;
81 // scrute the inscrutable, eff the ineffable, print the unprintable
82 else if ((len = wcrtomb(buf, wc, 0) ) == -1) len = 1;
83 if (FLAG(b)) {
84 char *to = buf, *from = buf+24;
85 int i, j;
86
87 memcpy(from, to, 8);
88 for (i = 0; i<len; i++) {
89 *to++ = '\\';
90 if (strchr(TT.escmore, from[i])) *to++ = from[i];
91 else if (-1 != (j = stridx("\\\a\b\e\f\n\r\t\v", from[i])))
92 *to++ = "\\abefnrtv"[j];
93 else to += sprintf(to, "%03o", from[i]);
94 }
95 len = to-buf;
96 }
97 }
98
99 if (cols<len) len = cols;
100 if (out) fwrite(buf, len, 1, out);
101
102 return len;
103 }
104
105 // Returns wcwidth(utf8) version of strlen with -qb escapes
strwidth(char * s)106 static int strwidth(char *s)
107 {
108 return crunch_str(&s, INT_MAX, 0, TT.escmore, crunch_qb);
109 }
110
endtype(struct stat * st)111 static char endtype(struct stat *st)
112 {
113 mode_t mode = st->st_mode;
114 if ((FLAG(F)||FLAG(p)) && S_ISDIR(mode)) return '/';
115 if (FLAG(F)) {
116 if (S_ISLNK(mode)) return '@';
117 if (S_ISREG(mode) && (mode&0111)) return '*';
118 if (S_ISFIFO(mode)) return '|';
119 if (S_ISSOCK(mode)) return '=';
120 }
121 return 0;
122 }
123
numlen(long long ll)124 static int numlen(long long ll)
125 {
126 return snprintf(0, 0, "%llu", ll);
127 }
128
print_with_h(char * s,long long value,int units)129 static int print_with_h(char *s, long long value, int units)
130 {
131 if (FLAG(h)) return human_readable(s, value*units, 0);
132 else return sprintf(s, "%lld", value);
133 }
134
135 // Figure out size of printable entry fields for display indent/wrap
136
entrylen(struct dirtree * dt,unsigned * len)137 static void entrylen(struct dirtree *dt, unsigned *len)
138 {
139 struct stat *st = &(dt->st);
140 char tmp[64];
141
142 *len = strwidth(dt->name);
143 if (endtype(st)) ++*len;
144 if (FLAG(m)) ++*len;
145
146 len[1] = FLAG(i) ? numlen(st->st_ino) : 0;
147 if (FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)) {
148 len[2] = numlen(st->st_nlink);
149 len[3] = FLAG(n) ? numlen(st->st_uid) : strwidth(getusername(st->st_uid));
150 len[4] = FLAG(n) ? numlen(st->st_gid) : strwidth(getgroupname(st->st_gid));
151 if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
152 // cheating slightly here: assuming minor is always 3 digits to avoid
153 // tracking another column
154 len[5] = numlen(dev_major(st->st_rdev))+5;
155 } else len[5] = print_with_h(tmp, st->st_size, 1);
156 }
157
158 len[6] = FLAG(s) ? print_with_h(tmp, st->st_blocks, 1024) : 0;
159 len[7] = FLAG(Z) ? strwidth((char *)dt->extra) : 0;
160 }
161
162 // Perform one or more comparisons on a pair of files.
163 // Reused FLAG_a to mean "alphabetical"
do_compare(struct dirtree * a,struct dirtree * b,long flags)164 static int do_compare(struct dirtree *a, struct dirtree *b, long flags)
165 {
166 struct timespec *ts1 = 0, *ts2;
167 char *s1, *s2;
168 int ret;
169
170 // TODO -? nocase -! dirfirst
171
172 if (flags&FLAG_S) {
173 if (a->st.st_size > b->st.st_size) return -1;
174 else if (a->st.st_size < b->st.st_size) return 1;
175 }
176
177 if (flags&FLAG_t) ts1 = &a->st.st_mtim, ts2 = &b->st.st_mtim;
178 if (flags&FLAG_u) ts1 = &a->st.st_atim, ts2 = &b->st.st_atim;
179 if (flags&FLAG_c) ts1 = &a->st.st_ctim, ts2 = &b->st.st_ctim;
180 if (ts1) {
181 if (ts1->tv_sec > ts2->tv_sec) return -1;
182 else if (ts1->tv_sec < ts2->tv_sec) return 1;
183 else if (ts1->tv_nsec > ts2->tv_nsec) return -1;
184 else if (ts1->tv_nsec < ts2->tv_nsec) return 1;
185 }
186 if (flags&FLAG_X21) // dirfirst
187 if (S_ISDIR(a->st.st_mode)!=S_ISDIR(b->st.st_mode))
188 return S_ISDIR(a->st.st_mode) ? -1 : 1;
189
190 // -X is a form of alphabetical sort, without -~ do case sensitive comparison
191 if ((flags&FLAG_X) && (s1 = strrchr(a->name, '.')) && (s2 = strrchr(b->name, '.'))) {
192 if (!(flags&FLAG_X7E)) flags |= FLAG_a;
193 } else {
194 s1 = a->name;
195 s2 = b->name;
196 }
197
198 // case insensitive sort falls back to case sensitive sort when equal
199 ret = (flags&FLAG_X7E) ? strcasecmp(s1, s2) : 0;
200 if (!ret && (flags&FLAG_a)) ret = strcmp(s1, s2);
201
202 return ret;
203 }
204
comma_start(char ** aa,char * b)205 int comma_start(char **aa, char *b)
206 {
207 return strstart(aa, b) && (!**aa || **aa==',');
208 }
209
210 // callback for qsort
compare(void * a,void * b)211 static int compare(void *a, void *b)
212 {
213 struct dirtree *dta = *(struct dirtree **)a;
214 struct dirtree *dtb = *(struct dirtree **)b;
215 char *ss = TT.sort;
216 long long ll = 0;
217 int ret = 0;
218
219 // TODO: test --sort=reverse with fallback alphabetical
220
221 if (ss) while (*ss) {
222 if (comma_start(&ss, "reverse")) toys.optflags |= FLAG_r;
223 else if (comma_start(&ss, "none")) goto skip;
224 else if (ret) continue;
225 else if (comma_start(&ss, "ctime")) ll = FLAG_c;
226 else if (comma_start(&ss, "size")) ll = FLAG_S;
227 else if (comma_start(&ss, "time")) ll = FLAG_t;
228 else if (comma_start(&ss, "atime")) ll = FLAG_u;
229 else if (comma_start(&ss, "nocase")) ll = FLAG_X7E;
230 else if (comma_start(&ss, "extension")) ll = FLAG_X;
231 else if (comma_start(&ss, "dirfirst")) ll = FLAG_X21;
232 else error_exit("bad --sort %s", ss);
233
234 ret = do_compare(dta, dtb, ll);
235 }
236
237 if (!ret) ret = do_compare(dta, dtb, toys.optflags|FLAG_a);
238 skip:
239 if (FLAG(r)) ret *= -1;
240
241 return ret;
242 }
243
244 // callback from dirtree_recurse() determining how to handle this entry.
245
filter(struct dirtree * new)246 static int filter(struct dirtree *new)
247 {
248 // Special case to handle enormous dirs without running out of memory.
249 if (toys.optflags == (FLAG_1|FLAG_f)) {
250 xprintf("%s\n", new->name);
251 return 0;
252 }
253
254 if (FLAG(Z)) {
255 if (!CFG_TOYBOX_LSM_NONE) {
256 // Linux doesn't support fgetxattr(2) on O_PATH file descriptors (though
257 // bionic works around that), and there are no *xattrat(2) calls, so we
258 // just use lgetxattr(2).
259 char *path = dirtree_path(new, 0);
260
261 (FLAG(L) ? lsm_get_context : lsm_lget_context)(path,(char **)&new->extra);
262 free(path);
263 }
264 if (CFG_TOYBOX_LSM_NONE || !new->extra) new->extra = (long)xstrdup("?");
265 }
266
267 if (FLAG(u)) new->st.st_mtime = new->st.st_atime;
268 if (FLAG(c)) new->st.st_mtime = new->st.st_ctime;
269 new->st.st_blocks >>= 1; // Use 1KiB blocks rather than 512B blocks.
270
271 if (FLAG(a)||FLAG(f)) return DIRTREE_SAVE;
272 if (!FLAG(A) && *new->name=='.') return 0;
273
274 return dirtree_notdotdot(new) & DIRTREE_SAVE;
275 }
276
277 // For column view, calculate horizontal position (for padding) and return
278 // index of next entry to display.
279
next_column(unsigned long ul,unsigned long dtlen,unsigned columns,unsigned * xpos)280 static unsigned long next_column(unsigned long ul, unsigned long dtlen,
281 unsigned columns, unsigned *xpos)
282 {
283 unsigned height, extra;
284
285 // Horizontal sort is easy
286 if (!FLAG(C)) {
287 *xpos = ul % columns;
288 return ul;
289 }
290
291 // vertical sort (-x), uneven rounding goes along right edge
292 height = (dtlen+columns-1)/columns; // round up
293 extra = dtlen%height; // how many rows are wider?
294 if (extra && ul >= extra*columns) ul -= extra*columns--;
295 else extra = 0;
296
297 return (*xpos = ul % columns)*height + extra + ul/columns;
298 }
299
color_from_mode(mode_t mode)300 static int color_from_mode(mode_t mode)
301 {
302 int color = 0;
303
304 if (S_ISDIR(mode)) color = 256+34;
305 else if (S_ISLNK(mode)) color = 256+36;
306 else if (S_ISBLK(mode) || S_ISCHR(mode)) color = 256+33;
307 else if (S_ISREG(mode) && (mode&0111)) color = 256+32;
308 else if (S_ISFIFO(mode)) color = 33;
309 else if (S_ISSOCK(mode)) color = 256+35;
310
311 return color;
312 }
313
zprint(int zap,char * pat,int len,unsigned long arg)314 static void zprint(int zap, char *pat, int len, unsigned long arg)
315 {
316 char tmp[32];
317
318 sprintf(tmp, "%%*%s", zap ? "s" : pat);
319 if (zap && pat[strlen(pat)-1]==' ') strcat(tmp, " ");
320 printf(tmp, len, zap ? (unsigned long)"?" : arg);
321 }
322
323 // Display a list of dirtree entries, according to current format
324 // Output types -1, -l, -C, or stream
325
listfiles(int dirfd,struct dirtree * indir)326 static void listfiles(int dirfd, struct dirtree *indir)
327 {
328 struct dirtree *dt, **sort;
329 unsigned long dtlen, ul = 0;
330 unsigned width, totals[8], len[8], totpad = 0,
331 *colsizes = (unsigned *)toybuf, columns = sizeof(toybuf)/4;
332 char tmp[64];
333
334 if (-1 == dirfd) {
335 perror_msg_raw(indir->name);
336
337 return;
338 }
339
340 memset(totals, 0, sizeof(totals));
341 memset(len, 0, sizeof(len));
342
343 // Top level directory was already populated by main()
344 if (!indir->parent) {
345 // Silently descend into single directory listed by itself on command line.
346 // In this case only show dirname/total header when given -R.
347 dt = indir->child;
348 if (dt && S_ISDIR(dt->st.st_mode) && !dt->next && !(FLAG(d)||FLAG(R))) {
349 listfiles(open(dt->name, 0), TT.singledir = dt);
350
351 return;
352 }
353
354 // Do preprocessing (Dirtree didn't populate, so callback wasn't called.)
355 for (;dt; dt = dt->next) filter(dt);
356 if (toys.optflags == (FLAG_1|FLAG_f)) return;
357 // Read directory contents. We dup() the fd because this will close it.
358 // This reads/saves contents to display later, except for in "ls -1f" mode.
359 } else dirtree_recurse(indir, filter, dirfd,
360 DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*FLAG(L));
361
362 // Copy linked list to array and sort it. Directories go in array because
363 // we visit them in sorted order too. (The nested loops let us measure and
364 // fill with the same inner loop.)
365 for (sort = 0;;sort = xmalloc(dtlen*sizeof(void *))) {
366 for (dtlen = 0, dt = indir->child; dt; dt = dt->next, dtlen++)
367 if (sort) sort[dtlen] = dt;
368 if (sort || !dtlen) break;
369 }
370
371 // Label directory if not top of tree, or if -R
372 if (indir->parent && (TT.singledir!=indir || FLAG(R))) {
373 char *path = dirtree_path(indir, 0);
374
375 if (TT.nl_title++) xputc('\n');
376 xprintf("%s:\n", path);
377 free(path);
378 }
379
380 // Measure each entry to work out whitespace padding and total blocks
381 if (!FLAG(f)) {
382 unsigned long long blocks = 0;
383
384 if (!FLAG(U)) qsort(sort, dtlen, sizeof(void *), (void *)compare);
385 for (ul = 0; ul<dtlen; ul++) {
386 entrylen(sort[ul], len);
387 for (width = 0; width<8; width++)
388 if (len[width]>totals[width]) totals[width] = len[width];
389 blocks += sort[ul]->st.st_blocks;
390 }
391 totpad = totals[1]+!!totals[1]+totals[6]+!!totals[6]+totals[7]+!!totals[7];
392 if ((FLAG(h)||FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)||FLAG(s)) && indir->parent)
393 {
394 print_with_h(tmp, blocks, 1024);
395 xprintf("total %s\n", tmp);
396 }
397 }
398
399 // Find largest entry in each field for display alignment
400 if (FLAG(C)||FLAG(x)) {
401
402 // columns can't be more than toybuf can hold, or more than files,
403 // or > 1/2 screen width (one char filename, one space).
404 if (columns > TT.screen_width/2) columns = TT.screen_width/2;
405 if (columns > dtlen) columns = dtlen;
406
407 // Try to fit as many columns as we can, dropping down by one each time
408 for (;columns > 1; columns--) {
409 unsigned c, cc, totlen = columns;
410
411 memset(colsizes, 0, columns*sizeof(unsigned));
412 for (ul = 0; ul<dtlen; ul++) {
413 cc = next_column(ul, dtlen, columns, &c);
414 if (cc>=dtlen) break; // tilt: remainder bigger than height
415 entrylen(sort[cc], len);
416 if (c<columns-1) *len += totpad+2; // 2 spaces between filenames
417 // Expand this column if necessary, break if that puts us over budget
418 if (*len > colsizes[c]) {
419 totlen += (*len)-colsizes[c];
420 colsizes[c] = *len;
421 if (totlen > TT.screen_width) break;
422 }
423 }
424 // If everything fit, stop here
425 if (ul == dtlen) break;
426 }
427 }
428
429 // Loop through again to produce output.
430 width = 0;
431 for (ul = 0; ul<dtlen; ul++) {
432 int ii, zap;
433 unsigned curcol, lastlen = *len, color = 0;
434 struct stat *st = &((dt = sort[next_column(ul,dtlen,columns,&curcol)])->st);
435 mode_t mode = st->st_mode;
436 char et = endtype(st), *ss;
437
438 // If we couldn't stat, output ? for most fields
439 zap = !st->st_blksize && !st->st_dev && !st->st_ino;
440
441 // Skip directories at the top of the tree when -d isn't set
442 if (S_ISDIR(mode) && !indir->parent && !FLAG(d)) continue;
443 TT.nl_title=1;
444
445 // Handle padding and wrapping for display purposes
446 entrylen(dt, len);
447 if (ul) {
448 if (FLAG(m)) xputc(',');
449 if (FLAG(C)||FLAG(x)) {
450 if (!curcol) xputc('\n');
451 else {
452 if (ul) next_column(ul-1, dtlen, columns, &curcol);
453 printf("%*c", colsizes[ul ? curcol : 0]-lastlen-totpad, ' ');
454 }
455 } else if (FLAG(1) || width+1+*len > TT.screen_width) {
456 xputc('\n');
457 width = 0;
458 } else {
459 xputsn(" "+FLAG(m));
460 width += 2-FLAG(m);
461 }
462 }
463 width += *len;
464
465 if (FLAG(i)) zprint(zap, "lu ", totals[1], st->st_ino);
466
467 if (FLAG(s)) {
468 print_with_h(tmp, st->st_blocks, 1024);
469 zprint(zap, "s ", totals[6], (unsigned long)tmp);
470 }
471
472 if (FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)) {
473 mode_to_string(mode, tmp);
474 if (zap) memset(tmp+1, '?', 9);
475 printf("%s", tmp);
476 zprint(zap, "ld", totals[2]+1, st->st_nlink);
477
478 // print user
479 if (!FLAG(g)) {
480 putchar(' ');
481 ii = -totals[3];
482 if (zap || FLAG(n)) zprint(zap, "lu", ii, st->st_uid);
483 else draw_trim_esc(getusername(st->st_uid), ii, abs(ii), TT.escmore,
484 crunch_qb);
485 }
486
487 // print group
488 if (!FLAG(o)) {
489 putchar(' ');
490 ii = -totals[4];
491 if (zap || FLAG(n)) zprint(zap, "lu", ii, st->st_gid);
492 else draw_trim_esc(getgroupname(st->st_gid), ii, abs(ii), TT.escmore,
493 crunch_qb);
494 }
495 }
496 if (FLAG(Z)) printf(" %-*s "+!FLAG(l), -(int)totals[7], (char *)dt->extra);
497
498 if (FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)) {
499 struct tm *tm;
500
501 // print major/minor, or size
502 if (!zap && (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)))
503 printf("% *d,% 4d", totals[5]-4, dev_major(st->st_rdev),
504 dev_minor(st->st_rdev));
505 else {
506 print_with_h(tmp, st->st_size, 1);
507 zprint(zap, "s", totals[5]+1, (unsigned long)tmp);
508 }
509
510 // print time, always in --time-style=long-iso
511 tm = localtime(&(st->st_mtime));
512 strftime(tmp, sizeof(tmp), " %F %H:%M", tm);
513 if (TT.l>1) {
514 char *s = tmp+strlen(tmp);
515
516 s += sprintf(s, ":%02d.%09d ", tm->tm_sec, (int)st->st_mtim.tv_nsec);
517 strftime(s, sizeof(tmp)-(s-tmp), "%z", tm);
518 }
519 zprint(zap, "s ", 17+(TT.l>1)*13, (unsigned long)tmp);
520 }
521
522 if (FLAG(color)) {
523 color = color_from_mode(st->st_mode);
524 if (color) printf("\e[%d;%dm", color>>8, color&255);
525 }
526
527 ss = dt->name;
528 crunch_str(&ss, INT_MAX, stdout, TT.escmore, crunch_qb);
529 if (color) printf("\e[0m");
530
531 if ((FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)) && S_ISLNK(mode)) {
532 printf(" -> ");
533 if (!zap && FLAG(color)) {
534 struct stat st2;
535
536 if (fstatat(dirfd, dt->symlink, &st2, 0)) color = 256+31;
537 else color = color_from_mode(st2.st_mode);
538
539 if (color) printf("\e[%d;%dm", color>>8, color&255);
540 }
541
542 zprint(zap, "s", 0, (unsigned long)dt->symlink);
543 if (!zap && color) printf("\e[0m");
544 }
545
546 if (et) xputc(et);
547 }
548
549 if (width) xputc('\n');
550
551 // Free directory entries, recursing first if necessary.
552
553 for (ul = 0; ul<dtlen; free(sort[ul++])) {
554 if (FLAG(d) || !S_ISDIR(sort[ul]->st.st_mode)) continue;
555
556 // Recurse into dirs if at top of the tree or given -R
557 if (!indir->parent || (FLAG(R) && dirtree_notdotdot(sort[ul])))
558 listfiles(openat(dirfd, sort[ul]->name, 0), sort[ul]);
559 free((void *)sort[ul]->extra);
560 }
561 free(sort);
562 if (dirfd != AT_FDCWD) close(dirfd);
563 }
564
ls_main(void)565 void ls_main(void)
566 {
567 char **s, *noargs[] = {".", 0};
568 struct dirtree *dt;
569
570 if (FLAG(full_time)) {
571 toys.optflags |= FLAG_l;
572 TT.l = 2;
573 }
574
575 // Do we have an implied -1
576 if (isatty(1)) {
577 if (!FLAG(show_control_chars)) toys.optflags |= FLAG_b;
578 if (FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)) toys.optflags |= FLAG_1;
579 else if (!(FLAG(1)||FLAG(x)||FLAG(m))) toys.optflags |= FLAG_C;
580 } else {
581 if (!FLAG(m)) toys.optflags |= FLAG_1;
582 if (TT.color) toys.optflags ^= FLAG_color;
583 }
584
585 // -N *doesn't* disable -q; you need --show-control-chars for that.
586 if (FLAG(N)) toys.optflags &= ~FLAG_b;
587
588 TT.screen_width = 80;
589 if (FLAG(w)) TT.screen_width = TT.w+2;
590 else terminal_size(&TT.screen_width, NULL);
591 if (TT.screen_width<2) TT.screen_width = 2;
592 if (FLAG(b)) TT.escmore = " \\";
593
594 // The optflags parsing infrastructure should really do this for us,
595 // but currently it has "switch off when this is set", so "-dR" and "-Rd"
596 // behave differently
597 if (FLAG(d)) toys.optflags &= ~FLAG_R;
598
599 // Iterate through command line arguments, collecting directories and files.
600 // Non-absolute paths are relative to current directory. Top of tree is
601 // a dummy node to collect command line arguments into pseudo-directory.
602 TT.files = dirtree_add_node(0, 0, 0);
603 TT.files->dirfd = AT_FDCWD;
604 for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
605 int sym = !(FLAG(l)||FLAG(d)||FLAG(F)) || FLAG(L) || FLAG(H);
606
607 dt = dirtree_add_node(0, *s, DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*sym);
608
609 // note: double_list->prev temporarily goes in dirtree->parent
610 if (dt) {
611 if (dt->again&DIRTREE_STATLESS) {
612 perror_msg_raw(*s);
613 free(dt);
614 } else dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
615 } else toys.exitval = 1;
616 }
617
618 // Convert double_list into dirtree.
619 dlist_terminate(TT.files->child);
620 for (dt = TT.files->child; dt; dt = dt->next) dt->parent = TT.files;
621
622 // Display the files we collected
623 listfiles(AT_FDCWD, TT.files);
624
625 if (CFG_TOYBOX_FREE) free(TT.files);
626 }
627