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