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