ls.c revision 88591
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 88591 2001-12-28 20:50:12Z joe $";
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 __P((FTSENT *, FTSENT *));
83static u_quad_t	 makenines __P((u_long));
84static int	 mastercmp __P((const FTSENT **, const FTSENT **));
85static void	 traverse __P((int, char **, int));
86
87static void (*printfcn) __P((DISPLAY *));
88static int (*sortfcn) __P((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(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
141#ifdef COLORLS
142	char termcapbuf[1024];		/* termcap definition buffer */
143	char tcapbuf[512];		/* capability buffer */
144	char *bp = tcapbuf;
145#endif
146
147	(void) setlocale(LC_ALL, "");
148
149	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
150	if (isatty(STDOUT_FILENO)) {
151		if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
152		    !win.ws_col) {
153			if ((p = getenv("COLUMNS")) != NULL)
154				termwidth = atoi(p);
155		}
156		else
157			termwidth = win.ws_col;
158		f_column = f_nonprint = 1;
159	} else {
160		f_singlecol = 1;
161		/* retrieve environment variable, in case of explicit -C */
162		if ((p = getenv("COLUMNS")))
163			termwidth = atoi(p);
164	}
165
166	/* Root is -A automatically. */
167	if (!getuid())
168		f_listdot = 1;
169
170	fts_options = FTS_PHYSICAL;
171 	while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklnoqrstu")) != -1) {
172		switch (ch) {
173		/*
174		 * The -1, -C and -l options all override each other so shell
175		 * aliasing works right.
176		 */
177		case '1':
178			f_singlecol = 1;
179			f_column = f_longform = 0;
180			break;
181		case 'B':
182			f_nonprint = 0;
183			f_octal = 1;
184		        f_octal_escape = 0;
185			break;
186		case 'C':
187			f_column = 1;
188			f_longform = f_singlecol = 0;
189			break;
190		case 'l':
191			f_longform = 1;
192			f_column = f_singlecol = 0;
193			break;
194		/* The -c and -u options override each other. */
195		case 'c':
196			f_statustime = 1;
197			f_accesstime = 0;
198			break;
199		case 'u':
200			f_accesstime = 1;
201			f_statustime = 0;
202			break;
203		case 'F':
204			f_type = 1;
205			break;
206		case 'H':
207		        fts_options |= FTS_COMFOLLOW;
208			break;
209		case 'G':
210			setenv("CLICOLOR", "", 1);
211			break;
212		case 'L':
213			fts_options &= ~FTS_PHYSICAL;
214			fts_options |= FTS_LOGICAL;
215			break;
216		case 'P':
217		        fts_options &= ~FTS_COMFOLLOW;
218			fts_options &= ~FTS_LOGICAL;
219			fts_options |= FTS_PHYSICAL;
220			break;
221		case 'R':
222			f_recursive = 1;
223			break;
224		case 'a':
225			fts_options |= FTS_SEEDOT;
226			/* FALLTHROUGH */
227		case 'A':
228			f_listdot = 1;
229			break;
230		/* The -d option turns off the -R option. */
231		case 'd':
232			f_listdir = 1;
233			f_recursive = 0;
234			break;
235		case 'f':
236			f_nosort = 1;
237			break;
238		case 'g':		/* Compatibility with 4.3BSD. */
239			break;
240		case 'h':
241			f_humanval = 1;
242			break;
243		case 'i':
244			f_inode = 1;
245			break;
246		case 'k':
247			f_kblocks = 1;
248			break;
249		case 'n':
250			f_numericonly = 1;
251			break;
252		case 'o':
253			f_flags = 1;
254			break;
255		case 'q':
256			f_nonprint = 1;
257			f_octal = 0;
258		        f_octal_escape = 0;
259			break;
260		case 'r':
261			f_reversesort = 1;
262			break;
263		case 's':
264			f_size = 1;
265			break;
266		case 'T':
267			f_sectime = 1;
268			break;
269		case 't':
270			f_timesort = 1;
271			break;
272		case 'W':
273			f_whiteout = 1;
274			break;
275		case 'b':
276			f_nonprint = 0;
277		        f_octal = 0;
278			f_octal_escape = 1;
279			break;
280		case 'Z':
281			f_lomac = 1;
282			break;
283		default:
284		case '?':
285			usage();
286		}
287	}
288	argc -= optind;
289	argv += optind;
290
291	/* Enabling of colours is conditional on the environment. */
292	if (getenv("CLICOLOR") &&
293	    (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
294#ifdef COLORLS
295		if (tgetent(termcapbuf, getenv("TERM")) == 1) {
296			ansi_fgcol = tgetstr("AF", &bp);
297			ansi_bgcol = tgetstr("AB", &bp);
298			attrs_off = tgetstr("me", &bp);
299			enter_bold = tgetstr("md", &bp);
300
301			/* To switch colours off use 'op' if
302			 * available, otherwise use 'oc', or
303			 * don't do colours at all. */
304			ansi_coloff = tgetstr("op", &bp);
305			if (!ansi_coloff)
306				ansi_coloff = tgetstr("oc", &bp);
307			if (ansi_fgcol && ansi_bgcol && ansi_coloff)
308				f_color = 1;
309		}
310#else
311		(void)fprintf(stderr, "Color support not compiled in.\n");
312#endif /*COLORLS*/
313
314#ifdef COLORLS
315	if (f_color) {
316		/*
317		 * We can't put tabs and color sequences together:
318		 * column number will be incremented incorrectly
319		 * for "stty oxtabs" mode.
320		 */
321		f_notabs = 1;
322		(void) signal(SIGINT, colorquit);
323		(void) signal(SIGQUIT, colorquit);
324		parsecolors(getenv("LSCOLORS"));
325	}
326#endif
327
328	/*
329	 * If not -F, -i, -l, -s or -t options, don't require stat
330	 * information, unless in color mode in which case we do
331	 * need this to determine which colors to display.
332	 */
333	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
334#ifdef COLORLS
335	    && !f_color
336#endif
337	   )
338		fts_options |= FTS_NOSTAT;
339
340	/*
341	 * If not -F, -d or -l options, follow any symbolic links listed on
342	 * the command line.
343	 */
344	if (!f_longform && !f_listdir && !f_type)
345		fts_options |= FTS_COMFOLLOW;
346
347	/*
348	 * If -W, show whiteout entries
349	 */
350#ifdef FTS_WHITEOUT
351	if (f_whiteout)
352		fts_options |= FTS_WHITEOUT;
353#endif
354
355	/* If -l or -s, figure out block size. */
356	if (f_longform || f_size) {
357		if (f_kblocks)
358			blocksize = 2;
359		else {
360			(void)getbsize(&notused, &blocksize);
361			blocksize /= 512;
362		}
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(argc, argv, options)
411	int argc, options;
412	char *argv[];
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
459			chp = fts_children(ftsp, ch_options);
460			display(p, chp);
461
462			if (!f_recursive && chp != NULL)
463				(void)fts_set(ftsp, p, FTS_SKIP);
464			break;
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 */
475static void
476display(p, list)
477	FTSENT *p, *list;
478{
479	struct stat *sp;
480	DISPLAY d;
481	FTSENT *cur;
482	NAMES *np;
483	u_quad_t maxsize;
484	u_long btotal, maxblock, maxinode, maxlen, maxnlink, maxlattr;
485	int bcfile, flen, glen, ulen, lattrlen, maxflags, maxgroup, maxuser;
486	char *initmax;
487	int entries, needstats;
488	char *user, *group, *flags, *lattr;
489	char buf[STRBUF_SIZEOF(u_quad_t) + 1];
490	char ngroup[STRBUF_SIZEOF(uid_t) + 1];
491	char nuser[STRBUF_SIZEOF(gid_t) + 1];
492
493	/*
494	 * If list is NULL there are two possibilities: that the parent
495	 * directory p has no children, or that fts_children() returned an
496	 * error.  We ignore the error case since it will be replicated
497	 * on the next call to fts_read() on the post-order visit to the
498	 * directory p, and will be signaled in traverse().
499	 */
500	if (list == NULL)
501		return;
502
503	needstats = f_inode || f_longform || f_size;
504	flen = 0;
505	btotal = 0;
506	initmax = getenv("LS_COLWIDTHS");
507	/* Fields match -lios order.  New ones should be added at the end. */
508	if (initmax != NULL && *initmax != '\0') {
509		char *initmax2, *jinitmax;
510		int ninitmax;
511
512		/* Fill-in "::" as "0:0:0" for the sake of scanf. */
513		jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2);
514		if (jinitmax == NULL)
515			err(1, NULL);
516		if (*initmax == ':')
517			strcpy(initmax2, "0:"), initmax2 += 2;
518		else
519			*initmax2++ = *initmax, *initmax2 = '\0';
520		for (initmax++; *initmax != '\0'; initmax++) {
521			if (initmax[-1] == ':' && initmax[0] == ':') {
522				*initmax2++ = '0';
523				*initmax2++ = initmax[0];
524				initmax2[1] = '\0';
525			} else {
526				*initmax2++ = initmax[0];
527				initmax2[1] = '\0';
528			}
529		}
530		if (initmax2[-1] == ':') strcpy(initmax2, "0");
531
532		ninitmax = sscanf(jinitmax,
533		    " %lu : %lu : %lu : %i : %i : %i : %qu : %lu : %lu ",
534		    &maxinode, &maxblock, &maxnlink, &maxuser,
535		    &maxgroup, &maxflags, &maxsize, &maxlen, &maxlattr);
536		f_notabs = 1;
537		switch (ninitmax) {
538		 case 0: maxinode = 0;
539		 case 1: maxblock = 0;
540		 case 2: maxnlink = 0;
541		 case 3: maxuser  = 0;
542		 case 4: maxgroup = 0;
543		 case 5: maxflags = 0;
544		 case 6: maxsize  = 0;
545		 case 7: maxlen   = 0;
546		 case 8: maxlattr = 0;
547#ifdef COLORLS
548		 if (!f_color)
549#endif
550			 f_notabs = 0;
551		}
552		maxinode = makenines(maxinode);
553		maxblock = makenines(maxblock);
554		maxnlink = makenines(maxnlink);
555		maxsize = makenines(maxsize);
556	} else if (initmax == NULL || *initmax == '\0')
557		maxlattr = maxblock = maxinode = maxlen = maxnlink =
558		    maxuser = maxgroup = maxflags = maxsize = 0;
559	if (f_lomac)
560		lomac_start();
561	bcfile = 0;
562	flags = NULL;
563	for (cur = list, entries = 0; cur; cur = cur->fts_link) {
564		if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
565			warnx("%s: %s",
566			    cur->fts_name, strerror(cur->fts_errno));
567			cur->fts_number = NO_PRINT;
568			rval = 1;
569			continue;
570		}
571
572		/*
573		 * P is NULL if list is the argv list, to which different rules
574		 * apply.
575		 */
576		if (p == NULL) {
577			/* Directories will be displayed later. */
578			if (cur->fts_info == FTS_D && !f_listdir) {
579				cur->fts_number = NO_PRINT;
580				continue;
581			}
582		} else {
583			/* Only display dot file if -a/-A set. */
584			if (cur->fts_name[0] == '.' && !f_listdot) {
585				cur->fts_number = NO_PRINT;
586				continue;
587			}
588		}
589		if (cur->fts_namelen > maxlen)
590			maxlen = cur->fts_namelen;
591		if (f_octal || f_octal_escape) {
592		        u_long t = len_octal(cur->fts_name, cur->fts_namelen);
593			if (t > maxlen) maxlen = t;
594		}
595		if (needstats) {
596			sp = cur->fts_statp;
597			if (sp->st_blocks > maxblock)
598				maxblock = sp->st_blocks;
599			if (sp->st_ino > maxinode)
600				maxinode = sp->st_ino;
601			if (sp->st_nlink > maxnlink)
602				maxnlink = sp->st_nlink;
603			if (sp->st_size > maxsize)
604				maxsize = sp->st_size;
605
606			btotal += sp->st_blocks;
607			if (f_longform) {
608				if (f_numericonly) {
609					(void)snprintf(nuser, sizeof(nuser),
610					    "%u", sp->st_uid);
611					(void)snprintf(ngroup, sizeof(ngroup),
612					    "%u", sp->st_gid);
613					user = nuser;
614					group = ngroup;
615				} else {
616					user = user_from_uid(sp->st_uid, 0);
617					group = group_from_gid(sp->st_gid, 0);
618				}
619				if ((ulen = strlen(user)) > maxuser)
620					maxuser = ulen;
621				if ((glen = strlen(group)) > maxgroup)
622					maxgroup = glen;
623				if (f_flags) {
624					flags = fflagstostr(sp->st_flags);
625					if (flags != NULL && *flags == '\0') {
626						free(flags);
627						flags = strdup("-");
628					}
629					if (flags == NULL)
630						err(1, NULL);
631					if ((flen = strlen(flags)) > maxflags)
632						maxflags = flen;
633				} else
634					flen = 0;
635				if (f_lomac) {
636					lattr = get_lattr(cur);
637					lattrlen = strlen(lattr);
638					if (lattrlen > maxlattr)
639						maxlattr = lattrlen;
640				} else
641					lattrlen = 0;
642
643				if ((np = malloc(sizeof(NAMES) + lattrlen +
644				    ulen + glen + flen + 4)) == NULL)
645					err(1, NULL);
646
647				np->user = &np->data[0];
648				(void)strcpy(np->user, user);
649				np->group = &np->data[ulen + 1];
650				(void)strcpy(np->group, group);
651
652				if (S_ISCHR(sp->st_mode) ||
653				    S_ISBLK(sp->st_mode))
654					bcfile = 1;
655
656				if (f_flags) {
657					np->flags = &np->data[ulen + glen + 2];
658				  	(void)strcpy(np->flags, flags);
659					free(flags);
660				}
661				if (f_lomac) {
662					np->lattr = &np->data[ulen + glen + 2
663					    + (f_flags ? flen + 1 : 0)];
664					(void)strcpy(np->lattr, lattr);
665					free(lattr);
666				}
667				cur->fts_pointer = np;
668			}
669		}
670		++entries;
671	}
672
673	if (!entries)
674		return;
675
676	d.list = list;
677	d.entries = entries;
678	d.maxlen = maxlen;
679	if (needstats) {
680		d.bcfile = bcfile;
681		d.btotal = btotal;
682		(void)snprintf(buf, sizeof(buf), "%lu", maxblock);
683		d.s_block = strlen(buf);
684		d.s_flags = maxflags;
685		d.s_lattr = maxlattr;
686		d.s_group = maxgroup;
687		(void)snprintf(buf, sizeof(buf), "%lu", maxinode);
688		d.s_inode = strlen(buf);
689		(void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
690		d.s_nlink = strlen(buf);
691		(void)snprintf(buf, sizeof(buf), "%qu", maxsize);
692		d.s_size = strlen(buf);
693		d.s_user = maxuser;
694	}
695
696	printfcn(&d);
697	output = 1;
698
699	if (f_longform)
700		for (cur = list; cur; cur = cur->fts_link)
701			free(cur->fts_pointer);
702	if (f_lomac)
703		lomac_stop();
704}
705
706/*
707 * Ordering for mastercmp:
708 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
709 * as larger than directories.  Within either group, use the sort function.
710 * All other levels use the sort function.  Error entries remain unsorted.
711 */
712static int
713mastercmp(a, b)
714	const FTSENT **a, **b;
715{
716	int a_info, b_info;
717
718	a_info = (*a)->fts_info;
719	if (a_info == FTS_ERR)
720		return (0);
721	b_info = (*b)->fts_info;
722	if (b_info == FTS_ERR)
723		return (0);
724
725	if (a_info == FTS_NS || b_info == FTS_NS)
726		return (namecmp(*a, *b));
727
728	if (a_info != b_info &&
729	    (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
730		if (a_info == FTS_D)
731			return (1);
732		if (b_info == FTS_D)
733			return (-1);
734	}
735	return (sortfcn(*a, *b));
736}
737
738/*
739 * Makenines() returns (10**n)-1.  This is useful for converting a width
740 * into a number that wide in decimal.
741 */
742static u_quad_t
743makenines(n)
744	u_long n;
745{
746	u_long i;
747	u_quad_t reg;
748
749	reg = 1;
750	/* Use a loop instead of pow(), since all values of n are small. */
751	for (i = 0; i < n; i++)
752		reg *= 10;
753	reg--;
754
755	return reg;
756}
757