xref: /freebsd/bin/ls/ls.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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 
39 __FBSDID("$FreeBSD$");
40 
41 #ifndef lint
42 static const char copyright[] =
43 "@(#) Copyright (c) 1989, 1993, 1994\n\
44 	The Regents of the University of California.  All rights reserved.\n";
45 #endif /* not lint */
46 
47 #if 0
48 #ifndef lint
49 static char sccsid[] = "@(#)ls.c	8.5 (Berkeley) 4/2/94";
50 #endif /* not lint */
51 #endif
52 
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/ioctl.h>
56 
57 #include <dirent.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fts.h>
61 #include <grp.h>
62 #include <limits.h>
63 #include <locale.h>
64 #include <pwd.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #ifdef COLORLS
70 #include <termcap.h>
71 #include <signal.h>
72 #endif
73 
74 #include "ls.h"
75 #include "extern.h"
76 #include "lomac.h"
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 FTSENT **);
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_statustime;	/* use time of last mode change */
117 static int f_timesort;		/* sort by time vice name */
118        int f_type;		/* add type character for non-regular files */
119 static int f_whiteout;		/* show whiteout entries */
120        int f_lomac;		/* show LOMAC attributes */
121 #ifdef COLORLS
122        int f_color;		/* add type in color for non-regular files */
123 
124 char *ansi_bgcol;		/* ANSI sequence to set background colour */
125 char *ansi_fgcol;		/* ANSI sequence to set foreground colour */
126 char *ansi_coloff;		/* ANSI sequence to reset colours */
127 char *attrs_off;		/* ANSI sequence to turn off attributes */
128 char *enter_bold;		/* ANSI sequence to set color to bold mode */
129 #endif
130 
131 static int rval;
132 
133 int
134 main(int argc, 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_nonprint = 1;
157 	} else {
158 		f_singlecol = 1;
159 		/* retrieve environment variable, in case of explicit -C */
160 		p = getenv("COLUMNS");
161 		if (p)
162 			termwidth = atoi(p);
163 	}
164 
165 	/* Root is -A automatically. */
166 	if (!getuid())
167 		f_listdot = 1;
168 
169 	fts_options = FTS_PHYSICAL;
170  	while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklnoqrstuw")) != -1) {
171 		switch (ch) {
172 		/*
173 		 * The -1, -C and -l options all override each other so shell
174 		 * aliasing works right.
175 		 */
176 		case '1':
177 			f_singlecol = 1;
178 			f_longform = 0;
179 			break;
180 		case 'B':
181 			f_nonprint = 0;
182 			f_octal = 1;
183 			f_octal_escape = 0;
184 			break;
185 		case 'C':
186 			f_longform = f_singlecol = 0;
187 			break;
188 		case 'l':
189 			f_longform = 1;
190 			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(int argc, char *argv[], int options)
413 {
414 	FTS *ftsp;
415 	FTSENT *p, *chp;
416 	int ch_options;
417 
418 	if ((ftsp =
419 	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
420 		err(1, NULL);
421 
422 	display(NULL, fts_children(ftsp, 0));
423 	if (f_listdir)
424 		return;
425 
426 	/*
427 	 * If not recursing down this tree and don't need stat info, just get
428 	 * the names.
429 	 */
430 	ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
431 
432 	while ((p = fts_read(ftsp)) != NULL)
433 		switch (p->fts_info) {
434 		case FTS_DC:
435 			warnx("%s: directory causes a cycle", p->fts_name);
436 			break;
437 		case FTS_DNR:
438 		case FTS_ERR:
439 			warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
440 			rval = 1;
441 			break;
442 		case FTS_D:
443 			if (p->fts_level != FTS_ROOTLEVEL &&
444 			    p->fts_name[0] == '.' && !f_listdot)
445 				break;
446 
447 			/*
448 			 * If already output something, put out a newline as
449 			 * a separator.  If multiple arguments, precede each
450 			 * directory with its name.
451 			 */
452 			if (output)
453 				(void)printf("\n%s:\n", p->fts_path);
454 			else if (argc > 1) {
455 				(void)printf("%s:\n", p->fts_path);
456 				output = 1;
457 			}
458 			chp = fts_children(ftsp, ch_options);
459 			display(p, chp);
460 
461 			if (!f_recursive && chp != NULL)
462 				(void)fts_set(ftsp, p, FTS_SKIP);
463 			break;
464 		default:
465 		}
466 	if (errno)
467 		err(1, "fts_read");
468 }
469 
470 /*
471  * Display() takes a linked list of FTSENT structures and passes the list
472  * along with any other necessary information to the print function.  P
473  * points to the parent directory of the display list.
474  */
475 static void
476 display(FTSENT *p, FTSENT *list)
477 {
478 	struct stat *sp;
479 	DISPLAY d;
480 	FTSENT *cur;
481 	NAMES *np;
482 	off_t maxsize;
483 	u_long btotal, lattrlen, maxblock, maxinode, maxlen, maxnlink, maxlattr;
484 	int bcfile, maxflags;
485 	gid_t maxgroup;
486 	uid_t maxuser;
487 	size_t flen, ulen, glen;
488 	char *initmax;
489 	int entries, needstats;
490 	char *user, *group, *flags, *lattr = NULL;
491 	char buf[STRBUF_SIZEOF(u_quad_t) + 1];
492 	char ngroup[STRBUF_SIZEOF(uid_t) + 1];
493 	char nuser[STRBUF_SIZEOF(gid_t) + 1];
494 
495 	/*
496 	 * If list is NULL there are two possibilities: that the parent
497 	 * directory p has no children, or that fts_children() returned an
498 	 * error.  We ignore the error case since it will be replicated
499 	 * on the next call to fts_read() on the post-order visit to the
500 	 * directory p, and will be signaled in traverse().
501 	 */
502 	if (list == NULL)
503 		return;
504 
505 	needstats = f_inode || f_longform || f_size;
506 	flen = 0;
507 	btotal = 0;
508 	initmax = getenv("LS_COLWIDTHS");
509 	/* Fields match -lios order.  New ones should be added at the end. */
510 	maxlattr = maxblock = maxinode = maxlen = maxnlink =
511 	    maxuser = maxgroup = maxflags = maxsize = 0;
512 	if (initmax != NULL && *initmax != '\0') {
513 		char *initmax2, *jinitmax;
514 		int ninitmax;
515 
516 		/* Fill-in "::" as "0:0:0" for the sake of scanf. */
517 		jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2);
518 		if (jinitmax == NULL)
519 			err(1, NULL);
520 		if (*initmax == ':')
521 			strcpy(initmax2, "0:"), initmax2 += 2;
522 		else
523 			*initmax2++ = *initmax, *initmax2 = '\0';
524 		for (initmax++; *initmax != '\0'; initmax++) {
525 			if (initmax[-1] == ':' && initmax[0] == ':') {
526 				*initmax2++ = '0';
527 				*initmax2++ = initmax[0];
528 				initmax2[1] = '\0';
529 			} else {
530 				*initmax2++ = initmax[0];
531 				initmax2[1] = '\0';
532 			}
533 		}
534 		if (initmax2[-1] == ':')
535 			strcpy(initmax2, "0");
536 
537 		ninitmax = sscanf(jinitmax,
538 		    " %lu : %lu : %lu : %i : %i : %i : %llu : %lu : %lu ",
539 		    &maxinode, &maxblock, &maxnlink, &maxuser,
540 		    &maxgroup, &maxflags, &maxsize, &maxlen, &maxlattr);
541 		f_notabs = 1;
542 		switch (ninitmax) {
543 		case 0:
544 			maxinode = 0;
545 			/* fall through */
546 		case 1:
547 			maxblock = 0;
548 			/* fall through */
549 		case 2:
550 			maxnlink = 0;
551 			/* fall through */
552 		case 3:
553 			maxuser = 0;
554 			/* fall through */
555 		case 4:
556 			maxgroup = 0;
557 			/* fall through */
558 		case 5:
559 			maxflags = 0;
560 			/* fall through */
561 		case 6:
562 			maxsize = 0;
563 			/* fall through */
564 		case 7:
565 			maxlen = 0;
566 			/* fall through */
567 		case 8:
568 			maxlattr = 0;
569 			/* fall through */
570 #ifdef COLORLS
571 			if (!f_color)
572 #endif
573 				f_notabs = 0;
574 			/* fall through */
575 		default:
576 		}
577 		maxinode = makenines(maxinode);
578 		maxblock = makenines(maxblock);
579 		maxnlink = makenines(maxnlink);
580 		maxsize = makenines(maxsize);
581 	}
582 	if (f_lomac)
583 		lomac_start();
584 	bcfile = 0;
585 	flags = NULL;
586 	for (cur = list, entries = 0; cur; cur = cur->fts_link) {
587 		if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
588 			warnx("%s: %s",
589 			    cur->fts_name, strerror(cur->fts_errno));
590 			cur->fts_number = NO_PRINT;
591 			rval = 1;
592 			continue;
593 		}
594 		/*
595 		 * P is NULL if list is the argv list, to which different rules
596 		 * apply.
597 		 */
598 		if (p == NULL) {
599 			/* Directories will be displayed later. */
600 			if (cur->fts_info == FTS_D && !f_listdir) {
601 				cur->fts_number = NO_PRINT;
602 				continue;
603 			}
604 		} else {
605 			/* Only display dot file if -a/-A set. */
606 			if (cur->fts_name[0] == '.' && !f_listdot) {
607 				cur->fts_number = NO_PRINT;
608 				continue;
609 			}
610 		}
611 		if (cur->fts_namelen > maxlen)
612 			maxlen = cur->fts_namelen;
613 		if (f_octal || f_octal_escape) {
614 			u_long t = len_octal(cur->fts_name, cur->fts_namelen);
615 
616 			if (t > maxlen)
617 				maxlen = t;
618 		}
619 		if (needstats) {
620 			sp = cur->fts_statp;
621 			if (sp->st_blocks > maxblock)
622 				maxblock = sp->st_blocks;
623 			if (sp->st_ino > maxinode)
624 				maxinode = sp->st_ino;
625 			if (sp->st_nlink > maxnlink)
626 				maxnlink = sp->st_nlink;
627 			if (sp->st_size > maxsize)
628 				maxsize = sp->st_size;
629 
630 			btotal += sp->st_blocks;
631 			if (f_longform) {
632 				if (f_numericonly) {
633 					(void)snprintf(nuser, sizeof(nuser),
634 					    "%u", sp->st_uid);
635 					(void)snprintf(ngroup, sizeof(ngroup),
636 					    "%u", sp->st_gid);
637 					user = nuser;
638 					group = ngroup;
639 				} else {
640 					user = user_from_uid(sp->st_uid, 0);
641 					group = group_from_gid(sp->st_gid, 0);
642 				}
643 				if ((ulen = strlen(user)) > maxuser)
644 					maxuser = ulen;
645 				if ((glen = strlen(group)) > maxgroup)
646 					maxgroup = glen;
647 				if (f_flags) {
648 					flags = fflagstostr(sp->st_flags);
649 					if (flags != NULL && *flags == '\0') {
650 						free(flags);
651 						flags = strdup("-");
652 					}
653 					if (flags == NULL)
654 						err(1, NULL);
655 					flen = strlen(flags);
656 					if (flen > (size_t)maxflags)
657 						maxflags = flen;
658 				} else
659 					flen = 0;
660 				lattr = NULL;
661 				if (f_lomac) {
662 					lattr = get_lattr(cur);
663 					lattrlen = strlen(lattr);
664 					if (lattrlen > maxlattr)
665 						maxlattr = lattrlen;
666 				} else
667 					lattrlen = 0;
668 
669 				if ((np = malloc(sizeof(NAMES) + lattrlen +
670 				    ulen + glen + flen + 4)) == NULL)
671 					err(1, NULL);
672 
673 				np->user = &np->data[0];
674 				(void)strcpy(np->user, user);
675 				np->group = &np->data[ulen + 1];
676 				(void)strcpy(np->group, group);
677 
678 				if (S_ISCHR(sp->st_mode) ||
679 				    S_ISBLK(sp->st_mode))
680 					bcfile = 1;
681 
682 				if (f_flags) {
683 					np->flags = &np->data[ulen + glen + 2];
684 					(void)strcpy(np->flags, flags);
685 					free(flags);
686 				}
687 				if (f_lomac) {
688 					np->lattr = &np->data[ulen + glen + 2
689 					    + (f_flags ? flen + 1 : 0)];
690 					(void)strcpy(np->lattr, lattr);
691 					free(lattr);
692 				}
693 				cur->fts_pointer = np;
694 			}
695 		}
696 		++entries;
697 	}
698 
699 	if (!entries)
700 		return;
701 
702 	d.list = list;
703 	d.entries = entries;
704 	d.maxlen = maxlen;
705 	if (needstats) {
706 		d.bcfile = bcfile;
707 		d.btotal = btotal;
708 		(void)snprintf(buf, sizeof(buf), "%lu", maxblock);
709 		d.s_block = strlen(buf);
710 		d.s_flags = maxflags;
711 		d.s_lattr = maxlattr;
712 		d.s_group = maxgroup;
713 		(void)snprintf(buf, sizeof(buf), "%lu", maxinode);
714 		d.s_inode = strlen(buf);
715 		(void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
716 		d.s_nlink = strlen(buf);
717 		(void)snprintf(buf, sizeof(buf), "%qu", maxsize);
718 		d.s_size = strlen(buf);
719 		d.s_user = maxuser;
720 	}
721 	printfcn(&d);
722 	output = 1;
723 
724 	if (f_longform)
725 		for (cur = list; cur; cur = cur->fts_link)
726 			free(cur->fts_pointer);
727 	if (f_lomac)
728 		lomac_stop();
729 }
730 
731 /*
732  * Ordering for mastercmp:
733  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
734  * as larger than directories.  Within either group, use the sort function.
735  * All other levels use the sort function.  Error entries remain unsorted.
736  */
737 static int
738 mastercmp(const FTSENT **a, const FTSENT **b)
739 {
740 	int a_info, b_info;
741 
742 	a_info = (*a)->fts_info;
743 	if (a_info == FTS_ERR)
744 		return (0);
745 	b_info = (*b)->fts_info;
746 	if (b_info == FTS_ERR)
747 		return (0);
748 
749 	if (a_info == FTS_NS || b_info == FTS_NS)
750 		return (namecmp(*a, *b));
751 
752 	if (a_info != b_info &&
753 	    (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
754 		if (a_info == FTS_D)
755 			return (1);
756 		if (b_info == FTS_D)
757 			return (-1);
758 	}
759 	return (sortfcn(*a, *b));
760 }
761 
762 /*
763  * Makenines() returns (10**n)-1.  This is useful for converting a width
764  * into a number that wide in decimal.
765  */
766 static u_quad_t
767 makenines(u_long n)
768 {
769 	u_long i;
770 	u_quad_t reg;
771 
772 	reg = 1;
773 	/* Use a loop instead of pow(), since all values of n are small. */
774 	for (i = 0; i < n; i++)
775 		reg *= 10;
776 	reg--;
777 
778 	return reg;
779 }
780