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