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