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 354b88c807SRodney W. Grimes #ifndef lint 36d46c1a60SSteve Price static const char copyright[] = 374b88c807SRodney W. Grimes "@(#) Copyright (c) 1989, 1993, 1994\n\ 384b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 39febad2fcSSteve Price #endif /* not lint */ 40febad2fcSSteve Price 41febad2fcSSteve Price #if 0 42c73d77ceSMark Murray #ifndef lint 43febad2fcSSteve Price static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94"; 444b88c807SRodney W. Grimes #endif /* not lint */ 45c73d77ceSMark Murray #endif 465eb43ac2SDavid E. O'Brien #include <sys/cdefs.h> 475eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 484b88c807SRodney W. Grimes 49cf147939SDag-Erling Smørgrav #include <sys/param.h> 504b88c807SRodney W. Grimes #include <sys/stat.h> 514b88c807SRodney W. Grimes #include <sys/ioctl.h> 524d33b62eSRobert Watson #include <sys/mac.h> 534b88c807SRodney W. Grimes 543fee777eSStefan Eßer #include <ctype.h> 554b88c807SRodney W. Grimes #include <dirent.h> 564b88c807SRodney W. Grimes #include <err.h> 574b88c807SRodney W. Grimes #include <errno.h> 584b88c807SRodney W. Grimes #include <fts.h> 59e10ba800SKyle Evans #include <getopt.h> 60576541a9SWarner Losh #include <grp.h> 6140feca3aSMark Murray #include <inttypes.h> 62008a4910SSheldon Hearn #include <limits.h> 63008a4910SSheldon Hearn #include <locale.h> 64576541a9SWarner Losh #include <pwd.h> 657db2f1feSKyle Evans #include <stdbool.h> 664b88c807SRodney W. Grimes #include <stdio.h> 674b88c807SRodney W. Grimes #include <stdlib.h> 684b88c807SRodney W. Grimes #include <string.h> 694b88c807SRodney W. Grimes #include <unistd.h> 70bd82d8abSAndrey A. Chernov #ifdef COLORLS 71bd82d8abSAndrey A. Chernov #include <termcap.h> 72bd82d8abSAndrey A. Chernov #include <signal.h> 73bd82d8abSAndrey A. Chernov #endif 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes #include "ls.h" 764b88c807SRodney W. Grimes #include "extern.h" 774b88c807SRodney W. Grimes 78008a4910SSheldon Hearn /* 79008a4910SSheldon Hearn * Upward approximation of the maximum number of characters needed to 80008a4910SSheldon Hearn * represent a value of integral type t as a string, excluding the 81008a4910SSheldon Hearn * NUL terminator, with provision for a sign. 82008a4910SSheldon Hearn */ 831f94b779SSheldon Hearn #define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1) 84008a4910SSheldon Hearn 8540feca3aSMark Murray /* 8640feca3aSMark Murray * MAKENINES(n) turns n into (10**n)-1. This is useful for converting a width 8740feca3aSMark Murray * into a number that wide in decimal. 8840feca3aSMark Murray * XXX: Overflows are not considered. 8940feca3aSMark Murray */ 9040feca3aSMark Murray #define MAKENINES(n) \ 9140feca3aSMark Murray do { \ 923fee777eSStefan Eßer intmax_t __i; \ 9340feca3aSMark Murray \ 9440feca3aSMark Murray /* Use a loop as all values of n are small. */ \ 953fee777eSStefan Eßer for (__i = 1; n > 0; __i *= 10) \ 9640feca3aSMark Murray n--; \ 973fee777eSStefan Eßer n = __i - 1; \ 9840feca3aSMark Murray } while(0) 9940feca3aSMark Murray 10040feca3aSMark Murray static void display(const FTSENT *, FTSENT *, int); 1010d3bcc2eSGarrett Wollman static int mastercmp(const FTSENT * const *, const FTSENT * const *); 10246251ddeSWarner Losh static void traverse(int, char **, int); 1034b88c807SRodney W. Grimes 104e10ba800SKyle Evans #define COLOR_OPT (CHAR_MAX + 1) 105e10ba800SKyle Evans 106e10ba800SKyle Evans static const struct option long_opts[] = 107e10ba800SKyle Evans { 108e10ba800SKyle Evans {"color", optional_argument, NULL, COLOR_OPT}, 109e10ba800SKyle Evans {NULL, no_argument, NULL, 0} 110e10ba800SKyle Evans }; 111e10ba800SKyle Evans 11240feca3aSMark Murray static void (*printfcn)(const DISPLAY *); 11346251ddeSWarner Losh static int (*sortfcn)(const FTSENT *, const FTSENT *); 1144b88c807SRodney W. Grimes 1154b88c807SRodney W. Grimes long blocksize; /* block size units */ 1164b88c807SRodney W. Grimes int termwidth = 80; /* default terminal width */ 1174b88c807SRodney W. Grimes 1184b88c807SRodney W. Grimes /* flags */ 1194b88c807SRodney W. Grimes int f_accesstime; /* use time of last access */ 120fe79420eSJohn Baldwin int f_birthtime; /* use time of birth */ 1214b88c807SRodney W. Grimes int f_flags; /* show flags associated with a file */ 1220e8d1551SJosef Karthauser int f_humanval; /* show human-readable file sizes */ 1234b88c807SRodney W. Grimes int f_inode; /* print inode */ 1249052855aSMark Murray static int f_kblocks; /* print size in kilobytes */ 1259aa68a3fSGreg Lehey int f_label; /* show MAC label */ 1269052855aSMark Murray static int f_listdir; /* list actual directory, not contents */ 1279052855aSMark Murray static int f_listdot; /* list files beginning with . */ 1284b88c807SRodney W. Grimes int f_longform; /* long listing format */ 1299aa68a3fSGreg Lehey static int f_noautodot; /* do not automatically enable -A for root */ 1301fe8c2a9SJaakko Heinonen static int f_nofollow; /* don't follow symbolic link arguments */ 1314b88c807SRodney W. Grimes int f_nonprint; /* show unprintables as ? */ 1329052855aSMark Murray static int f_nosort; /* don't sort output */ 133008a4910SSheldon Hearn int f_notabs; /* don't use tab-separated multi-col output */ 1340fdf7fa8SConrad Meyer static int f_numericonly; /* don't convert uid/gid to name */ 1357ea30648SDag-Erling Smørgrav int f_octal; /* show unprintables as \xxx */ 1360d86878cSDag-Erling Smørgrav int f_octal_escape; /* like f_octal but use C escapes if possible */ 1379052855aSMark Murray static int f_recursive; /* ls subdirectories also */ 1389052855aSMark Murray static int f_reversesort; /* reverse whatever sort is used */ 139e2662256SAymeric Wibo static int f_verssort; /* sort names using strverscmp(3) rather than strcoll(3) */ 1409aa68a3fSGreg Lehey int f_samesort; /* sort time and name in same direction */ 1419aa68a3fSGreg Lehey int f_sectime; /* print full time information */ 1429052855aSMark Murray static int f_singlecol; /* use single column output */ 1434b88c807SRodney W. Grimes int f_size; /* list size in short listing */ 1449aa68a3fSGreg Lehey static int f_sizesort; 14594274c73STim J. Robbins int f_slash; /* similar to f_type, but only for dirs */ 14694274c73STim J. Robbins int f_sortacross; /* sort across rows, not down columns */ 1473bfbb521SMinsoo Choo int f_sowner; /* disable showing owner's name */ 1484b88c807SRodney W. Grimes int f_statustime; /* use time of last mode change */ 14940feca3aSMark Murray static int f_stream; /* stream the output, separate with commas */ 1509aa68a3fSGreg Lehey int f_thousands; /* show file sizes with thousands separators */ 1512269fa57SGreg Lehey char *f_timeformat; /* user-specified time format */ 1529aa68a3fSGreg Lehey static int f_timesort; /* sort by time vice name */ 1534b88c807SRodney W. Grimes int f_type; /* add type character for non-regular files */ 1549052855aSMark Murray static int f_whiteout; /* show whiteout entries */ 15574985094SJosef Karthauser #ifdef COLORLS 156c0f34dedSKyle Evans int colorflag = COLORFLAG_NEVER; /* passed in colorflag */ 1573885812cSJosef Karthauser int f_color; /* add type in color for non-regular files */ 158e10ba800SKyle Evans bool explicitansi; /* Explicit ANSI sequences, no termcap(5) */ 1595a890e22SJosef Karthauser char *ansi_bgcol; /* ANSI sequence to set background colour */ 1605a890e22SJosef Karthauser char *ansi_fgcol; /* ANSI sequence to set foreground colour */ 1615a890e22SJosef Karthauser char *ansi_coloff; /* ANSI sequence to reset colours */ 162c1499cf6SJosef Karthauser char *attrs_off; /* ANSI sequence to turn off attributes */ 163c1499cf6SJosef Karthauser char *enter_bold; /* ANSI sequence to set color to bold mode */ 16497c31821SCameron Katri char *enter_underline; /* ANSI sequence to enter underline mode */ 16574985094SJosef Karthauser #endif 1664b88c807SRodney W. Grimes 1679052855aSMark Murray static int rval; 168fb1000d6SAdam David 1697db2f1feSKyle Evans static bool 1707db2f1feSKyle Evans do_color_from_env(void) 1717db2f1feSKyle Evans { 1727db2f1feSKyle Evans const char *p; 1737db2f1feSKyle Evans bool doit; 1747db2f1feSKyle Evans 1757db2f1feSKyle Evans doit = false; 1767db2f1feSKyle Evans p = getenv("CLICOLOR"); 1777db2f1feSKyle Evans if (p == NULL) { 1787db2f1feSKyle Evans /* 1797db2f1feSKyle Evans * COLORTERM is the more standard name for this variable. We'll 1807db2f1feSKyle Evans * honor it as long as it's both set and not empty. 1817db2f1feSKyle Evans */ 1827db2f1feSKyle Evans p = getenv("COLORTERM"); 1837db2f1feSKyle Evans if (p != NULL && *p != '\0') 1847db2f1feSKyle Evans doit = true; 1857db2f1feSKyle Evans } else 1867db2f1feSKyle Evans doit = true; 1877db2f1feSKyle Evans 1887db2f1feSKyle Evans return (doit && 1897db2f1feSKyle Evans (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE"))); 1907db2f1feSKyle Evans } 1917db2f1feSKyle Evans 192e10ba800SKyle Evans static bool 193e10ba800SKyle Evans do_color(void) 194e10ba800SKyle Evans { 195e10ba800SKyle Evans 196e10ba800SKyle Evans #ifdef COLORLS 197e10ba800SKyle Evans if (colorflag == COLORFLAG_NEVER) 198e10ba800SKyle Evans return (false); 199e10ba800SKyle Evans else if (colorflag == COLORFLAG_ALWAYS) 200e10ba800SKyle Evans return (true); 201e10ba800SKyle Evans #endif 202e10ba800SKyle Evans return (do_color_from_env()); 203e10ba800SKyle Evans } 204e10ba800SKyle Evans 205517d0a90SKyle Evans #ifdef COLORLS 206041e6eb1SKyle Evans static bool 207041e6eb1SKyle Evans do_color_always(const char *term) 208041e6eb1SKyle Evans { 209041e6eb1SKyle Evans 210041e6eb1SKyle Evans return (strcmp(term, "always") == 0 || strcmp(term, "yes") == 0 || 211041e6eb1SKyle Evans strcmp(term, "force") == 0); 212041e6eb1SKyle Evans } 213041e6eb1SKyle Evans 214041e6eb1SKyle Evans static bool 215041e6eb1SKyle Evans do_color_never(const char *term) 216041e6eb1SKyle Evans { 217041e6eb1SKyle Evans 218041e6eb1SKyle Evans return (strcmp(term, "never") == 0 || strcmp(term, "no") == 0 || 219041e6eb1SKyle Evans strcmp(term, "none") == 0); 220041e6eb1SKyle Evans } 221041e6eb1SKyle Evans 222041e6eb1SKyle Evans static bool 223041e6eb1SKyle Evans do_color_auto(const char *term) 224041e6eb1SKyle Evans { 225041e6eb1SKyle Evans 226041e6eb1SKyle Evans return (strcmp(term, "auto") == 0 || strcmp(term, "tty") == 0 || 227041e6eb1SKyle Evans strcmp(term, "if-tty") == 0); 228041e6eb1SKyle Evans } 229517d0a90SKyle Evans #endif /* COLORLS */ 230041e6eb1SKyle Evans 2314b88c807SRodney W. Grimes int 23246251ddeSWarner Losh main(int argc, char *argv[]) 2334b88c807SRodney W. Grimes { 2344b88c807SRodney W. Grimes static char dot[] = ".", *dotav[] = {dot, NULL}; 2354b88c807SRodney W. Grimes struct winsize win; 2364b88c807SRodney W. Grimes int ch, fts_options, notused; 2374b88c807SRodney W. Grimes char *p; 238d4bf4151SBaptiste Daroussin const char *errstr = NULL; 2395a890e22SJosef Karthauser #ifdef COLORLS 2405a890e22SJosef Karthauser char termcapbuf[1024]; /* termcap definition buffer */ 2415a890e22SJosef Karthauser char tcapbuf[512]; /* capability buffer */ 242e10ba800SKyle Evans char *bp = tcapbuf, *term; 2435a890e22SJosef Karthauser #endif 2445a890e22SJosef Karthauser 245f5bd01c6SAndrey A. Chernov (void)setlocale(LC_ALL, ""); 246f5bd01c6SAndrey A. Chernov 2474b88c807SRodney W. Grimes /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 2484b88c807SRodney W. Grimes if (isatty(STDOUT_FILENO)) { 249a28edf9aSTim J. Robbins termwidth = 80; 250a28edf9aSTim J. Robbins if ((p = getenv("COLUMNS")) != NULL && *p != '\0') 251d4bf4151SBaptiste Daroussin termwidth = strtonum(p, 0, INT_MAX, &errstr); 252a28edf9aSTim J. Robbins else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && 253a28edf9aSTim J. Robbins win.ws_col > 0) 2544b88c807SRodney W. Grimes termwidth = win.ws_col; 2559052855aSMark Murray f_nonprint = 1; 25634994fcdSJoerg Wunsch } else { 2574b88c807SRodney W. Grimes f_singlecol = 1; 25834994fcdSJoerg Wunsch /* retrieve environment variable, in case of explicit -C */ 2599052855aSMark Murray p = getenv("COLUMNS"); 2609052855aSMark Murray if (p) 261d4bf4151SBaptiste Daroussin termwidth = strtonum(p, 0, INT_MAX, &errstr); 26234994fcdSJoerg Wunsch } 2634b88c807SRodney W. Grimes 264d4bf4151SBaptiste Daroussin if (errstr) 265d4bf4151SBaptiste Daroussin termwidth = 80; 266d4bf4151SBaptiste Daroussin 2674b88c807SRodney W. Grimes fts_options = FTS_PHYSICAL; 2689aa68a3fSGreg Lehey if (getenv("LS_SAMESORT")) 2699aa68a3fSGreg Lehey f_samesort = 1; 270a408dc20SKyle Evans 271a408dc20SKyle Evans /* 272a408dc20SKyle Evans * For historical compatibility, we'll use our autodetection if CLICOLOR 273a408dc20SKyle Evans * is set. 274a408dc20SKyle Evans */ 275151a7e11SKyle Evans #ifdef COLORLS 276a408dc20SKyle Evans if (getenv("CLICOLOR")) 277a408dc20SKyle Evans colorflag = COLORFLAG_AUTO; 278151a7e11SKyle Evans #endif 279e10ba800SKyle Evans while ((ch = getopt_long(argc, argv, 280e2662256SAymeric Wibo "+1ABCD:FGHILPRSTUWXZabcdfghiklmnopqrstuvwxy,", long_opts, 281e10ba800SKyle Evans NULL)) != -1) { 2824b88c807SRodney W. Grimes switch (ch) { 2834b88c807SRodney W. Grimes /* 28494274c73STim J. Robbins * The -1, -C, -x and -l options all override each other so 28594274c73STim J. Robbins * shell aliasing works right. 2864b88c807SRodney W. Grimes */ 2874b88c807SRodney W. Grimes case '1': 2884b88c807SRodney W. Grimes f_singlecol = 1; 2899052855aSMark Murray f_longform = 0; 29094274c73STim J. Robbins f_stream = 0; 2914b88c807SRodney W. Grimes break; 2924b88c807SRodney W. Grimes case 'C': 29394274c73STim J. Robbins f_sortacross = f_longform = f_singlecol = 0; 2944b88c807SRodney W. Grimes break; 2954b88c807SRodney W. Grimes case 'l': 2964b88c807SRodney W. Grimes f_longform = 1; 2979052855aSMark Murray f_singlecol = 0; 29894274c73STim J. Robbins f_stream = 0; 29994274c73STim J. Robbins break; 30094274c73STim J. Robbins case 'x': 30194274c73STim J. Robbins f_sortacross = 1; 30294274c73STim J. Robbins f_longform = 0; 30394274c73STim J. Robbins f_singlecol = 0; 3044b88c807SRodney W. Grimes break; 305fe79420eSJohn Baldwin /* The -c, -u, and -U options override each other. */ 3064b88c807SRodney W. Grimes case 'c': 3074b88c807SRodney W. Grimes f_statustime = 1; 3084b88c807SRodney W. Grimes f_accesstime = 0; 309fe79420eSJohn Baldwin f_birthtime = 0; 3104b88c807SRodney W. Grimes break; 3114b88c807SRodney W. Grimes case 'u': 3124b88c807SRodney W. Grimes f_accesstime = 1; 3134b88c807SRodney W. Grimes f_statustime = 0; 314fe79420eSJohn Baldwin f_birthtime = 0; 315fe79420eSJohn Baldwin break; 316fe79420eSJohn Baldwin case 'U': 317fe79420eSJohn Baldwin f_birthtime = 1; 318fe79420eSJohn Baldwin f_accesstime = 0; 319fe79420eSJohn Baldwin f_statustime = 0; 3204b88c807SRodney W. Grimes break; 3213651faa5SGreg Lehey case 'f': 3223651faa5SGreg Lehey f_nosort = 1; 3233651faa5SGreg Lehey /* FALLTHROUGH */ 32444f17a81SGreg Lehey case 'a': 32544f17a81SGreg Lehey fts_options |= FTS_SEEDOT; 32644f17a81SGreg Lehey /* FALLTHROUGH */ 32744f17a81SGreg Lehey case 'A': 32844f17a81SGreg Lehey f_listdot = 1; 32944f17a81SGreg Lehey break; 33044f17a81SGreg Lehey /* The -t and -S options override each other. */ 33144f17a81SGreg Lehey case 'S': 33244f17a81SGreg Lehey f_sizesort = 1; 33344f17a81SGreg Lehey f_timesort = 0; 33444f17a81SGreg Lehey break; 33544f17a81SGreg Lehey case 't': 33644f17a81SGreg Lehey f_timesort = 1; 33744f17a81SGreg Lehey f_sizesort = 0; 33844f17a81SGreg Lehey break; 33944f17a81SGreg Lehey /* Other flags. Please keep alphabetic. */ 3409aa68a3fSGreg Lehey case ',': 3419aa68a3fSGreg Lehey f_thousands = 1; 3429aa68a3fSGreg Lehey break; 34344f17a81SGreg Lehey case 'B': 34444f17a81SGreg Lehey f_nonprint = 0; 34544f17a81SGreg Lehey f_octal = 1; 34644f17a81SGreg Lehey f_octal_escape = 0; 34744f17a81SGreg Lehey break; 34844f17a81SGreg Lehey case 'D': 34944f17a81SGreg Lehey f_timeformat = optarg; 35044f17a81SGreg Lehey break; 3514b88c807SRodney W. Grimes case 'F': 3524b88c807SRodney W. Grimes f_type = 1; 35394274c73STim J. Robbins f_slash = 0; 3544b88c807SRodney W. Grimes break; 35544f17a81SGreg Lehey case 'G': 356a408dc20SKyle Evans /* 357a408dc20SKyle Evans * We both set CLICOLOR here and set colorflag to 358a408dc20SKyle Evans * COLORFLAG_AUTO, because -G should not force color if 359a408dc20SKyle Evans * stdout isn't a tty. 360a408dc20SKyle Evans */ 36144f17a81SGreg Lehey setenv("CLICOLOR", "", 1); 362151a7e11SKyle Evans #ifdef COLORLS 363a408dc20SKyle Evans colorflag = COLORFLAG_AUTO; 364151a7e11SKyle Evans #endif 36544f17a81SGreg Lehey break; 3663a34dbf7SDag-Erling Smørgrav case 'H': 3673a34dbf7SDag-Erling Smørgrav fts_options |= FTS_COMFOLLOW; 3681fe8c2a9SJaakko Heinonen f_nofollow = 0; 3693a34dbf7SDag-Erling Smørgrav break; 37044f17a81SGreg Lehey case 'I': 37144f17a81SGreg Lehey f_noautodot = 1; 3723885812cSJosef Karthauser break; 3734b88c807SRodney W. Grimes case 'L': 3744b88c807SRodney W. Grimes fts_options &= ~FTS_PHYSICAL; 3754b88c807SRodney W. Grimes fts_options |= FTS_LOGICAL; 3761fe8c2a9SJaakko Heinonen f_nofollow = 0; 3774b88c807SRodney W. Grimes break; 3783a34dbf7SDag-Erling Smørgrav case 'P': 3793a34dbf7SDag-Erling Smørgrav fts_options &= ~FTS_COMFOLLOW; 3803a34dbf7SDag-Erling Smørgrav fts_options &= ~FTS_LOGICAL; 3813a34dbf7SDag-Erling Smørgrav fts_options |= FTS_PHYSICAL; 3821fe8c2a9SJaakko Heinonen f_nofollow = 1; 3833a34dbf7SDag-Erling Smørgrav break; 3844b88c807SRodney W. Grimes case 'R': 3854b88c807SRodney W. Grimes f_recursive = 1; 3864b88c807SRodney W. Grimes break; 38744f17a81SGreg Lehey case 'T': 38844f17a81SGreg Lehey f_sectime = 1; 3894b88c807SRodney W. Grimes break; 39044f17a81SGreg Lehey case 'W': 39144f17a81SGreg Lehey f_whiteout = 1; 39244f17a81SGreg Lehey break; 39344f17a81SGreg Lehey case 'Z': 39444f17a81SGreg Lehey f_label = 1; 39544f17a81SGreg Lehey break; 39644f17a81SGreg Lehey case 'b': 39744f17a81SGreg Lehey f_nonprint = 0; 39844f17a81SGreg Lehey f_octal = 0; 39944f17a81SGreg Lehey f_octal_escape = 1; 4007b7d153bSMaxime Henrion break; 4014b88c807SRodney W. Grimes /* The -d option turns off the -R option. */ 4024b88c807SRodney W. Grimes case 'd': 4034b88c807SRodney W. Grimes f_listdir = 1; 4044b88c807SRodney W. Grimes f_recursive = 0; 4054b88c807SRodney W. Grimes break; 4063bfbb521SMinsoo Choo case 'g': 4073bfbb521SMinsoo Choo f_longform = 1; 4083bfbb521SMinsoo Choo f_singlecol = 0; 4093bfbb521SMinsoo Choo f_stream = 0; 4103bfbb521SMinsoo Choo f_sowner = 1; 4114b88c807SRodney W. Grimes break; 4120e8d1551SJosef Karthauser case 'h': 4130e8d1551SJosef Karthauser f_humanval = 1; 4140e8d1551SJosef Karthauser break; 4154b88c807SRodney W. Grimes case 'i': 4164b88c807SRodney W. Grimes f_inode = 1; 4174b88c807SRodney W. Grimes break; 418475727a0SPaul Traina case 'k': 419d5f9f41cSDavid E. O'Brien f_humanval = 0; 420475727a0SPaul Traina f_kblocks = 1; 421475727a0SPaul Traina break; 42294274c73STim J. Robbins case 'm': 42394274c73STim J. Robbins f_stream = 1; 42494274c73STim J. Robbins f_singlecol = 0; 42594274c73STim J. Robbins f_longform = 0; 42694274c73STim J. Robbins break; 427f3a6a64eSSheldon Hearn case 'n': 428f3a6a64eSSheldon Hearn f_numericonly = 1; 4293bfbb521SMinsoo Choo f_longform = 1; 4303bfbb521SMinsoo Choo f_singlecol = 0; 4313bfbb521SMinsoo Choo f_stream = 0; 432f3a6a64eSSheldon Hearn break; 4334b88c807SRodney W. Grimes case 'o': 4344b88c807SRodney W. Grimes f_flags = 1; 4354b88c807SRodney W. Grimes break; 43694274c73STim J. Robbins case 'p': 43794274c73STim J. Robbins f_slash = 1; 43894274c73STim J. Robbins f_type = 1; 43994274c73STim J. Robbins break; 4404b88c807SRodney W. Grimes case 'q': 4414b88c807SRodney W. Grimes f_nonprint = 1; 4427ea30648SDag-Erling Smørgrav f_octal = 0; 4430d86878cSDag-Erling Smørgrav f_octal_escape = 0; 4444b88c807SRodney W. Grimes break; 4454b88c807SRodney W. Grimes case 'r': 4464b88c807SRodney W. Grimes f_reversesort = 1; 4474b88c807SRodney W. Grimes break; 4484b88c807SRodney W. Grimes case 's': 4494b88c807SRodney W. Grimes f_size = 1; 4504b88c807SRodney W. Grimes break; 451e2662256SAymeric Wibo case 'v': 452e2662256SAymeric Wibo f_verssort = 1; 453e2662256SAymeric Wibo break; 45447f884f0SJosef Karthauser case 'w': 45547f884f0SJosef Karthauser f_nonprint = 0; 45647f884f0SJosef Karthauser f_octal = 0; 45747f884f0SJosef Karthauser f_octal_escape = 0; 45847f884f0SJosef Karthauser break; 4599aa68a3fSGreg Lehey case 'y': 4609aa68a3fSGreg Lehey f_samesort = 1; 4619aa68a3fSGreg Lehey break; 462e10ba800SKyle Evans case COLOR_OPT: 463ced2dcadSPiotr Pawel Stefaniak #ifdef COLORLS 464041e6eb1SKyle Evans if (optarg == NULL || do_color_always(optarg)) 465e10ba800SKyle Evans colorflag = COLORFLAG_ALWAYS; 466041e6eb1SKyle Evans else if (do_color_auto(optarg)) 467e10ba800SKyle Evans colorflag = COLORFLAG_AUTO; 468041e6eb1SKyle Evans else if (do_color_never(optarg)) 469e10ba800SKyle Evans colorflag = COLORFLAG_NEVER; 470e10ba800SKyle Evans else 471e10ba800SKyle Evans errx(2, "unsupported --color value '%s' (must be always, auto, or never)", 472e10ba800SKyle Evans optarg); 473e10ba800SKyle Evans break; 474ced2dcadSPiotr Pawel Stefaniak #else 475ced2dcadSPiotr Pawel Stefaniak warnx("color support not compiled in"); 476e10ba800SKyle Evans #endif 4774b88c807SRodney W. Grimes default: 4784b88c807SRodney W. Grimes case '?': 4794b88c807SRodney W. Grimes usage(); 4804b88c807SRodney W. Grimes } 4814b88c807SRodney W. Grimes } 4824b88c807SRodney W. Grimes argc -= optind; 4834b88c807SRodney W. Grimes argv += optind; 4844b88c807SRodney W. Grimes 485390a478eSRuslan Ermilov /* Root is -A automatically unless -I. */ 486390a478eSRuslan Ermilov if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot) 4877b7d153bSMaxime Henrion f_listdot = 1; 4887b7d153bSMaxime Henrion 489e10ba800SKyle Evans /* 490e10ba800SKyle Evans * Enabling of colours is conditional on the environment in conjunction 491e10ba800SKyle Evans * with the --color and -G arguments, if supplied. 492e10ba800SKyle Evans */ 493e10ba800SKyle Evans if (do_color()) { 494d4413063SJosef Karthauser #ifdef COLORLS 495e10ba800SKyle Evans if ((term = getenv("TERM")) != NULL && 496e10ba800SKyle Evans tgetent(termcapbuf, term) == 1) { 4973d2ddc9eSJosef Karthauser ansi_fgcol = tgetstr("AF", &bp); 4983d2ddc9eSJosef Karthauser ansi_bgcol = tgetstr("AB", &bp); 499c1499cf6SJosef Karthauser attrs_off = tgetstr("me", &bp); 500c1499cf6SJosef Karthauser enter_bold = tgetstr("md", &bp); 50197c31821SCameron Katri enter_underline = tgetstr("us", &bp); 5023d2ddc9eSJosef Karthauser 5033d2ddc9eSJosef Karthauser /* To switch colours off use 'op' if 5043d2ddc9eSJosef Karthauser * available, otherwise use 'oc', or 5053d2ddc9eSJosef Karthauser * don't do colours at all. */ 5063d2ddc9eSJosef Karthauser ansi_coloff = tgetstr("op", &bp); 5073d2ddc9eSJosef Karthauser if (!ansi_coloff) 5083d2ddc9eSJosef Karthauser ansi_coloff = tgetstr("oc", &bp); 5093d2ddc9eSJosef Karthauser if (ansi_fgcol && ansi_bgcol && ansi_coloff) 5103d2ddc9eSJosef Karthauser f_color = 1; 511e10ba800SKyle Evans } else if (colorflag == COLORFLAG_ALWAYS) { 512e10ba800SKyle Evans /* 513e10ba800SKyle Evans * If we're *always* doing color but we don't have 514e10ba800SKyle Evans * a functional TERM supplied, we'll fallback to 515e10ba800SKyle Evans * outputting raw ANSI sequences. 516e10ba800SKyle Evans */ 517e10ba800SKyle Evans f_color = 1; 518e10ba800SKyle Evans explicitansi = true; 5193d2ddc9eSJosef Karthauser } 520d4413063SJosef Karthauser #endif /*COLORLS*/ 521e10ba800SKyle Evans } 5223d2ddc9eSJosef Karthauser 523d4413063SJosef Karthauser #ifdef COLORLS 524bd82d8abSAndrey A. Chernov if (f_color) { 52522ff3e9eSAndrey A. Chernov /* 52622ff3e9eSAndrey A. Chernov * We can't put tabs and color sequences together: 52722ff3e9eSAndrey A. Chernov * column number will be incremented incorrectly 52822ff3e9eSAndrey A. Chernov * for "stty oxtabs" mode. 52922ff3e9eSAndrey A. Chernov */ 53022ff3e9eSAndrey A. Chernov f_notabs = 1; 531bd82d8abSAndrey A. Chernov (void)signal(SIGINT, colorquit); 532ab08444fSMartin Cracauer (void)signal(SIGQUIT, colorquit); 5333885812cSJosef Karthauser parsecolors(getenv("LSCOLORS")); 534bd82d8abSAndrey A. Chernov } 53574985094SJosef Karthauser #endif 5363885812cSJosef Karthauser 5374b88c807SRodney W. Grimes /* 53871b8b748SDima Dorfman * If not -F, -i, -l, -s, -S or -t options, don't require stat 5393885812cSJosef Karthauser * information, unless in color mode in which case we do 5403885812cSJosef Karthauser * need this to determine which colors to display. 5414b88c807SRodney W. Grimes */ 54271b8b748SDima Dorfman if (!f_inode && !f_longform && !f_size && !f_timesort && 54371b8b748SDima Dorfman !f_sizesort && !f_type 54474985094SJosef Karthauser #ifdef COLORLS 54574985094SJosef Karthauser && !f_color 54674985094SJosef Karthauser #endif 54774985094SJosef Karthauser ) 5484b88c807SRodney W. Grimes fts_options |= FTS_NOSTAT; 5494b88c807SRodney W. Grimes 5504b88c807SRodney W. Grimes /* 5511fe8c2a9SJaakko Heinonen * If not -F, -P, -d or -l options, follow any symbolic links listed on 552183714f3SXin LI * the command line, unless in color mode in which case we need to 553183714f3SXin LI * distinguish file type for a symbolic link itself and its target. 5544b88c807SRodney W. Grimes */ 555183714f3SXin LI if (!f_nofollow && !f_longform && !f_listdir && (!f_type || f_slash) 556183714f3SXin LI #ifdef COLORLS 557183714f3SXin LI && !f_color 558183714f3SXin LI #endif 559183714f3SXin LI ) 5604b88c807SRodney W. Grimes fts_options |= FTS_COMFOLLOW; 5614b88c807SRodney W. Grimes 562fb5cb208SSteve Price /* 563fb5cb208SSteve Price * If -W, show whiteout entries 564fb5cb208SSteve Price */ 565fb5cb208SSteve Price #ifdef FTS_WHITEOUT 566fb5cb208SSteve Price if (f_whiteout) 567fb5cb208SSteve Price fts_options |= FTS_WHITEOUT; 568fb5cb208SSteve Price #endif 569fb5cb208SSteve Price 5704d4dcc7aSDag-Erling Smørgrav /* If -i, -l or -s, figure out block size. */ 5714d4dcc7aSDag-Erling Smørgrav if (f_inode || f_longform || f_size) { 57290b0ec31SPoul-Henning Kamp if (f_kblocks) 57390b0ec31SPoul-Henning Kamp blocksize = 2; 57490b0ec31SPoul-Henning Kamp else { 5754b88c807SRodney W. Grimes (void)getbsize(¬used, &blocksize); 5764b88c807SRodney W. Grimes blocksize /= 512; 57790b0ec31SPoul-Henning Kamp } 5784b88c807SRodney W. Grimes } 5794b88c807SRodney W. Grimes /* Select a sort function. */ 5804b88c807SRodney W. Grimes if (f_reversesort) { 581e2662256SAymeric Wibo if (f_sizesort) 58286cca1e7SJohn Baldwin sortfcn = revsizecmp; 583e2662256SAymeric Wibo else if (f_verssort) 584e2662256SAymeric Wibo sortfcn = revverscmp; 585e2662256SAymeric Wibo else if (!f_timesort) 586e2662256SAymeric Wibo sortfcn = revnamecmp; 5874b88c807SRodney W. Grimes else if (f_accesstime) 5884b88c807SRodney W. Grimes sortfcn = revacccmp; 589fe79420eSJohn Baldwin else if (f_birthtime) 590fe79420eSJohn Baldwin sortfcn = revbirthcmp; 5914b88c807SRodney W. Grimes else if (f_statustime) 5924b88c807SRodney W. Grimes sortfcn = revstatcmp; 5934b88c807SRodney W. Grimes else /* Use modification time. */ 5944b88c807SRodney W. Grimes sortfcn = revmodcmp; 5954b88c807SRodney W. Grimes } else { 596e2662256SAymeric Wibo if (f_sizesort) 59786cca1e7SJohn Baldwin sortfcn = sizecmp; 598e2662256SAymeric Wibo else if (f_verssort) 599e2662256SAymeric Wibo sortfcn = verscmp; 600e2662256SAymeric Wibo else if (!f_timesort) 601e2662256SAymeric Wibo sortfcn = namecmp; 6024b88c807SRodney W. Grimes else if (f_accesstime) 6034b88c807SRodney W. Grimes sortfcn = acccmp; 604fe79420eSJohn Baldwin else if (f_birthtime) 605fe79420eSJohn Baldwin sortfcn = birthcmp; 6064b88c807SRodney W. Grimes else if (f_statustime) 6074b88c807SRodney W. Grimes sortfcn = statcmp; 6084b88c807SRodney W. Grimes else /* Use modification time. */ 6094b88c807SRodney W. Grimes sortfcn = modcmp; 6104b88c807SRodney W. Grimes } 6114b88c807SRodney W. Grimes 6124b88c807SRodney W. Grimes /* Select a print function. */ 6134b88c807SRodney W. Grimes if (f_singlecol) 6144b88c807SRodney W. Grimes printfcn = printscol; 6154b88c807SRodney W. Grimes else if (f_longform) 6164b88c807SRodney W. Grimes printfcn = printlong; 61794274c73STim J. Robbins else if (f_stream) 61894274c73STim J. Robbins printfcn = printstream; 6194b88c807SRodney W. Grimes else 6204b88c807SRodney W. Grimes printfcn = printcol; 6214b88c807SRodney W. Grimes 6224b88c807SRodney W. Grimes if (argc) 6234b88c807SRodney W. Grimes traverse(argc, argv, fts_options); 6244b88c807SRodney W. Grimes else 6254b88c807SRodney W. Grimes traverse(1, dotav, fts_options); 626fb1000d6SAdam David exit(rval); 6274b88c807SRodney W. Grimes } 6284b88c807SRodney W. Grimes 6294b88c807SRodney W. Grimes static int output; /* If anything output. */ 6304b88c807SRodney W. Grimes 6314b88c807SRodney W. Grimes /* 6324b88c807SRodney W. Grimes * Traverse() walks the logical directory structure specified by the argv list 6334b88c807SRodney W. Grimes * in the order specified by the mastercmp() comparison function. During the 6344b88c807SRodney W. Grimes * traversal it passes linked lists of structures to display() which represent 6354b88c807SRodney W. Grimes * a superset (may be exact set) of the files to be displayed. 6364b88c807SRodney W. Grimes */ 6374b88c807SRodney W. Grimes static void 63846251ddeSWarner Losh traverse(int argc, char *argv[], int options) 6394b88c807SRodney W. Grimes { 6404b88c807SRodney W. Grimes FTS *ftsp; 6414b88c807SRodney W. Grimes FTSENT *p, *chp; 6424b88c807SRodney W. Grimes int ch_options; 6434b88c807SRodney W. Grimes 6444b88c807SRodney W. Grimes if ((ftsp = 6454b88c807SRodney W. Grimes fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 6460fdf7fa8SConrad Meyer err(1, "fts_open"); 6474b88c807SRodney W. Grimes 64870bad4f7SDavid Schultz /* 64970bad4f7SDavid Schultz * We ignore errors from fts_children here since they will be 65070bad4f7SDavid Schultz * replicated and signalled on the next call to fts_read() below. 65170bad4f7SDavid Schultz */ 65270bad4f7SDavid Schultz chp = fts_children(ftsp, 0); 65370bad4f7SDavid Schultz if (chp != NULL) 65470bad4f7SDavid Schultz display(NULL, chp, options); 6554b88c807SRodney W. Grimes if (f_listdir) 6564b88c807SRodney W. Grimes return; 6574b88c807SRodney W. Grimes 6584b88c807SRodney W. Grimes /* 6594b88c807SRodney W. Grimes * If not recursing down this tree and don't need stat info, just get 6604b88c807SRodney W. Grimes * the names. 6614b88c807SRodney W. Grimes */ 6623c3f5f9cSRobert Watson ch_options = !f_recursive && !f_label && 6633c3f5f9cSRobert Watson options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 6644b88c807SRodney W. Grimes 6652dfa4b66SBryan Drewery while (errno = 0, (p = fts_read(ftsp)) != NULL) 6664b88c807SRodney W. Grimes switch (p->fts_info) { 6674b88c807SRodney W. Grimes case FTS_DC: 6680fdf7fa8SConrad Meyer warnx("%s: directory causes a cycle", p->fts_name); 6694b88c807SRodney W. Grimes break; 6704b88c807SRodney W. Grimes case FTS_DNR: 6714b88c807SRodney W. Grimes case FTS_ERR: 6720fdf7fa8SConrad Meyer warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 673fb1000d6SAdam David rval = 1; 6744b88c807SRodney W. Grimes break; 6754b88c807SRodney W. Grimes case FTS_D: 6764b88c807SRodney W. Grimes if (p->fts_level != FTS_ROOTLEVEL && 677390a478eSRuslan Ermilov p->fts_name[0] == '.' && !f_listdot) 6784b88c807SRodney W. Grimes break; 6794b88c807SRodney W. Grimes 6804b88c807SRodney W. Grimes /* 6814b88c807SRodney W. Grimes * If already output something, put out a newline as 6824b88c807SRodney W. Grimes * a separator. If multiple arguments, precede each 6834b88c807SRodney W. Grimes * directory with its name. 6844b88c807SRodney W. Grimes */ 6851656f850STim J. Robbins if (output) { 6860fdf7fa8SConrad Meyer putchar('\n'); 6870fdf7fa8SConrad Meyer (void)printname(p->fts_path); 6880fdf7fa8SConrad Meyer puts(":"); 6891656f850STim J. Robbins } else if (argc > 1) { 6900fdf7fa8SConrad Meyer (void)printname(p->fts_path); 6910fdf7fa8SConrad Meyer puts(":"); 6924b88c807SRodney W. Grimes output = 1; 6934b88c807SRodney W. Grimes } 6944b88c807SRodney W. Grimes chp = fts_children(ftsp, ch_options); 6954d33b62eSRobert Watson display(p, chp, options); 6964b88c807SRodney W. Grimes 6974b88c807SRodney W. Grimes if (!f_recursive && chp != NULL) 6984b88c807SRodney W. Grimes (void)fts_set(ftsp, p, FTS_SKIP); 6994b88c807SRodney W. Grimes break; 700568dcd5fSBill Fumerola default: 701568dcd5fSBill Fumerola break; 7024b88c807SRodney W. Grimes } 7034b88c807SRodney W. Grimes if (errno) 7040fdf7fa8SConrad Meyer err(1, "fts_read"); 7054b88c807SRodney W. Grimes } 7064b88c807SRodney W. Grimes 7074b88c807SRodney W. Grimes /* 7084b88c807SRodney W. Grimes * Display() takes a linked list of FTSENT structures and passes the list 7094b88c807SRodney W. Grimes * along with any other necessary information to the print function. P 7104b88c807SRodney W. Grimes * points to the parent directory of the display list. 7114b88c807SRodney W. Grimes */ 7124b88c807SRodney W. Grimes static void 71340feca3aSMark Murray display(const FTSENT *p, FTSENT *list, int options) 7144b88c807SRodney W. Grimes { 7154b88c807SRodney W. Grimes struct stat *sp; 7164b88c807SRodney W. Grimes DISPLAY d; 7174b88c807SRodney W. Grimes FTSENT *cur; 7184b88c807SRodney W. Grimes NAMES *np; 7199052855aSMark Murray off_t maxsize; 72040feca3aSMark Murray long maxblock; 7216db1a7f1SMatthew D Fleming uintmax_t maxinode; 7226db1a7f1SMatthew D Fleming u_long btotal, labelstrlen, maxlen, maxnlink; 7234d33b62eSRobert Watson u_long maxlabelstr; 7249f365aa1SEd Schouten u_int sizelen; 72555926a66SJaakko Heinonen int maxflags; 7269052855aSMark Murray gid_t maxgroup; 7279052855aSMark Murray uid_t maxuser; 7289052855aSMark Murray size_t flen, ulen, glen; 729545f583cSTim Vanderhoek char *initmax; 7304b88c807SRodney W. Grimes int entries, needstats; 7310928a7f1SJuli Mallett const char *user, *group; 7324d33b62eSRobert Watson char *flags, *labelstr = NULL; 733008a4910SSheldon Hearn char ngroup[STRBUF_SIZEOF(uid_t) + 1]; 734008a4910SSheldon Hearn char nuser[STRBUF_SIZEOF(gid_t) + 1]; 7353fee777eSStefan Eßer u_long width[9]; 7363fee777eSStefan Eßer int i; 7374b88c807SRodney W. Grimes 7384b88c807SRodney W. Grimes needstats = f_inode || f_longform || f_size; 7394b88c807SRodney W. Grimes flen = 0; 740545f583cSTim Vanderhoek btotal = 0; 7413fee777eSStefan Eßer 7423fee777eSStefan Eßer #define LS_COLWIDTHS_FIELDS 9 743545f583cSTim Vanderhoek initmax = getenv("LS_COLWIDTHS"); 744545f583cSTim Vanderhoek 7453fee777eSStefan Eßer for (i = 0 ; i < LS_COLWIDTHS_FIELDS; i++) 7463fee777eSStefan Eßer width[i] = 0; 7473fee777eSStefan Eßer 7483fee777eSStefan Eßer if (initmax != NULL) { 7493fee777eSStefan Eßer char *endp; 7503fee777eSStefan Eßer 7513fee777eSStefan Eßer for (i = 0; i < LS_COLWIDTHS_FIELDS && *initmax != '\0'; i++) { 7523fee777eSStefan Eßer if (*initmax == ':') { 7533fee777eSStefan Eßer width[i] = 0; 754545f583cSTim Vanderhoek } else { 7553fee777eSStefan Eßer width[i] = strtoul(initmax, &endp, 10); 7563fee777eSStefan Eßer initmax = endp; 7573fee777eSStefan Eßer while (isspace(*initmax)) 7583fee777eSStefan Eßer initmax++; 7593fee777eSStefan Eßer if (*initmax != ':') 7603fee777eSStefan Eßer break; 7613fee777eSStefan Eßer initmax++; 762545f583cSTim Vanderhoek } 763545f583cSTim Vanderhoek } 7643fee777eSStefan Eßer if (i < LS_COLWIDTHS_FIELDS) 76518d8a22bSAndrey A. Chernov #ifdef COLORLS 76618d8a22bSAndrey A. Chernov if (!f_color) 76722ff3e9eSAndrey A. Chernov #endif 76818d8a22bSAndrey A. Chernov f_notabs = 0; 769545f583cSTim Vanderhoek } 7703fee777eSStefan Eßer 7713fee777eSStefan Eßer /* Fields match -lios order. New ones should be added at the end. */ 7723fee777eSStefan Eßer maxinode = width[0]; 7733fee777eSStefan Eßer maxblock = width[1]; 7743fee777eSStefan Eßer maxnlink = width[2]; 7753fee777eSStefan Eßer maxuser = width[3]; 7763fee777eSStefan Eßer maxgroup = width[4]; 7773fee777eSStefan Eßer maxflags = width[5]; 7783fee777eSStefan Eßer maxsize = width[6]; 7793fee777eSStefan Eßer maxlen = width[7]; 7803fee777eSStefan Eßer maxlabelstr = width[8]; 7813fee777eSStefan Eßer 78240feca3aSMark Murray MAKENINES(maxinode); 78340feca3aSMark Murray MAKENINES(maxblock); 78440feca3aSMark Murray MAKENINES(maxnlink); 78540feca3aSMark Murray MAKENINES(maxsize); 7863fee777eSStefan Eßer 7879f365aa1SEd Schouten d.s_size = 0; 7889f365aa1SEd Schouten sizelen = 0; 7890fd510b7SJoerg Wunsch flags = NULL; 7904b88c807SRodney W. Grimes for (cur = list, entries = 0; cur; cur = cur->fts_link) { 7914b88c807SRodney W. Grimes if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 7920fdf7fa8SConrad Meyer warnx("%s: %s", 7934b88c807SRodney W. Grimes cur->fts_name, strerror(cur->fts_errno)); 7944b88c807SRodney W. Grimes cur->fts_number = NO_PRINT; 795fb1000d6SAdam David rval = 1; 7964b88c807SRodney W. Grimes continue; 7974b88c807SRodney W. Grimes } 7984b88c807SRodney W. Grimes /* 7994b88c807SRodney W. Grimes * P is NULL if list is the argv list, to which different rules 8004b88c807SRodney W. Grimes * apply. 8014b88c807SRodney W. Grimes */ 8024b88c807SRodney W. Grimes if (p == NULL) { 8034b88c807SRodney W. Grimes /* Directories will be displayed later. */ 8044b88c807SRodney W. Grimes if (cur->fts_info == FTS_D && !f_listdir) { 8054b88c807SRodney W. Grimes cur->fts_number = NO_PRINT; 8064b88c807SRodney W. Grimes continue; 8074b88c807SRodney W. Grimes } 8084b88c807SRodney W. Grimes } else { 8094b88c807SRodney W. Grimes /* Only display dot file if -a/-A set. */ 810390a478eSRuslan Ermilov if (cur->fts_name[0] == '.' && !f_listdot) { 8114b88c807SRodney W. Grimes cur->fts_number = NO_PRINT; 8124b88c807SRodney W. Grimes continue; 8134b88c807SRodney W. Grimes } 8144b88c807SRodney W. Grimes } 8154b88c807SRodney W. Grimes if (cur->fts_namelen > maxlen) 8164b88c807SRodney W. Grimes maxlen = cur->fts_namelen; 8170d86878cSDag-Erling Smørgrav if (f_octal || f_octal_escape) { 8186bd042dfSJosef Karthauser u_long t = len_octal(cur->fts_name, cur->fts_namelen); 8195dda5d0dSJosef Karthauser 8205dda5d0dSJosef Karthauser if (t > maxlen) 8215dda5d0dSJosef Karthauser maxlen = t; 8227ea30648SDag-Erling Smørgrav } 8234b88c807SRodney W. Grimes if (needstats) { 8244b88c807SRodney W. Grimes sp = cur->fts_statp; 8254b88c807SRodney W. Grimes if (sp->st_blocks > maxblock) 8264b88c807SRodney W. Grimes maxblock = sp->st_blocks; 8274b88c807SRodney W. Grimes if (sp->st_ino > maxinode) 8284b88c807SRodney W. Grimes maxinode = sp->st_ino; 8294b88c807SRodney W. Grimes if (sp->st_nlink > maxnlink) 8304b88c807SRodney W. Grimes maxnlink = sp->st_nlink; 8314b88c807SRodney W. Grimes if (sp->st_size > maxsize) 8324b88c807SRodney W. Grimes maxsize = sp->st_size; 8334b88c807SRodney W. Grimes 8344b88c807SRodney W. Grimes btotal += sp->st_blocks; 8354b88c807SRodney W. Grimes if (f_longform) { 836f3a6a64eSSheldon Hearn if (f_numericonly) { 837f3a6a64eSSheldon Hearn (void)snprintf(nuser, sizeof(nuser), 838f3a6a64eSSheldon Hearn "%u", sp->st_uid); 839f3a6a64eSSheldon Hearn (void)snprintf(ngroup, sizeof(ngroup), 840f3a6a64eSSheldon Hearn "%u", sp->st_gid); 841f3a6a64eSSheldon Hearn user = nuser; 842f3a6a64eSSheldon Hearn group = ngroup; 843f3a6a64eSSheldon Hearn } else { 8444b88c807SRodney W. Grimes user = user_from_uid(sp->st_uid, 0); 845530d2d67SConrad Meyer /* 846530d2d67SConrad Meyer * user_from_uid(..., 0) only returns 847530d2d67SConrad Meyer * NULL in OOM conditions. We could 848530d2d67SConrad Meyer * format the uid here, but (1) in 849530d2d67SConrad Meyer * general ls(1) exits on OOM, and (2) 850530d2d67SConrad Meyer * there is another allocation/exit 851530d2d67SConrad Meyer * path directly below, which will 852530d2d67SConrad Meyer * likely exit anyway. 853530d2d67SConrad Meyer */ 854530d2d67SConrad Meyer if (user == NULL) 855530d2d67SConrad Meyer err(1, "user_from_uid"); 856f3a6a64eSSheldon Hearn group = group_from_gid(sp->st_gid, 0); 857530d2d67SConrad Meyer /* Ditto. */ 858530d2d67SConrad Meyer if (group == NULL) 859530d2d67SConrad Meyer err(1, "group_from_gid"); 860f3a6a64eSSheldon Hearn } 8614b88c807SRodney W. Grimes if ((ulen = strlen(user)) > maxuser) 8624b88c807SRodney W. Grimes maxuser = ulen; 8634b88c807SRodney W. Grimes if ((glen = strlen(group)) > maxgroup) 8644b88c807SRodney W. Grimes maxgroup = glen; 8654b88c807SRodney W. Grimes if (f_flags) { 866141d77b8SJosef Karthauser flags = fflagstostr(sp->st_flags); 867141d77b8SJosef Karthauser if (flags != NULL && *flags == '\0') { 868141d77b8SJosef Karthauser free(flags); 869141d77b8SJosef Karthauser flags = strdup("-"); 870141d77b8SJosef Karthauser } 871141d77b8SJosef Karthauser if (flags == NULL) 8720fdf7fa8SConrad Meyer err(1, "fflagstostr"); 8739052855aSMark Murray flen = strlen(flags); 8749052855aSMark Murray if (flen > (size_t)maxflags) 8754b88c807SRodney W. Grimes maxflags = flen; 8764b88c807SRodney W. Grimes } else 8774b88c807SRodney W. Grimes flen = 0; 8784d33b62eSRobert Watson labelstr = NULL; 8794d33b62eSRobert Watson if (f_label) { 8804df6dabaSRobert Watson char name[PATH_MAX + 1]; 8814d33b62eSRobert Watson mac_t label; 8824d33b62eSRobert Watson int error; 8834b88c807SRodney W. Grimes 8844d33b62eSRobert Watson error = mac_prepare_file_label(&label); 8854d33b62eSRobert Watson if (error == -1) { 8860fdf7fa8SConrad Meyer warn("MAC label for %s/%s", 8873c3f5f9cSRobert Watson cur->fts_parent->fts_path, 8883c3f5f9cSRobert Watson cur->fts_name); 8894d33b62eSRobert Watson goto label_out; 8904d33b62eSRobert Watson } 8914d33b62eSRobert Watson 8924df6dabaSRobert Watson if (cur->fts_level == FTS_ROOTLEVEL) 8934df6dabaSRobert Watson snprintf(name, sizeof(name), 8944df6dabaSRobert Watson "%s", cur->fts_name); 8954d33b62eSRobert Watson else 8964df6dabaSRobert Watson snprintf(name, sizeof(name), 8973c3f5f9cSRobert Watson "%s/%s", cur->fts_parent-> 8983c3f5f9cSRobert Watson fts_accpath, cur->fts_name); 8994df6dabaSRobert Watson 9004df6dabaSRobert Watson if (options & FTS_LOGICAL) 9014df6dabaSRobert Watson error = mac_get_file(name, 9024df6dabaSRobert Watson label); 9034df6dabaSRobert Watson else 9044df6dabaSRobert Watson error = mac_get_link(name, 9054df6dabaSRobert Watson label); 9064d33b62eSRobert Watson if (error == -1) { 9070fdf7fa8SConrad Meyer warn("MAC label for %s/%s", 9083c3f5f9cSRobert Watson cur->fts_parent->fts_path, 9093c3f5f9cSRobert Watson cur->fts_name); 9104d33b62eSRobert Watson mac_free(label); 9114d33b62eSRobert Watson goto label_out; 9124d33b62eSRobert Watson } 9134d33b62eSRobert Watson 9144d33b62eSRobert Watson error = mac_to_text(label, 9154d33b62eSRobert Watson &labelstr); 9164d33b62eSRobert Watson if (error == -1) { 9170fdf7fa8SConrad Meyer warn("MAC label for %s/%s", 9183c3f5f9cSRobert Watson cur->fts_parent->fts_path, 9193c3f5f9cSRobert Watson cur->fts_name); 9204d33b62eSRobert Watson mac_free(label); 9214d33b62eSRobert Watson goto label_out; 9224d33b62eSRobert Watson } 9234d33b62eSRobert Watson mac_free(label); 9244d33b62eSRobert Watson label_out: 9254d33b62eSRobert Watson if (labelstr == NULL) 926317f1d53SRobert Watson labelstr = strdup("-"); 9274d33b62eSRobert Watson labelstrlen = strlen(labelstr); 9284d33b62eSRobert Watson if (labelstrlen > maxlabelstr) 9294d33b62eSRobert Watson maxlabelstr = labelstrlen; 9304d33b62eSRobert Watson } else 9314d33b62eSRobert Watson labelstrlen = 0; 9324d33b62eSRobert Watson 9334d33b62eSRobert Watson if ((np = malloc(sizeof(NAMES) + labelstrlen + 9347304f61fSBrian Feldman ulen + glen + flen + 4)) == NULL) 9350fdf7fa8SConrad Meyer err(1, "malloc"); 9364b88c807SRodney W. Grimes 9374b88c807SRodney W. Grimes np->user = &np->data[0]; 9384b88c807SRodney W. Grimes (void)strcpy(np->user, user); 9394b88c807SRodney W. Grimes np->group = &np->data[ulen + 1]; 9404b88c807SRodney W. Grimes (void)strcpy(np->group, group); 9414b88c807SRodney W. Grimes 9429f365aa1SEd Schouten if (S_ISCHR(sp->st_mode) || 9439f365aa1SEd Schouten S_ISBLK(sp->st_mode)) { 9449f365aa1SEd Schouten sizelen = snprintf(NULL, 0, 9459f365aa1SEd Schouten "%#jx", (uintmax_t)sp->st_rdev); 9469f365aa1SEd Schouten if (d.s_size < sizelen) 9479f365aa1SEd Schouten d.s_size = sizelen; 94855926a66SJaakko Heinonen } 9494b88c807SRodney W. Grimes 9504b88c807SRodney W. Grimes if (f_flags) { 9514b88c807SRodney W. Grimes np->flags = &np->data[ulen + glen + 2]; 9524b88c807SRodney W. Grimes (void)strcpy(np->flags, flags); 953141d77b8SJosef Karthauser free(flags); 9544b88c807SRodney W. Grimes } 9554d33b62eSRobert Watson if (f_label) { 9564d33b62eSRobert Watson np->label = &np->data[ulen + glen + 2 9577304f61fSBrian Feldman + (f_flags ? flen + 1 : 0)]; 9584d33b62eSRobert Watson (void)strcpy(np->label, labelstr); 9594d33b62eSRobert Watson free(labelstr); 9607304f61fSBrian Feldman } 9614b88c807SRodney W. Grimes cur->fts_pointer = np; 9624b88c807SRodney W. Grimes } 9634b88c807SRodney W. Grimes } 9644b88c807SRodney W. Grimes ++entries; 9654b88c807SRodney W. Grimes } 9664b88c807SRodney W. Grimes 96770bad4f7SDavid Schultz /* 96870bad4f7SDavid Schultz * If there are no entries to display, we normally stop right 96970bad4f7SDavid Schultz * here. However, we must continue if we have to display the 97070bad4f7SDavid Schultz * total block count. In this case, we display the total only 97170bad4f7SDavid Schultz * on the second (p != NULL) pass. 97270bad4f7SDavid Schultz */ 97370bad4f7SDavid Schultz if (!entries && (!(f_longform || f_size) || p == NULL)) 9744b88c807SRodney W. Grimes return; 9754b88c807SRodney W. Grimes 9764b88c807SRodney W. Grimes d.list = list; 9774b88c807SRodney W. Grimes d.entries = entries; 9784b88c807SRodney W. Grimes d.maxlen = maxlen; 9794b88c807SRodney W. Grimes if (needstats) { 9804b88c807SRodney W. Grimes d.btotal = btotal; 981cf147939SDag-Erling Smørgrav d.s_block = snprintf(NULL, 0, "%lu", howmany(maxblock, blocksize)); 9824b88c807SRodney W. Grimes d.s_flags = maxflags; 9834d33b62eSRobert Watson d.s_label = maxlabelstr; 9844b88c807SRodney W. Grimes d.s_group = maxgroup; 9856db1a7f1SMatthew D Fleming d.s_inode = snprintf(NULL, 0, "%ju", maxinode); 9869f365aa1SEd Schouten d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink); 9879f365aa1SEd Schouten sizelen = f_humanval ? HUMANVALSTR_LEN : 9889f365aa1SEd Schouten snprintf(NULL, 0, "%ju", maxsize); 9899f365aa1SEd Schouten if (d.s_size < sizelen) 9909f365aa1SEd Schouten d.s_size = sizelen; 9914b88c807SRodney W. Grimes d.s_user = maxuser; 9924b88c807SRodney W. Grimes } 9939aa68a3fSGreg Lehey if (f_thousands) /* make space for commas */ 9949aa68a3fSGreg Lehey d.s_size += (d.s_size - 1) / 3; 9954b88c807SRodney W. Grimes printfcn(&d); 9964b88c807SRodney W. Grimes output = 1; 9974b88c807SRodney W. Grimes 9984b88c807SRodney W. Grimes if (f_longform) 9994b88c807SRodney W. Grimes for (cur = list; cur; cur = cur->fts_link) 10004b88c807SRodney W. Grimes free(cur->fts_pointer); 10014b88c807SRodney W. Grimes } 10024b88c807SRodney W. Grimes 10034b88c807SRodney W. Grimes /* 10044b88c807SRodney W. Grimes * Ordering for mastercmp: 10054b88c807SRodney W. Grimes * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 10064b88c807SRodney W. Grimes * as larger than directories. Within either group, use the sort function. 10074b88c807SRodney W. Grimes * All other levels use the sort function. Error entries remain unsorted. 10084b88c807SRodney W. Grimes */ 10094b88c807SRodney W. Grimes static int 10100d3bcc2eSGarrett Wollman mastercmp(const FTSENT * const *a, const FTSENT * const *b) 10114b88c807SRodney W. Grimes { 10124b88c807SRodney W. Grimes int a_info, b_info; 10134b88c807SRodney W. Grimes 10144b88c807SRodney W. Grimes a_info = (*a)->fts_info; 10154b88c807SRodney W. Grimes if (a_info == FTS_ERR) 10164b88c807SRodney W. Grimes return (0); 10174b88c807SRodney W. Grimes b_info = (*b)->fts_info; 10184b88c807SRodney W. Grimes if (b_info == FTS_ERR) 10194b88c807SRodney W. Grimes return (0); 10204b88c807SRodney W. Grimes 10214b88c807SRodney W. Grimes if (a_info == FTS_NS || b_info == FTS_NS) 10224b88c807SRodney W. Grimes return (namecmp(*a, *b)); 10234b88c807SRodney W. Grimes 102451f26ac5SSean Eric Fagan if (a_info != b_info && 102551f26ac5SSean Eric Fagan (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) { 10264b88c807SRodney W. Grimes if (a_info == FTS_D) 10274b88c807SRodney W. Grimes return (1); 102851f26ac5SSean Eric Fagan if (b_info == FTS_D) 10294b88c807SRodney W. Grimes return (-1); 103051f26ac5SSean Eric Fagan } 10314b88c807SRodney W. Grimes return (sortfcn(*a, *b)); 10324b88c807SRodney W. Grimes } 1033