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