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