xref: /freebsd/bin/ls/ls.c (revision 55926a66)
19ddb49cbSWarner Losh /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Michael Fischbein.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
34d46c1a60SSteve Price static const char copyright[] =
354b88c807SRodney W. Grimes "@(#) Copyright (c) 1989, 1993, 1994\n\
364b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
37febad2fcSSteve Price #endif /* not lint */
38febad2fcSSteve Price 
39febad2fcSSteve Price #if 0
40c73d77ceSMark Murray #ifndef lint
41febad2fcSSteve Price static char sccsid[] = "@(#)ls.c	8.5 (Berkeley) 4/2/94";
424b88c807SRodney W. Grimes #endif /* not lint */
43c73d77ceSMark Murray #endif
445eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
455eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes #include <sys/types.h>
484b88c807SRodney W. Grimes #include <sys/stat.h>
494b88c807SRodney W. Grimes #include <sys/ioctl.h>
504d33b62eSRobert Watson #include <sys/mac.h>
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes #include <dirent.h>
534b88c807SRodney W. Grimes #include <err.h>
544b88c807SRodney W. Grimes #include <errno.h>
554b88c807SRodney W. Grimes #include <fts.h>
56576541a9SWarner Losh #include <grp.h>
5740feca3aSMark Murray #include <inttypes.h>
58008a4910SSheldon Hearn #include <limits.h>
59008a4910SSheldon Hearn #include <locale.h>
60576541a9SWarner Losh #include <pwd.h>
614b88c807SRodney W. Grimes #include <stdio.h>
624b88c807SRodney W. Grimes #include <stdlib.h>
634b88c807SRodney W. Grimes #include <string.h>
644b88c807SRodney W. Grimes #include <unistd.h>
65bd82d8abSAndrey A. Chernov #ifdef COLORLS
66bd82d8abSAndrey A. Chernov #include <termcap.h>
67bd82d8abSAndrey A. Chernov #include <signal.h>
68bd82d8abSAndrey A. Chernov #endif
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes #include "ls.h"
714b88c807SRodney W. Grimes #include "extern.h"
724b88c807SRodney W. Grimes 
73008a4910SSheldon Hearn /*
74008a4910SSheldon Hearn  * Upward approximation of the maximum number of characters needed to
75008a4910SSheldon Hearn  * represent a value of integral type t as a string, excluding the
76008a4910SSheldon Hearn  * NUL terminator, with provision for a sign.
77008a4910SSheldon Hearn  */
781f94b779SSheldon Hearn #define	STRBUF_SIZEOF(t)	(1 + CHAR_BIT * sizeof(t) / 3 + 1)
79008a4910SSheldon Hearn 
8040feca3aSMark Murray /*
8140feca3aSMark Murray  * MAKENINES(n) turns n into (10**n)-1.  This is useful for converting a width
8240feca3aSMark Murray  * into a number that wide in decimal.
8340feca3aSMark Murray  * XXX: Overflows are not considered.
8440feca3aSMark Murray  */
8540feca3aSMark Murray #define MAKENINES(n)							\
8640feca3aSMark Murray 	do {								\
8740feca3aSMark Murray 		intmax_t i;						\
8840feca3aSMark Murray 									\
8940feca3aSMark Murray 		/* Use a loop as all values of n are small. */		\
9040feca3aSMark Murray 		for (i = 1; n > 0; i *= 10)				\
9140feca3aSMark Murray 			n--;						\
9240feca3aSMark Murray 		n = i - 1;						\
9340feca3aSMark Murray 	} while(0)
9440feca3aSMark Murray 
9540feca3aSMark Murray static void	 display(const FTSENT *, FTSENT *, int);
960d3bcc2eSGarrett Wollman static int	 mastercmp(const FTSENT * const *, const FTSENT * const *);
9746251ddeSWarner Losh static void	 traverse(int, char **, int);
984b88c807SRodney W. Grimes 
9940feca3aSMark Murray static void (*printfcn)(const DISPLAY *);
10046251ddeSWarner Losh static int (*sortfcn)(const FTSENT *, const FTSENT *);
1014b88c807SRodney W. Grimes 
1024b88c807SRodney W. Grimes long blocksize;			/* block size units */
1034b88c807SRodney W. Grimes int termwidth = 80;		/* default terminal width */
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes /* flags */
1064b88c807SRodney W. Grimes        int f_accesstime;	/* use time of last access */
107fe79420eSJohn Baldwin        int f_birthtime;		/* use time of birth */
1084b88c807SRodney W. Grimes        int f_flags;		/* show flags associated with a file */
1090e8d1551SJosef Karthauser        int f_humanval;		/* show human-readable file sizes */
1104b88c807SRodney W. Grimes        int f_inode;		/* print inode */
1119052855aSMark Murray static int f_kblocks;		/* print size in kilobytes */
1129052855aSMark Murray static int f_listdir;		/* list actual directory, not contents */
1139052855aSMark Murray static int f_listdot;		/* list files beginning with . */
114390a478eSRuslan Ermilov static int f_noautodot;		/* do not automatically enable -A for root */
1154b88c807SRodney W. Grimes        int f_longform;		/* long listing format */
1164b88c807SRodney W. Grimes        int f_nonprint;		/* show unprintables as ? */
1179052855aSMark Murray static int f_nosort;		/* don't sort output */
118008a4910SSheldon Hearn        int f_notabs;		/* don't use tab-separated multi-col output */
1199052855aSMark Murray static int f_numericonly;	/* don't convert uid/gid to name */
1207ea30648SDag-Erling Smørgrav        int f_octal;		/* show unprintables as \xxx */
1210d86878cSDag-Erling Smørgrav        int f_octal_escape;	/* like f_octal but use C escapes if possible */
1229052855aSMark Murray static int f_recursive;		/* ls subdirectories also */
1239052855aSMark Murray static int f_reversesort;	/* reverse whatever sort is used */
1244b88c807SRodney W. Grimes        int f_sectime;		/* print the real time for all files */
1259052855aSMark Murray static int f_singlecol;		/* use single column output */
1264b88c807SRodney W. Grimes        int f_size;		/* list size in short listing */
12794274c73STim J. Robbins        int f_slash;		/* similar to f_type, but only for dirs */
12894274c73STim J. Robbins        int f_sortacross;	/* sort across rows, not down columns */
1294b88c807SRodney W. Grimes        int f_statustime;	/* use time of last mode change */
13040feca3aSMark Murray static int f_stream;		/* stream the output, separate with commas */
1319052855aSMark Murray static int f_timesort;		/* sort by time vice name */
1322269fa57SGreg Lehey        char *f_timeformat;      /* user-specified time format */
13371b8b748SDima Dorfman static int f_sizesort;
1344b88c807SRodney W. Grimes        int f_type;		/* add type character for non-regular files */
1359052855aSMark Murray static int f_whiteout;		/* show whiteout entries */
1364d33b62eSRobert Watson        int f_label;		/* show MAC label */
13774985094SJosef Karthauser #ifdef COLORLS
1383885812cSJosef Karthauser        int f_color;		/* add type in color for non-regular files */
1395a890e22SJosef Karthauser 
1405a890e22SJosef Karthauser char *ansi_bgcol;		/* ANSI sequence to set background colour */
1415a890e22SJosef Karthauser char *ansi_fgcol;		/* ANSI sequence to set foreground colour */
1425a890e22SJosef Karthauser char *ansi_coloff;		/* ANSI sequence to reset colours */
143c1499cf6SJosef Karthauser char *attrs_off;		/* ANSI sequence to turn off attributes */
144c1499cf6SJosef Karthauser char *enter_bold;		/* ANSI sequence to set color to bold mode */
14574985094SJosef Karthauser #endif
1464b88c807SRodney W. Grimes 
1479052855aSMark Murray static int rval;
148fb1000d6SAdam David 
1494b88c807SRodney W. Grimes int
15046251ddeSWarner Losh main(int argc, char *argv[])
1514b88c807SRodney W. Grimes {
1524b88c807SRodney W. Grimes 	static char dot[] = ".", *dotav[] = {dot, NULL};
1534b88c807SRodney W. Grimes 	struct winsize win;
1544b88c807SRodney W. Grimes 	int ch, fts_options, notused;
1554b88c807SRodney W. Grimes 	char *p;
1565a890e22SJosef Karthauser #ifdef COLORLS
1575a890e22SJosef Karthauser 	char termcapbuf[1024];	/* termcap definition buffer */
1585a890e22SJosef Karthauser 	char tcapbuf[512];	/* capability buffer */
1595a890e22SJosef Karthauser 	char *bp = tcapbuf;
1605a890e22SJosef Karthauser #endif
1615a890e22SJosef Karthauser 
162f5bd01c6SAndrey A. Chernov 	(void)setlocale(LC_ALL, "");
163f5bd01c6SAndrey A. Chernov 
1644b88c807SRodney W. Grimes 	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
1654b88c807SRodney W. Grimes 	if (isatty(STDOUT_FILENO)) {
166a28edf9aSTim J. Robbins 		termwidth = 80;
167a28edf9aSTim J. Robbins 		if ((p = getenv("COLUMNS")) != NULL && *p != '\0')
1684b88c807SRodney W. Grimes 			termwidth = atoi(p);
169a28edf9aSTim J. Robbins 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
170a28edf9aSTim J. Robbins 		    win.ws_col > 0)
1714b88c807SRodney W. Grimes 			termwidth = win.ws_col;
1729052855aSMark Murray 		f_nonprint = 1;
17334994fcdSJoerg Wunsch 	} else {
1744b88c807SRodney W. Grimes 		f_singlecol = 1;
17534994fcdSJoerg Wunsch 		/* retrieve environment variable, in case of explicit -C */
1769052855aSMark Murray 		p = getenv("COLUMNS");
1779052855aSMark Murray 		if (p)
17834994fcdSJoerg Wunsch 			termwidth = atoi(p);
17934994fcdSJoerg Wunsch 	}
1804b88c807SRodney W. Grimes 
1814b88c807SRodney W. Grimes 	fts_options = FTS_PHYSICAL;
18271b8b748SDima Dorfman  	while ((ch = getopt(argc, argv,
1832269fa57SGreg Lehey 	    "1ABCD:FGHILPRSTUWZabcdfghiklmnopqrstuwx")) != -1) {
1844b88c807SRodney W. Grimes 		switch (ch) {
1854b88c807SRodney W. Grimes 		/*
18694274c73STim J. Robbins 		 * The -1, -C, -x and -l options all override each other so
18794274c73STim J. Robbins 		 * shell aliasing works right.
1884b88c807SRodney W. Grimes 		 */
1894b88c807SRodney W. Grimes 		case '1':
1904b88c807SRodney W. Grimes 			f_singlecol = 1;
1919052855aSMark Murray 			f_longform = 0;
19294274c73STim J. Robbins 			f_stream = 0;
1934b88c807SRodney W. Grimes 			break;
1940d86878cSDag-Erling Smørgrav 		case 'B':
1950d86878cSDag-Erling Smørgrav 			f_nonprint = 0;
1960d86878cSDag-Erling Smørgrav 			f_octal = 1;
1970d86878cSDag-Erling Smørgrav 			f_octal_escape = 0;
1980d86878cSDag-Erling Smørgrav 			break;
1994b88c807SRodney W. Grimes 		case 'C':
20094274c73STim J. Robbins 			f_sortacross = f_longform = f_singlecol = 0;
2014b88c807SRodney W. Grimes 			break;
2022269fa57SGreg Lehey                 case 'D':
2032269fa57SGreg Lehey                         f_timeformat = optarg;
2042269fa57SGreg Lehey                         break;
2054b88c807SRodney W. Grimes 		case 'l':
2064b88c807SRodney W. Grimes 			f_longform = 1;
2079052855aSMark Murray 			f_singlecol = 0;
20894274c73STim J. Robbins 			f_stream = 0;
20994274c73STim J. Robbins 			break;
21094274c73STim J. Robbins 		case 'x':
21194274c73STim J. Robbins 			f_sortacross = 1;
21294274c73STim J. Robbins 			f_longform = 0;
21394274c73STim J. Robbins 			f_singlecol = 0;
2144b88c807SRodney W. Grimes 			break;
215fe79420eSJohn Baldwin 		/* The -c, -u, and -U options override each other. */
2164b88c807SRodney W. Grimes 		case 'c':
2174b88c807SRodney W. Grimes 			f_statustime = 1;
2184b88c807SRodney W. Grimes 			f_accesstime = 0;
219fe79420eSJohn Baldwin 			f_birthtime = 0;
2204b88c807SRodney W. Grimes 			break;
2214b88c807SRodney W. Grimes 		case 'u':
2224b88c807SRodney W. Grimes 			f_accesstime = 1;
2234b88c807SRodney W. Grimes 			f_statustime = 0;
224fe79420eSJohn Baldwin 			f_birthtime = 0;
225fe79420eSJohn Baldwin 			break;
226fe79420eSJohn Baldwin 		case 'U':
227fe79420eSJohn Baldwin 			f_birthtime = 1;
228fe79420eSJohn Baldwin 			f_accesstime = 0;
229fe79420eSJohn Baldwin 			f_statustime = 0;
2304b88c807SRodney W. Grimes 			break;
2314b88c807SRodney W. Grimes 		case 'F':
2324b88c807SRodney W. Grimes 			f_type = 1;
23394274c73STim J. Robbins 			f_slash = 0;
2344b88c807SRodney W. Grimes 			break;
2353a34dbf7SDag-Erling Smørgrav 		case 'H':
2363a34dbf7SDag-Erling Smørgrav 			fts_options |= FTS_COMFOLLOW;
2373a34dbf7SDag-Erling Smørgrav 			break;
2383885812cSJosef Karthauser 		case 'G':
2393d2ddc9eSJosef Karthauser 			setenv("CLICOLOR", "", 1);
2403885812cSJosef Karthauser 			break;
2414b88c807SRodney W. Grimes 		case 'L':
2424b88c807SRodney W. Grimes 			fts_options &= ~FTS_PHYSICAL;
2434b88c807SRodney W. Grimes 			fts_options |= FTS_LOGICAL;
2444b88c807SRodney W. Grimes 			break;
2453a34dbf7SDag-Erling Smørgrav 		case 'P':
2463a34dbf7SDag-Erling Smørgrav 			fts_options &= ~FTS_COMFOLLOW;
2473a34dbf7SDag-Erling Smørgrav 			fts_options &= ~FTS_LOGICAL;
2483a34dbf7SDag-Erling Smørgrav 			fts_options |= FTS_PHYSICAL;
2493a34dbf7SDag-Erling Smørgrav 			break;
2504b88c807SRodney W. Grimes 		case 'R':
2514b88c807SRodney W. Grimes 			f_recursive = 1;
2524b88c807SRodney W. Grimes 			break;
2534b88c807SRodney W. Grimes 		case 'a':
2544b88c807SRodney W. Grimes 			fts_options |= FTS_SEEDOT;
255390a478eSRuslan Ermilov 			/* FALLTHROUGH */
2564b88c807SRodney W. Grimes 		case 'A':
2574b88c807SRodney W. Grimes 			f_listdot = 1;
2584b88c807SRodney W. Grimes 			break;
2597b7d153bSMaxime Henrion 		case 'I':
260390a478eSRuslan Ermilov 			f_noautodot = 1;
2617b7d153bSMaxime Henrion 			break;
2624b88c807SRodney W. Grimes 		/* The -d option turns off the -R option. */
2634b88c807SRodney W. Grimes 		case 'd':
2644b88c807SRodney W. Grimes 			f_listdir = 1;
2654b88c807SRodney W. Grimes 			f_recursive = 0;
2664b88c807SRodney W. Grimes 			break;
2674b88c807SRodney W. Grimes 		case 'f':
2684b88c807SRodney W. Grimes 			f_nosort = 1;
2694b88c807SRodney W. Grimes 			break;
2704b88c807SRodney W. Grimes 		case 'g':	/* Compatibility with 4.3BSD. */
2714b88c807SRodney W. Grimes 			break;
2720e8d1551SJosef Karthauser 		case 'h':
2730e8d1551SJosef Karthauser 			f_humanval = 1;
2740e8d1551SJosef Karthauser 			break;
2754b88c807SRodney W. Grimes 		case 'i':
2764b88c807SRodney W. Grimes 			f_inode = 1;
2774b88c807SRodney W. Grimes 			break;
278475727a0SPaul Traina 		case 'k':
279d5f9f41cSDavid E. O'Brien 			f_humanval = 0;
280475727a0SPaul Traina 			f_kblocks = 1;
281475727a0SPaul Traina 			break;
28294274c73STim J. Robbins 		case 'm':
28394274c73STim J. Robbins 			f_stream = 1;
28494274c73STim J. Robbins 			f_singlecol = 0;
28594274c73STim J. Robbins 			f_longform = 0;
28694274c73STim J. Robbins 			break;
287f3a6a64eSSheldon Hearn 		case 'n':
288f3a6a64eSSheldon Hearn 			f_numericonly = 1;
289f3a6a64eSSheldon Hearn 			break;
2904b88c807SRodney W. Grimes 		case 'o':
2914b88c807SRodney W. Grimes 			f_flags = 1;
2924b88c807SRodney W. Grimes 			break;
29394274c73STim J. Robbins 		case 'p':
29494274c73STim J. Robbins 			f_slash = 1;
29594274c73STim J. Robbins 			f_type = 1;
29694274c73STim J. Robbins 			break;
2974b88c807SRodney W. Grimes 		case 'q':
2984b88c807SRodney W. Grimes 			f_nonprint = 1;
2997ea30648SDag-Erling Smørgrav 			f_octal = 0;
3000d86878cSDag-Erling Smørgrav 			f_octal_escape = 0;
3014b88c807SRodney W. Grimes 			break;
3024b88c807SRodney W. Grimes 		case 'r':
3034b88c807SRodney W. Grimes 			f_reversesort = 1;
3044b88c807SRodney W. Grimes 			break;
3054b88c807SRodney W. Grimes 		case 's':
3064b88c807SRodney W. Grimes 			f_size = 1;
3074b88c807SRodney W. Grimes 			break;
3084b88c807SRodney W. Grimes 		case 'T':
3094b88c807SRodney W. Grimes 			f_sectime = 1;
3104b88c807SRodney W. Grimes 			break;
31193a5035fSJohn Baldwin 		/* The -t and -S options override each other. */
3124b88c807SRodney W. Grimes 		case 't':
3134b88c807SRodney W. Grimes 			f_timesort = 1;
31493a5035fSJohn Baldwin 			f_sizesort = 0;
3154b88c807SRodney W. Grimes 			break;
31671b8b748SDima Dorfman 		case 'S':
31771b8b748SDima Dorfman 			f_sizesort = 1;
31893a5035fSJohn Baldwin 			f_timesort = 0;
31971b8b748SDima Dorfman 			break;
320fb5cb208SSteve Price 		case 'W':
321fb5cb208SSteve Price 			f_whiteout = 1;
322fb5cb208SSteve Price 			break;
3237ea30648SDag-Erling Smørgrav 		case 'b':
3247ea30648SDag-Erling Smørgrav 			f_nonprint = 0;
3250d86878cSDag-Erling Smørgrav 			f_octal = 0;
3260d86878cSDag-Erling Smørgrav 			f_octal_escape = 1;
3277ea30648SDag-Erling Smørgrav 			break;
32847f884f0SJosef Karthauser 		case 'w':
32947f884f0SJosef Karthauser 			f_nonprint = 0;
33047f884f0SJosef Karthauser 			f_octal = 0;
33147f884f0SJosef Karthauser 			f_octal_escape = 0;
33247f884f0SJosef Karthauser 			break;
3337304f61fSBrian Feldman 		case 'Z':
3344d33b62eSRobert Watson 			f_label = 1;
3357304f61fSBrian Feldman 			break;
3364b88c807SRodney W. Grimes 		default:
3374b88c807SRodney W. Grimes 		case '?':
3384b88c807SRodney W. Grimes 			usage();
3394b88c807SRodney W. Grimes 		}
3404b88c807SRodney W. Grimes 	}
3414b88c807SRodney W. Grimes 	argc -= optind;
3424b88c807SRodney W. Grimes 	argv += optind;
3434b88c807SRodney W. Grimes 
344390a478eSRuslan Ermilov 	/* Root is -A automatically unless -I. */
345390a478eSRuslan Ermilov 	if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot)
3467b7d153bSMaxime Henrion 		f_listdot = 1;
3477b7d153bSMaxime Henrion 
3483d2ddc9eSJosef Karthauser 	/* Enabling of colours is conditional on the environment. */
3493d2ddc9eSJosef Karthauser 	if (getenv("CLICOLOR") &&
3503d2ddc9eSJosef Karthauser 	    (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
351d4413063SJosef Karthauser #ifdef COLORLS
3523d2ddc9eSJosef Karthauser 		if (tgetent(termcapbuf, getenv("TERM")) == 1) {
3533d2ddc9eSJosef Karthauser 			ansi_fgcol = tgetstr("AF", &bp);
3543d2ddc9eSJosef Karthauser 			ansi_bgcol = tgetstr("AB", &bp);
355c1499cf6SJosef Karthauser 			attrs_off = tgetstr("me", &bp);
356c1499cf6SJosef Karthauser 			enter_bold = tgetstr("md", &bp);
3573d2ddc9eSJosef Karthauser 
3583d2ddc9eSJosef Karthauser 			/* To switch colours off use 'op' if
3593d2ddc9eSJosef Karthauser 			 * available, otherwise use 'oc', or
3603d2ddc9eSJosef Karthauser 			 * don't do colours at all. */
3613d2ddc9eSJosef Karthauser 			ansi_coloff = tgetstr("op", &bp);
3623d2ddc9eSJosef Karthauser 			if (!ansi_coloff)
3633d2ddc9eSJosef Karthauser 				ansi_coloff = tgetstr("oc", &bp);
3643d2ddc9eSJosef Karthauser 			if (ansi_fgcol && ansi_bgcol && ansi_coloff)
3653d2ddc9eSJosef Karthauser 				f_color = 1;
3663d2ddc9eSJosef Karthauser 		}
367d4413063SJosef Karthauser #else
368e09fdabdSTim J. Robbins 		warnx("color support not compiled in");
369d4413063SJosef Karthauser #endif /*COLORLS*/
3703d2ddc9eSJosef Karthauser 
371d4413063SJosef Karthauser #ifdef COLORLS
372bd82d8abSAndrey A. Chernov 	if (f_color) {
37322ff3e9eSAndrey A. Chernov 		/*
37422ff3e9eSAndrey A. Chernov 		 * We can't put tabs and color sequences together:
37522ff3e9eSAndrey A. Chernov 		 * column number will be incremented incorrectly
37622ff3e9eSAndrey A. Chernov 		 * for "stty oxtabs" mode.
37722ff3e9eSAndrey A. Chernov 		 */
37822ff3e9eSAndrey A. Chernov 		f_notabs = 1;
379bd82d8abSAndrey A. Chernov 		(void)signal(SIGINT, colorquit);
380ab08444fSMartin Cracauer 		(void)signal(SIGQUIT, colorquit);
3813885812cSJosef Karthauser 		parsecolors(getenv("LSCOLORS"));
382bd82d8abSAndrey A. Chernov 	}
38374985094SJosef Karthauser #endif
3843885812cSJosef Karthauser 
3854b88c807SRodney W. Grimes 	/*
38671b8b748SDima Dorfman 	 * If not -F, -i, -l, -s, -S or -t options, don't require stat
3873885812cSJosef Karthauser 	 * information, unless in color mode in which case we do
3883885812cSJosef Karthauser 	 * need this to determine which colors to display.
3894b88c807SRodney W. Grimes 	 */
39071b8b748SDima Dorfman 	if (!f_inode && !f_longform && !f_size && !f_timesort &&
39171b8b748SDima Dorfman 	    !f_sizesort && !f_type
39274985094SJosef Karthauser #ifdef COLORLS
39374985094SJosef Karthauser 	    && !f_color
39474985094SJosef Karthauser #endif
39574985094SJosef Karthauser 	    )
3964b88c807SRodney W. Grimes 		fts_options |= FTS_NOSTAT;
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes 	/*
3994b88c807SRodney W. Grimes 	 * If not -F, -d or -l options, follow any symbolic links listed on
4004b88c807SRodney W. Grimes 	 * the command line.
4014b88c807SRodney W. Grimes 	 */
4025c3743e3SJilles Tjoelker 	if (!f_longform && !f_listdir && (!f_type || f_slash))
4034b88c807SRodney W. Grimes 		fts_options |= FTS_COMFOLLOW;
4044b88c807SRodney W. Grimes 
405fb5cb208SSteve Price 	/*
406fb5cb208SSteve Price 	 * If -W, show whiteout entries
407fb5cb208SSteve Price 	 */
408fb5cb208SSteve Price #ifdef FTS_WHITEOUT
409fb5cb208SSteve Price 	if (f_whiteout)
410fb5cb208SSteve Price 		fts_options |= FTS_WHITEOUT;
411fb5cb208SSteve Price #endif
412fb5cb208SSteve Price 
4134b88c807SRodney W. Grimes 	/* If -l or -s, figure out block size. */
4144b88c807SRodney W. Grimes 	if (f_longform || f_size) {
41590b0ec31SPoul-Henning Kamp 		if (f_kblocks)
41690b0ec31SPoul-Henning Kamp 			blocksize = 2;
41790b0ec31SPoul-Henning Kamp 		else {
4184b88c807SRodney W. Grimes 			(void)getbsize(&notused, &blocksize);
4194b88c807SRodney W. Grimes 			blocksize /= 512;
42090b0ec31SPoul-Henning Kamp 		}
4214b88c807SRodney W. Grimes 	}
4224b88c807SRodney W. Grimes 	/* Select a sort function. */
4234b88c807SRodney W. Grimes 	if (f_reversesort) {
42471b8b748SDima Dorfman 		if (!f_timesort && !f_sizesort)
4254b88c807SRodney W. Grimes 			sortfcn = revnamecmp;
42686cca1e7SJohn Baldwin 		else if (f_sizesort)
42786cca1e7SJohn Baldwin 			sortfcn = revsizecmp;
4284b88c807SRodney W. Grimes 		else if (f_accesstime)
4294b88c807SRodney W. Grimes 			sortfcn = revacccmp;
430fe79420eSJohn Baldwin 		else if (f_birthtime)
431fe79420eSJohn Baldwin 			sortfcn = revbirthcmp;
4324b88c807SRodney W. Grimes 		else if (f_statustime)
4334b88c807SRodney W. Grimes 			sortfcn = revstatcmp;
4344b88c807SRodney W. Grimes 		else		/* Use modification time. */
4354b88c807SRodney W. Grimes 			sortfcn = revmodcmp;
4364b88c807SRodney W. Grimes 	} else {
43771b8b748SDima Dorfman 		if (!f_timesort && !f_sizesort)
4384b88c807SRodney W. Grimes 			sortfcn = namecmp;
43986cca1e7SJohn Baldwin 		else if (f_sizesort)
44086cca1e7SJohn Baldwin 			sortfcn = sizecmp;
4414b88c807SRodney W. Grimes 		else if (f_accesstime)
4424b88c807SRodney W. Grimes 			sortfcn = acccmp;
443fe79420eSJohn Baldwin 		else if (f_birthtime)
444fe79420eSJohn Baldwin 			sortfcn = birthcmp;
4454b88c807SRodney W. Grimes 		else if (f_statustime)
4464b88c807SRodney W. Grimes 			sortfcn = statcmp;
4474b88c807SRodney W. Grimes 		else		/* Use modification time. */
4484b88c807SRodney W. Grimes 			sortfcn = modcmp;
4494b88c807SRodney W. Grimes 	}
4504b88c807SRodney W. Grimes 
4514b88c807SRodney W. Grimes 	/* Select a print function. */
4524b88c807SRodney W. Grimes 	if (f_singlecol)
4534b88c807SRodney W. Grimes 		printfcn = printscol;
4544b88c807SRodney W. Grimes 	else if (f_longform)
4554b88c807SRodney W. Grimes 		printfcn = printlong;
45694274c73STim J. Robbins 	else if (f_stream)
45794274c73STim J. Robbins 		printfcn = printstream;
4584b88c807SRodney W. Grimes 	else
4594b88c807SRodney W. Grimes 		printfcn = printcol;
4604b88c807SRodney W. Grimes 
4614b88c807SRodney W. Grimes 	if (argc)
4624b88c807SRodney W. Grimes 		traverse(argc, argv, fts_options);
4634b88c807SRodney W. Grimes 	else
4644b88c807SRodney W. Grimes 		traverse(1, dotav, fts_options);
465fb1000d6SAdam David 	exit(rval);
4664b88c807SRodney W. Grimes }
4674b88c807SRodney W. Grimes 
4684b88c807SRodney W. Grimes static int output;		/* If anything output. */
4694b88c807SRodney W. Grimes 
4704b88c807SRodney W. Grimes /*
4714b88c807SRodney W. Grimes  * Traverse() walks the logical directory structure specified by the argv list
4724b88c807SRodney W. Grimes  * in the order specified by the mastercmp() comparison function.  During the
4734b88c807SRodney W. Grimes  * traversal it passes linked lists of structures to display() which represent
4744b88c807SRodney W. Grimes  * a superset (may be exact set) of the files to be displayed.
4754b88c807SRodney W. Grimes  */
4764b88c807SRodney W. Grimes static void
47746251ddeSWarner Losh traverse(int argc, char *argv[], int options)
4784b88c807SRodney W. Grimes {
4794b88c807SRodney W. Grimes 	FTS *ftsp;
4804b88c807SRodney W. Grimes 	FTSENT *p, *chp;
4814b88c807SRodney W. Grimes 	int ch_options;
4824b88c807SRodney W. Grimes 
4834b88c807SRodney W. Grimes 	if ((ftsp =
4844b88c807SRodney W. Grimes 	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
4855ad9e45fSMatthew Dillon 		err(1, "fts_open");
4864b88c807SRodney W. Grimes 
48770bad4f7SDavid Schultz 	/*
48870bad4f7SDavid Schultz 	 * We ignore errors from fts_children here since they will be
48970bad4f7SDavid Schultz 	 * replicated and signalled on the next call to fts_read() below.
49070bad4f7SDavid Schultz 	 */
49170bad4f7SDavid Schultz 	chp = fts_children(ftsp, 0);
49270bad4f7SDavid Schultz 	if (chp != NULL)
49370bad4f7SDavid Schultz 		display(NULL, chp, options);
4944b88c807SRodney W. Grimes 	if (f_listdir)
4954b88c807SRodney W. Grimes 		return;
4964b88c807SRodney W. Grimes 
4974b88c807SRodney W. Grimes 	/*
4984b88c807SRodney W. Grimes 	 * If not recursing down this tree and don't need stat info, just get
4994b88c807SRodney W. Grimes 	 * the names.
5004b88c807SRodney W. Grimes 	 */
5013c3f5f9cSRobert Watson 	ch_options = !f_recursive && !f_label &&
5023c3f5f9cSRobert Watson 	    options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
5034b88c807SRodney W. Grimes 
5044b88c807SRodney W. Grimes 	while ((p = fts_read(ftsp)) != NULL)
5054b88c807SRodney W. Grimes 		switch (p->fts_info) {
5064b88c807SRodney W. Grimes 		case FTS_DC:
5074b88c807SRodney W. Grimes 			warnx("%s: directory causes a cycle", p->fts_name);
5084b88c807SRodney W. Grimes 			break;
5094b88c807SRodney W. Grimes 		case FTS_DNR:
5104b88c807SRodney W. Grimes 		case FTS_ERR:
511ba027bbfSJaakko Heinonen 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
512fb1000d6SAdam David 			rval = 1;
5134b88c807SRodney W. Grimes 			break;
5144b88c807SRodney W. Grimes 		case FTS_D:
5154b88c807SRodney W. Grimes 			if (p->fts_level != FTS_ROOTLEVEL &&
516390a478eSRuslan Ermilov 			    p->fts_name[0] == '.' && !f_listdot)
5174b88c807SRodney W. Grimes 				break;
5184b88c807SRodney W. Grimes 
5194b88c807SRodney W. Grimes 			/*
5204b88c807SRodney W. Grimes 			 * If already output something, put out a newline as
5214b88c807SRodney W. Grimes 			 * a separator.  If multiple arguments, precede each
5224b88c807SRodney W. Grimes 			 * directory with its name.
5234b88c807SRodney W. Grimes 			 */
5241656f850STim J. Robbins 			if (output) {
5251656f850STim J. Robbins 				putchar('\n');
52640feca3aSMark Murray 				(void)printname(p->fts_path);
5271656f850STim J. Robbins 				puts(":");
5281656f850STim J. Robbins 			} else if (argc > 1) {
52940feca3aSMark Murray 				(void)printname(p->fts_path);
5301656f850STim J. Robbins 				puts(":");
5314b88c807SRodney W. Grimes 				output = 1;
5324b88c807SRodney W. Grimes 			}
5334b88c807SRodney W. Grimes 			chp = fts_children(ftsp, ch_options);
5344d33b62eSRobert Watson 			display(p, chp, options);
5354b88c807SRodney W. Grimes 
5364b88c807SRodney W. Grimes 			if (!f_recursive && chp != NULL)
5374b88c807SRodney W. Grimes 				(void)fts_set(ftsp, p, FTS_SKIP);
5384b88c807SRodney W. Grimes 			break;
539568dcd5fSBill Fumerola 		default:
540568dcd5fSBill Fumerola 			break;
5414b88c807SRodney W. Grimes 		}
5424b88c807SRodney W. Grimes 	if (errno)
5434b88c807SRodney W. Grimes 		err(1, "fts_read");
5444b88c807SRodney W. Grimes }
5454b88c807SRodney W. Grimes 
5464b88c807SRodney W. Grimes /*
5474b88c807SRodney W. Grimes  * Display() takes a linked list of FTSENT structures and passes the list
5484b88c807SRodney W. Grimes  * along with any other necessary information to the print function.  P
5494b88c807SRodney W. Grimes  * points to the parent directory of the display list.
5504b88c807SRodney W. Grimes  */
5514b88c807SRodney W. Grimes static void
55240feca3aSMark Murray display(const FTSENT *p, FTSENT *list, int options)
5534b88c807SRodney W. Grimes {
5544b88c807SRodney W. Grimes 	struct stat *sp;
5554b88c807SRodney W. Grimes 	DISPLAY d;
5564b88c807SRodney W. Grimes 	FTSENT *cur;
5574b88c807SRodney W. Grimes 	NAMES *np;
5589052855aSMark Murray 	off_t maxsize;
55940feca3aSMark Murray 	long maxblock;
56040feca3aSMark Murray 	u_long btotal, labelstrlen, maxinode, maxlen, maxnlink;
5614d33b62eSRobert Watson 	u_long maxlabelstr;
56255926a66SJaakko Heinonen 	u_int devstrlen;
56355926a66SJaakko Heinonen 	int maxflags;
5649052855aSMark Murray 	gid_t maxgroup;
5659052855aSMark Murray 	uid_t maxuser;
5669052855aSMark Murray 	size_t flen, ulen, glen;
567545f583cSTim Vanderhoek 	char *initmax;
5684b88c807SRodney W. Grimes 	int entries, needstats;
5690928a7f1SJuli Mallett 	const char *user, *group;
5704d33b62eSRobert Watson 	char *flags, *labelstr = NULL;
571008a4910SSheldon Hearn 	char buf[STRBUF_SIZEOF(u_quad_t) + 1];
572008a4910SSheldon Hearn 	char ngroup[STRBUF_SIZEOF(uid_t) + 1];
573008a4910SSheldon Hearn 	char nuser[STRBUF_SIZEOF(gid_t) + 1];
5744b88c807SRodney W. Grimes 
5754b88c807SRodney W. Grimes 	needstats = f_inode || f_longform || f_size;
5764b88c807SRodney W. Grimes 	flen = 0;
577545f583cSTim Vanderhoek 	btotal = 0;
578545f583cSTim Vanderhoek 	initmax = getenv("LS_COLWIDTHS");
579545f583cSTim Vanderhoek 	/* Fields match -lios order.  New ones should be added at the end. */
5804d33b62eSRobert Watson 	maxlabelstr = maxblock = maxinode = maxlen = maxnlink =
5819052855aSMark Murray 	    maxuser = maxgroup = maxflags = maxsize = 0;
582545f583cSTim Vanderhoek 	if (initmax != NULL && *initmax != '\0') {
583545f583cSTim Vanderhoek 		char *initmax2, *jinitmax;
584545f583cSTim Vanderhoek 		int ninitmax;
585545f583cSTim Vanderhoek 
586545f583cSTim Vanderhoek 		/* Fill-in "::" as "0:0:0" for the sake of scanf. */
58740feca3aSMark Murray 		jinitmax = malloc(strlen(initmax) * 2 + 2);
588545f583cSTim Vanderhoek 		if (jinitmax == NULL)
5895ad9e45fSMatthew Dillon 			err(1, "malloc");
59040feca3aSMark Murray 		initmax2 = jinitmax;
591545f583cSTim Vanderhoek 		if (*initmax == ':')
592545f583cSTim Vanderhoek 			strcpy(initmax2, "0:"), initmax2 += 2;
593545f583cSTim Vanderhoek 		else
594545f583cSTim Vanderhoek 			*initmax2++ = *initmax, *initmax2 = '\0';
595545f583cSTim Vanderhoek 		for (initmax++; *initmax != '\0'; initmax++) {
596545f583cSTim Vanderhoek 			if (initmax[-1] == ':' && initmax[0] == ':') {
597545f583cSTim Vanderhoek 				*initmax2++ = '0';
598545f583cSTim Vanderhoek 				*initmax2++ = initmax[0];
599545f583cSTim Vanderhoek 				initmax2[1] = '\0';
600545f583cSTim Vanderhoek 			} else {
601545f583cSTim Vanderhoek 				*initmax2++ = initmax[0];
602545f583cSTim Vanderhoek 				initmax2[1] = '\0';
603545f583cSTim Vanderhoek 			}
604545f583cSTim Vanderhoek 		}
6055dda5d0dSJosef Karthauser 		if (initmax2[-1] == ':')
6065dda5d0dSJosef Karthauser 			strcpy(initmax2, "0");
607545f583cSTim Vanderhoek 
608545f583cSTim Vanderhoek 		ninitmax = sscanf(jinitmax,
60940feca3aSMark Murray 		    " %lu : %ld : %lu : %u : %u : %i : %jd : %lu : %lu ",
610545f583cSTim Vanderhoek 		    &maxinode, &maxblock, &maxnlink, &maxuser,
6114d33b62eSRobert Watson 		    &maxgroup, &maxflags, &maxsize, &maxlen, &maxlabelstr);
612545f583cSTim Vanderhoek 		f_notabs = 1;
613545f583cSTim Vanderhoek 		switch (ninitmax) {
6145dda5d0dSJosef Karthauser 		case 0:
6155dda5d0dSJosef Karthauser 			maxinode = 0;
6160d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6175dda5d0dSJosef Karthauser 		case 1:
6185dda5d0dSJosef Karthauser 			maxblock = 0;
6190d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6205dda5d0dSJosef Karthauser 		case 2:
6215dda5d0dSJosef Karthauser 			maxnlink = 0;
6220d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6235dda5d0dSJosef Karthauser 		case 3:
6245dda5d0dSJosef Karthauser 			maxuser = 0;
6250d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6265dda5d0dSJosef Karthauser 		case 4:
6275dda5d0dSJosef Karthauser 			maxgroup = 0;
6280d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6295dda5d0dSJosef Karthauser 		case 5:
6305dda5d0dSJosef Karthauser 			maxflags = 0;
6310d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6325dda5d0dSJosef Karthauser 		case 6:
6335dda5d0dSJosef Karthauser 			maxsize = 0;
6340d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6355dda5d0dSJosef Karthauser 		case 7:
6365dda5d0dSJosef Karthauser 			maxlen = 0;
6370d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6385dda5d0dSJosef Karthauser 		case 8:
6394d33b62eSRobert Watson 			maxlabelstr = 0;
6400d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
64118d8a22bSAndrey A. Chernov #ifdef COLORLS
64218d8a22bSAndrey A. Chernov 			if (!f_color)
64322ff3e9eSAndrey A. Chernov #endif
64418d8a22bSAndrey A. Chernov 				f_notabs = 0;
6450d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
6469052855aSMark Murray 		default:
647568dcd5fSBill Fumerola 			break;
648545f583cSTim Vanderhoek 		}
64940feca3aSMark Murray 		MAKENINES(maxinode);
65040feca3aSMark Murray 		MAKENINES(maxblock);
65140feca3aSMark Murray 		MAKENINES(maxnlink);
65240feca3aSMark Murray 		MAKENINES(maxsize);
6537fcc4669SLukas Ertl 		free(jinitmax);
6549052855aSMark Murray 	}
65555926a66SJaakko Heinonen 	devstrlen = 0;
6560fd510b7SJoerg Wunsch 	flags = NULL;
6574b88c807SRodney W. Grimes 	for (cur = list, entries = 0; cur; cur = cur->fts_link) {
6584b88c807SRodney W. Grimes 		if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
6594b88c807SRodney W. Grimes 			warnx("%s: %s",
6604b88c807SRodney W. Grimes 			    cur->fts_name, strerror(cur->fts_errno));
6614b88c807SRodney W. Grimes 			cur->fts_number = NO_PRINT;
662fb1000d6SAdam David 			rval = 1;
6634b88c807SRodney W. Grimes 			continue;
6644b88c807SRodney W. Grimes 		}
6654b88c807SRodney W. Grimes 		/*
6664b88c807SRodney W. Grimes 		 * P is NULL if list is the argv list, to which different rules
6674b88c807SRodney W. Grimes 		 * apply.
6684b88c807SRodney W. Grimes 		 */
6694b88c807SRodney W. Grimes 		if (p == NULL) {
6704b88c807SRodney W. Grimes 			/* Directories will be displayed later. */
6714b88c807SRodney W. Grimes 			if (cur->fts_info == FTS_D && !f_listdir) {
6724b88c807SRodney W. Grimes 				cur->fts_number = NO_PRINT;
6734b88c807SRodney W. Grimes 				continue;
6744b88c807SRodney W. Grimes 			}
6754b88c807SRodney W. Grimes 		} else {
6764b88c807SRodney W. Grimes 			/* Only display dot file if -a/-A set. */
677390a478eSRuslan Ermilov 			if (cur->fts_name[0] == '.' && !f_listdot) {
6784b88c807SRodney W. Grimes 				cur->fts_number = NO_PRINT;
6794b88c807SRodney W. Grimes 				continue;
6804b88c807SRodney W. Grimes 			}
6814b88c807SRodney W. Grimes 		}
6824b88c807SRodney W. Grimes 		if (cur->fts_namelen > maxlen)
6834b88c807SRodney W. Grimes 			maxlen = cur->fts_namelen;
6840d86878cSDag-Erling Smørgrav 		if (f_octal || f_octal_escape) {
6856bd042dfSJosef Karthauser 			u_long t = len_octal(cur->fts_name, cur->fts_namelen);
6865dda5d0dSJosef Karthauser 
6875dda5d0dSJosef Karthauser 			if (t > maxlen)
6885dda5d0dSJosef Karthauser 				maxlen = t;
6897ea30648SDag-Erling Smørgrav 		}
6904b88c807SRodney W. Grimes 		if (needstats) {
6914b88c807SRodney W. Grimes 			sp = cur->fts_statp;
6924b88c807SRodney W. Grimes 			if (sp->st_blocks > maxblock)
6934b88c807SRodney W. Grimes 				maxblock = sp->st_blocks;
6944b88c807SRodney W. Grimes 			if (sp->st_ino > maxinode)
6954b88c807SRodney W. Grimes 				maxinode = sp->st_ino;
6964b88c807SRodney W. Grimes 			if (sp->st_nlink > maxnlink)
6974b88c807SRodney W. Grimes 				maxnlink = sp->st_nlink;
6984b88c807SRodney W. Grimes 			if (sp->st_size > maxsize)
6994b88c807SRodney W. Grimes 				maxsize = sp->st_size;
7004b88c807SRodney W. Grimes 
7014b88c807SRodney W. Grimes 			btotal += sp->st_blocks;
7024b88c807SRodney W. Grimes 			if (f_longform) {
703f3a6a64eSSheldon Hearn 				if (f_numericonly) {
704f3a6a64eSSheldon Hearn 					(void)snprintf(nuser, sizeof(nuser),
705f3a6a64eSSheldon Hearn 					    "%u", sp->st_uid);
706f3a6a64eSSheldon Hearn 					(void)snprintf(ngroup, sizeof(ngroup),
707f3a6a64eSSheldon Hearn 					    "%u", sp->st_gid);
708f3a6a64eSSheldon Hearn 					user = nuser;
709f3a6a64eSSheldon Hearn 					group = ngroup;
710f3a6a64eSSheldon Hearn 				} else {
7114b88c807SRodney W. Grimes 					user = user_from_uid(sp->st_uid, 0);
712f3a6a64eSSheldon Hearn 					group = group_from_gid(sp->st_gid, 0);
713f3a6a64eSSheldon Hearn 				}
7144b88c807SRodney W. Grimes 				if ((ulen = strlen(user)) > maxuser)
7154b88c807SRodney W. Grimes 					maxuser = ulen;
7164b88c807SRodney W. Grimes 				if ((glen = strlen(group)) > maxgroup)
7174b88c807SRodney W. Grimes 					maxgroup = glen;
7184b88c807SRodney W. Grimes 				if (f_flags) {
719141d77b8SJosef Karthauser 					flags = fflagstostr(sp->st_flags);
720141d77b8SJosef Karthauser 					if (flags != NULL && *flags == '\0') {
721141d77b8SJosef Karthauser 						free(flags);
722141d77b8SJosef Karthauser 						flags = strdup("-");
723141d77b8SJosef Karthauser 					}
724141d77b8SJosef Karthauser 					if (flags == NULL)
7255ad9e45fSMatthew Dillon 						err(1, "fflagstostr");
7269052855aSMark Murray 					flen = strlen(flags);
7279052855aSMark Murray 					if (flen > (size_t)maxflags)
7284b88c807SRodney W. Grimes 						maxflags = flen;
7294b88c807SRodney W. Grimes 				} else
7304b88c807SRodney W. Grimes 					flen = 0;
7314d33b62eSRobert Watson 				labelstr = NULL;
7324d33b62eSRobert Watson 				if (f_label) {
7334df6dabaSRobert Watson 					char name[PATH_MAX + 1];
7344d33b62eSRobert Watson 					mac_t label;
7354d33b62eSRobert Watson 					int error;
7364b88c807SRodney W. Grimes 
7374d33b62eSRobert Watson 					error = mac_prepare_file_label(&label);
7384d33b62eSRobert Watson 					if (error == -1) {
7393c3f5f9cSRobert Watson 						warn("MAC label for %s/%s",
7403c3f5f9cSRobert Watson 						    cur->fts_parent->fts_path,
7413c3f5f9cSRobert Watson 						    cur->fts_name);
7424d33b62eSRobert Watson 						goto label_out;
7434d33b62eSRobert Watson 					}
7444d33b62eSRobert Watson 
7454df6dabaSRobert Watson 					if (cur->fts_level == FTS_ROOTLEVEL)
7464df6dabaSRobert Watson 						snprintf(name, sizeof(name),
7474df6dabaSRobert Watson 						    "%s", cur->fts_name);
7484d33b62eSRobert Watson 					else
7494df6dabaSRobert Watson 						snprintf(name, sizeof(name),
7503c3f5f9cSRobert Watson 						    "%s/%s", cur->fts_parent->
7513c3f5f9cSRobert Watson 						    fts_accpath, cur->fts_name);
7524df6dabaSRobert Watson 
7534df6dabaSRobert Watson 					if (options & FTS_LOGICAL)
7544df6dabaSRobert Watson 						error = mac_get_file(name,
7554df6dabaSRobert Watson 						    label);
7564df6dabaSRobert Watson 					else
7574df6dabaSRobert Watson 						error = mac_get_link(name,
7584df6dabaSRobert Watson 						    label);
7594d33b62eSRobert Watson 					if (error == -1) {
7603c3f5f9cSRobert Watson 						warn("MAC label for %s/%s",
7613c3f5f9cSRobert Watson 						    cur->fts_parent->fts_path,
7623c3f5f9cSRobert Watson 						    cur->fts_name);
7634d33b62eSRobert Watson 						mac_free(label);
7644d33b62eSRobert Watson 						goto label_out;
7654d33b62eSRobert Watson 					}
7664d33b62eSRobert Watson 
7674d33b62eSRobert Watson 					error = mac_to_text(label,
7684d33b62eSRobert Watson 					    &labelstr);
7694d33b62eSRobert Watson 					if (error == -1) {
7703c3f5f9cSRobert Watson 						warn("MAC label for %s/%s",
7713c3f5f9cSRobert Watson 						    cur->fts_parent->fts_path,
7723c3f5f9cSRobert Watson 						    cur->fts_name);
7734d33b62eSRobert Watson 						mac_free(label);
7744d33b62eSRobert Watson 						goto label_out;
7754d33b62eSRobert Watson 					}
7764d33b62eSRobert Watson 					mac_free(label);
7774d33b62eSRobert Watson label_out:
7784d33b62eSRobert Watson 					if (labelstr == NULL)
779317f1d53SRobert Watson 						labelstr = strdup("-");
7804d33b62eSRobert Watson 					labelstrlen = strlen(labelstr);
7814d33b62eSRobert Watson 					if (labelstrlen > maxlabelstr)
7824d33b62eSRobert Watson 						maxlabelstr = labelstrlen;
7834d33b62eSRobert Watson 				} else
7844d33b62eSRobert Watson 					labelstrlen = 0;
7854d33b62eSRobert Watson 
7864d33b62eSRobert Watson 				if ((np = malloc(sizeof(NAMES) + labelstrlen +
7877304f61fSBrian Feldman 				    ulen + glen + flen + 4)) == NULL)
7885ad9e45fSMatthew Dillon 					err(1, "malloc");
7894b88c807SRodney W. Grimes 
7904b88c807SRodney W. Grimes 				np->user = &np->data[0];
7914b88c807SRodney W. Grimes 				(void)strcpy(np->user, user);
7924b88c807SRodney W. Grimes 				np->group = &np->data[ulen + 1];
7934b88c807SRodney W. Grimes 				(void)strcpy(np->group, group);
7944b88c807SRodney W. Grimes 
79555926a66SJaakko Heinonen 				if ((S_ISCHR(sp->st_mode) ||
79655926a66SJaakko Heinonen 				    S_ISBLK(sp->st_mode)) &&
79755926a66SJaakko Heinonen 				    devstrlen < DEVSTR_HEX_LEN) {
79855926a66SJaakko Heinonen 					if (minor(sp->st_rdev) > 255 ||
79955926a66SJaakko Heinonen 					    minor(sp->st_rdev) < 0)
80055926a66SJaakko Heinonen 						devstrlen = DEVSTR_HEX_LEN;
80155926a66SJaakko Heinonen 					else
80255926a66SJaakko Heinonen 						devstrlen = DEVSTR_LEN;
80355926a66SJaakko Heinonen 				}
8044b88c807SRodney W. Grimes 
8054b88c807SRodney W. Grimes 				if (f_flags) {
8064b88c807SRodney W. Grimes 					np->flags = &np->data[ulen + glen + 2];
8074b88c807SRodney W. Grimes 					(void)strcpy(np->flags, flags);
808141d77b8SJosef Karthauser 					free(flags);
8094b88c807SRodney W. Grimes 				}
8104d33b62eSRobert Watson 				if (f_label) {
8114d33b62eSRobert Watson 					np->label = &np->data[ulen + glen + 2
8127304f61fSBrian Feldman 					    + (f_flags ? flen + 1 : 0)];
8134d33b62eSRobert Watson 					(void)strcpy(np->label, labelstr);
8144d33b62eSRobert Watson 					free(labelstr);
8157304f61fSBrian Feldman 				}
8164b88c807SRodney W. Grimes 				cur->fts_pointer = np;
8174b88c807SRodney W. Grimes 			}
8184b88c807SRodney W. Grimes 		}
8194b88c807SRodney W. Grimes 		++entries;
8204b88c807SRodney W. Grimes 	}
8214b88c807SRodney W. Grimes 
82270bad4f7SDavid Schultz 	/*
82370bad4f7SDavid Schultz 	 * If there are no entries to display, we normally stop right
82470bad4f7SDavid Schultz 	 * here.  However, we must continue if we have to display the
82570bad4f7SDavid Schultz 	 * total block count.  In this case, we display the total only
82670bad4f7SDavid Schultz 	 * on the second (p != NULL) pass.
82770bad4f7SDavid Schultz 	 */
82870bad4f7SDavid Schultz 	if (!entries && (!(f_longform || f_size) || p == NULL))
8294b88c807SRodney W. Grimes 		return;
8304b88c807SRodney W. Grimes 
8314b88c807SRodney W. Grimes 	d.list = list;
8324b88c807SRodney W. Grimes 	d.entries = entries;
8334b88c807SRodney W. Grimes 	d.maxlen = maxlen;
8344b88c807SRodney W. Grimes 	if (needstats) {
8354b88c807SRodney W. Grimes 		d.btotal = btotal;
8364b88c807SRodney W. Grimes 		(void)snprintf(buf, sizeof(buf), "%lu", maxblock);
8374b88c807SRodney W. Grimes 		d.s_block = strlen(buf);
8384b88c807SRodney W. Grimes 		d.s_flags = maxflags;
8394d33b62eSRobert Watson 		d.s_label = maxlabelstr;
8404b88c807SRodney W. Grimes 		d.s_group = maxgroup;
8414b88c807SRodney W. Grimes 		(void)snprintf(buf, sizeof(buf), "%lu", maxinode);
8424b88c807SRodney W. Grimes 		d.s_inode = strlen(buf);
8434b88c807SRodney W. Grimes 		(void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
8444b88c807SRodney W. Grimes 		d.s_nlink = strlen(buf);
84555926a66SJaakko Heinonen 		if (f_humanval)
84655926a66SJaakko Heinonen 			d.s_size = HUMANVALSTR_LEN;
84755926a66SJaakko Heinonen 		else {
84840feca3aSMark Murray 			(void)snprintf(buf, sizeof(buf), "%ju", maxsize);
8494b88c807SRodney W. Grimes 			d.s_size = strlen(buf);
85055926a66SJaakko Heinonen 		}
85155926a66SJaakko Heinonen 		if (d.s_size < devstrlen)
85255926a66SJaakko Heinonen 			d.s_size = devstrlen;
8534b88c807SRodney W. Grimes 		d.s_user = maxuser;
8544b88c807SRodney W. Grimes 	}
8554b88c807SRodney W. Grimes 	printfcn(&d);
8564b88c807SRodney W. Grimes 	output = 1;
8574b88c807SRodney W. Grimes 
8584b88c807SRodney W. Grimes 	if (f_longform)
8594b88c807SRodney W. Grimes 		for (cur = list; cur; cur = cur->fts_link)
8604b88c807SRodney W. Grimes 			free(cur->fts_pointer);
8614b88c807SRodney W. Grimes }
8624b88c807SRodney W. Grimes 
8634b88c807SRodney W. Grimes /*
8644b88c807SRodney W. Grimes  * Ordering for mastercmp:
8654b88c807SRodney W. Grimes  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
8664b88c807SRodney W. Grimes  * as larger than directories.  Within either group, use the sort function.
8674b88c807SRodney W. Grimes  * All other levels use the sort function.  Error entries remain unsorted.
8684b88c807SRodney W. Grimes  */
8694b88c807SRodney W. Grimes static int
8700d3bcc2eSGarrett Wollman mastercmp(const FTSENT * const *a, const FTSENT * const *b)
8714b88c807SRodney W. Grimes {
8724b88c807SRodney W. Grimes 	int a_info, b_info;
8734b88c807SRodney W. Grimes 
8744b88c807SRodney W. Grimes 	a_info = (*a)->fts_info;
8754b88c807SRodney W. Grimes 	if (a_info == FTS_ERR)
8764b88c807SRodney W. Grimes 		return (0);
8774b88c807SRodney W. Grimes 	b_info = (*b)->fts_info;
8784b88c807SRodney W. Grimes 	if (b_info == FTS_ERR)
8794b88c807SRodney W. Grimes 		return (0);
8804b88c807SRodney W. Grimes 
8814b88c807SRodney W. Grimes 	if (a_info == FTS_NS || b_info == FTS_NS)
8824b88c807SRodney W. Grimes 		return (namecmp(*a, *b));
8834b88c807SRodney W. Grimes 
88451f26ac5SSean Eric Fagan 	if (a_info != b_info &&
88551f26ac5SSean Eric Fagan 	    (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
8864b88c807SRodney W. Grimes 		if (a_info == FTS_D)
8874b88c807SRodney W. Grimes 			return (1);
88851f26ac5SSean Eric Fagan 		if (b_info == FTS_D)
8894b88c807SRodney W. Grimes 			return (-1);
89051f26ac5SSean Eric Fagan 	}
8914b88c807SRodney W. Grimes 	return (sortfcn(*a, *b));
8924b88c807SRodney W. Grimes }
893