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