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