1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Michael Fischbein. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1989, 1993, 1994\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 41 #if 0 42 #ifndef lint 43 static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94"; 44 #endif /* not lint */ 45 #endif 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <sys/param.h> 50 #include <sys/stat.h> 51 #include <sys/ioctl.h> 52 #include <sys/mac.h> 53 54 #include <dirent.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <fts.h> 58 #include <grp.h> 59 #include <inttypes.h> 60 #include <limits.h> 61 #include <locale.h> 62 #include <pwd.h> 63 #include <stdbool.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 #ifdef COLORLS 69 #include <termcap.h> 70 #include <signal.h> 71 #endif 72 73 #include "ls.h" 74 #include "extern.h" 75 76 /* 77 * Upward approximation of the maximum number of characters needed to 78 * represent a value of integral type t as a string, excluding the 79 * NUL terminator, with provision for a sign. 80 */ 81 #define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1) 82 83 /* 84 * MAKENINES(n) turns n into (10**n)-1. This is useful for converting a width 85 * into a number that wide in decimal. 86 * XXX: Overflows are not considered. 87 */ 88 #define MAKENINES(n) \ 89 do { \ 90 intmax_t i; \ 91 \ 92 /* Use a loop as all values of n are small. */ \ 93 for (i = 1; n > 0; i *= 10) \ 94 n--; \ 95 n = i - 1; \ 96 } while(0) 97 98 static void display(const FTSENT *, FTSENT *, int); 99 static int mastercmp(const FTSENT * const *, const FTSENT * const *); 100 static void traverse(int, char **, int); 101 102 static void (*printfcn)(const DISPLAY *); 103 static int (*sortfcn)(const FTSENT *, const FTSENT *); 104 105 long blocksize; /* block size units */ 106 int termwidth = 80; /* default terminal width */ 107 108 /* flags */ 109 int f_accesstime; /* use time of last access */ 110 int f_birthtime; /* use time of birth */ 111 int f_flags; /* show flags associated with a file */ 112 int f_humanval; /* show human-readable file sizes */ 113 int f_inode; /* print inode */ 114 static int f_kblocks; /* print size in kilobytes */ 115 int f_label; /* show MAC label */ 116 static int f_listdir; /* list actual directory, not contents */ 117 static int f_listdot; /* list files beginning with . */ 118 int f_longform; /* long listing format */ 119 static int f_noautodot; /* do not automatically enable -A for root */ 120 static int f_nofollow; /* don't follow symbolic link arguments */ 121 int f_nonprint; /* show unprintables as ? */ 122 static int f_nosort; /* don't sort output */ 123 int f_notabs; /* don't use tab-separated multi-col output */ 124 static int f_numericonly; /* don't convert uid/gid to name */ 125 int f_octal; /* show unprintables as \xxx */ 126 int f_octal_escape; /* like f_octal but use C escapes if possible */ 127 static int f_recursive; /* ls subdirectories also */ 128 static int f_reversesort; /* reverse whatever sort is used */ 129 int f_samesort; /* sort time and name in same direction */ 130 int f_sectime; /* print full time information */ 131 static int f_singlecol; /* use single column output */ 132 int f_size; /* list size in short listing */ 133 static int f_sizesort; 134 int f_slash; /* similar to f_type, but only for dirs */ 135 int f_sortacross; /* sort across rows, not down columns */ 136 int f_statustime; /* use time of last mode change */ 137 static int f_stream; /* stream the output, separate with commas */ 138 int f_thousands; /* show file sizes with thousands separators */ 139 char *f_timeformat; /* user-specified time format */ 140 static int f_timesort; /* sort by time vice name */ 141 int f_type; /* add type character for non-regular files */ 142 static int f_whiteout; /* show whiteout entries */ 143 144 #ifdef COLORLS 145 int f_color; /* add type in color for non-regular files */ 146 147 char *ansi_bgcol; /* ANSI sequence to set background colour */ 148 char *ansi_fgcol; /* ANSI sequence to set foreground colour */ 149 char *ansi_coloff; /* ANSI sequence to reset colours */ 150 char *attrs_off; /* ANSI sequence to turn off attributes */ 151 char *enter_bold; /* ANSI sequence to set color to bold mode */ 152 #endif 153 154 static int rval; 155 156 static bool 157 do_color_from_env(void) 158 { 159 const char *p; 160 bool doit; 161 162 doit = false; 163 p = getenv("CLICOLOR"); 164 if (p == NULL) { 165 /* 166 * COLORTERM is the more standard name for this variable. We'll 167 * honor it as long as it's both set and not empty. 168 */ 169 p = getenv("COLORTERM"); 170 if (p != NULL && *p != '\0') 171 doit = true; 172 } else 173 doit = true; 174 175 return (doit && 176 (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE"))); 177 } 178 179 int 180 main(int argc, char *argv[]) 181 { 182 static char dot[] = ".", *dotav[] = {dot, NULL}; 183 struct winsize win; 184 int ch, fts_options, notused; 185 char *p; 186 const char *errstr = NULL; 187 #ifdef COLORLS 188 char termcapbuf[1024]; /* termcap definition buffer */ 189 char tcapbuf[512]; /* capability buffer */ 190 char *bp = tcapbuf; 191 #endif 192 193 (void)setlocale(LC_ALL, ""); 194 195 /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 196 if (isatty(STDOUT_FILENO)) { 197 termwidth = 80; 198 if ((p = getenv("COLUMNS")) != NULL && *p != '\0') 199 termwidth = strtonum(p, 0, INT_MAX, &errstr); 200 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && 201 win.ws_col > 0) 202 termwidth = win.ws_col; 203 f_nonprint = 1; 204 } else { 205 f_singlecol = 1; 206 /* retrieve environment variable, in case of explicit -C */ 207 p = getenv("COLUMNS"); 208 if (p) 209 termwidth = strtonum(p, 0, INT_MAX, &errstr); 210 } 211 212 if (errstr) 213 termwidth = 80; 214 215 fts_options = FTS_PHYSICAL; 216 if (getenv("LS_SAMESORT")) 217 f_samesort = 1; 218 while ((ch = getopt(argc, argv, 219 "1ABCD:FGHILPRSTUWXZabcdfghiklmnopqrstuwxy,")) != -1) { 220 switch (ch) { 221 /* 222 * The -1, -C, -x and -l options all override each other so 223 * shell aliasing works right. 224 */ 225 case '1': 226 f_singlecol = 1; 227 f_longform = 0; 228 f_stream = 0; 229 break; 230 case 'C': 231 f_sortacross = f_longform = f_singlecol = 0; 232 break; 233 case 'l': 234 f_longform = 1; 235 f_singlecol = 0; 236 f_stream = 0; 237 break; 238 case 'x': 239 f_sortacross = 1; 240 f_longform = 0; 241 f_singlecol = 0; 242 break; 243 /* The -c, -u, and -U options override each other. */ 244 case 'c': 245 f_statustime = 1; 246 f_accesstime = 0; 247 f_birthtime = 0; 248 break; 249 case 'u': 250 f_accesstime = 1; 251 f_statustime = 0; 252 f_birthtime = 0; 253 break; 254 case 'U': 255 f_birthtime = 1; 256 f_accesstime = 0; 257 f_statustime = 0; 258 break; 259 case 'f': 260 f_nosort = 1; 261 /* FALLTHROUGH */ 262 case 'a': 263 fts_options |= FTS_SEEDOT; 264 /* FALLTHROUGH */ 265 case 'A': 266 f_listdot = 1; 267 break; 268 /* The -t and -S options override each other. */ 269 case 'S': 270 f_sizesort = 1; 271 f_timesort = 0; 272 break; 273 case 't': 274 f_timesort = 1; 275 f_sizesort = 0; 276 break; 277 /* Other flags. Please keep alphabetic. */ 278 case ',': 279 f_thousands = 1; 280 break; 281 case 'B': 282 f_nonprint = 0; 283 f_octal = 1; 284 f_octal_escape = 0; 285 break; 286 case 'D': 287 f_timeformat = optarg; 288 break; 289 case 'F': 290 f_type = 1; 291 f_slash = 0; 292 break; 293 case 'G': 294 setenv("CLICOLOR", "", 1); 295 break; 296 case 'H': 297 fts_options |= FTS_COMFOLLOW; 298 f_nofollow = 0; 299 break; 300 case 'I': 301 f_noautodot = 1; 302 break; 303 case 'L': 304 fts_options &= ~FTS_PHYSICAL; 305 fts_options |= FTS_LOGICAL; 306 f_nofollow = 0; 307 break; 308 case 'P': 309 fts_options &= ~FTS_COMFOLLOW; 310 fts_options &= ~FTS_LOGICAL; 311 fts_options |= FTS_PHYSICAL; 312 f_nofollow = 1; 313 break; 314 case 'R': 315 f_recursive = 1; 316 break; 317 case 'T': 318 f_sectime = 1; 319 break; 320 case 'W': 321 f_whiteout = 1; 322 break; 323 case 'Z': 324 f_label = 1; 325 break; 326 case 'b': 327 f_nonprint = 0; 328 f_octal = 0; 329 f_octal_escape = 1; 330 break; 331 /* The -d option turns off the -R option. */ 332 case 'd': 333 f_listdir = 1; 334 f_recursive = 0; 335 break; 336 case 'g': /* Compatibility with 4.3BSD. */ 337 break; 338 case 'h': 339 f_humanval = 1; 340 break; 341 case 'i': 342 f_inode = 1; 343 break; 344 case 'k': 345 f_humanval = 0; 346 f_kblocks = 1; 347 break; 348 case 'm': 349 f_stream = 1; 350 f_singlecol = 0; 351 f_longform = 0; 352 break; 353 case 'n': 354 f_numericonly = 1; 355 break; 356 case 'o': 357 f_flags = 1; 358 break; 359 case 'p': 360 f_slash = 1; 361 f_type = 1; 362 break; 363 case 'q': 364 f_nonprint = 1; 365 f_octal = 0; 366 f_octal_escape = 0; 367 break; 368 case 'r': 369 f_reversesort = 1; 370 break; 371 case 's': 372 f_size = 1; 373 break; 374 case 'w': 375 f_nonprint = 0; 376 f_octal = 0; 377 f_octal_escape = 0; 378 break; 379 case 'y': 380 f_samesort = 1; 381 break; 382 default: 383 case '?': 384 usage(); 385 } 386 } 387 argc -= optind; 388 argv += optind; 389 390 /* Root is -A automatically unless -I. */ 391 if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot) 392 f_listdot = 1; 393 394 /* Enabling of colours is conditional on the environment. */ 395 if (do_color_from_env()) 396 #ifdef COLORLS 397 if (tgetent(termcapbuf, getenv("TERM")) == 1) { 398 ansi_fgcol = tgetstr("AF", &bp); 399 ansi_bgcol = tgetstr("AB", &bp); 400 attrs_off = tgetstr("me", &bp); 401 enter_bold = tgetstr("md", &bp); 402 403 /* To switch colours off use 'op' if 404 * available, otherwise use 'oc', or 405 * don't do colours at all. */ 406 ansi_coloff = tgetstr("op", &bp); 407 if (!ansi_coloff) 408 ansi_coloff = tgetstr("oc", &bp); 409 if (ansi_fgcol && ansi_bgcol && ansi_coloff) 410 f_color = 1; 411 } 412 #else 413 warnx("color support not compiled in"); 414 #endif /*COLORLS*/ 415 416 #ifdef COLORLS 417 if (f_color) { 418 /* 419 * We can't put tabs and color sequences together: 420 * column number will be incremented incorrectly 421 * for "stty oxtabs" mode. 422 */ 423 f_notabs = 1; 424 (void)signal(SIGINT, colorquit); 425 (void)signal(SIGQUIT, colorquit); 426 parsecolors(getenv("LSCOLORS")); 427 } 428 #endif 429 430 /* 431 * If not -F, -i, -l, -s, -S or -t options, don't require stat 432 * information, unless in color mode in which case we do 433 * need this to determine which colors to display. 434 */ 435 if (!f_inode && !f_longform && !f_size && !f_timesort && 436 !f_sizesort && !f_type 437 #ifdef COLORLS 438 && !f_color 439 #endif 440 ) 441 fts_options |= FTS_NOSTAT; 442 443 /* 444 * If not -F, -P, -d or -l options, follow any symbolic links listed on 445 * the command line, unless in color mode in which case we need to 446 * distinguish file type for a symbolic link itself and its target. 447 */ 448 if (!f_nofollow && !f_longform && !f_listdir && (!f_type || f_slash) 449 #ifdef COLORLS 450 && !f_color 451 #endif 452 ) 453 fts_options |= FTS_COMFOLLOW; 454 455 /* 456 * If -W, show whiteout entries 457 */ 458 #ifdef FTS_WHITEOUT 459 if (f_whiteout) 460 fts_options |= FTS_WHITEOUT; 461 #endif 462 463 /* If -i, -l or -s, figure out block size. */ 464 if (f_inode || f_longform || f_size) { 465 if (f_kblocks) 466 blocksize = 2; 467 else { 468 (void)getbsize(¬used, &blocksize); 469 blocksize /= 512; 470 } 471 } 472 /* Select a sort function. */ 473 if (f_reversesort) { 474 if (!f_timesort && !f_sizesort) 475 sortfcn = revnamecmp; 476 else if (f_sizesort) 477 sortfcn = revsizecmp; 478 else if (f_accesstime) 479 sortfcn = revacccmp; 480 else if (f_birthtime) 481 sortfcn = revbirthcmp; 482 else if (f_statustime) 483 sortfcn = revstatcmp; 484 else /* Use modification time. */ 485 sortfcn = revmodcmp; 486 } else { 487 if (!f_timesort && !f_sizesort) 488 sortfcn = namecmp; 489 else if (f_sizesort) 490 sortfcn = sizecmp; 491 else if (f_accesstime) 492 sortfcn = acccmp; 493 else if (f_birthtime) 494 sortfcn = birthcmp; 495 else if (f_statustime) 496 sortfcn = statcmp; 497 else /* Use modification time. */ 498 sortfcn = modcmp; 499 } 500 501 /* Select a print function. */ 502 if (f_singlecol) 503 printfcn = printscol; 504 else if (f_longform) 505 printfcn = printlong; 506 else if (f_stream) 507 printfcn = printstream; 508 else 509 printfcn = printcol; 510 511 if (argc) 512 traverse(argc, argv, fts_options); 513 else 514 traverse(1, dotav, fts_options); 515 exit(rval); 516 } 517 518 static int output; /* If anything output. */ 519 520 /* 521 * Traverse() walks the logical directory structure specified by the argv list 522 * in the order specified by the mastercmp() comparison function. During the 523 * traversal it passes linked lists of structures to display() which represent 524 * a superset (may be exact set) of the files to be displayed. 525 */ 526 static void 527 traverse(int argc, char *argv[], int options) 528 { 529 FTS *ftsp; 530 FTSENT *p, *chp; 531 int ch_options; 532 533 if ((ftsp = 534 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 535 err(1, "fts_open"); 536 537 /* 538 * We ignore errors from fts_children here since they will be 539 * replicated and signalled on the next call to fts_read() below. 540 */ 541 chp = fts_children(ftsp, 0); 542 if (chp != NULL) 543 display(NULL, chp, options); 544 if (f_listdir) 545 return; 546 547 /* 548 * If not recursing down this tree and don't need stat info, just get 549 * the names. 550 */ 551 ch_options = !f_recursive && !f_label && 552 options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 553 554 while ((p = fts_read(ftsp)) != NULL) 555 switch (p->fts_info) { 556 case FTS_DC: 557 warnx("%s: directory causes a cycle", p->fts_name); 558 break; 559 case FTS_DNR: 560 case FTS_ERR: 561 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 562 rval = 1; 563 break; 564 case FTS_D: 565 if (p->fts_level != FTS_ROOTLEVEL && 566 p->fts_name[0] == '.' && !f_listdot) 567 break; 568 569 /* 570 * If already output something, put out a newline as 571 * a separator. If multiple arguments, precede each 572 * directory with its name. 573 */ 574 if (output) { 575 putchar('\n'); 576 (void)printname(p->fts_path); 577 puts(":"); 578 } else if (argc > 1) { 579 (void)printname(p->fts_path); 580 puts(":"); 581 output = 1; 582 } 583 chp = fts_children(ftsp, ch_options); 584 display(p, chp, options); 585 586 if (!f_recursive && chp != NULL) 587 (void)fts_set(ftsp, p, FTS_SKIP); 588 break; 589 default: 590 break; 591 } 592 if (errno) 593 err(1, "fts_read"); 594 } 595 596 /* 597 * Display() takes a linked list of FTSENT structures and passes the list 598 * along with any other necessary information to the print function. P 599 * points to the parent directory of the display list. 600 */ 601 static void 602 display(const FTSENT *p, FTSENT *list, int options) 603 { 604 struct stat *sp; 605 DISPLAY d; 606 FTSENT *cur; 607 NAMES *np; 608 off_t maxsize; 609 long maxblock; 610 uintmax_t maxinode; 611 u_long btotal, labelstrlen, maxlen, maxnlink; 612 u_long maxlabelstr; 613 u_int sizelen; 614 int maxflags; 615 gid_t maxgroup; 616 uid_t maxuser; 617 size_t flen, ulen, glen; 618 char *initmax; 619 int entries, needstats; 620 const char *user, *group; 621 char *flags, *labelstr = NULL; 622 char ngroup[STRBUF_SIZEOF(uid_t) + 1]; 623 char nuser[STRBUF_SIZEOF(gid_t) + 1]; 624 625 needstats = f_inode || f_longform || f_size; 626 flen = 0; 627 btotal = 0; 628 initmax = getenv("LS_COLWIDTHS"); 629 /* Fields match -lios order. New ones should be added at the end. */ 630 maxlabelstr = maxblock = maxlen = maxnlink = 0; 631 maxuser = maxgroup = maxflags = maxsize = 0; 632 maxinode = 0; 633 if (initmax != NULL && *initmax != '\0') { 634 char *initmax2, *jinitmax; 635 int ninitmax; 636 637 /* Fill-in "::" as "0:0:0" for the sake of scanf. */ 638 jinitmax = malloc(strlen(initmax) * 2 + 2); 639 if (jinitmax == NULL) 640 err(1, "malloc"); 641 initmax2 = jinitmax; 642 if (*initmax == ':') 643 strcpy(initmax2, "0:"), initmax2 += 2; 644 else 645 *initmax2++ = *initmax, *initmax2 = '\0'; 646 for (initmax++; *initmax != '\0'; initmax++) { 647 if (initmax[-1] == ':' && initmax[0] == ':') { 648 *initmax2++ = '0'; 649 *initmax2++ = initmax[0]; 650 initmax2[1] = '\0'; 651 } else { 652 *initmax2++ = initmax[0]; 653 initmax2[1] = '\0'; 654 } 655 } 656 if (initmax2[-1] == ':') 657 strcpy(initmax2, "0"); 658 659 ninitmax = sscanf(jinitmax, 660 " %ju : %ld : %lu : %u : %u : %i : %jd : %lu : %lu ", 661 &maxinode, &maxblock, &maxnlink, &maxuser, 662 &maxgroup, &maxflags, &maxsize, &maxlen, &maxlabelstr); 663 f_notabs = 1; 664 switch (ninitmax) { 665 case 0: 666 maxinode = 0; 667 /* FALLTHROUGH */ 668 case 1: 669 maxblock = 0; 670 /* FALLTHROUGH */ 671 case 2: 672 maxnlink = 0; 673 /* FALLTHROUGH */ 674 case 3: 675 maxuser = 0; 676 /* FALLTHROUGH */ 677 case 4: 678 maxgroup = 0; 679 /* FALLTHROUGH */ 680 case 5: 681 maxflags = 0; 682 /* FALLTHROUGH */ 683 case 6: 684 maxsize = 0; 685 /* FALLTHROUGH */ 686 case 7: 687 maxlen = 0; 688 /* FALLTHROUGH */ 689 case 8: 690 maxlabelstr = 0; 691 /* FALLTHROUGH */ 692 #ifdef COLORLS 693 if (!f_color) 694 #endif 695 f_notabs = 0; 696 /* FALLTHROUGH */ 697 default: 698 break; 699 } 700 MAKENINES(maxinode); 701 MAKENINES(maxblock); 702 MAKENINES(maxnlink); 703 MAKENINES(maxsize); 704 free(jinitmax); 705 } 706 d.s_size = 0; 707 sizelen = 0; 708 flags = NULL; 709 for (cur = list, entries = 0; cur; cur = cur->fts_link) { 710 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 711 warnx("%s: %s", 712 cur->fts_name, strerror(cur->fts_errno)); 713 cur->fts_number = NO_PRINT; 714 rval = 1; 715 continue; 716 } 717 /* 718 * P is NULL if list is the argv list, to which different rules 719 * apply. 720 */ 721 if (p == NULL) { 722 /* Directories will be displayed later. */ 723 if (cur->fts_info == FTS_D && !f_listdir) { 724 cur->fts_number = NO_PRINT; 725 continue; 726 } 727 } else { 728 /* Only display dot file if -a/-A set. */ 729 if (cur->fts_name[0] == '.' && !f_listdot) { 730 cur->fts_number = NO_PRINT; 731 continue; 732 } 733 } 734 if (cur->fts_namelen > maxlen) 735 maxlen = cur->fts_namelen; 736 if (f_octal || f_octal_escape) { 737 u_long t = len_octal(cur->fts_name, cur->fts_namelen); 738 739 if (t > maxlen) 740 maxlen = t; 741 } 742 if (needstats) { 743 sp = cur->fts_statp; 744 if (sp->st_blocks > maxblock) 745 maxblock = sp->st_blocks; 746 if (sp->st_ino > maxinode) 747 maxinode = sp->st_ino; 748 if (sp->st_nlink > maxnlink) 749 maxnlink = sp->st_nlink; 750 if (sp->st_size > maxsize) 751 maxsize = sp->st_size; 752 753 btotal += sp->st_blocks; 754 if (f_longform) { 755 if (f_numericonly) { 756 (void)snprintf(nuser, sizeof(nuser), 757 "%u", sp->st_uid); 758 (void)snprintf(ngroup, sizeof(ngroup), 759 "%u", sp->st_gid); 760 user = nuser; 761 group = ngroup; 762 } else { 763 user = user_from_uid(sp->st_uid, 0); 764 group = group_from_gid(sp->st_gid, 0); 765 } 766 if ((ulen = strlen(user)) > maxuser) 767 maxuser = ulen; 768 if ((glen = strlen(group)) > maxgroup) 769 maxgroup = glen; 770 if (f_flags) { 771 flags = fflagstostr(sp->st_flags); 772 if (flags != NULL && *flags == '\0') { 773 free(flags); 774 flags = strdup("-"); 775 } 776 if (flags == NULL) 777 err(1, "fflagstostr"); 778 flen = strlen(flags); 779 if (flen > (size_t)maxflags) 780 maxflags = flen; 781 } else 782 flen = 0; 783 labelstr = NULL; 784 if (f_label) { 785 char name[PATH_MAX + 1]; 786 mac_t label; 787 int error; 788 789 error = mac_prepare_file_label(&label); 790 if (error == -1) { 791 warn("MAC label for %s/%s", 792 cur->fts_parent->fts_path, 793 cur->fts_name); 794 goto label_out; 795 } 796 797 if (cur->fts_level == FTS_ROOTLEVEL) 798 snprintf(name, sizeof(name), 799 "%s", cur->fts_name); 800 else 801 snprintf(name, sizeof(name), 802 "%s/%s", cur->fts_parent-> 803 fts_accpath, cur->fts_name); 804 805 if (options & FTS_LOGICAL) 806 error = mac_get_file(name, 807 label); 808 else 809 error = mac_get_link(name, 810 label); 811 if (error == -1) { 812 warn("MAC label for %s/%s", 813 cur->fts_parent->fts_path, 814 cur->fts_name); 815 mac_free(label); 816 goto label_out; 817 } 818 819 error = mac_to_text(label, 820 &labelstr); 821 if (error == -1) { 822 warn("MAC label for %s/%s", 823 cur->fts_parent->fts_path, 824 cur->fts_name); 825 mac_free(label); 826 goto label_out; 827 } 828 mac_free(label); 829 label_out: 830 if (labelstr == NULL) 831 labelstr = strdup("-"); 832 labelstrlen = strlen(labelstr); 833 if (labelstrlen > maxlabelstr) 834 maxlabelstr = labelstrlen; 835 } else 836 labelstrlen = 0; 837 838 if ((np = malloc(sizeof(NAMES) + labelstrlen + 839 ulen + glen + flen + 4)) == NULL) 840 err(1, "malloc"); 841 842 np->user = &np->data[0]; 843 (void)strcpy(np->user, user); 844 np->group = &np->data[ulen + 1]; 845 (void)strcpy(np->group, group); 846 847 if (S_ISCHR(sp->st_mode) || 848 S_ISBLK(sp->st_mode)) { 849 sizelen = snprintf(NULL, 0, 850 "%#jx", (uintmax_t)sp->st_rdev); 851 if (d.s_size < sizelen) 852 d.s_size = sizelen; 853 } 854 855 if (f_flags) { 856 np->flags = &np->data[ulen + glen + 2]; 857 (void)strcpy(np->flags, flags); 858 free(flags); 859 } 860 if (f_label) { 861 np->label = &np->data[ulen + glen + 2 862 + (f_flags ? flen + 1 : 0)]; 863 (void)strcpy(np->label, labelstr); 864 free(labelstr); 865 } 866 cur->fts_pointer = np; 867 } 868 } 869 ++entries; 870 } 871 872 /* 873 * If there are no entries to display, we normally stop right 874 * here. However, we must continue if we have to display the 875 * total block count. In this case, we display the total only 876 * on the second (p != NULL) pass. 877 */ 878 if (!entries && (!(f_longform || f_size) || p == NULL)) 879 return; 880 881 d.list = list; 882 d.entries = entries; 883 d.maxlen = maxlen; 884 if (needstats) { 885 d.btotal = btotal; 886 d.s_block = snprintf(NULL, 0, "%lu", howmany(maxblock, blocksize)); 887 d.s_flags = maxflags; 888 d.s_label = maxlabelstr; 889 d.s_group = maxgroup; 890 d.s_inode = snprintf(NULL, 0, "%ju", maxinode); 891 d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink); 892 sizelen = f_humanval ? HUMANVALSTR_LEN : 893 snprintf(NULL, 0, "%ju", maxsize); 894 if (d.s_size < sizelen) 895 d.s_size = sizelen; 896 d.s_user = maxuser; 897 } 898 if (f_thousands) /* make space for commas */ 899 d.s_size += (d.s_size - 1) / 3; 900 printfcn(&d); 901 output = 1; 902 903 if (f_longform) 904 for (cur = list; cur; cur = cur->fts_link) 905 free(cur->fts_pointer); 906 } 907 908 /* 909 * Ordering for mastercmp: 910 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 911 * as larger than directories. Within either group, use the sort function. 912 * All other levels use the sort function. Error entries remain unsorted. 913 */ 914 static int 915 mastercmp(const FTSENT * const *a, const FTSENT * const *b) 916 { 917 int a_info, b_info; 918 919 a_info = (*a)->fts_info; 920 if (a_info == FTS_ERR) 921 return (0); 922 b_info = (*b)->fts_info; 923 if (b_info == FTS_ERR) 924 return (0); 925 926 if (a_info == FTS_NS || b_info == FTS_NS) 927 return (namecmp(*a, *b)); 928 929 if (a_info != b_info && 930 (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) { 931 if (a_info == FTS_D) 932 return (1); 933 if (b_info == FTS_D) 934 return (-1); 935 } 936 return (sortfcn(*a, *b)); 937 } 938