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