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