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 908b929778SPiotr Pawel Stefaniak enum { 918b929778SPiotr Pawel Stefaniak GRP_NONE = 0, 928b929778SPiotr Pawel Stefaniak GRP_DIR_FIRST = -1, 938b929778SPiotr Pawel Stefaniak GRP_DIR_LAST = 1 948b929778SPiotr Pawel Stefaniak }; 958b929778SPiotr Pawel Stefaniak 968b929778SPiotr Pawel Stefaniak enum { 978b929778SPiotr Pawel Stefaniak BIN_OPT = CHAR_MAX, 988b929778SPiotr Pawel Stefaniak COLOR_OPT, 998b929778SPiotr Pawel Stefaniak GROUP_OPT 1008b929778SPiotr Pawel Stefaniak }; 101e10ba800SKyle Evans 102e10ba800SKyle Evans static const struct option long_opts[] = 103e10ba800SKyle Evans { 104e10ba800SKyle Evans {"color", optional_argument, NULL, COLOR_OPT}, 1058b929778SPiotr Pawel Stefaniak {"group-directories", optional_argument, NULL, GROUP_OPT}, 1068b929778SPiotr Pawel Stefaniak {"group-directories-first", no_argument, NULL, GROUP_OPT}, 107e10ba800SKyle Evans {NULL, no_argument, NULL, 0} 108e10ba800SKyle Evans }; 109e10ba800SKyle Evans 11040feca3aSMark Murray static void (*printfcn)(const DISPLAY *); 11146251ddeSWarner Losh static int (*sortfcn)(const FTSENT *, const FTSENT *); 1124b88c807SRodney W. Grimes 1134b88c807SRodney W. Grimes long blocksize; /* block size units */ 1144b88c807SRodney W. Grimes int termwidth = 80; /* default terminal width */ 1154b88c807SRodney W. Grimes 1164b88c807SRodney W. Grimes /* flags */ 1174b88c807SRodney W. Grimes int f_accesstime; /* use time of last access */ 118fe79420eSJohn Baldwin int f_birthtime; /* use time of birth */ 1194b88c807SRodney W. Grimes int f_flags; /* show flags associated with a file */ 1208b929778SPiotr Pawel Stefaniak static int f_groupdir = GRP_NONE;/* group directories first/last */ 1210e8d1551SJosef Karthauser int f_humanval; /* show human-readable file sizes */ 1224b88c807SRodney W. Grimes int f_inode; /* print inode */ 1239052855aSMark Murray static int f_kblocks; /* print size in kilobytes */ 1249aa68a3fSGreg Lehey int f_label; /* show MAC label */ 1259052855aSMark Murray static int f_listdir; /* list actual directory, not contents */ 1269052855aSMark Murray static int f_listdot; /* list files beginning with . */ 1274b88c807SRodney W. Grimes int f_longform; /* long listing format */ 1289aa68a3fSGreg Lehey static int f_noautodot; /* do not automatically enable -A for root */ 1291fe8c2a9SJaakko Heinonen static int f_nofollow; /* don't follow symbolic link arguments */ 1304b88c807SRodney W. Grimes int f_nonprint; /* show unprintables as ? */ 1319052855aSMark Murray static int f_nosort; /* don't sort output */ 132008a4910SSheldon Hearn int f_notabs; /* don't use tab-separated multi-col output */ 1330fdf7fa8SConrad Meyer static int f_numericonly; /* don't convert uid/gid to name */ 1347ea30648SDag-Erling Smørgrav int f_octal; /* show unprintables as \xxx */ 1350d86878cSDag-Erling Smørgrav int f_octal_escape; /* like f_octal but use C escapes if possible */ 1369052855aSMark Murray static int f_recursive; /* ls subdirectories also */ 1379052855aSMark Murray static int f_reversesort; /* reverse whatever sort is used */ 138e2662256SAymeric Wibo static int f_verssort; /* sort names using strverscmp(3) rather than strcoll(3) */ 1399aa68a3fSGreg Lehey int f_samesort; /* sort time and name in same direction */ 1409aa68a3fSGreg Lehey int f_sectime; /* print full time information */ 1419052855aSMark Murray static int f_singlecol; /* use single column output */ 1424b88c807SRodney W. Grimes int f_size; /* list size in short listing */ 1439aa68a3fSGreg Lehey static int f_sizesort; 14494274c73STim J. Robbins int f_slash; /* similar to f_type, but only for dirs */ 14594274c73STim J. Robbins int f_sortacross; /* sort across rows, not down columns */ 1463bfbb521SMinsoo Choo int f_sowner; /* disable showing owner's name */ 1474b88c807SRodney W. Grimes int f_statustime; /* use time of last mode change */ 14840feca3aSMark Murray static int f_stream; /* stream the output, separate with commas */ 1499aa68a3fSGreg Lehey int f_thousands; /* show file sizes with thousands separators */ 1502269fa57SGreg Lehey char *f_timeformat; /* user-specified time format */ 1519aa68a3fSGreg Lehey static int f_timesort; /* sort by time vice name */ 1524b88c807SRodney W. Grimes int f_type; /* add type character for non-regular files */ 1539052855aSMark Murray static int f_whiteout; /* show whiteout entries */ 15474985094SJosef Karthauser #ifdef COLORLS 155c0f34dedSKyle Evans int colorflag = COLORFLAG_NEVER; /* passed in colorflag */ 1563885812cSJosef Karthauser int f_color; /* add type in color for non-regular files */ 157e10ba800SKyle Evans bool explicitansi; /* Explicit ANSI sequences, no termcap(5) */ 1585a890e22SJosef Karthauser char *ansi_bgcol; /* ANSI sequence to set background colour */ 1595a890e22SJosef Karthauser char *ansi_fgcol; /* ANSI sequence to set foreground colour */ 1605a890e22SJosef Karthauser char *ansi_coloff; /* ANSI sequence to reset colours */ 161c1499cf6SJosef Karthauser char *attrs_off; /* ANSI sequence to turn off attributes */ 162c1499cf6SJosef Karthauser char *enter_bold; /* ANSI sequence to set color to bold mode */ 16397c31821SCameron Katri char *enter_underline; /* ANSI sequence to enter underline mode */ 16474985094SJosef Karthauser #endif 1654b88c807SRodney W. Grimes 1669052855aSMark Murray static int rval; 167fb1000d6SAdam David 1687db2f1feSKyle Evans static bool 1697db2f1feSKyle Evans do_color_from_env(void) 1707db2f1feSKyle Evans { 1717db2f1feSKyle Evans const char *p; 1727db2f1feSKyle Evans bool doit; 1737db2f1feSKyle Evans 1747db2f1feSKyle Evans doit = false; 1757db2f1feSKyle Evans p = getenv("CLICOLOR"); 1767db2f1feSKyle Evans if (p == NULL) { 1777db2f1feSKyle Evans /* 1787db2f1feSKyle Evans * COLORTERM is the more standard name for this variable. We'll 1797db2f1feSKyle Evans * honor it as long as it's both set and not empty. 1807db2f1feSKyle Evans */ 1817db2f1feSKyle Evans p = getenv("COLORTERM"); 1827db2f1feSKyle Evans if (p != NULL && *p != '\0') 1837db2f1feSKyle Evans doit = true; 1847db2f1feSKyle Evans } else 1857db2f1feSKyle Evans doit = true; 1867db2f1feSKyle Evans 1877db2f1feSKyle Evans return (doit && 1887db2f1feSKyle Evans (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE"))); 1897db2f1feSKyle Evans } 1907db2f1feSKyle Evans 191e10ba800SKyle Evans static bool 192e10ba800SKyle Evans do_color(void) 193e10ba800SKyle Evans { 194e10ba800SKyle Evans 195e10ba800SKyle Evans #ifdef COLORLS 196e10ba800SKyle Evans if (colorflag == COLORFLAG_NEVER) 197e10ba800SKyle Evans return (false); 198e10ba800SKyle Evans else if (colorflag == COLORFLAG_ALWAYS) 199e10ba800SKyle Evans return (true); 200e10ba800SKyle Evans #endif 201e10ba800SKyle Evans return (do_color_from_env()); 202e10ba800SKyle Evans } 203e10ba800SKyle Evans 204517d0a90SKyle Evans #ifdef COLORLS 205041e6eb1SKyle Evans static bool 206041e6eb1SKyle Evans do_color_always(const char *term) 207041e6eb1SKyle Evans { 208041e6eb1SKyle Evans 209041e6eb1SKyle Evans return (strcmp(term, "always") == 0 || strcmp(term, "yes") == 0 || 210041e6eb1SKyle Evans strcmp(term, "force") == 0); 211041e6eb1SKyle Evans } 212041e6eb1SKyle Evans 213041e6eb1SKyle Evans static bool 214041e6eb1SKyle Evans do_color_never(const char *term) 215041e6eb1SKyle Evans { 216041e6eb1SKyle Evans 217041e6eb1SKyle Evans return (strcmp(term, "never") == 0 || strcmp(term, "no") == 0 || 218041e6eb1SKyle Evans strcmp(term, "none") == 0); 219041e6eb1SKyle Evans } 220041e6eb1SKyle Evans 221041e6eb1SKyle Evans static bool 222041e6eb1SKyle Evans do_color_auto(const char *term) 223041e6eb1SKyle Evans { 224041e6eb1SKyle Evans 225041e6eb1SKyle Evans return (strcmp(term, "auto") == 0 || strcmp(term, "tty") == 0 || 226041e6eb1SKyle Evans strcmp(term, "if-tty") == 0); 227041e6eb1SKyle Evans } 228517d0a90SKyle Evans #endif /* COLORLS */ 229041e6eb1SKyle Evans 2304b88c807SRodney W. Grimes int 23146251ddeSWarner Losh main(int argc, char *argv[]) 2324b88c807SRodney W. Grimes { 2334b88c807SRodney W. Grimes static char dot[] = ".", *dotav[] = {dot, NULL}; 2344b88c807SRodney W. Grimes struct winsize win; 2354b88c807SRodney W. Grimes int ch, fts_options, notused; 2364b88c807SRodney W. Grimes char *p; 237d4bf4151SBaptiste Daroussin const char *errstr = NULL; 2385a890e22SJosef Karthauser #ifdef COLORLS 2395a890e22SJosef Karthauser char termcapbuf[1024]; /* termcap definition buffer */ 2405a890e22SJosef Karthauser char tcapbuf[512]; /* capability buffer */ 241e10ba800SKyle Evans char *bp = tcapbuf, *term; 2425a890e22SJosef Karthauser #endif 2435a890e22SJosef Karthauser 244f5bd01c6SAndrey A. Chernov (void)setlocale(LC_ALL, ""); 245f5bd01c6SAndrey A. Chernov 2464b88c807SRodney W. Grimes /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 2474b88c807SRodney W. Grimes if (isatty(STDOUT_FILENO)) { 248a28edf9aSTim J. Robbins termwidth = 80; 249a28edf9aSTim J. Robbins if ((p = getenv("COLUMNS")) != NULL && *p != '\0') 250d4bf4151SBaptiste Daroussin termwidth = strtonum(p, 0, INT_MAX, &errstr); 251a28edf9aSTim J. Robbins else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && 252a28edf9aSTim J. Robbins win.ws_col > 0) 2534b88c807SRodney W. Grimes termwidth = win.ws_col; 2549052855aSMark Murray f_nonprint = 1; 25534994fcdSJoerg Wunsch } else { 2564b88c807SRodney W. Grimes f_singlecol = 1; 25734994fcdSJoerg Wunsch /* retrieve environment variable, in case of explicit -C */ 2589052855aSMark Murray p = getenv("COLUMNS"); 2599052855aSMark Murray if (p) 260d4bf4151SBaptiste Daroussin termwidth = strtonum(p, 0, INT_MAX, &errstr); 26134994fcdSJoerg Wunsch } 2624b88c807SRodney W. Grimes 263d4bf4151SBaptiste Daroussin if (errstr) 264d4bf4151SBaptiste Daroussin termwidth = 80; 265d4bf4151SBaptiste Daroussin 2664b88c807SRodney W. Grimes fts_options = FTS_PHYSICAL; 2679aa68a3fSGreg Lehey if (getenv("LS_SAMESORT")) 2689aa68a3fSGreg Lehey f_samesort = 1; 269a408dc20SKyle Evans 270a408dc20SKyle Evans /* 271a408dc20SKyle Evans * For historical compatibility, we'll use our autodetection if CLICOLOR 272a408dc20SKyle Evans * is set. 273a408dc20SKyle Evans */ 274151a7e11SKyle Evans #ifdef COLORLS 275a408dc20SKyle Evans if (getenv("CLICOLOR")) 276a408dc20SKyle Evans colorflag = COLORFLAG_AUTO; 277151a7e11SKyle Evans #endif 278e10ba800SKyle Evans while ((ch = getopt_long(argc, argv, 279e2662256SAymeric Wibo "+1ABCD:FGHILPRSTUWXZabcdfghiklmnopqrstuvwxy,", long_opts, 280e10ba800SKyle Evans NULL)) != -1) { 2814b88c807SRodney W. Grimes switch (ch) { 2824b88c807SRodney W. Grimes /* 28394274c73STim J. Robbins * The -1, -C, -x and -l options all override each other so 28494274c73STim J. Robbins * shell aliasing works right. 2854b88c807SRodney W. Grimes */ 2864b88c807SRodney W. Grimes case '1': 2874b88c807SRodney W. Grimes f_singlecol = 1; 2889052855aSMark Murray f_longform = 0; 28994274c73STim J. Robbins f_stream = 0; 2904b88c807SRodney W. Grimes break; 2914b88c807SRodney W. Grimes case 'C': 29294274c73STim J. Robbins f_sortacross = f_longform = f_singlecol = 0; 2934b88c807SRodney W. Grimes break; 2944b88c807SRodney W. Grimes case 'l': 2954b88c807SRodney W. Grimes f_longform = 1; 2969052855aSMark Murray f_singlecol = 0; 29794274c73STim J. Robbins f_stream = 0; 29894274c73STim J. Robbins break; 29994274c73STim J. Robbins case 'x': 30094274c73STim J. Robbins f_sortacross = 1; 30194274c73STim J. Robbins f_longform = 0; 30294274c73STim J. Robbins f_singlecol = 0; 3034b88c807SRodney W. Grimes break; 304fe79420eSJohn Baldwin /* The -c, -u, and -U options override each other. */ 3054b88c807SRodney W. Grimes case 'c': 3064b88c807SRodney W. Grimes f_statustime = 1; 3074b88c807SRodney W. Grimes f_accesstime = 0; 308fe79420eSJohn Baldwin f_birthtime = 0; 3094b88c807SRodney W. Grimes break; 3104b88c807SRodney W. Grimes case 'u': 3114b88c807SRodney W. Grimes f_accesstime = 1; 3124b88c807SRodney W. Grimes f_statustime = 0; 313fe79420eSJohn Baldwin f_birthtime = 0; 314fe79420eSJohn Baldwin break; 315fe79420eSJohn Baldwin case 'U': 316fe79420eSJohn Baldwin f_birthtime = 1; 317fe79420eSJohn Baldwin f_accesstime = 0; 318fe79420eSJohn Baldwin f_statustime = 0; 3194b88c807SRodney W. Grimes break; 3203651faa5SGreg Lehey case 'f': 3213651faa5SGreg Lehey f_nosort = 1; 3223651faa5SGreg Lehey /* FALLTHROUGH */ 32344f17a81SGreg Lehey case 'a': 32444f17a81SGreg Lehey fts_options |= FTS_SEEDOT; 32544f17a81SGreg Lehey /* FALLTHROUGH */ 32644f17a81SGreg Lehey case 'A': 32744f17a81SGreg Lehey f_listdot = 1; 32844f17a81SGreg Lehey break; 329d854370fSAlexander Ziaee /* The -S, -t and -v options override each other. */ 33044f17a81SGreg Lehey case 'S': 33144f17a81SGreg Lehey f_sizesort = 1; 33244f17a81SGreg Lehey f_timesort = 0; 333d854370fSAlexander Ziaee f_verssort = 0; 33444f17a81SGreg Lehey break; 33544f17a81SGreg Lehey case 't': 33644f17a81SGreg Lehey f_timesort = 1; 33744f17a81SGreg Lehey f_sizesort = 0; 338d854370fSAlexander Ziaee f_verssort = 0; 339d854370fSAlexander Ziaee break; 340d854370fSAlexander Ziaee case 'v': 341d854370fSAlexander Ziaee f_verssort = 1; 342d854370fSAlexander Ziaee f_sizesort = 0; 343ef75877fSWarner Losh f_timesort = 0; 34444f17a81SGreg Lehey break; 34544f17a81SGreg Lehey /* Other flags. Please keep alphabetic. */ 3469aa68a3fSGreg Lehey case ',': 3479aa68a3fSGreg Lehey f_thousands = 1; 3489aa68a3fSGreg Lehey break; 34944f17a81SGreg Lehey case 'B': 35044f17a81SGreg Lehey f_nonprint = 0; 35144f17a81SGreg Lehey f_octal = 1; 35244f17a81SGreg Lehey f_octal_escape = 0; 35344f17a81SGreg Lehey break; 35444f17a81SGreg Lehey case 'D': 35544f17a81SGreg Lehey f_timeformat = optarg; 35644f17a81SGreg Lehey break; 3574b88c807SRodney W. Grimes case 'F': 3584b88c807SRodney W. Grimes f_type = 1; 35994274c73STim J. Robbins f_slash = 0; 3604b88c807SRodney W. Grimes break; 36144f17a81SGreg Lehey case 'G': 362a408dc20SKyle Evans /* 363a408dc20SKyle Evans * We both set CLICOLOR here and set colorflag to 364a408dc20SKyle Evans * COLORFLAG_AUTO, because -G should not force color if 365a408dc20SKyle Evans * stdout isn't a tty. 366a408dc20SKyle Evans */ 36744f17a81SGreg Lehey setenv("CLICOLOR", "", 1); 368151a7e11SKyle Evans #ifdef COLORLS 369a408dc20SKyle Evans colorflag = COLORFLAG_AUTO; 370151a7e11SKyle Evans #endif 37144f17a81SGreg Lehey break; 3723a34dbf7SDag-Erling Smørgrav case 'H': 3733a34dbf7SDag-Erling Smørgrav fts_options |= FTS_COMFOLLOW; 3741fe8c2a9SJaakko Heinonen f_nofollow = 0; 3753a34dbf7SDag-Erling Smørgrav break; 37644f17a81SGreg Lehey case 'I': 37744f17a81SGreg Lehey f_noautodot = 1; 3783885812cSJosef Karthauser break; 3794b88c807SRodney W. Grimes case 'L': 3804b88c807SRodney W. Grimes fts_options &= ~FTS_PHYSICAL; 3814b88c807SRodney W. Grimes fts_options |= FTS_LOGICAL; 3821fe8c2a9SJaakko Heinonen f_nofollow = 0; 3834b88c807SRodney W. Grimes break; 3843a34dbf7SDag-Erling Smørgrav case 'P': 3853a34dbf7SDag-Erling Smørgrav fts_options &= ~FTS_COMFOLLOW; 3863a34dbf7SDag-Erling Smørgrav fts_options &= ~FTS_LOGICAL; 3873a34dbf7SDag-Erling Smørgrav fts_options |= FTS_PHYSICAL; 3881fe8c2a9SJaakko Heinonen f_nofollow = 1; 3893a34dbf7SDag-Erling Smørgrav break; 3904b88c807SRodney W. Grimes case 'R': 3914b88c807SRodney W. Grimes f_recursive = 1; 3924b88c807SRodney W. Grimes break; 39344f17a81SGreg Lehey case 'T': 39444f17a81SGreg Lehey f_sectime = 1; 3954b88c807SRodney W. Grimes break; 39644f17a81SGreg Lehey case 'W': 39744f17a81SGreg Lehey f_whiteout = 1; 39844f17a81SGreg Lehey break; 39944f17a81SGreg Lehey case 'Z': 40044f17a81SGreg Lehey f_label = 1; 40144f17a81SGreg Lehey break; 40244f17a81SGreg Lehey case 'b': 40344f17a81SGreg Lehey f_nonprint = 0; 40444f17a81SGreg Lehey f_octal = 0; 40544f17a81SGreg Lehey f_octal_escape = 1; 4067b7d153bSMaxime Henrion break; 4074b88c807SRodney W. Grimes /* The -d option turns off the -R option. */ 4084b88c807SRodney W. Grimes case 'd': 4094b88c807SRodney W. Grimes f_listdir = 1; 4104b88c807SRodney W. Grimes f_recursive = 0; 4114b88c807SRodney W. Grimes break; 4123bfbb521SMinsoo Choo case 'g': 4133bfbb521SMinsoo Choo f_longform = 1; 4143bfbb521SMinsoo Choo f_singlecol = 0; 4153bfbb521SMinsoo Choo f_stream = 0; 4163bfbb521SMinsoo Choo f_sowner = 1; 4174b88c807SRodney W. Grimes break; 4180e8d1551SJosef Karthauser case 'h': 4190e8d1551SJosef Karthauser f_humanval = 1; 4200e8d1551SJosef Karthauser break; 4214b88c807SRodney W. Grimes case 'i': 4224b88c807SRodney W. Grimes f_inode = 1; 4234b88c807SRodney W. Grimes break; 424475727a0SPaul Traina case 'k': 425d5f9f41cSDavid E. O'Brien f_humanval = 0; 426475727a0SPaul Traina f_kblocks = 1; 427475727a0SPaul Traina break; 42894274c73STim J. Robbins case 'm': 42994274c73STim J. Robbins f_stream = 1; 43094274c73STim J. Robbins f_singlecol = 0; 43194274c73STim J. Robbins f_longform = 0; 43294274c73STim J. Robbins break; 433f3a6a64eSSheldon Hearn case 'n': 434f3a6a64eSSheldon Hearn f_numericonly = 1; 4353bfbb521SMinsoo Choo f_longform = 1; 4363bfbb521SMinsoo Choo f_singlecol = 0; 4373bfbb521SMinsoo Choo f_stream = 0; 438f3a6a64eSSheldon Hearn break; 4394b88c807SRodney W. Grimes case 'o': 4404b88c807SRodney W. Grimes f_flags = 1; 4414b88c807SRodney W. Grimes break; 44294274c73STim J. Robbins case 'p': 44394274c73STim J. Robbins f_slash = 1; 44494274c73STim J. Robbins f_type = 1; 44594274c73STim J. Robbins break; 4464b88c807SRodney W. Grimes case 'q': 4474b88c807SRodney W. Grimes f_nonprint = 1; 4487ea30648SDag-Erling Smørgrav f_octal = 0; 4490d86878cSDag-Erling Smørgrav f_octal_escape = 0; 4504b88c807SRodney W. Grimes break; 4514b88c807SRodney W. Grimes case 'r': 4524b88c807SRodney W. Grimes f_reversesort = 1; 4534b88c807SRodney W. Grimes break; 4544b88c807SRodney W. Grimes case 's': 4554b88c807SRodney W. Grimes f_size = 1; 4564b88c807SRodney W. Grimes break; 45747f884f0SJosef Karthauser case 'w': 45847f884f0SJosef Karthauser f_nonprint = 0; 45947f884f0SJosef Karthauser f_octal = 0; 46047f884f0SJosef Karthauser f_octal_escape = 0; 46147f884f0SJosef Karthauser break; 4629aa68a3fSGreg Lehey case 'y': 4639aa68a3fSGreg Lehey f_samesort = 1; 4649aa68a3fSGreg Lehey break; 4658b929778SPiotr Pawel Stefaniak case GROUP_OPT: 4668b929778SPiotr Pawel Stefaniak if (optarg == NULL || strcmp(optarg, "first") == 0) 4678b929778SPiotr Pawel Stefaniak f_groupdir = GRP_DIR_FIRST; 4688b929778SPiotr Pawel Stefaniak else if (strcmp(optarg, "last") == 0) 4698b929778SPiotr Pawel Stefaniak f_groupdir = GRP_DIR_LAST; 4708b929778SPiotr Pawel Stefaniak else 4718b929778SPiotr Pawel Stefaniak errx(2, "unsupported --group-directories value '%s' (must be first or last)", 4728b929778SPiotr Pawel Stefaniak optarg); 4738b929778SPiotr Pawel Stefaniak break; 474e10ba800SKyle Evans case COLOR_OPT: 475ced2dcadSPiotr Pawel Stefaniak #ifdef COLORLS 476041e6eb1SKyle Evans if (optarg == NULL || do_color_always(optarg)) 477e10ba800SKyle Evans colorflag = COLORFLAG_ALWAYS; 478041e6eb1SKyle Evans else if (do_color_auto(optarg)) 479e10ba800SKyle Evans colorflag = COLORFLAG_AUTO; 480041e6eb1SKyle Evans else if (do_color_never(optarg)) 481e10ba800SKyle Evans colorflag = COLORFLAG_NEVER; 482e10ba800SKyle Evans else 483e10ba800SKyle Evans errx(2, "unsupported --color value '%s' (must be always, auto, or never)", 484e10ba800SKyle Evans optarg); 485e10ba800SKyle Evans break; 486ced2dcadSPiotr Pawel Stefaniak #else 487ced2dcadSPiotr Pawel Stefaniak warnx("color support not compiled in"); 488e10ba800SKyle Evans #endif 4894b88c807SRodney W. Grimes default: 4904b88c807SRodney W. Grimes case '?': 4914b88c807SRodney W. Grimes usage(); 4924b88c807SRodney W. Grimes } 4934b88c807SRodney W. Grimes } 4944b88c807SRodney W. Grimes argc -= optind; 4954b88c807SRodney W. Grimes argv += optind; 4964b88c807SRodney W. Grimes 497390a478eSRuslan Ermilov /* Root is -A automatically unless -I. */ 498390a478eSRuslan Ermilov if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot) 4997b7d153bSMaxime Henrion f_listdot = 1; 5007b7d153bSMaxime Henrion 501e10ba800SKyle Evans /* 502e10ba800SKyle Evans * Enabling of colours is conditional on the environment in conjunction 503e10ba800SKyle Evans * with the --color and -G arguments, if supplied. 504e10ba800SKyle Evans */ 505e10ba800SKyle Evans if (do_color()) { 506d4413063SJosef Karthauser #ifdef COLORLS 507e10ba800SKyle Evans if ((term = getenv("TERM")) != NULL && 508e10ba800SKyle Evans tgetent(termcapbuf, term) == 1) { 5093d2ddc9eSJosef Karthauser ansi_fgcol = tgetstr("AF", &bp); 5103d2ddc9eSJosef Karthauser ansi_bgcol = tgetstr("AB", &bp); 511c1499cf6SJosef Karthauser attrs_off = tgetstr("me", &bp); 512c1499cf6SJosef Karthauser enter_bold = tgetstr("md", &bp); 51397c31821SCameron Katri enter_underline = tgetstr("us", &bp); 5143d2ddc9eSJosef Karthauser 5153d2ddc9eSJosef Karthauser /* To switch colours off use 'op' if 5163d2ddc9eSJosef Karthauser * available, otherwise use 'oc', or 5173d2ddc9eSJosef Karthauser * don't do colours at all. */ 5183d2ddc9eSJosef Karthauser ansi_coloff = tgetstr("op", &bp); 5193d2ddc9eSJosef Karthauser if (!ansi_coloff) 5203d2ddc9eSJosef Karthauser ansi_coloff = tgetstr("oc", &bp); 5213d2ddc9eSJosef Karthauser if (ansi_fgcol && ansi_bgcol && ansi_coloff) 5223d2ddc9eSJosef Karthauser f_color = 1; 523e10ba800SKyle Evans } else if (colorflag == COLORFLAG_ALWAYS) { 524e10ba800SKyle Evans /* 525e10ba800SKyle Evans * If we're *always* doing color but we don't have 526e10ba800SKyle Evans * a functional TERM supplied, we'll fallback to 527e10ba800SKyle Evans * outputting raw ANSI sequences. 528e10ba800SKyle Evans */ 529e10ba800SKyle Evans f_color = 1; 530e10ba800SKyle Evans explicitansi = true; 5313d2ddc9eSJosef Karthauser } 532d4413063SJosef Karthauser #endif /*COLORLS*/ 533e10ba800SKyle Evans } 5343d2ddc9eSJosef Karthauser 535d4413063SJosef Karthauser #ifdef COLORLS 536bd82d8abSAndrey A. Chernov if (f_color) { 53722ff3e9eSAndrey A. Chernov /* 53822ff3e9eSAndrey A. Chernov * We can't put tabs and color sequences together: 53922ff3e9eSAndrey A. Chernov * column number will be incremented incorrectly 54022ff3e9eSAndrey A. Chernov * for "stty oxtabs" mode. 54122ff3e9eSAndrey A. Chernov */ 54222ff3e9eSAndrey A. Chernov f_notabs = 1; 543bd82d8abSAndrey A. Chernov (void)signal(SIGINT, colorquit); 544ab08444fSMartin Cracauer (void)signal(SIGQUIT, colorquit); 5453885812cSJosef Karthauser parsecolors(getenv("LSCOLORS")); 546bd82d8abSAndrey A. Chernov } 54774985094SJosef Karthauser #endif 5483885812cSJosef Karthauser 5494b88c807SRodney W. Grimes /* 550*6ba2f08cSPiotr Pawel Stefaniak * If not -F, -i, -l, -s, -S, -t or --group-directories options, 551*6ba2f08cSPiotr Pawel Stefaniak * don't require stat information, unless in color mode in which case 552*6ba2f08cSPiotr Pawel Stefaniak * we do need this to determine which colors to display. 5534b88c807SRodney W. Grimes */ 55471b8b748SDima Dorfman if (!f_inode && !f_longform && !f_size && !f_timesort && 555*6ba2f08cSPiotr Pawel Stefaniak !f_sizesort && !f_type && f_groupdir == GRP_NONE 55674985094SJosef Karthauser #ifdef COLORLS 55774985094SJosef Karthauser && !f_color 55874985094SJosef Karthauser #endif 55974985094SJosef Karthauser ) 5604b88c807SRodney W. Grimes fts_options |= FTS_NOSTAT; 5614b88c807SRodney W. Grimes 5624b88c807SRodney W. Grimes /* 5631fe8c2a9SJaakko Heinonen * If not -F, -P, -d or -l options, follow any symbolic links listed on 564183714f3SXin LI * the command line, unless in color mode in which case we need to 565183714f3SXin LI * distinguish file type for a symbolic link itself and its target. 5664b88c807SRodney W. Grimes */ 567183714f3SXin LI if (!f_nofollow && !f_longform && !f_listdir && (!f_type || f_slash) 568183714f3SXin LI #ifdef COLORLS 569183714f3SXin LI && !f_color 570183714f3SXin LI #endif 571183714f3SXin LI ) 5724b88c807SRodney W. Grimes fts_options |= FTS_COMFOLLOW; 5734b88c807SRodney W. Grimes 574fb5cb208SSteve Price /* 575fb5cb208SSteve Price * If -W, show whiteout entries 576fb5cb208SSteve Price */ 577fb5cb208SSteve Price #ifdef FTS_WHITEOUT 578fb5cb208SSteve Price if (f_whiteout) 579fb5cb208SSteve Price fts_options |= FTS_WHITEOUT; 580fb5cb208SSteve Price #endif 581fb5cb208SSteve Price 5824d4dcc7aSDag-Erling Smørgrav /* If -i, -l or -s, figure out block size. */ 5834d4dcc7aSDag-Erling Smørgrav if (f_inode || f_longform || f_size) { 58490b0ec31SPoul-Henning Kamp if (f_kblocks) 58590b0ec31SPoul-Henning Kamp blocksize = 2; 58690b0ec31SPoul-Henning Kamp else { 5874b88c807SRodney W. Grimes (void)getbsize(¬used, &blocksize); 5884b88c807SRodney W. Grimes blocksize /= 512; 58990b0ec31SPoul-Henning Kamp } 5904b88c807SRodney W. Grimes } 591ef75877fSWarner Losh 5924b88c807SRodney W. Grimes /* Select a sort function. */ 5934b88c807SRodney W. Grimes if (f_reversesort) { 594e2662256SAymeric Wibo if (f_sizesort) 59586cca1e7SJohn Baldwin sortfcn = revsizecmp; 596e2662256SAymeric Wibo else if (f_verssort) 597e2662256SAymeric Wibo sortfcn = revverscmp; 598e2662256SAymeric Wibo else if (!f_timesort) 599e2662256SAymeric Wibo sortfcn = revnamecmp; 6004b88c807SRodney W. Grimes else if (f_accesstime) 6014b88c807SRodney W. Grimes sortfcn = revacccmp; 602fe79420eSJohn Baldwin else if (f_birthtime) 603fe79420eSJohn Baldwin sortfcn = revbirthcmp; 6044b88c807SRodney W. Grimes else if (f_statustime) 6054b88c807SRodney W. Grimes sortfcn = revstatcmp; 6064b88c807SRodney W. Grimes else /* Use modification time. */ 6074b88c807SRodney W. Grimes sortfcn = revmodcmp; 6084b88c807SRodney W. Grimes } else { 609e2662256SAymeric Wibo if (f_sizesort) 61086cca1e7SJohn Baldwin sortfcn = sizecmp; 611e2662256SAymeric Wibo else if (f_verssort) 612e2662256SAymeric Wibo sortfcn = verscmp; 613e2662256SAymeric Wibo else if (!f_timesort) 614e2662256SAymeric Wibo sortfcn = namecmp; 6154b88c807SRodney W. Grimes else if (f_accesstime) 6164b88c807SRodney W. Grimes sortfcn = acccmp; 617fe79420eSJohn Baldwin else if (f_birthtime) 618fe79420eSJohn Baldwin sortfcn = birthcmp; 6194b88c807SRodney W. Grimes else if (f_statustime) 6204b88c807SRodney W. Grimes sortfcn = statcmp; 6214b88c807SRodney W. Grimes else /* Use modification time. */ 6224b88c807SRodney W. Grimes sortfcn = modcmp; 6234b88c807SRodney W. Grimes } 6244b88c807SRodney W. Grimes 6254b88c807SRodney W. Grimes /* Select a print function. */ 6264b88c807SRodney W. Grimes if (f_singlecol) 6274b88c807SRodney W. Grimes printfcn = printscol; 6284b88c807SRodney W. Grimes else if (f_longform) 6294b88c807SRodney W. Grimes printfcn = printlong; 63094274c73STim J. Robbins else if (f_stream) 63194274c73STim J. Robbins printfcn = printstream; 6324b88c807SRodney W. Grimes else 6334b88c807SRodney W. Grimes printfcn = printcol; 6344b88c807SRodney W. Grimes 6354b88c807SRodney W. Grimes if (argc) 6364b88c807SRodney W. Grimes traverse(argc, argv, fts_options); 6374b88c807SRodney W. Grimes else 6384b88c807SRodney W. Grimes traverse(1, dotav, fts_options); 639fb1000d6SAdam David exit(rval); 6404b88c807SRodney W. Grimes } 6414b88c807SRodney W. Grimes 6424b88c807SRodney W. Grimes static int output; /* If anything output. */ 6434b88c807SRodney W. Grimes 6444b88c807SRodney W. Grimes /* 6454b88c807SRodney W. Grimes * Traverse() walks the logical directory structure specified by the argv list 6464b88c807SRodney W. Grimes * in the order specified by the mastercmp() comparison function. During the 6474b88c807SRodney W. Grimes * traversal it passes linked lists of structures to display() which represent 6484b88c807SRodney W. Grimes * a superset (may be exact set) of the files to be displayed. 6494b88c807SRodney W. Grimes */ 6504b88c807SRodney W. Grimes static void 65146251ddeSWarner Losh traverse(int argc, char *argv[], int options) 6524b88c807SRodney W. Grimes { 6534b88c807SRodney W. Grimes FTS *ftsp; 6544b88c807SRodney W. Grimes FTSENT *p, *chp; 6554b88c807SRodney W. Grimes int ch_options; 6564b88c807SRodney W. Grimes 6574b88c807SRodney W. Grimes if ((ftsp = 6584b88c807SRodney W. Grimes fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 6590fdf7fa8SConrad Meyer err(1, "fts_open"); 6604b88c807SRodney W. Grimes 66170bad4f7SDavid Schultz /* 66270bad4f7SDavid Schultz * We ignore errors from fts_children here since they will be 66370bad4f7SDavid Schultz * replicated and signalled on the next call to fts_read() below. 66470bad4f7SDavid Schultz */ 66570bad4f7SDavid Schultz chp = fts_children(ftsp, 0); 66670bad4f7SDavid Schultz if (chp != NULL) 66770bad4f7SDavid Schultz display(NULL, chp, options); 668e6c9c463SMark Johnston if (f_listdir) { 669e6c9c463SMark Johnston fts_close(ftsp); 6704b88c807SRodney W. Grimes return; 671e6c9c463SMark Johnston } 6724b88c807SRodney W. Grimes 6734b88c807SRodney W. Grimes /* 6744b88c807SRodney W. Grimes * If not recursing down this tree and don't need stat info, just get 6754b88c807SRodney W. Grimes * the names. 6764b88c807SRodney W. Grimes */ 6773c3f5f9cSRobert Watson ch_options = !f_recursive && !f_label && 6783c3f5f9cSRobert Watson options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 6794b88c807SRodney W. Grimes 6802dfa4b66SBryan Drewery while (errno = 0, (p = fts_read(ftsp)) != NULL) 6814b88c807SRodney W. Grimes switch (p->fts_info) { 6824b88c807SRodney W. Grimes case FTS_DC: 6830fdf7fa8SConrad Meyer warnx("%s: directory causes a cycle", p->fts_name); 6844b88c807SRodney W. Grimes break; 6854b88c807SRodney W. Grimes case FTS_DNR: 6864b88c807SRodney W. Grimes case FTS_ERR: 6870fdf7fa8SConrad Meyer warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 688fb1000d6SAdam David rval = 1; 6894b88c807SRodney W. Grimes break; 6904b88c807SRodney W. Grimes case FTS_D: 6914b88c807SRodney W. Grimes if (p->fts_level != FTS_ROOTLEVEL && 692390a478eSRuslan Ermilov p->fts_name[0] == '.' && !f_listdot) 6934b88c807SRodney W. Grimes break; 6944b88c807SRodney W. Grimes 6954b88c807SRodney W. Grimes /* 6964b88c807SRodney W. Grimes * If already output something, put out a newline as 6974b88c807SRodney W. Grimes * a separator. If multiple arguments, precede each 6984b88c807SRodney W. Grimes * directory with its name. 6994b88c807SRodney W. Grimes */ 7001656f850STim J. Robbins if (output) { 7010fdf7fa8SConrad Meyer putchar('\n'); 7020fdf7fa8SConrad Meyer (void)printname(p->fts_path); 7030fdf7fa8SConrad Meyer puts(":"); 7041656f850STim J. Robbins } else if (argc > 1) { 7050fdf7fa8SConrad Meyer (void)printname(p->fts_path); 7060fdf7fa8SConrad Meyer puts(":"); 7074b88c807SRodney W. Grimes output = 1; 7084b88c807SRodney W. Grimes } 7094b88c807SRodney W. Grimes chp = fts_children(ftsp, ch_options); 7104d33b62eSRobert Watson display(p, chp, options); 7114b88c807SRodney W. Grimes 7124b88c807SRodney W. Grimes if (!f_recursive && chp != NULL) 7134b88c807SRodney W. Grimes (void)fts_set(ftsp, p, FTS_SKIP); 7144b88c807SRodney W. Grimes break; 715568dcd5fSBill Fumerola default: 716568dcd5fSBill Fumerola break; 7174b88c807SRodney W. Grimes } 7184b88c807SRodney W. Grimes if (errno) 7190fdf7fa8SConrad Meyer err(1, "fts_read"); 720e6c9c463SMark Johnston fts_close(ftsp); 7214b88c807SRodney W. Grimes } 7224b88c807SRodney W. Grimes 7234b88c807SRodney W. Grimes /* 7244b88c807SRodney W. Grimes * Display() takes a linked list of FTSENT structures and passes the list 7254b88c807SRodney W. Grimes * along with any other necessary information to the print function. P 7264b88c807SRodney W. Grimes * points to the parent directory of the display list. 7274b88c807SRodney W. Grimes */ 7284b88c807SRodney W. Grimes static void 72940feca3aSMark Murray display(const FTSENT *p, FTSENT *list, int options) 7304b88c807SRodney W. Grimes { 7314b88c807SRodney W. Grimes struct stat *sp; 7324b88c807SRodney W. Grimes DISPLAY d; 7334b88c807SRodney W. Grimes FTSENT *cur; 7344b88c807SRodney W. Grimes NAMES *np; 7359052855aSMark Murray off_t maxsize; 73640feca3aSMark Murray long maxblock; 7376db1a7f1SMatthew D Fleming uintmax_t maxinode; 7386db1a7f1SMatthew D Fleming u_long btotal, labelstrlen, maxlen, maxnlink; 7394d33b62eSRobert Watson u_long maxlabelstr; 7409f365aa1SEd Schouten u_int sizelen; 74155926a66SJaakko Heinonen int maxflags; 7429052855aSMark Murray gid_t maxgroup; 7439052855aSMark Murray uid_t maxuser; 7449052855aSMark Murray size_t flen, ulen, glen; 745545f583cSTim Vanderhoek char *initmax; 7464b88c807SRodney W. Grimes int entries, needstats; 7470928a7f1SJuli Mallett const char *user, *group; 7484d33b62eSRobert Watson char *flags, *labelstr = NULL; 749008a4910SSheldon Hearn char ngroup[STRBUF_SIZEOF(uid_t) + 1]; 750008a4910SSheldon Hearn char nuser[STRBUF_SIZEOF(gid_t) + 1]; 7513fee777eSStefan Eßer u_long width[9]; 7523fee777eSStefan Eßer int i; 7534b88c807SRodney W. Grimes 7544b88c807SRodney W. Grimes needstats = f_inode || f_longform || f_size; 7554b88c807SRodney W. Grimes flen = 0; 756545f583cSTim Vanderhoek btotal = 0; 7573fee777eSStefan Eßer 7583fee777eSStefan Eßer #define LS_COLWIDTHS_FIELDS 9 759545f583cSTim Vanderhoek initmax = getenv("LS_COLWIDTHS"); 760545f583cSTim Vanderhoek 7613fee777eSStefan Eßer for (i = 0 ; i < LS_COLWIDTHS_FIELDS; i++) 7623fee777eSStefan Eßer width[i] = 0; 7633fee777eSStefan Eßer 7643fee777eSStefan Eßer if (initmax != NULL) { 7653fee777eSStefan Eßer char *endp; 7663fee777eSStefan Eßer 7673fee777eSStefan Eßer for (i = 0; i < LS_COLWIDTHS_FIELDS && *initmax != '\0'; i++) { 7683fee777eSStefan Eßer if (*initmax == ':') { 7693fee777eSStefan Eßer width[i] = 0; 770545f583cSTim Vanderhoek } else { 7713fee777eSStefan Eßer width[i] = strtoul(initmax, &endp, 10); 7723fee777eSStefan Eßer initmax = endp; 7733fee777eSStefan Eßer while (isspace(*initmax)) 7743fee777eSStefan Eßer initmax++; 7753fee777eSStefan Eßer if (*initmax != ':') 7763fee777eSStefan Eßer break; 7773fee777eSStefan Eßer initmax++; 778545f583cSTim Vanderhoek } 779545f583cSTim Vanderhoek } 7803fee777eSStefan Eßer if (i < LS_COLWIDTHS_FIELDS) 78118d8a22bSAndrey A. Chernov #ifdef COLORLS 78218d8a22bSAndrey A. Chernov if (!f_color) 78322ff3e9eSAndrey A. Chernov #endif 78418d8a22bSAndrey A. Chernov f_notabs = 0; 785545f583cSTim Vanderhoek } 7863fee777eSStefan Eßer 7873fee777eSStefan Eßer /* Fields match -lios order. New ones should be added at the end. */ 7883fee777eSStefan Eßer maxinode = width[0]; 7893fee777eSStefan Eßer maxblock = width[1]; 7903fee777eSStefan Eßer maxnlink = width[2]; 7913fee777eSStefan Eßer maxuser = width[3]; 7923fee777eSStefan Eßer maxgroup = width[4]; 7933fee777eSStefan Eßer maxflags = width[5]; 7943fee777eSStefan Eßer maxsize = width[6]; 7953fee777eSStefan Eßer maxlen = width[7]; 7963fee777eSStefan Eßer maxlabelstr = width[8]; 7973fee777eSStefan Eßer 79840feca3aSMark Murray MAKENINES(maxinode); 79940feca3aSMark Murray MAKENINES(maxblock); 80040feca3aSMark Murray MAKENINES(maxnlink); 80140feca3aSMark Murray MAKENINES(maxsize); 8023fee777eSStefan Eßer 8039f365aa1SEd Schouten d.s_size = 0; 8049f365aa1SEd Schouten sizelen = 0; 8050fd510b7SJoerg Wunsch flags = NULL; 8064b88c807SRodney W. Grimes for (cur = list, entries = 0; cur; cur = cur->fts_link) { 8074b88c807SRodney W. Grimes if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 8080fdf7fa8SConrad Meyer warnx("%s: %s", 8094b88c807SRodney W. Grimes cur->fts_name, strerror(cur->fts_errno)); 8104b88c807SRodney W. Grimes cur->fts_number = NO_PRINT; 811fb1000d6SAdam David rval = 1; 8124b88c807SRodney W. Grimes continue; 8134b88c807SRodney W. Grimes } 8144b88c807SRodney W. Grimes /* 8154b88c807SRodney W. Grimes * P is NULL if list is the argv list, to which different rules 8164b88c807SRodney W. Grimes * apply. 8174b88c807SRodney W. Grimes */ 8184b88c807SRodney W. Grimes if (p == NULL) { 8194b88c807SRodney W. Grimes /* Directories will be displayed later. */ 8204b88c807SRodney W. Grimes if (cur->fts_info == FTS_D && !f_listdir) { 8214b88c807SRodney W. Grimes cur->fts_number = NO_PRINT; 8224b88c807SRodney W. Grimes continue; 8234b88c807SRodney W. Grimes } 8244b88c807SRodney W. Grimes } else { 8254b88c807SRodney W. Grimes /* Only display dot file if -a/-A set. */ 826390a478eSRuslan Ermilov if (cur->fts_name[0] == '.' && !f_listdot) { 8274b88c807SRodney W. Grimes cur->fts_number = NO_PRINT; 8284b88c807SRodney W. Grimes continue; 8294b88c807SRodney W. Grimes } 8304b88c807SRodney W. Grimes } 8314b88c807SRodney W. Grimes if (cur->fts_namelen > maxlen) 8324b88c807SRodney W. Grimes maxlen = cur->fts_namelen; 8330d86878cSDag-Erling Smørgrav if (f_octal || f_octal_escape) { 8346bd042dfSJosef Karthauser u_long t = len_octal(cur->fts_name, cur->fts_namelen); 8355dda5d0dSJosef Karthauser 8365dda5d0dSJosef Karthauser if (t > maxlen) 8375dda5d0dSJosef Karthauser maxlen = t; 8387ea30648SDag-Erling Smørgrav } 8394b88c807SRodney W. Grimes if (needstats) { 8404b88c807SRodney W. Grimes sp = cur->fts_statp; 8414b88c807SRodney W. Grimes if (sp->st_blocks > maxblock) 8424b88c807SRodney W. Grimes maxblock = sp->st_blocks; 8434b88c807SRodney W. Grimes if (sp->st_ino > maxinode) 8444b88c807SRodney W. Grimes maxinode = sp->st_ino; 8454b88c807SRodney W. Grimes if (sp->st_nlink > maxnlink) 8464b88c807SRodney W. Grimes maxnlink = sp->st_nlink; 8474b88c807SRodney W. Grimes if (sp->st_size > maxsize) 8484b88c807SRodney W. Grimes maxsize = sp->st_size; 8494b88c807SRodney W. Grimes 8504b88c807SRodney W. Grimes btotal += sp->st_blocks; 8514b88c807SRodney W. Grimes if (f_longform) { 852f3a6a64eSSheldon Hearn if (f_numericonly) { 853f3a6a64eSSheldon Hearn (void)snprintf(nuser, sizeof(nuser), 854f3a6a64eSSheldon Hearn "%u", sp->st_uid); 855f3a6a64eSSheldon Hearn (void)snprintf(ngroup, sizeof(ngroup), 856f3a6a64eSSheldon Hearn "%u", sp->st_gid); 857f3a6a64eSSheldon Hearn user = nuser; 858f3a6a64eSSheldon Hearn group = ngroup; 859f3a6a64eSSheldon Hearn } else { 8604b88c807SRodney W. Grimes user = user_from_uid(sp->st_uid, 0); 861530d2d67SConrad Meyer /* 862530d2d67SConrad Meyer * user_from_uid(..., 0) only returns 863530d2d67SConrad Meyer * NULL in OOM conditions. We could 864530d2d67SConrad Meyer * format the uid here, but (1) in 865530d2d67SConrad Meyer * general ls(1) exits on OOM, and (2) 866530d2d67SConrad Meyer * there is another allocation/exit 867530d2d67SConrad Meyer * path directly below, which will 868530d2d67SConrad Meyer * likely exit anyway. 869530d2d67SConrad Meyer */ 870530d2d67SConrad Meyer if (user == NULL) 871530d2d67SConrad Meyer err(1, "user_from_uid"); 872f3a6a64eSSheldon Hearn group = group_from_gid(sp->st_gid, 0); 873530d2d67SConrad Meyer /* Ditto. */ 874530d2d67SConrad Meyer if (group == NULL) 875530d2d67SConrad Meyer err(1, "group_from_gid"); 876f3a6a64eSSheldon Hearn } 8774b88c807SRodney W. Grimes if ((ulen = strlen(user)) > maxuser) 8784b88c807SRodney W. Grimes maxuser = ulen; 8794b88c807SRodney W. Grimes if ((glen = strlen(group)) > maxgroup) 8804b88c807SRodney W. Grimes maxgroup = glen; 8814b88c807SRodney W. Grimes if (f_flags) { 882141d77b8SJosef Karthauser flags = fflagstostr(sp->st_flags); 883141d77b8SJosef Karthauser if (flags != NULL && *flags == '\0') { 884141d77b8SJosef Karthauser free(flags); 885141d77b8SJosef Karthauser flags = strdup("-"); 886141d77b8SJosef Karthauser } 887141d77b8SJosef Karthauser if (flags == NULL) 8880fdf7fa8SConrad Meyer err(1, "fflagstostr"); 8899052855aSMark Murray flen = strlen(flags); 8909052855aSMark Murray if (flen > (size_t)maxflags) 8914b88c807SRodney W. Grimes maxflags = flen; 8924b88c807SRodney W. Grimes } else 8934b88c807SRodney W. Grimes flen = 0; 8944d33b62eSRobert Watson labelstr = NULL; 8954d33b62eSRobert Watson if (f_label) { 8964df6dabaSRobert Watson char name[PATH_MAX + 1]; 8974d33b62eSRobert Watson mac_t label; 8984d33b62eSRobert Watson int error; 8994b88c807SRodney W. Grimes 9004d33b62eSRobert Watson error = mac_prepare_file_label(&label); 9014d33b62eSRobert Watson if (error == -1) { 9020fdf7fa8SConrad Meyer warn("MAC label for %s/%s", 9033c3f5f9cSRobert Watson cur->fts_parent->fts_path, 9043c3f5f9cSRobert Watson cur->fts_name); 9054d33b62eSRobert Watson goto label_out; 9064d33b62eSRobert Watson } 9074d33b62eSRobert Watson 9084df6dabaSRobert Watson if (cur->fts_level == FTS_ROOTLEVEL) 9094df6dabaSRobert Watson snprintf(name, sizeof(name), 9104df6dabaSRobert Watson "%s", cur->fts_name); 9114d33b62eSRobert Watson else 9124df6dabaSRobert Watson snprintf(name, sizeof(name), 9133c3f5f9cSRobert Watson "%s/%s", cur->fts_parent-> 9143c3f5f9cSRobert Watson fts_accpath, cur->fts_name); 9154df6dabaSRobert Watson 9164df6dabaSRobert Watson if (options & FTS_LOGICAL) 9174df6dabaSRobert Watson error = mac_get_file(name, 9184df6dabaSRobert Watson label); 9194df6dabaSRobert Watson else 9204df6dabaSRobert Watson error = mac_get_link(name, 9214df6dabaSRobert Watson label); 9224d33b62eSRobert Watson if (error == -1) { 9230fdf7fa8SConrad Meyer warn("MAC label for %s/%s", 9243c3f5f9cSRobert Watson cur->fts_parent->fts_path, 9253c3f5f9cSRobert Watson cur->fts_name); 9264d33b62eSRobert Watson mac_free(label); 9274d33b62eSRobert Watson goto label_out; 9284d33b62eSRobert Watson } 9294d33b62eSRobert Watson 9304d33b62eSRobert Watson error = mac_to_text(label, 9314d33b62eSRobert Watson &labelstr); 9324d33b62eSRobert Watson if (error == -1) { 9330fdf7fa8SConrad Meyer warn("MAC label for %s/%s", 9343c3f5f9cSRobert Watson cur->fts_parent->fts_path, 9353c3f5f9cSRobert Watson cur->fts_name); 9364d33b62eSRobert Watson mac_free(label); 9374d33b62eSRobert Watson goto label_out; 9384d33b62eSRobert Watson } 9394d33b62eSRobert Watson mac_free(label); 9404d33b62eSRobert Watson label_out: 9414d33b62eSRobert Watson if (labelstr == NULL) 942317f1d53SRobert Watson labelstr = strdup("-"); 9434d33b62eSRobert Watson labelstrlen = strlen(labelstr); 9444d33b62eSRobert Watson if (labelstrlen > maxlabelstr) 9454d33b62eSRobert Watson maxlabelstr = labelstrlen; 9464d33b62eSRobert Watson } else 9474d33b62eSRobert Watson labelstrlen = 0; 9484d33b62eSRobert Watson 9494d33b62eSRobert Watson if ((np = malloc(sizeof(NAMES) + labelstrlen + 9507304f61fSBrian Feldman ulen + glen + flen + 4)) == NULL) 9510fdf7fa8SConrad Meyer err(1, "malloc"); 9524b88c807SRodney W. Grimes 9534b88c807SRodney W. Grimes np->user = &np->data[0]; 9544b88c807SRodney W. Grimes (void)strcpy(np->user, user); 9554b88c807SRodney W. Grimes np->group = &np->data[ulen + 1]; 9564b88c807SRodney W. Grimes (void)strcpy(np->group, group); 9574b88c807SRodney W. Grimes 9589f365aa1SEd Schouten if (S_ISCHR(sp->st_mode) || 9599f365aa1SEd Schouten S_ISBLK(sp->st_mode)) { 9609f365aa1SEd Schouten sizelen = snprintf(NULL, 0, 9619f365aa1SEd Schouten "%#jx", (uintmax_t)sp->st_rdev); 9629f365aa1SEd Schouten if (d.s_size < sizelen) 9639f365aa1SEd Schouten d.s_size = sizelen; 96455926a66SJaakko Heinonen } 9654b88c807SRodney W. Grimes 9664b88c807SRodney W. Grimes if (f_flags) { 9674b88c807SRodney W. Grimes np->flags = &np->data[ulen + glen + 2]; 9684b88c807SRodney W. Grimes (void)strcpy(np->flags, flags); 969141d77b8SJosef Karthauser free(flags); 9704b88c807SRodney W. Grimes } 9714d33b62eSRobert Watson if (f_label) { 9724d33b62eSRobert Watson np->label = &np->data[ulen + glen + 2 9737304f61fSBrian Feldman + (f_flags ? flen + 1 : 0)]; 9744d33b62eSRobert Watson (void)strcpy(np->label, labelstr); 9754d33b62eSRobert Watson free(labelstr); 9767304f61fSBrian Feldman } 9774b88c807SRodney W. Grimes cur->fts_pointer = np; 9784b88c807SRodney W. Grimes } 9794b88c807SRodney W. Grimes } 9804b88c807SRodney W. Grimes ++entries; 9814b88c807SRodney W. Grimes } 9824b88c807SRodney W. Grimes 98370bad4f7SDavid Schultz /* 98470bad4f7SDavid Schultz * If there are no entries to display, we normally stop right 98570bad4f7SDavid Schultz * here. However, we must continue if we have to display the 98670bad4f7SDavid Schultz * total block count. In this case, we display the total only 98770bad4f7SDavid Schultz * on the second (p != NULL) pass. 98870bad4f7SDavid Schultz */ 98970bad4f7SDavid Schultz if (!entries && (!(f_longform || f_size) || p == NULL)) 9904b88c807SRodney W. Grimes return; 9914b88c807SRodney W. Grimes 9924b88c807SRodney W. Grimes d.list = list; 9934b88c807SRodney W. Grimes d.entries = entries; 9944b88c807SRodney W. Grimes d.maxlen = maxlen; 9954b88c807SRodney W. Grimes if (needstats) { 9964b88c807SRodney W. Grimes d.btotal = btotal; 997647d4a8cSDag-Erling Smørgrav d.s_block = snprintf(NULL, 0, f_thousands ? "%'ld" : "%ld", 998647d4a8cSDag-Erling Smørgrav howmany(maxblock, blocksize)); 9994b88c807SRodney W. Grimes d.s_flags = maxflags; 10004d33b62eSRobert Watson d.s_label = maxlabelstr; 10014b88c807SRodney W. Grimes d.s_group = maxgroup; 10026db1a7f1SMatthew D Fleming d.s_inode = snprintf(NULL, 0, "%ju", maxinode); 10039f365aa1SEd Schouten d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink); 10049f365aa1SEd Schouten sizelen = f_humanval ? HUMANVALSTR_LEN : 10059f365aa1SEd Schouten snprintf(NULL, 0, "%ju", maxsize); 10069f365aa1SEd Schouten if (d.s_size < sizelen) 10079f365aa1SEd Schouten d.s_size = sizelen; 10084b88c807SRodney W. Grimes d.s_user = maxuser; 10094b88c807SRodney W. Grimes } 10109aa68a3fSGreg Lehey if (f_thousands) /* make space for commas */ 10119aa68a3fSGreg Lehey d.s_size += (d.s_size - 1) / 3; 10124b88c807SRodney W. Grimes printfcn(&d); 10134b88c807SRodney W. Grimes output = 1; 10144b88c807SRodney W. Grimes 10154b88c807SRodney W. Grimes if (f_longform) 10164b88c807SRodney W. Grimes for (cur = list; cur; cur = cur->fts_link) 10174b88c807SRodney W. Grimes free(cur->fts_pointer); 10184b88c807SRodney W. Grimes } 10194b88c807SRodney W. Grimes 10204b88c807SRodney W. Grimes /* 10214b88c807SRodney W. Grimes * Ordering for mastercmp: 10224b88c807SRodney W. Grimes * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 10234b88c807SRodney W. Grimes * as larger than directories. Within either group, use the sort function. 10244b88c807SRodney W. Grimes * All other levels use the sort function. Error entries remain unsorted. 10254b88c807SRodney W. Grimes */ 10264b88c807SRodney W. Grimes static int 10270d3bcc2eSGarrett Wollman mastercmp(const FTSENT * const *a, const FTSENT * const *b) 10284b88c807SRodney W. Grimes { 10298b929778SPiotr Pawel Stefaniak int a_info, b_info, dir; 10304b88c807SRodney W. Grimes 10314b88c807SRodney W. Grimes a_info = (*a)->fts_info; 10324b88c807SRodney W. Grimes if (a_info == FTS_ERR) 10334b88c807SRodney W. Grimes return (0); 10344b88c807SRodney W. Grimes b_info = (*b)->fts_info; 10354b88c807SRodney W. Grimes if (b_info == FTS_ERR) 10364b88c807SRodney W. Grimes return (0); 10374b88c807SRodney W. Grimes 10384b88c807SRodney W. Grimes if (a_info == FTS_NS || b_info == FTS_NS) 10394b88c807SRodney W. Grimes return (namecmp(*a, *b)); 10404b88c807SRodney W. Grimes 104151f26ac5SSean Eric Fagan if (a_info != b_info && 104251f26ac5SSean Eric Fagan (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) { 10434b88c807SRodney W. Grimes if (a_info == FTS_D) 10444b88c807SRodney W. Grimes return (1); 104551f26ac5SSean Eric Fagan if (b_info == FTS_D) 10464b88c807SRodney W. Grimes return (-1); 104751f26ac5SSean Eric Fagan } 10488b929778SPiotr Pawel Stefaniak 10498b929778SPiotr Pawel Stefaniak if (f_groupdir != GRP_NONE) 10508b929778SPiotr Pawel Stefaniak if ((dir = (a_info == FTS_D) - (b_info == FTS_D)) != 0) 10518b929778SPiotr Pawel Stefaniak return (f_groupdir * dir); 10528b929778SPiotr Pawel Stefaniak 10534b88c807SRodney W. Grimes return (sortfcn(*a, *b)); 10544b88c807SRodney W. Grimes } 1055