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