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