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