1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49#define FOR_ls
50#include "toys.h"
51
52
53
54
55
56GLOBALS(
57 long l;
58 char *color;
59
60 struct dirtree *files, *singledir;
61 unsigned screen_width;
62 int nl_title;
63 char *escmore;
64)
65
66
67static int crunch_qb(FILE *out, int cols, int wc)
68{
69 int len = 1;
70 char buf[32];
71
72 if (FLAG(q)) *buf = '?';
73 else {
74 if (wc<256) *buf = wc;
75
76 else if ((len = wcrtomb(buf, wc, 0) ) == -1) len = 1;
77 if (FLAG(b)) {
78 char *to = buf, *from = buf+24;
79 int i, j;
80
81 memcpy(from, to, 8);
82 for (i = 0; i<len; i++) {
83 *to++ = '\\';
84 if (strchr(TT.escmore, from[i])) *to++ = from[i];
85 else if (-1 != (j = stridx("\\\a\b\033\f\n\r\t\v", from[i])))
86 *to++ = "\\abefnrtv"[j];
87 else to += sprintf(to, "%03o", from[i]);
88 }
89 len = to-buf;
90 }
91 }
92
93 if (cols<len) len = cols;
94 if (out) fwrite(buf, len, 1, out);
95
96 return len;
97}
98
99
100static int strwidth(char *s)
101{
102 return crunch_str(&s, INT_MAX, 0, TT.escmore, crunch_qb);
103}
104
105static char endtype(struct stat *st)
106{
107 mode_t mode = st->st_mode;
108 if ((FLAG(F)||FLAG(p)) && S_ISDIR(mode)) return '/';
109 if (FLAG(F)) {
110 if (S_ISLNK(mode)) return '@';
111 if (S_ISREG(mode) && (mode&0111)) return '*';
112 if (S_ISFIFO(mode)) return '|';
113 if (S_ISSOCK(mode)) return '=';
114 }
115 return 0;
116}
117
118static int numlen(long long ll)
119{
120 return snprintf(0, 0, "%llu", ll);
121}
122
123static int print_with_h(char *s, long long value, int units)
124{
125 if (FLAG(h)) return human_readable(s, value*units, 0);
126 else return sprintf(s, "%lld", value);
127}
128
129
130
131static void entrylen(struct dirtree *dt, unsigned *len)
132{
133 struct stat *st = &(dt->st);
134 unsigned flags = toys.optflags;
135 char tmp[64];
136
137 *len = strwidth(dt->name);
138 if (endtype(st)) ++*len;
139 if (flags & FLAG_m) ++*len;
140
141 len[1] = (flags & FLAG_i) ? numlen(st->st_ino) : 0;
142 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
143 unsigned fn = flags & FLAG_n;
144
145 len[2] = numlen(st->st_nlink);
146 len[3] = fn ? numlen(st->st_uid) : strwidth(getusername(st->st_uid));
147 len[4] = fn ? numlen(st->st_gid) : strwidth(getgroupname(st->st_gid));
148 if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
149
150
151 len[5] = numlen(dev_major(st->st_rdev))+5;
152 } else len[5] = print_with_h(tmp, st->st_size, 1);
153 }
154
155 len[6] = (flags & FLAG_s) ? print_with_h(tmp, st->st_blocks, 512) : 0;
156 len[7] = (flags & FLAG_Z) ? strwidth((char *)dt->extra) : 0;
157}
158
159static int compare(void *a, void *b)
160{
161 struct dirtree *dta = *(struct dirtree **)a;
162 struct dirtree *dtb = *(struct dirtree **)b;
163 int ret = 0, reverse = FLAG(r) ? -1 : 1;
164
165 if (FLAG(S)) {
166 if (dta->st.st_size > dtb->st.st_size) ret = -1;
167 else if (dta->st.st_size < dtb->st.st_size) ret = 1;
168 }
169 if (FLAG(t)) {
170 if (dta->st.st_mtime > dtb->st.st_mtime) ret = -1;
171 else if (dta->st.st_mtime < dtb->st.st_mtime) ret = 1;
172 else if (dta->st.st_mtim.tv_nsec > dtb->st.st_mtim.tv_nsec) ret = -1;
173 else if (dta->st.st_mtim.tv_nsec < dtb->st.st_mtim.tv_nsec) ret = 1;
174 }
175 if (!ret) ret = strcmp(dta->name, dtb->name);
176 return ret * reverse;
177}
178
179
180
181static int filter(struct dirtree *new)
182{
183 int flags = toys.optflags;
184
185
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
195
196
197 int fd = openat(dirtree_parentfd(new), new->name,
198 O_PATH|(O_NOFOLLOW*!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
206
207
208
209
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
231
232
233static 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
240 if (!FLAG(C)) {
241 *xpos = ul % columns;
242 return ul;
243 }
244
245
246
247
248 height = (dtlen+columns-1)/columns;
249
250
251
252 if (height*columns - dtlen >= height) {
253 *xpos = columns;
254 return 0;
255 }
256
257
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
272static 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
286static void zprint(int zap, char *pat, int len, unsigned long arg)
287{
288 char tmp[32];
289
290 sprintf(tmp, "%%*%s", zap ? "s" : pat);
291 if (zap && pat[strlen(pat)-1]==' ') strcat(tmp, " ");
292 printf(tmp, len, zap ? (unsigned long)"?" : arg);
293}
294
295
296
297
298static void listfiles(int dirfd, struct dirtree *indir)
299{
300 struct dirtree *dt, **sort;
301 unsigned long dtlen, ul = 0;
302 unsigned width, flags = toys.optflags, totals[8], len[8], totpad = 0,
303 *colsizes = (unsigned *)toybuf, columns = sizeof(toybuf)/4;
304 char tmp[64];
305
306 if (-1 == dirfd) {
307 perror_msg_raw(indir->name);
308
309 return;
310 }
311
312 memset(totals, 0, sizeof(totals));
313 if (CFG_TOYBOX_DEBUG) memset(len, 0, sizeof(len));
314
315
316 if (!indir->parent) {
317
318
319 dt = indir->child;
320 if (dt && S_ISDIR(dt->st.st_mode) && !dt->next && !(flags&(FLAG_d|FLAG_R)))
321 {
322 listfiles(open(dt->name, 0), TT.singledir = dt);
323
324 return;
325 }
326
327
328 for (;dt; dt = dt->next) filter(dt);
329 if (flags == (FLAG_1|FLAG_f)) return;
330
331
332 } else dirtree_recurse(indir, filter, dup(dirfd),
333 DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*!!(flags&FLAG_L));
334
335
336
337
338 for (sort = 0;;sort = xmalloc(dtlen*sizeof(void *))) {
339 for (dtlen = 0, dt = indir->child; dt; dt = dt->next, dtlen++)
340 if (sort) sort[dtlen] = dt;
341 if (sort || !dtlen) break;
342 }
343
344
345 if (indir->parent && (TT.singledir!=indir || (flags&FLAG_R)))
346 {
347 char *path = dirtree_path(indir, 0);
348
349 if (TT.nl_title++) xputc('\n');
350 xprintf("%s:\n", path);
351 free(path);
352 }
353
354
355 if (!(flags & FLAG_f)) {
356 unsigned long long blocks = 0;
357
358 qsort(sort, dtlen, sizeof(void *), (void *)compare);
359 for (ul = 0; ul<dtlen; ul++) {
360 entrylen(sort[ul], len);
361 for (width = 0; width<8; width++)
362 if (len[width]>totals[width]) totals[width] = len[width];
363 blocks += sort[ul]->st.st_blocks;
364 }
365 totpad = totals[1]+!!totals[1]+totals[6]+!!totals[6]+totals[7]+!!totals[7];
366 if ((flags&(FLAG_h|FLAG_l|FLAG_o|FLAG_n|FLAG_g|FLAG_s)) && indir->parent) {
367 print_with_h(tmp, blocks, 512);
368 xprintf("total %s\n", tmp);
369 }
370 }
371
372
373 if (flags & (FLAG_C|FLAG_x)) {
374
375
376
377 if (columns > TT.screen_width/2) columns = TT.screen_width/2;
378 if (columns > dtlen) columns = dtlen;
379
380
381 for (;columns > 1; columns--) {
382 unsigned c, totlen = columns;
383
384 memset(colsizes, 0, columns*sizeof(unsigned));
385 for (ul=0; ul<dtlen; ul++) {
386 entrylen(sort[next_column(ul, dtlen, columns, &c)], len);
387
388 *len += totpad+2;
389 if (c == columns) break;
390
391 if (*len > colsizes[c]) {
392 totlen += (*len)-colsizes[c];
393 colsizes[c] = *len;
394 if (totlen > TT.screen_width) break;
395 }
396 }
397
398 if (ul == dtlen) break;
399 }
400 }
401
402
403 width = 0;
404 for (ul = 0; ul<dtlen; ul++) {
405 int ii, zap;
406 unsigned curcol, color = 0;
407 unsigned long next = next_column(ul, dtlen, columns, &curcol);
408 struct stat *st = &(sort[next]->st);
409 mode_t mode = st->st_mode;
410 char et = endtype(st), *ss;
411
412
413 zap = !st->st_blksize && !st->st_dev && !st->st_ino;
414
415
416 if (S_ISDIR(mode) && !indir->parent && !(flags & FLAG_d)) continue;
417 TT.nl_title=1;
418
419
420 entrylen(sort[next], len);
421 if (ul) {
422 int mm = !!(flags & FLAG_m);
423
424 if (mm) xputc(',');
425 if (flags & (FLAG_C|FLAG_x)) {
426 if (!curcol) xputc('\n');
427 } else if ((flags & FLAG_1) || width+1+*len > TT.screen_width) {
428 xputc('\n');
429 width = 0;
430 } else {
431 printf(" "+mm, 0);
432 width += 2-mm;
433 }
434 }
435 width += *len;
436
437 if (flags & FLAG_i) zprint(zap, "lu ", totals[1], st->st_ino);
438
439 if (flags & FLAG_s) {
440 print_with_h(tmp, st->st_blocks, 512);
441 zprint(zap, "s ", totals[6], (unsigned long)tmp);
442 }
443
444 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
445
446
447 mode_to_string(mode, tmp);
448 if (zap) memset(tmp+1, '?', 9);
449 printf("%s", tmp);
450 zprint(zap, "ld", totals[2]+1, st->st_nlink);
451
452
453 if (!(flags&FLAG_g)) {
454 putchar(' ');
455 ii = -totals[3];
456 if (zap || (flags&FLAG_n)) zprint(zap, "lu", ii, st->st_uid);
457 else draw_trim_esc(getusername(st->st_uid), ii, abs(ii), TT.escmore,
458 crunch_qb);
459 }
460
461
462 if (!(flags&FLAG_o)) {
463 putchar(' ');
464 ii = -totals[4];
465 if (zap || (flags&FLAG_n)) zprint(zap, "lu", ii, st->st_gid);
466 else draw_trim_esc(getgroupname(st->st_gid), ii, abs(ii), TT.escmore,
467 crunch_qb);
468 }
469 }
470 if (FLAG(Z))
471 printf(" %-*s "+!FLAG(l), -(int)totals[7], (char *)sort[next]->extra);
472
473 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
474 struct tm *tm;
475
476
477 if (!zap && (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)))
478 printf("% *d,% 4d", totals[5]-4, dev_major(st->st_rdev),
479 dev_minor(st->st_rdev));
480 else {
481 print_with_h(tmp, st->st_size, 1);
482 zprint(zap, "s", totals[5]+1, (unsigned long)tmp);
483 }
484
485
486 tm = localtime(&(st->st_mtime));
487 strftime(tmp, sizeof(tmp), " %F %H:%M", tm);
488 if (TT.l>1) {
489 char *s = tmp+strlen(tmp);
490
491 s += sprintf(s, ":%02d.%09d ", tm->tm_sec, (int)st->st_mtim.tv_nsec);
492 strftime(s, sizeof(tmp)-(s-tmp), "%z", tm);
493 }
494 zprint(zap, "s ", 17+(TT.l>1)*13, (unsigned long)tmp);
495 }
496
497 if (flags & FLAG_color) {
498 color = color_from_mode(st->st_mode);
499 if (color) printf("\033[%d;%dm", color>>8, color&255);
500 }
501
502 ss = sort[next]->name;
503 crunch_str(&ss, INT_MAX, stdout, TT.escmore, crunch_qb);
504 if (color) printf("\033[0m");
505
506 if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode)) {
507 printf(" -> ");
508 if (!zap && (flags & FLAG_color)) {
509 struct stat st2;
510
511 if (fstatat(dirfd, sort[next]->symlink, &st2, 0)) color = 256+31;
512 else color = color_from_mode(st2.st_mode);
513
514 if (color) printf("\033[%d;%dm", color>>8, color&255);
515 }
516
517 zprint(zap, "s", 0, (unsigned long)sort[next]->symlink);
518 if (!zap && color) printf("\033[0m");
519 }
520
521 if (et) xputc(et);
522
523
524 if (flags & (FLAG_C|FLAG_x)) {
525 curcol = colsizes[curcol]-(*len)-totpad;
526 if (curcol < 255) printf("%*c", curcol, ' ');
527 }
528 }
529
530 if (width) xputc('\n');
531
532
533
534 for (ul = 0; ul<dtlen; free(sort[ul++])) {
535 if ((flags & FLAG_d) || !S_ISDIR(sort[ul]->st.st_mode)) continue;
536
537
538 if (!indir->parent || ((flags&FLAG_R) && dirtree_notdotdot(sort[ul])))
539 listfiles(openat(dirfd, sort[ul]->name, 0), sort[ul]);
540 free((void *)sort[ul]->extra);
541 }
542 free(sort);
543 if (dirfd != AT_FDCWD) close(dirfd);
544}
545
546void ls_main(void)
547{
548 char **s, *noargs[] = {".", 0};
549 struct dirtree *dt;
550
551 if (FLAG(full_time)) {
552 toys.optflags |= FLAG_l;
553 TT.l = 2;
554 }
555
556
557 if (isatty(1)) {
558 if (!FLAG(show_control_chars)) toys.optflags |= FLAG_b;
559 if (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)) toys.optflags |= FLAG_1;
560 else if (!(toys.optflags&(FLAG_1|FLAG_x|FLAG_m))) toys.optflags |= FLAG_C;
561 } else {
562 if (!FLAG(m)) toys.optflags |= FLAG_1;
563 if (TT.color) toys.optflags ^= FLAG_color;
564 }
565
566 TT.screen_width = 80;
567 terminal_size(&TT.screen_width, NULL);
568 if (TT.screen_width<2) TT.screen_width = 2;
569 if (FLAG(b)) TT.escmore = " \\";
570
571
572
573
574 if (FLAG(d)) toys.optflags &= ~FLAG_R;
575
576
577
578
579 TT.files = dirtree_add_node(0, 0, 0);
580 TT.files->dirfd = AT_FDCWD;
581 for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
582 int sym = !(toys.optflags&(FLAG_l|FLAG_d|FLAG_F))
583 || (toys.optflags&(FLAG_L|FLAG_H));
584
585 dt = dirtree_add_node(0, *s, DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*sym);
586
587
588 if (dt) {
589 if (dt->again&2) {
590 perror_msg_raw(*s);
591 free(dt);
592 } else dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
593 } else toys.exitval = 1;
594 }
595
596
597 dlist_terminate(TT.files->child);
598 for (dt = TT.files->child; dt; dt = dt->next) dt->parent = TT.files;
599
600
601 listfiles(AT_FDCWD, TT.files);
602
603 if (CFG_TOYBOX_FREE) free(TT.files);
604}
605