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