19ddb49cbSWarner Losh /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1989, 1993, 1994
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes * Michael Fischbein.
94b88c807SRodney W. Grimes *
104b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes * are met:
134b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes * without specific prior written permission.
214b88c807SRodney W. Grimes *
224b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes * SUCH DAMAGE.
334b88c807SRodney W. Grimes */
344b88c807SRodney W. Grimes
35cf147939SDag-Erling Smørgrav #include <sys/param.h>
364b88c807SRodney W. Grimes #include <sys/stat.h>
374b88c807SRodney W. Grimes #include <sys/ioctl.h>
384d33b62eSRobert Watson #include <sys/mac.h>
394b88c807SRodney W. Grimes
403fee777eSStefan Eßer #include <ctype.h>
414b88c807SRodney W. Grimes #include <dirent.h>
424b88c807SRodney W. Grimes #include <err.h>
434b88c807SRodney W. Grimes #include <errno.h>
444b88c807SRodney W. Grimes #include <fts.h>
45e10ba800SKyle Evans #include <getopt.h>
46576541a9SWarner Losh #include <grp.h>
4740feca3aSMark Murray #include <inttypes.h>
48008a4910SSheldon Hearn #include <limits.h>
49008a4910SSheldon Hearn #include <locale.h>
50576541a9SWarner Losh #include <pwd.h>
517db2f1feSKyle Evans #include <stdbool.h>
524b88c807SRodney W. Grimes #include <stdio.h>
534b88c807SRodney W. Grimes #include <stdlib.h>
544b88c807SRodney W. Grimes #include <string.h>
554b88c807SRodney W. Grimes #include <unistd.h>
56bd82d8abSAndrey A. Chernov #ifdef COLORLS
57bd82d8abSAndrey A. Chernov #include <termcap.h>
58bd82d8abSAndrey A. Chernov #include <signal.h>
59bd82d8abSAndrey A. Chernov #endif
604b88c807SRodney W. Grimes
614b88c807SRodney W. Grimes #include "ls.h"
624b88c807SRodney W. Grimes #include "extern.h"
634b88c807SRodney W. Grimes
64008a4910SSheldon Hearn /*
65008a4910SSheldon Hearn * Upward approximation of the maximum number of characters needed to
66008a4910SSheldon Hearn * represent a value of integral type t as a string, excluding the
67008a4910SSheldon Hearn * NUL terminator, with provision for a sign.
68008a4910SSheldon Hearn */
691f94b779SSheldon Hearn #define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1)
70008a4910SSheldon Hearn
7140feca3aSMark Murray /*
7240feca3aSMark Murray * MAKENINES(n) turns n into (10**n)-1. This is useful for converting a width
7340feca3aSMark Murray * into a number that wide in decimal.
7440feca3aSMark Murray * XXX: Overflows are not considered.
7540feca3aSMark Murray */
7640feca3aSMark Murray #define MAKENINES(n) \
7740feca3aSMark Murray do { \
783fee777eSStefan Eßer intmax_t __i; \
7940feca3aSMark Murray \
8040feca3aSMark Murray /* Use a loop as all values of n are small. */ \
813fee777eSStefan Eßer for (__i = 1; n > 0; __i *= 10) \
8240feca3aSMark Murray n--; \
833fee777eSStefan Eßer n = __i - 1; \
8440feca3aSMark Murray } while(0)
8540feca3aSMark Murray
8640feca3aSMark Murray static void display(const FTSENT *, FTSENT *, int);
870d3bcc2eSGarrett Wollman static int mastercmp(const FTSENT * const *, const FTSENT * const *);
8846251ddeSWarner Losh static void traverse(int, char **, int);
894b88c807SRodney W. Grimes
90e10ba800SKyle Evans #define COLOR_OPT (CHAR_MAX + 1)
91e10ba800SKyle Evans
92e10ba800SKyle Evans static const struct option long_opts[] =
93e10ba800SKyle Evans {
94e10ba800SKyle Evans {"color", optional_argument, NULL, COLOR_OPT},
95e10ba800SKyle Evans {NULL, no_argument, NULL, 0}
96e10ba800SKyle Evans };
97e10ba800SKyle Evans
9840feca3aSMark Murray static void (*printfcn)(const DISPLAY *);
9946251ddeSWarner Losh static int (*sortfcn)(const FTSENT *, const FTSENT *);
1004b88c807SRodney W. Grimes
1014b88c807SRodney W. Grimes long blocksize; /* block size units */
1024b88c807SRodney W. Grimes int termwidth = 80; /* default terminal width */
1034b88c807SRodney W. Grimes
1044b88c807SRodney W. Grimes /* flags */
1054b88c807SRodney W. Grimes int f_accesstime; /* use time of last access */
106fe79420eSJohn Baldwin int f_birthtime; /* use time of birth */
1074b88c807SRodney W. Grimes int f_flags; /* show flags associated with a file */
1080e8d1551SJosef Karthauser int f_humanval; /* show human-readable file sizes */
1094b88c807SRodney W. Grimes int f_inode; /* print inode */
1109052855aSMark Murray static int f_kblocks; /* print size in kilobytes */
1119aa68a3fSGreg Lehey int f_label; /* show MAC label */
1129052855aSMark Murray static int f_listdir; /* list actual directory, not contents */
1139052855aSMark Murray static int f_listdot; /* list files beginning with . */
1144b88c807SRodney W. Grimes int f_longform; /* long listing format */
1159aa68a3fSGreg Lehey static int f_noautodot; /* do not automatically enable -A for root */
1161fe8c2a9SJaakko Heinonen static int f_nofollow; /* don't follow symbolic link arguments */
1174b88c807SRodney W. Grimes int f_nonprint; /* show unprintables as ? */
1189052855aSMark Murray static int f_nosort; /* don't sort output */
119008a4910SSheldon Hearn int f_notabs; /* don't use tab-separated multi-col output */
1200fdf7fa8SConrad Meyer static int f_numericonly; /* don't convert uid/gid to name */
1217ea30648SDag-Erling Smørgrav int f_octal; /* show unprintables as \xxx */
1220d86878cSDag-Erling Smørgrav int f_octal_escape; /* like f_octal but use C escapes if possible */
1239052855aSMark Murray static int f_recursive; /* ls subdirectories also */
1249052855aSMark Murray static int f_reversesort; /* reverse whatever sort is used */
125e2662256SAymeric Wibo static int f_verssort; /* sort names using strverscmp(3) rather than strcoll(3) */
1269aa68a3fSGreg Lehey int f_samesort; /* sort time and name in same direction */
1279aa68a3fSGreg Lehey int f_sectime; /* print full time information */
1289052855aSMark Murray static int f_singlecol; /* use single column output */
1294b88c807SRodney W. Grimes int f_size; /* list size in short listing */
1309aa68a3fSGreg Lehey static int f_sizesort;
13194274c73STim J. Robbins int f_slash; /* similar to f_type, but only for dirs */
13294274c73STim J. Robbins int f_sortacross; /* sort across rows, not down columns */
1333bfbb521SMinsoo Choo int f_sowner; /* disable showing owner's name */
1344b88c807SRodney W. Grimes int f_statustime; /* use time of last mode change */
13540feca3aSMark Murray static int f_stream; /* stream the output, separate with commas */
1369aa68a3fSGreg Lehey int f_thousands; /* show file sizes with thousands separators */
1372269fa57SGreg Lehey char *f_timeformat; /* user-specified time format */
1389aa68a3fSGreg Lehey static int f_timesort; /* sort by time vice name */
1394b88c807SRodney W. Grimes int f_type; /* add type character for non-regular files */
1409052855aSMark Murray static int f_whiteout; /* show whiteout entries */
14174985094SJosef Karthauser #ifdef COLORLS
142c0f34dedSKyle Evans int colorflag = COLORFLAG_NEVER; /* passed in colorflag */
1433885812cSJosef Karthauser int f_color; /* add type in color for non-regular files */
144e10ba800SKyle Evans bool explicitansi; /* Explicit ANSI sequences, no termcap(5) */
1455a890e22SJosef Karthauser char *ansi_bgcol; /* ANSI sequence to set background colour */
1465a890e22SJosef Karthauser char *ansi_fgcol; /* ANSI sequence to set foreground colour */
1475a890e22SJosef Karthauser char *ansi_coloff; /* ANSI sequence to reset colours */
148c1499cf6SJosef Karthauser char *attrs_off; /* ANSI sequence to turn off attributes */
149c1499cf6SJosef Karthauser char *enter_bold; /* ANSI sequence to set color to bold mode */
15097c31821SCameron Katri char *enter_underline; /* ANSI sequence to enter underline mode */
15174985094SJosef Karthauser #endif
1524b88c807SRodney W. Grimes
1539052855aSMark Murray static int rval;
154fb1000d6SAdam David
1557db2f1feSKyle Evans static bool
do_color_from_env(void)1567db2f1feSKyle Evans do_color_from_env(void)
1577db2f1feSKyle Evans {
1587db2f1feSKyle Evans const char *p;
1597db2f1feSKyle Evans bool doit;
1607db2f1feSKyle Evans
1617db2f1feSKyle Evans doit = false;
1627db2f1feSKyle Evans p = getenv("CLICOLOR");
1637db2f1feSKyle Evans if (p == NULL) {
1647db2f1feSKyle Evans /*
1657db2f1feSKyle Evans * COLORTERM is the more standard name for this variable. We'll
1667db2f1feSKyle Evans * honor it as long as it's both set and not empty.
1677db2f1feSKyle Evans */
1687db2f1feSKyle Evans p = getenv("COLORTERM");
1697db2f1feSKyle Evans if (p != NULL && *p != '\0')
1707db2f1feSKyle Evans doit = true;
1717db2f1feSKyle Evans } else
1727db2f1feSKyle Evans doit = true;
1737db2f1feSKyle Evans
1747db2f1feSKyle Evans return (doit &&
1757db2f1feSKyle Evans (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")));
1767db2f1feSKyle Evans }
1777db2f1feSKyle Evans
178e10ba800SKyle Evans static bool
do_color(void)179e10ba800SKyle Evans do_color(void)
180e10ba800SKyle Evans {
181e10ba800SKyle Evans
182e10ba800SKyle Evans #ifdef COLORLS
183e10ba800SKyle Evans if (colorflag == COLORFLAG_NEVER)
184e10ba800SKyle Evans return (false);
185e10ba800SKyle Evans else if (colorflag == COLORFLAG_ALWAYS)
186e10ba800SKyle Evans return (true);
187e10ba800SKyle Evans #endif
188e10ba800SKyle Evans return (do_color_from_env());
189e10ba800SKyle Evans }
190e10ba800SKyle Evans
191517d0a90SKyle Evans #ifdef COLORLS
192041e6eb1SKyle Evans static bool
do_color_always(const char * term)193041e6eb1SKyle Evans do_color_always(const char *term)
194041e6eb1SKyle Evans {
195041e6eb1SKyle Evans
196041e6eb1SKyle Evans return (strcmp(term, "always") == 0 || strcmp(term, "yes") == 0 ||
197041e6eb1SKyle Evans strcmp(term, "force") == 0);
198041e6eb1SKyle Evans }
199041e6eb1SKyle Evans
200041e6eb1SKyle Evans static bool
do_color_never(const char * term)201041e6eb1SKyle Evans do_color_never(const char *term)
202041e6eb1SKyle Evans {
203041e6eb1SKyle Evans
204041e6eb1SKyle Evans return (strcmp(term, "never") == 0 || strcmp(term, "no") == 0 ||
205041e6eb1SKyle Evans strcmp(term, "none") == 0);
206041e6eb1SKyle Evans }
207041e6eb1SKyle Evans
208041e6eb1SKyle Evans static bool
do_color_auto(const char * term)209041e6eb1SKyle Evans do_color_auto(const char *term)
210041e6eb1SKyle Evans {
211041e6eb1SKyle Evans
212041e6eb1SKyle Evans return (strcmp(term, "auto") == 0 || strcmp(term, "tty") == 0 ||
213041e6eb1SKyle Evans strcmp(term, "if-tty") == 0);
214041e6eb1SKyle Evans }
215517d0a90SKyle Evans #endif /* COLORLS */
216041e6eb1SKyle Evans
2174b88c807SRodney W. Grimes int
main(int argc,char * argv[])21846251ddeSWarner Losh main(int argc, char *argv[])
2194b88c807SRodney W. Grimes {
2204b88c807SRodney W. Grimes static char dot[] = ".", *dotav[] = {dot, NULL};
2214b88c807SRodney W. Grimes struct winsize win;
2224b88c807SRodney W. Grimes int ch, fts_options, notused;
2234b88c807SRodney W. Grimes char *p;
224d4bf4151SBaptiste Daroussin const char *errstr = NULL;
2255a890e22SJosef Karthauser #ifdef COLORLS
2265a890e22SJosef Karthauser char termcapbuf[1024]; /* termcap definition buffer */
2275a890e22SJosef Karthauser char tcapbuf[512]; /* capability buffer */
228e10ba800SKyle Evans char *bp = tcapbuf, *term;
2295a890e22SJosef Karthauser #endif
2305a890e22SJosef Karthauser
231f5bd01c6SAndrey A. Chernov (void)setlocale(LC_ALL, "");
232f5bd01c6SAndrey A. Chernov
2334b88c807SRodney W. Grimes /* Terminal defaults to -Cq, non-terminal defaults to -1. */
2344b88c807SRodney W. Grimes if (isatty(STDOUT_FILENO)) {
235a28edf9aSTim J. Robbins termwidth = 80;
236a28edf9aSTim J. Robbins if ((p = getenv("COLUMNS")) != NULL && *p != '\0')
237d4bf4151SBaptiste Daroussin termwidth = strtonum(p, 0, INT_MAX, &errstr);
238a28edf9aSTim J. Robbins else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
239a28edf9aSTim J. Robbins win.ws_col > 0)
2404b88c807SRodney W. Grimes termwidth = win.ws_col;
2419052855aSMark Murray f_nonprint = 1;
24234994fcdSJoerg Wunsch } else {
2434b88c807SRodney W. Grimes f_singlecol = 1;
24434994fcdSJoerg Wunsch /* retrieve environment variable, in case of explicit -C */
2459052855aSMark Murray p = getenv("COLUMNS");
2469052855aSMark Murray if (p)
247d4bf4151SBaptiste Daroussin termwidth = strtonum(p, 0, INT_MAX, &errstr);
24834994fcdSJoerg Wunsch }
2494b88c807SRodney W. Grimes
250d4bf4151SBaptiste Daroussin if (errstr)
251d4bf4151SBaptiste Daroussin termwidth = 80;
252d4bf4151SBaptiste Daroussin
2534b88c807SRodney W. Grimes fts_options = FTS_PHYSICAL;
2549aa68a3fSGreg Lehey if (getenv("LS_SAMESORT"))
2559aa68a3fSGreg Lehey f_samesort = 1;
256a408dc20SKyle Evans
257a408dc20SKyle Evans /*
258a408dc20SKyle Evans * For historical compatibility, we'll use our autodetection if CLICOLOR
259a408dc20SKyle Evans * is set.
260a408dc20SKyle Evans */
261151a7e11SKyle Evans #ifdef COLORLS
262a408dc20SKyle Evans if (getenv("CLICOLOR"))
263a408dc20SKyle Evans colorflag = COLORFLAG_AUTO;
264151a7e11SKyle Evans #endif
265e10ba800SKyle Evans while ((ch = getopt_long(argc, argv,
266e2662256SAymeric Wibo "+1ABCD:FGHILPRSTUWXZabcdfghiklmnopqrstuvwxy,", long_opts,
267e10ba800SKyle Evans NULL)) != -1) {
2684b88c807SRodney W. Grimes switch (ch) {
2694b88c807SRodney W. Grimes /*
27094274c73STim J. Robbins * The -1, -C, -x and -l options all override each other so
27194274c73STim J. Robbins * shell aliasing works right.
2724b88c807SRodney W. Grimes */
2734b88c807SRodney W. Grimes case '1':
2744b88c807SRodney W. Grimes f_singlecol = 1;
2759052855aSMark Murray f_longform = 0;
27694274c73STim J. Robbins f_stream = 0;
2774b88c807SRodney W. Grimes break;
2784b88c807SRodney W. Grimes case 'C':
27994274c73STim J. Robbins f_sortacross = f_longform = f_singlecol = 0;
2804b88c807SRodney W. Grimes break;
2814b88c807SRodney W. Grimes case 'l':
2824b88c807SRodney W. Grimes f_longform = 1;
2839052855aSMark Murray f_singlecol = 0;
28494274c73STim J. Robbins f_stream = 0;
28594274c73STim J. Robbins break;
28694274c73STim J. Robbins case 'x':
28794274c73STim J. Robbins f_sortacross = 1;
28894274c73STim J. Robbins f_longform = 0;
28994274c73STim J. Robbins f_singlecol = 0;
2904b88c807SRodney W. Grimes break;
291fe79420eSJohn Baldwin /* The -c, -u, and -U options override each other. */
2924b88c807SRodney W. Grimes case 'c':
2934b88c807SRodney W. Grimes f_statustime = 1;
2944b88c807SRodney W. Grimes f_accesstime = 0;
295fe79420eSJohn Baldwin f_birthtime = 0;
2964b88c807SRodney W. Grimes break;
2974b88c807SRodney W. Grimes case 'u':
2984b88c807SRodney W. Grimes f_accesstime = 1;
2994b88c807SRodney W. Grimes f_statustime = 0;
300fe79420eSJohn Baldwin f_birthtime = 0;
301fe79420eSJohn Baldwin break;
302fe79420eSJohn Baldwin case 'U':
303fe79420eSJohn Baldwin f_birthtime = 1;
304fe79420eSJohn Baldwin f_accesstime = 0;
305fe79420eSJohn Baldwin f_statustime = 0;
3064b88c807SRodney W. Grimes break;
3073651faa5SGreg Lehey case 'f':
3083651faa5SGreg Lehey f_nosort = 1;
3093651faa5SGreg Lehey /* FALLTHROUGH */
31044f17a81SGreg Lehey case 'a':
31144f17a81SGreg Lehey fts_options |= FTS_SEEDOT;
31244f17a81SGreg Lehey /* FALLTHROUGH */
31344f17a81SGreg Lehey case 'A':
31444f17a81SGreg Lehey f_listdot = 1;
31544f17a81SGreg Lehey break;
316d854370fSAlexander Ziaee /* The -S, -t and -v options override each other. */
31744f17a81SGreg Lehey case 'S':
31844f17a81SGreg Lehey f_sizesort = 1;
31944f17a81SGreg Lehey f_timesort = 0;
320d854370fSAlexander Ziaee f_verssort = 0;
32144f17a81SGreg Lehey break;
32244f17a81SGreg Lehey case 't':
32344f17a81SGreg Lehey f_timesort = 1;
32444f17a81SGreg Lehey f_sizesort = 0;
325d854370fSAlexander Ziaee f_verssort = 0;
326d854370fSAlexander Ziaee break;
327d854370fSAlexander Ziaee case 'v':
328d854370fSAlexander Ziaee f_verssort = 1;
329d854370fSAlexander Ziaee f_sizesort = 0;
330ef75877fSWarner Losh f_timesort = 0;
33144f17a81SGreg Lehey break;
33244f17a81SGreg Lehey /* Other flags. Please keep alphabetic. */
3339aa68a3fSGreg Lehey case ',':
3349aa68a3fSGreg Lehey f_thousands = 1;
3359aa68a3fSGreg Lehey break;
33644f17a81SGreg Lehey case 'B':
33744f17a81SGreg Lehey f_nonprint = 0;
33844f17a81SGreg Lehey f_octal = 1;
33944f17a81SGreg Lehey f_octal_escape = 0;
34044f17a81SGreg Lehey break;
34144f17a81SGreg Lehey case 'D':
34244f17a81SGreg Lehey f_timeformat = optarg;
34344f17a81SGreg Lehey break;
3444b88c807SRodney W. Grimes case 'F':
3454b88c807SRodney W. Grimes f_type = 1;
34694274c73STim J. Robbins f_slash = 0;
3474b88c807SRodney W. Grimes break;
34844f17a81SGreg Lehey case 'G':
349a408dc20SKyle Evans /*
350a408dc20SKyle Evans * We both set CLICOLOR here and set colorflag to
351a408dc20SKyle Evans * COLORFLAG_AUTO, because -G should not force color if
352a408dc20SKyle Evans * stdout isn't a tty.
353a408dc20SKyle Evans */
35444f17a81SGreg Lehey setenv("CLICOLOR", "", 1);
355151a7e11SKyle Evans #ifdef COLORLS
356a408dc20SKyle Evans colorflag = COLORFLAG_AUTO;
357151a7e11SKyle Evans #endif
35844f17a81SGreg Lehey break;
3593a34dbf7SDag-Erling Smørgrav case 'H':
3603a34dbf7SDag-Erling Smørgrav fts_options |= FTS_COMFOLLOW;
3611fe8c2a9SJaakko Heinonen f_nofollow = 0;
3623a34dbf7SDag-Erling Smørgrav break;
36344f17a81SGreg Lehey case 'I':
36444f17a81SGreg Lehey f_noautodot = 1;
3653885812cSJosef Karthauser break;
3664b88c807SRodney W. Grimes case 'L':
3674b88c807SRodney W. Grimes fts_options &= ~FTS_PHYSICAL;
3684b88c807SRodney W. Grimes fts_options |= FTS_LOGICAL;
3691fe8c2a9SJaakko Heinonen f_nofollow = 0;
3704b88c807SRodney W. Grimes break;
3713a34dbf7SDag-Erling Smørgrav case 'P':
3723a34dbf7SDag-Erling Smørgrav fts_options &= ~FTS_COMFOLLOW;
3733a34dbf7SDag-Erling Smørgrav fts_options &= ~FTS_LOGICAL;
3743a34dbf7SDag-Erling Smørgrav fts_options |= FTS_PHYSICAL;
3751fe8c2a9SJaakko Heinonen f_nofollow = 1;
3763a34dbf7SDag-Erling Smørgrav break;
3774b88c807SRodney W. Grimes case 'R':
3784b88c807SRodney W. Grimes f_recursive = 1;
3794b88c807SRodney W. Grimes break;
38044f17a81SGreg Lehey case 'T':
38144f17a81SGreg Lehey f_sectime = 1;
3824b88c807SRodney W. Grimes break;
38344f17a81SGreg Lehey case 'W':
38444f17a81SGreg Lehey f_whiteout = 1;
38544f17a81SGreg Lehey break;
38644f17a81SGreg Lehey case 'Z':
38744f17a81SGreg Lehey f_label = 1;
38844f17a81SGreg Lehey break;
38944f17a81SGreg Lehey case 'b':
39044f17a81SGreg Lehey f_nonprint = 0;
39144f17a81SGreg Lehey f_octal = 0;
39244f17a81SGreg Lehey f_octal_escape = 1;
3937b7d153bSMaxime Henrion break;
3944b88c807SRodney W. Grimes /* The -d option turns off the -R option. */
3954b88c807SRodney W. Grimes case 'd':
3964b88c807SRodney W. Grimes f_listdir = 1;
3974b88c807SRodney W. Grimes f_recursive = 0;
3984b88c807SRodney W. Grimes break;
3993bfbb521SMinsoo Choo case 'g':
4003bfbb521SMinsoo Choo f_longform = 1;
4013bfbb521SMinsoo Choo f_singlecol = 0;
4023bfbb521SMinsoo Choo f_stream = 0;
4033bfbb521SMinsoo Choo f_sowner = 1;
4044b88c807SRodney W. Grimes break;
4050e8d1551SJosef Karthauser case 'h':
4060e8d1551SJosef Karthauser f_humanval = 1;
4070e8d1551SJosef Karthauser break;
4084b88c807SRodney W. Grimes case 'i':
4094b88c807SRodney W. Grimes f_inode = 1;
4104b88c807SRodney W. Grimes break;
411475727a0SPaul Traina case 'k':
412d5f9f41cSDavid E. O'Brien f_humanval = 0;
413475727a0SPaul Traina f_kblocks = 1;
414475727a0SPaul Traina break;
41594274c73STim J. Robbins case 'm':
41694274c73STim J. Robbins f_stream = 1;
41794274c73STim J. Robbins f_singlecol = 0;
41894274c73STim J. Robbins f_longform = 0;
41994274c73STim J. Robbins break;
420f3a6a64eSSheldon Hearn case 'n':
421f3a6a64eSSheldon Hearn f_numericonly = 1;
4223bfbb521SMinsoo Choo f_longform = 1;
4233bfbb521SMinsoo Choo f_singlecol = 0;
4243bfbb521SMinsoo Choo f_stream = 0;
425f3a6a64eSSheldon Hearn break;
4264b88c807SRodney W. Grimes case 'o':
4274b88c807SRodney W. Grimes f_flags = 1;
4284b88c807SRodney W. Grimes break;
42994274c73STim J. Robbins case 'p':
43094274c73STim J. Robbins f_slash = 1;
43194274c73STim J. Robbins f_type = 1;
43294274c73STim J. Robbins break;
4334b88c807SRodney W. Grimes case 'q':
4344b88c807SRodney W. Grimes f_nonprint = 1;
4357ea30648SDag-Erling Smørgrav f_octal = 0;
4360d86878cSDag-Erling Smørgrav f_octal_escape = 0;
4374b88c807SRodney W. Grimes break;
4384b88c807SRodney W. Grimes case 'r':
4394b88c807SRodney W. Grimes f_reversesort = 1;
4404b88c807SRodney W. Grimes break;
4414b88c807SRodney W. Grimes case 's':
4424b88c807SRodney W. Grimes f_size = 1;
4434b88c807SRodney W. Grimes break;
44447f884f0SJosef Karthauser case 'w':
44547f884f0SJosef Karthauser f_nonprint = 0;
44647f884f0SJosef Karthauser f_octal = 0;
44747f884f0SJosef Karthauser f_octal_escape = 0;
44847f884f0SJosef Karthauser break;
4499aa68a3fSGreg Lehey case 'y':
4509aa68a3fSGreg Lehey f_samesort = 1;
4519aa68a3fSGreg Lehey break;
452e10ba800SKyle Evans case COLOR_OPT:
453ced2dcadSPiotr Pawel Stefaniak #ifdef COLORLS
454041e6eb1SKyle Evans if (optarg == NULL || do_color_always(optarg))
455e10ba800SKyle Evans colorflag = COLORFLAG_ALWAYS;
456041e6eb1SKyle Evans else if (do_color_auto(optarg))
457e10ba800SKyle Evans colorflag = COLORFLAG_AUTO;
458041e6eb1SKyle Evans else if (do_color_never(optarg))
459e10ba800SKyle Evans colorflag = COLORFLAG_NEVER;
460e10ba800SKyle Evans else
461e10ba800SKyle Evans errx(2, "unsupported --color value '%s' (must be always, auto, or never)",
462e10ba800SKyle Evans optarg);
463e10ba800SKyle Evans break;
464ced2dcadSPiotr Pawel Stefaniak #else
465ced2dcadSPiotr Pawel Stefaniak warnx("color support not compiled in");
466e10ba800SKyle Evans #endif
4674b88c807SRodney W. Grimes default:
4684b88c807SRodney W. Grimes case '?':
4694b88c807SRodney W. Grimes usage();
4704b88c807SRodney W. Grimes }
4714b88c807SRodney W. Grimes }
4724b88c807SRodney W. Grimes argc -= optind;
4734b88c807SRodney W. Grimes argv += optind;
4744b88c807SRodney W. Grimes
475390a478eSRuslan Ermilov /* Root is -A automatically unless -I. */
476390a478eSRuslan Ermilov if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot)
4777b7d153bSMaxime Henrion f_listdot = 1;
4787b7d153bSMaxime Henrion
479e10ba800SKyle Evans /*
480e10ba800SKyle Evans * Enabling of colours is conditional on the environment in conjunction
481e10ba800SKyle Evans * with the --color and -G arguments, if supplied.
482e10ba800SKyle Evans */
483e10ba800SKyle Evans if (do_color()) {
484d4413063SJosef Karthauser #ifdef COLORLS
485e10ba800SKyle Evans if ((term = getenv("TERM")) != NULL &&
486e10ba800SKyle Evans tgetent(termcapbuf, term) == 1) {
4873d2ddc9eSJosef Karthauser ansi_fgcol = tgetstr("AF", &bp);
4883d2ddc9eSJosef Karthauser ansi_bgcol = tgetstr("AB", &bp);
489c1499cf6SJosef Karthauser attrs_off = tgetstr("me", &bp);
490c1499cf6SJosef Karthauser enter_bold = tgetstr("md", &bp);
49197c31821SCameron Katri enter_underline = tgetstr("us", &bp);
4923d2ddc9eSJosef Karthauser
4933d2ddc9eSJosef Karthauser /* To switch colours off use 'op' if
4943d2ddc9eSJosef Karthauser * available, otherwise use 'oc', or
4953d2ddc9eSJosef Karthauser * don't do colours at all. */
4963d2ddc9eSJosef Karthauser ansi_coloff = tgetstr("op", &bp);
4973d2ddc9eSJosef Karthauser if (!ansi_coloff)
4983d2ddc9eSJosef Karthauser ansi_coloff = tgetstr("oc", &bp);
4993d2ddc9eSJosef Karthauser if (ansi_fgcol && ansi_bgcol && ansi_coloff)
5003d2ddc9eSJosef Karthauser f_color = 1;
501e10ba800SKyle Evans } else if (colorflag == COLORFLAG_ALWAYS) {
502e10ba800SKyle Evans /*
503e10ba800SKyle Evans * If we're *always* doing color but we don't have
504e10ba800SKyle Evans * a functional TERM supplied, we'll fallback to
505e10ba800SKyle Evans * outputting raw ANSI sequences.
506e10ba800SKyle Evans */
507e10ba800SKyle Evans f_color = 1;
508e10ba800SKyle Evans explicitansi = true;
5093d2ddc9eSJosef Karthauser }
510d4413063SJosef Karthauser #endif /*COLORLS*/
511e10ba800SKyle Evans }
5123d2ddc9eSJosef Karthauser
513d4413063SJosef Karthauser #ifdef COLORLS
514bd82d8abSAndrey A. Chernov if (f_color) {
51522ff3e9eSAndrey A. Chernov /*
51622ff3e9eSAndrey A. Chernov * We can't put tabs and color sequences together:
51722ff3e9eSAndrey A. Chernov * column number will be incremented incorrectly
51822ff3e9eSAndrey A. Chernov * for "stty oxtabs" mode.
51922ff3e9eSAndrey A. Chernov */
52022ff3e9eSAndrey A. Chernov f_notabs = 1;
521bd82d8abSAndrey A. Chernov (void)signal(SIGINT, colorquit);
522ab08444fSMartin Cracauer (void)signal(SIGQUIT, colorquit);
5233885812cSJosef Karthauser parsecolors(getenv("LSCOLORS"));
524bd82d8abSAndrey A. Chernov }
52574985094SJosef Karthauser #endif
5263885812cSJosef Karthauser
5274b88c807SRodney W. Grimes /*
52871b8b748SDima Dorfman * If not -F, -i, -l, -s, -S or -t options, don't require stat
5293885812cSJosef Karthauser * information, unless in color mode in which case we do
5303885812cSJosef Karthauser * need this to determine which colors to display.
5314b88c807SRodney W. Grimes */
53271b8b748SDima Dorfman if (!f_inode && !f_longform && !f_size && !f_timesort &&
53371b8b748SDima Dorfman !f_sizesort && !f_type
53474985094SJosef Karthauser #ifdef COLORLS
53574985094SJosef Karthauser && !f_color
53674985094SJosef Karthauser #endif
53774985094SJosef Karthauser )
5384b88c807SRodney W. Grimes fts_options |= FTS_NOSTAT;
5394b88c807SRodney W. Grimes
5404b88c807SRodney W. Grimes /*
5411fe8c2a9SJaakko Heinonen * If not -F, -P, -d or -l options, follow any symbolic links listed on
542183714f3SXin LI * the command line, unless in color mode in which case we need to
543183714f3SXin LI * distinguish file type for a symbolic link itself and its target.
5444b88c807SRodney W. Grimes */
545183714f3SXin LI if (!f_nofollow && !f_longform && !f_listdir && (!f_type || f_slash)
546183714f3SXin LI #ifdef COLORLS
547183714f3SXin LI && !f_color
548183714f3SXin LI #endif
549183714f3SXin LI )
5504b88c807SRodney W. Grimes fts_options |= FTS_COMFOLLOW;
5514b88c807SRodney W. Grimes
552fb5cb208SSteve Price /*
553fb5cb208SSteve Price * If -W, show whiteout entries
554fb5cb208SSteve Price */
555fb5cb208SSteve Price #ifdef FTS_WHITEOUT
556fb5cb208SSteve Price if (f_whiteout)
557fb5cb208SSteve Price fts_options |= FTS_WHITEOUT;
558fb5cb208SSteve Price #endif
559fb5cb208SSteve Price
5604d4dcc7aSDag-Erling Smørgrav /* If -i, -l or -s, figure out block size. */
5614d4dcc7aSDag-Erling Smørgrav if (f_inode || f_longform || f_size) {
56290b0ec31SPoul-Henning Kamp if (f_kblocks)
56390b0ec31SPoul-Henning Kamp blocksize = 2;
56490b0ec31SPoul-Henning Kamp else {
5654b88c807SRodney W. Grimes (void)getbsize(¬used, &blocksize);
5664b88c807SRodney W. Grimes blocksize /= 512;
56790b0ec31SPoul-Henning Kamp }
5684b88c807SRodney W. Grimes }
569ef75877fSWarner Losh
5704b88c807SRodney W. Grimes /* Select a sort function. */
5714b88c807SRodney W. Grimes if (f_reversesort) {
572e2662256SAymeric Wibo if (f_sizesort)
57386cca1e7SJohn Baldwin sortfcn = revsizecmp;
574e2662256SAymeric Wibo else if (f_verssort)
575e2662256SAymeric Wibo sortfcn = revverscmp;
576e2662256SAymeric Wibo else if (!f_timesort)
577e2662256SAymeric Wibo sortfcn = revnamecmp;
5784b88c807SRodney W. Grimes else if (f_accesstime)
5794b88c807SRodney W. Grimes sortfcn = revacccmp;
580fe79420eSJohn Baldwin else if (f_birthtime)
581fe79420eSJohn Baldwin sortfcn = revbirthcmp;
5824b88c807SRodney W. Grimes else if (f_statustime)
5834b88c807SRodney W. Grimes sortfcn = revstatcmp;
5844b88c807SRodney W. Grimes else /* Use modification time. */
5854b88c807SRodney W. Grimes sortfcn = revmodcmp;
5864b88c807SRodney W. Grimes } else {
587e2662256SAymeric Wibo if (f_sizesort)
58886cca1e7SJohn Baldwin sortfcn = sizecmp;
589e2662256SAymeric Wibo else if (f_verssort)
590e2662256SAymeric Wibo sortfcn = verscmp;
591e2662256SAymeric Wibo else if (!f_timesort)
592e2662256SAymeric Wibo sortfcn = namecmp;
5934b88c807SRodney W. Grimes else if (f_accesstime)
5944b88c807SRodney W. Grimes sortfcn = acccmp;
595fe79420eSJohn Baldwin else if (f_birthtime)
596fe79420eSJohn Baldwin sortfcn = birthcmp;
5974b88c807SRodney W. Grimes else if (f_statustime)
5984b88c807SRodney W. Grimes sortfcn = statcmp;
5994b88c807SRodney W. Grimes else /* Use modification time. */
6004b88c807SRodney W. Grimes sortfcn = modcmp;
6014b88c807SRodney W. Grimes }
6024b88c807SRodney W. Grimes
6034b88c807SRodney W. Grimes /* Select a print function. */
6044b88c807SRodney W. Grimes if (f_singlecol)
6054b88c807SRodney W. Grimes printfcn = printscol;
6064b88c807SRodney W. Grimes else if (f_longform)
6074b88c807SRodney W. Grimes printfcn = printlong;
60894274c73STim J. Robbins else if (f_stream)
60994274c73STim J. Robbins printfcn = printstream;
6104b88c807SRodney W. Grimes else
6114b88c807SRodney W. Grimes printfcn = printcol;
6124b88c807SRodney W. Grimes
6134b88c807SRodney W. Grimes if (argc)
6144b88c807SRodney W. Grimes traverse(argc, argv, fts_options);
6154b88c807SRodney W. Grimes else
6164b88c807SRodney W. Grimes traverse(1, dotav, fts_options);
617fb1000d6SAdam David exit(rval);
6184b88c807SRodney W. Grimes }
6194b88c807SRodney W. Grimes
6204b88c807SRodney W. Grimes static int output; /* If anything output. */
6214b88c807SRodney W. Grimes
6224b88c807SRodney W. Grimes /*
6234b88c807SRodney W. Grimes * Traverse() walks the logical directory structure specified by the argv list
6244b88c807SRodney W. Grimes * in the order specified by the mastercmp() comparison function. During the
6254b88c807SRodney W. Grimes * traversal it passes linked lists of structures to display() which represent
6264b88c807SRodney W. Grimes * a superset (may be exact set) of the files to be displayed.
6274b88c807SRodney W. Grimes */
6284b88c807SRodney W. Grimes static void
traverse(int argc,char * argv[],int options)62946251ddeSWarner Losh traverse(int argc, char *argv[], int options)
6304b88c807SRodney W. Grimes {
6314b88c807SRodney W. Grimes FTS *ftsp;
6324b88c807SRodney W. Grimes FTSENT *p, *chp;
6334b88c807SRodney W. Grimes int ch_options;
6344b88c807SRodney W. Grimes
6354b88c807SRodney W. Grimes if ((ftsp =
6364b88c807SRodney W. Grimes fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
6370fdf7fa8SConrad Meyer err(1, "fts_open");
6384b88c807SRodney W. Grimes
63970bad4f7SDavid Schultz /*
64070bad4f7SDavid Schultz * We ignore errors from fts_children here since they will be
64170bad4f7SDavid Schultz * replicated and signalled on the next call to fts_read() below.
64270bad4f7SDavid Schultz */
64370bad4f7SDavid Schultz chp = fts_children(ftsp, 0);
64470bad4f7SDavid Schultz if (chp != NULL)
64570bad4f7SDavid Schultz display(NULL, chp, options);
6464b88c807SRodney W. Grimes if (f_listdir)
6474b88c807SRodney W. Grimes return;
6484b88c807SRodney W. Grimes
6494b88c807SRodney W. Grimes /*
6504b88c807SRodney W. Grimes * If not recursing down this tree and don't need stat info, just get
6514b88c807SRodney W. Grimes * the names.
6524b88c807SRodney W. Grimes */
6533c3f5f9cSRobert Watson ch_options = !f_recursive && !f_label &&
6543c3f5f9cSRobert Watson options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
6554b88c807SRodney W. Grimes
6562dfa4b66SBryan Drewery while (errno = 0, (p = fts_read(ftsp)) != NULL)
6574b88c807SRodney W. Grimes switch (p->fts_info) {
6584b88c807SRodney W. Grimes case FTS_DC:
6590fdf7fa8SConrad Meyer warnx("%s: directory causes a cycle", p->fts_name);
6604b88c807SRodney W. Grimes break;
6614b88c807SRodney W. Grimes case FTS_DNR:
6624b88c807SRodney W. Grimes case FTS_ERR:
6630fdf7fa8SConrad Meyer warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
664fb1000d6SAdam David rval = 1;
6654b88c807SRodney W. Grimes break;
6664b88c807SRodney W. Grimes case FTS_D:
6674b88c807SRodney W. Grimes if (p->fts_level != FTS_ROOTLEVEL &&
668390a478eSRuslan Ermilov p->fts_name[0] == '.' && !f_listdot)
6694b88c807SRodney W. Grimes break;
6704b88c807SRodney W. Grimes
6714b88c807SRodney W. Grimes /*
6724b88c807SRodney W. Grimes * If already output something, put out a newline as
6734b88c807SRodney W. Grimes * a separator. If multiple arguments, precede each
6744b88c807SRodney W. Grimes * directory with its name.
6754b88c807SRodney W. Grimes */
6761656f850STim J. Robbins if (output) {
6770fdf7fa8SConrad Meyer putchar('\n');
6780fdf7fa8SConrad Meyer (void)printname(p->fts_path);
6790fdf7fa8SConrad Meyer puts(":");
6801656f850STim J. Robbins } else if (argc > 1) {
6810fdf7fa8SConrad Meyer (void)printname(p->fts_path);
6820fdf7fa8SConrad Meyer puts(":");
6834b88c807SRodney W. Grimes output = 1;
6844b88c807SRodney W. Grimes }
6854b88c807SRodney W. Grimes chp = fts_children(ftsp, ch_options);
6864d33b62eSRobert Watson display(p, chp, options);
6874b88c807SRodney W. Grimes
6884b88c807SRodney W. Grimes if (!f_recursive && chp != NULL)
6894b88c807SRodney W. Grimes (void)fts_set(ftsp, p, FTS_SKIP);
6904b88c807SRodney W. Grimes break;
691568dcd5fSBill Fumerola default:
692568dcd5fSBill Fumerola break;
6934b88c807SRodney W. Grimes }
6944b88c807SRodney W. Grimes if (errno)
6950fdf7fa8SConrad Meyer err(1, "fts_read");
6964b88c807SRodney W. Grimes }
6974b88c807SRodney W. Grimes
6984b88c807SRodney W. Grimes /*
6994b88c807SRodney W. Grimes * Display() takes a linked list of FTSENT structures and passes the list
7004b88c807SRodney W. Grimes * along with any other necessary information to the print function. P
7014b88c807SRodney W. Grimes * points to the parent directory of the display list.
7024b88c807SRodney W. Grimes */
7034b88c807SRodney W. Grimes static void
display(const FTSENT * p,FTSENT * list,int options)70440feca3aSMark Murray display(const FTSENT *p, FTSENT *list, int options)
7054b88c807SRodney W. Grimes {
7064b88c807SRodney W. Grimes struct stat *sp;
7074b88c807SRodney W. Grimes DISPLAY d;
7084b88c807SRodney W. Grimes FTSENT *cur;
7094b88c807SRodney W. Grimes NAMES *np;
7109052855aSMark Murray off_t maxsize;
71140feca3aSMark Murray long maxblock;
7126db1a7f1SMatthew D Fleming uintmax_t maxinode;
7136db1a7f1SMatthew D Fleming u_long btotal, labelstrlen, maxlen, maxnlink;
7144d33b62eSRobert Watson u_long maxlabelstr;
7159f365aa1SEd Schouten u_int sizelen;
71655926a66SJaakko Heinonen int maxflags;
7179052855aSMark Murray gid_t maxgroup;
7189052855aSMark Murray uid_t maxuser;
7199052855aSMark Murray size_t flen, ulen, glen;
720545f583cSTim Vanderhoek char *initmax;
7214b88c807SRodney W. Grimes int entries, needstats;
7220928a7f1SJuli Mallett const char *user, *group;
7234d33b62eSRobert Watson char *flags, *labelstr = NULL;
724008a4910SSheldon Hearn char ngroup[STRBUF_SIZEOF(uid_t) + 1];
725008a4910SSheldon Hearn char nuser[STRBUF_SIZEOF(gid_t) + 1];
7263fee777eSStefan Eßer u_long width[9];
7273fee777eSStefan Eßer int i;
7284b88c807SRodney W. Grimes
7294b88c807SRodney W. Grimes needstats = f_inode || f_longform || f_size;
7304b88c807SRodney W. Grimes flen = 0;
731545f583cSTim Vanderhoek btotal = 0;
7323fee777eSStefan Eßer
7333fee777eSStefan Eßer #define LS_COLWIDTHS_FIELDS 9
734545f583cSTim Vanderhoek initmax = getenv("LS_COLWIDTHS");
735545f583cSTim Vanderhoek
7363fee777eSStefan Eßer for (i = 0 ; i < LS_COLWIDTHS_FIELDS; i++)
7373fee777eSStefan Eßer width[i] = 0;
7383fee777eSStefan Eßer
7393fee777eSStefan Eßer if (initmax != NULL) {
7403fee777eSStefan Eßer char *endp;
7413fee777eSStefan Eßer
7423fee777eSStefan Eßer for (i = 0; i < LS_COLWIDTHS_FIELDS && *initmax != '\0'; i++) {
7433fee777eSStefan Eßer if (*initmax == ':') {
7443fee777eSStefan Eßer width[i] = 0;
745545f583cSTim Vanderhoek } else {
7463fee777eSStefan Eßer width[i] = strtoul(initmax, &endp, 10);
7473fee777eSStefan Eßer initmax = endp;
7483fee777eSStefan Eßer while (isspace(*initmax))
7493fee777eSStefan Eßer initmax++;
7503fee777eSStefan Eßer if (*initmax != ':')
7513fee777eSStefan Eßer break;
7523fee777eSStefan Eßer initmax++;
753545f583cSTim Vanderhoek }
754545f583cSTim Vanderhoek }
7553fee777eSStefan Eßer if (i < LS_COLWIDTHS_FIELDS)
75618d8a22bSAndrey A. Chernov #ifdef COLORLS
75718d8a22bSAndrey A. Chernov if (!f_color)
75822ff3e9eSAndrey A. Chernov #endif
75918d8a22bSAndrey A. Chernov f_notabs = 0;
760545f583cSTim Vanderhoek }
7613fee777eSStefan Eßer
7623fee777eSStefan Eßer /* Fields match -lios order. New ones should be added at the end. */
7633fee777eSStefan Eßer maxinode = width[0];
7643fee777eSStefan Eßer maxblock = width[1];
7653fee777eSStefan Eßer maxnlink = width[2];
7663fee777eSStefan Eßer maxuser = width[3];
7673fee777eSStefan Eßer maxgroup = width[4];
7683fee777eSStefan Eßer maxflags = width[5];
7693fee777eSStefan Eßer maxsize = width[6];
7703fee777eSStefan Eßer maxlen = width[7];
7713fee777eSStefan Eßer maxlabelstr = width[8];
7723fee777eSStefan Eßer
77340feca3aSMark Murray MAKENINES(maxinode);
77440feca3aSMark Murray MAKENINES(maxblock);
77540feca3aSMark Murray MAKENINES(maxnlink);
77640feca3aSMark Murray MAKENINES(maxsize);
7773fee777eSStefan Eßer
7789f365aa1SEd Schouten d.s_size = 0;
7799f365aa1SEd Schouten sizelen = 0;
7800fd510b7SJoerg Wunsch flags = NULL;
7814b88c807SRodney W. Grimes for (cur = list, entries = 0; cur; cur = cur->fts_link) {
7824b88c807SRodney W. Grimes if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
7830fdf7fa8SConrad Meyer warnx("%s: %s",
7844b88c807SRodney W. Grimes cur->fts_name, strerror(cur->fts_errno));
7854b88c807SRodney W. Grimes cur->fts_number = NO_PRINT;
786fb1000d6SAdam David rval = 1;
7874b88c807SRodney W. Grimes continue;
7884b88c807SRodney W. Grimes }
7894b88c807SRodney W. Grimes /*
7904b88c807SRodney W. Grimes * P is NULL if list is the argv list, to which different rules
7914b88c807SRodney W. Grimes * apply.
7924b88c807SRodney W. Grimes */
7934b88c807SRodney W. Grimes if (p == NULL) {
7944b88c807SRodney W. Grimes /* Directories will be displayed later. */
7954b88c807SRodney W. Grimes if (cur->fts_info == FTS_D && !f_listdir) {
7964b88c807SRodney W. Grimes cur->fts_number = NO_PRINT;
7974b88c807SRodney W. Grimes continue;
7984b88c807SRodney W. Grimes }
7994b88c807SRodney W. Grimes } else {
8004b88c807SRodney W. Grimes /* Only display dot file if -a/-A set. */
801390a478eSRuslan Ermilov if (cur->fts_name[0] == '.' && !f_listdot) {
8024b88c807SRodney W. Grimes cur->fts_number = NO_PRINT;
8034b88c807SRodney W. Grimes continue;
8044b88c807SRodney W. Grimes }
8054b88c807SRodney W. Grimes }
8064b88c807SRodney W. Grimes if (cur->fts_namelen > maxlen)
8074b88c807SRodney W. Grimes maxlen = cur->fts_namelen;
8080d86878cSDag-Erling Smørgrav if (f_octal || f_octal_escape) {
8096bd042dfSJosef Karthauser u_long t = len_octal(cur->fts_name, cur->fts_namelen);
8105dda5d0dSJosef Karthauser
8115dda5d0dSJosef Karthauser if (t > maxlen)
8125dda5d0dSJosef Karthauser maxlen = t;
8137ea30648SDag-Erling Smørgrav }
8144b88c807SRodney W. Grimes if (needstats) {
8154b88c807SRodney W. Grimes sp = cur->fts_statp;
8164b88c807SRodney W. Grimes if (sp->st_blocks > maxblock)
8174b88c807SRodney W. Grimes maxblock = sp->st_blocks;
8184b88c807SRodney W. Grimes if (sp->st_ino > maxinode)
8194b88c807SRodney W. Grimes maxinode = sp->st_ino;
8204b88c807SRodney W. Grimes if (sp->st_nlink > maxnlink)
8214b88c807SRodney W. Grimes maxnlink = sp->st_nlink;
8224b88c807SRodney W. Grimes if (sp->st_size > maxsize)
8234b88c807SRodney W. Grimes maxsize = sp->st_size;
8244b88c807SRodney W. Grimes
8254b88c807SRodney W. Grimes btotal += sp->st_blocks;
8264b88c807SRodney W. Grimes if (f_longform) {
827f3a6a64eSSheldon Hearn if (f_numericonly) {
828f3a6a64eSSheldon Hearn (void)snprintf(nuser, sizeof(nuser),
829f3a6a64eSSheldon Hearn "%u", sp->st_uid);
830f3a6a64eSSheldon Hearn (void)snprintf(ngroup, sizeof(ngroup),
831f3a6a64eSSheldon Hearn "%u", sp->st_gid);
832f3a6a64eSSheldon Hearn user = nuser;
833f3a6a64eSSheldon Hearn group = ngroup;
834f3a6a64eSSheldon Hearn } else {
8354b88c807SRodney W. Grimes user = user_from_uid(sp->st_uid, 0);
836530d2d67SConrad Meyer /*
837530d2d67SConrad Meyer * user_from_uid(..., 0) only returns
838530d2d67SConrad Meyer * NULL in OOM conditions. We could
839530d2d67SConrad Meyer * format the uid here, but (1) in
840530d2d67SConrad Meyer * general ls(1) exits on OOM, and (2)
841530d2d67SConrad Meyer * there is another allocation/exit
842530d2d67SConrad Meyer * path directly below, which will
843530d2d67SConrad Meyer * likely exit anyway.
844530d2d67SConrad Meyer */
845530d2d67SConrad Meyer if (user == NULL)
846530d2d67SConrad Meyer err(1, "user_from_uid");
847f3a6a64eSSheldon Hearn group = group_from_gid(sp->st_gid, 0);
848530d2d67SConrad Meyer /* Ditto. */
849530d2d67SConrad Meyer if (group == NULL)
850530d2d67SConrad Meyer err(1, "group_from_gid");
851f3a6a64eSSheldon Hearn }
8524b88c807SRodney W. Grimes if ((ulen = strlen(user)) > maxuser)
8534b88c807SRodney W. Grimes maxuser = ulen;
8544b88c807SRodney W. Grimes if ((glen = strlen(group)) > maxgroup)
8554b88c807SRodney W. Grimes maxgroup = glen;
8564b88c807SRodney W. Grimes if (f_flags) {
857141d77b8SJosef Karthauser flags = fflagstostr(sp->st_flags);
858141d77b8SJosef Karthauser if (flags != NULL && *flags == '\0') {
859141d77b8SJosef Karthauser free(flags);
860141d77b8SJosef Karthauser flags = strdup("-");
861141d77b8SJosef Karthauser }
862141d77b8SJosef Karthauser if (flags == NULL)
8630fdf7fa8SConrad Meyer err(1, "fflagstostr");
8649052855aSMark Murray flen = strlen(flags);
8659052855aSMark Murray if (flen > (size_t)maxflags)
8664b88c807SRodney W. Grimes maxflags = flen;
8674b88c807SRodney W. Grimes } else
8684b88c807SRodney W. Grimes flen = 0;
8694d33b62eSRobert Watson labelstr = NULL;
8704d33b62eSRobert Watson if (f_label) {
8714df6dabaSRobert Watson char name[PATH_MAX + 1];
8724d33b62eSRobert Watson mac_t label;
8734d33b62eSRobert Watson int error;
8744b88c807SRodney W. Grimes
8754d33b62eSRobert Watson error = mac_prepare_file_label(&label);
8764d33b62eSRobert Watson if (error == -1) {
8770fdf7fa8SConrad Meyer warn("MAC label for %s/%s",
8783c3f5f9cSRobert Watson cur->fts_parent->fts_path,
8793c3f5f9cSRobert Watson cur->fts_name);
8804d33b62eSRobert Watson goto label_out;
8814d33b62eSRobert Watson }
8824d33b62eSRobert Watson
8834df6dabaSRobert Watson if (cur->fts_level == FTS_ROOTLEVEL)
8844df6dabaSRobert Watson snprintf(name, sizeof(name),
8854df6dabaSRobert Watson "%s", cur->fts_name);
8864d33b62eSRobert Watson else
8874df6dabaSRobert Watson snprintf(name, sizeof(name),
8883c3f5f9cSRobert Watson "%s/%s", cur->fts_parent->
8893c3f5f9cSRobert Watson fts_accpath, cur->fts_name);
8904df6dabaSRobert Watson
8914df6dabaSRobert Watson if (options & FTS_LOGICAL)
8924df6dabaSRobert Watson error = mac_get_file(name,
8934df6dabaSRobert Watson label);
8944df6dabaSRobert Watson else
8954df6dabaSRobert Watson error = mac_get_link(name,
8964df6dabaSRobert Watson label);
8974d33b62eSRobert Watson if (error == -1) {
8980fdf7fa8SConrad Meyer warn("MAC label for %s/%s",
8993c3f5f9cSRobert Watson cur->fts_parent->fts_path,
9003c3f5f9cSRobert Watson cur->fts_name);
9014d33b62eSRobert Watson mac_free(label);
9024d33b62eSRobert Watson goto label_out;
9034d33b62eSRobert Watson }
9044d33b62eSRobert Watson
9054d33b62eSRobert Watson error = mac_to_text(label,
9064d33b62eSRobert Watson &labelstr);
9074d33b62eSRobert Watson if (error == -1) {
9080fdf7fa8SConrad Meyer warn("MAC label for %s/%s",
9093c3f5f9cSRobert Watson cur->fts_parent->fts_path,
9103c3f5f9cSRobert Watson cur->fts_name);
9114d33b62eSRobert Watson mac_free(label);
9124d33b62eSRobert Watson goto label_out;
9134d33b62eSRobert Watson }
9144d33b62eSRobert Watson mac_free(label);
9154d33b62eSRobert Watson label_out:
9164d33b62eSRobert Watson if (labelstr == NULL)
917317f1d53SRobert Watson labelstr = strdup("-");
9184d33b62eSRobert Watson labelstrlen = strlen(labelstr);
9194d33b62eSRobert Watson if (labelstrlen > maxlabelstr)
9204d33b62eSRobert Watson maxlabelstr = labelstrlen;
9214d33b62eSRobert Watson } else
9224d33b62eSRobert Watson labelstrlen = 0;
9234d33b62eSRobert Watson
9244d33b62eSRobert Watson if ((np = malloc(sizeof(NAMES) + labelstrlen +
9257304f61fSBrian Feldman ulen + glen + flen + 4)) == NULL)
9260fdf7fa8SConrad Meyer err(1, "malloc");
9274b88c807SRodney W. Grimes
9284b88c807SRodney W. Grimes np->user = &np->data[0];
9294b88c807SRodney W. Grimes (void)strcpy(np->user, user);
9304b88c807SRodney W. Grimes np->group = &np->data[ulen + 1];
9314b88c807SRodney W. Grimes (void)strcpy(np->group, group);
9324b88c807SRodney W. Grimes
9339f365aa1SEd Schouten if (S_ISCHR(sp->st_mode) ||
9349f365aa1SEd Schouten S_ISBLK(sp->st_mode)) {
9359f365aa1SEd Schouten sizelen = snprintf(NULL, 0,
9369f365aa1SEd Schouten "%#jx", (uintmax_t)sp->st_rdev);
9379f365aa1SEd Schouten if (d.s_size < sizelen)
9389f365aa1SEd Schouten d.s_size = sizelen;
93955926a66SJaakko Heinonen }
9404b88c807SRodney W. Grimes
9414b88c807SRodney W. Grimes if (f_flags) {
9424b88c807SRodney W. Grimes np->flags = &np->data[ulen + glen + 2];
9434b88c807SRodney W. Grimes (void)strcpy(np->flags, flags);
944141d77b8SJosef Karthauser free(flags);
9454b88c807SRodney W. Grimes }
9464d33b62eSRobert Watson if (f_label) {
9474d33b62eSRobert Watson np->label = &np->data[ulen + glen + 2
9487304f61fSBrian Feldman + (f_flags ? flen + 1 : 0)];
9494d33b62eSRobert Watson (void)strcpy(np->label, labelstr);
9504d33b62eSRobert Watson free(labelstr);
9517304f61fSBrian Feldman }
9524b88c807SRodney W. Grimes cur->fts_pointer = np;
9534b88c807SRodney W. Grimes }
9544b88c807SRodney W. Grimes }
9554b88c807SRodney W. Grimes ++entries;
9564b88c807SRodney W. Grimes }
9574b88c807SRodney W. Grimes
95870bad4f7SDavid Schultz /*
95970bad4f7SDavid Schultz * If there are no entries to display, we normally stop right
96070bad4f7SDavid Schultz * here. However, we must continue if we have to display the
96170bad4f7SDavid Schultz * total block count. In this case, we display the total only
96270bad4f7SDavid Schultz * on the second (p != NULL) pass.
96370bad4f7SDavid Schultz */
96470bad4f7SDavid Schultz if (!entries && (!(f_longform || f_size) || p == NULL))
9654b88c807SRodney W. Grimes return;
9664b88c807SRodney W. Grimes
9674b88c807SRodney W. Grimes d.list = list;
9684b88c807SRodney W. Grimes d.entries = entries;
9694b88c807SRodney W. Grimes d.maxlen = maxlen;
9704b88c807SRodney W. Grimes if (needstats) {
9714b88c807SRodney W. Grimes d.btotal = btotal;
972cf147939SDag-Erling Smørgrav d.s_block = snprintf(NULL, 0, "%lu", howmany(maxblock, blocksize));
9734b88c807SRodney W. Grimes d.s_flags = maxflags;
9744d33b62eSRobert Watson d.s_label = maxlabelstr;
9754b88c807SRodney W. Grimes d.s_group = maxgroup;
9766db1a7f1SMatthew D Fleming d.s_inode = snprintf(NULL, 0, "%ju", maxinode);
9779f365aa1SEd Schouten d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink);
9789f365aa1SEd Schouten sizelen = f_humanval ? HUMANVALSTR_LEN :
9799f365aa1SEd Schouten snprintf(NULL, 0, "%ju", maxsize);
9809f365aa1SEd Schouten if (d.s_size < sizelen)
9819f365aa1SEd Schouten d.s_size = sizelen;
9824b88c807SRodney W. Grimes d.s_user = maxuser;
9834b88c807SRodney W. Grimes }
9849aa68a3fSGreg Lehey if (f_thousands) /* make space for commas */
9859aa68a3fSGreg Lehey d.s_size += (d.s_size - 1) / 3;
9864b88c807SRodney W. Grimes printfcn(&d);
9874b88c807SRodney W. Grimes output = 1;
9884b88c807SRodney W. Grimes
9894b88c807SRodney W. Grimes if (f_longform)
9904b88c807SRodney W. Grimes for (cur = list; cur; cur = cur->fts_link)
9914b88c807SRodney W. Grimes free(cur->fts_pointer);
9924b88c807SRodney W. Grimes }
9934b88c807SRodney W. Grimes
9944b88c807SRodney W. Grimes /*
9954b88c807SRodney W. Grimes * Ordering for mastercmp:
9964b88c807SRodney W. Grimes * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
9974b88c807SRodney W. Grimes * as larger than directories. Within either group, use the sort function.
9984b88c807SRodney W. Grimes * All other levels use the sort function. Error entries remain unsorted.
9994b88c807SRodney W. Grimes */
10004b88c807SRodney W. Grimes static int
mastercmp(const FTSENT * const * a,const FTSENT * const * b)10010d3bcc2eSGarrett Wollman mastercmp(const FTSENT * const *a, const FTSENT * const *b)
10024b88c807SRodney W. Grimes {
10034b88c807SRodney W. Grimes int a_info, b_info;
10044b88c807SRodney W. Grimes
10054b88c807SRodney W. Grimes a_info = (*a)->fts_info;
10064b88c807SRodney W. Grimes if (a_info == FTS_ERR)
10074b88c807SRodney W. Grimes return (0);
10084b88c807SRodney W. Grimes b_info = (*b)->fts_info;
10094b88c807SRodney W. Grimes if (b_info == FTS_ERR)
10104b88c807SRodney W. Grimes return (0);
10114b88c807SRodney W. Grimes
10124b88c807SRodney W. Grimes if (a_info == FTS_NS || b_info == FTS_NS)
10134b88c807SRodney W. Grimes return (namecmp(*a, *b));
10144b88c807SRodney W. Grimes
101551f26ac5SSean Eric Fagan if (a_info != b_info &&
101651f26ac5SSean Eric Fagan (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
10174b88c807SRodney W. Grimes if (a_info == FTS_D)
10184b88c807SRodney W. Grimes return (1);
101951f26ac5SSean Eric Fagan if (b_info == FTS_D)
10204b88c807SRodney W. Grimes return (-1);
102151f26ac5SSean Eric Fagan }
10224b88c807SRodney W. Grimes return (sortfcn(*a, *b));
10234b88c807SRodney W. Grimes }
1024