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