ls.c revision 88602
1228879Sed/*
2228879Sed * Copyright (c) 1989, 1993, 1994
3228879Sed *	The Regents of the University of California.  All rights reserved.
4228879Sed *
5228879Sed * This code is derived from software contributed to Berkeley by
6228879Sed * Michael Fischbein.
7228879Sed *
8228879Sed * Redistribution and use in source and binary forms, with or without
9228879Sed * modification, are permitted provided that the following conditions
10228879Sed * are met:
11228879Sed * 1. Redistributions of source code must retain the above copyright
12228879Sed *    notice, this list of conditions and the following disclaimer.
13228879Sed * 2. Redistributions in binary form must reproduce the above copyright
14228879Sed *    notice, this list of conditions and the following disclaimer in the
15228879Sed *    documentation and/or other materials provided with the distribution.
16228879Sed * 3. All advertising materials mentioning features or use of this software
17228879Sed *    must display the following acknowledgement:
18228879Sed *	This product includes software developed by the University of
19228879Sed *	California, Berkeley and its contributors.
20228879Sed * 4. Neither the name of the University nor the names of its contributors
21228879Sed *    may be used to endorse or promote products derived from this software
22228879Sed *    without specific prior written permission.
23228879Sed *
24228879Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25228879Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26228879Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27228879Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28228879Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29229437Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30229437Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31229437Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32229437Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33228879Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34228879Sed * SUCH DAMAGE.
35228879Sed */
36228879Sed
37228879Sed#ifndef lint
38228879Sedstatic 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 88602 2001-12-29 00:18:16Z 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#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, "1ABCFGHLPRTWZabcdfghiklnoqrstu")) != -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 'Z':
279			f_lomac = 1;
280			break;
281		default:
282		case '?':
283			usage();
284		}
285	}
286	argc -= optind;
287	argv += optind;
288
289	/* Enabling of colours is conditional on the environment. */
290	if (getenv("CLICOLOR") &&
291	    (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
292#ifdef COLORLS
293		if (tgetent(termcapbuf, getenv("TERM")) == 1) {
294			ansi_fgcol = tgetstr("AF", &bp);
295			ansi_bgcol = tgetstr("AB", &bp);
296			attrs_off = tgetstr("me", &bp);
297			enter_bold = tgetstr("md", &bp);
298
299			/* To switch colours off use 'op' if
300			 * available, otherwise use 'oc', or
301			 * don't do colours at all. */
302			ansi_coloff = tgetstr("op", &bp);
303			if (!ansi_coloff)
304				ansi_coloff = tgetstr("oc", &bp);
305			if (ansi_fgcol && ansi_bgcol && ansi_coloff)
306				f_color = 1;
307		}
308#else
309		(void)fprintf(stderr, "Color support not compiled in.\n");
310#endif /*COLORLS*/
311
312#ifdef COLORLS
313	if (f_color) {
314		/*
315		 * We can't put tabs and color sequences together:
316		 * column number will be incremented incorrectly
317		 * for "stty oxtabs" mode.
318		 */
319		f_notabs = 1;
320		(void)signal(SIGINT, colorquit);
321		(void)signal(SIGQUIT, colorquit);
322		parsecolors(getenv("LSCOLORS"));
323	}
324#endif
325
326	/*
327	 * If not -F, -i, -l, -s or -t options, don't require stat
328	 * information, unless in color mode in which case we do
329	 * need this to determine which colors to display.
330	 */
331	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
332#ifdef COLORLS
333	    && !f_color
334#endif
335	    )
336		fts_options |= FTS_NOSTAT;
337
338	/*
339	 * If not -F, -d or -l options, follow any symbolic links listed on
340	 * the command line.
341	 */
342	if (!f_longform && !f_listdir && !f_type)
343		fts_options |= FTS_COMFOLLOW;
344
345	/*
346	 * If -W, show whiteout entries
347	 */
348#ifdef FTS_WHITEOUT
349	if (f_whiteout)
350		fts_options |= FTS_WHITEOUT;
351#endif
352
353	/* If -l or -s, figure out block size. */
354	if (f_longform || f_size) {
355		if (f_kblocks)
356			blocksize = 2;
357		else {
358			(void)getbsize(&notused, &blocksize);
359			blocksize /= 512;
360		}
361	}
362	/* Select a sort function. */
363	if (f_reversesort) {
364		if (!f_timesort)
365			sortfcn = revnamecmp;
366		else if (f_accesstime)
367			sortfcn = revacccmp;
368		else if (f_statustime)
369			sortfcn = revstatcmp;
370		else		/* Use modification time. */
371			sortfcn = revmodcmp;
372	} else {
373		if (!f_timesort)
374			sortfcn = namecmp;
375		else if (f_accesstime)
376			sortfcn = acccmp;
377		else if (f_statustime)
378			sortfcn = statcmp;
379		else		/* Use modification time. */
380			sortfcn = modcmp;
381	}
382
383	/* Select a print function. */
384	if (f_singlecol)
385		printfcn = printscol;
386	else if (f_longform)
387		printfcn = printlong;
388	else
389		printfcn = printcol;
390
391	if (argc)
392		traverse(argc, argv, fts_options);
393	else
394		traverse(1, dotav, fts_options);
395	exit(rval);
396}
397
398static int output;		/* If anything output. */
399
400/*
401 * Traverse() walks the logical directory structure specified by the argv list
402 * in the order specified by the mastercmp() comparison function.  During the
403 * traversal it passes linked lists of structures to display() which represent
404 * a superset (may be exact set) of the files to be displayed.
405 */
406static void
407traverse(argc, argv, options)
408	int argc, options;
409	char *argv[];
410{
411	FTS *ftsp;
412	FTSENT *p, *chp;
413	int ch_options;
414
415	if ((ftsp =
416	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
417		err(1, NULL);
418
419	display(NULL, fts_children(ftsp, 0));
420	if (f_listdir)
421		return;
422
423	/*
424	 * If not recursing down this tree and don't need stat info, just get
425	 * the names.
426	 */
427	ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
428
429	while ((p = fts_read(ftsp)) != NULL)
430		switch (p->fts_info) {
431		case FTS_DC:
432			warnx("%s: directory causes a cycle", p->fts_name);
433			break;
434		case FTS_DNR:
435		case FTS_ERR:
436			warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
437			rval = 1;
438			break;
439		case FTS_D:
440			if (p->fts_level != FTS_ROOTLEVEL &&
441			    p->fts_name[0] == '.' && !f_listdot)
442				break;
443
444			/*
445			 * If already output something, put out a newline as
446			 * a separator.  If multiple arguments, precede each
447			 * directory with its name.
448			 */
449			if (output)
450				(void)printf("\n%s:\n", p->fts_path);
451			else if (argc > 1) {
452				(void)printf("%s:\n", p->fts_path);
453				output = 1;
454			}
455			chp = fts_children(ftsp, ch_options);
456			display(p, chp);
457
458			if (!f_recursive && chp != NULL)
459				(void)fts_set(ftsp, p, FTS_SKIP);
460			break;
461		}
462	if (errno)
463		err(1, "fts_read");
464}
465
466/*
467 * Display() takes a linked list of FTSENT structures and passes the list
468 * along with any other necessary information to the print function.  P
469 * points to the parent directory of the display list.
470 */
471static void
472display(p, list)
473	FTSENT *p, *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(a, b)
720	const FTSENT **a, **b;
721{
722	int a_info, b_info;
723
724	a_info = (*a)->fts_info;
725	if (a_info == FTS_ERR)
726		return (0);
727	b_info = (*b)->fts_info;
728	if (b_info == FTS_ERR)
729		return (0);
730
731	if (a_info == FTS_NS || b_info == FTS_NS)
732		return (namecmp(*a, *b));
733
734	if (a_info != b_info &&
735	    (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
736		if (a_info == FTS_D)
737			return (1);
738		if (b_info == FTS_D)
739			return (-1);
740	}
741	return (sortfcn(*a, *b));
742}
743
744/*
745 * Makenines() returns (10**n)-1.  This is useful for converting a width
746 * into a number that wide in decimal.
747 */
748static u_quad_t
749makenines(n)
750	u_long n;
751{
752	u_long i;
753	u_quad_t reg;
754
755	reg = 1;
756	/* Use a loop instead of pow(), since all values of n are small. */
757	for (i = 0; i < n; i++)
758		reg *= 10;
759	reg--;
760
761	return reg;
762}
763