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