Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | * tiny-ls.c version 0.1.0: A minimalist 'ls' |
| 4 | * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com> |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 9 | /* [date unknown. Perhaps before year 2000] |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | * To achieve a small memory footprint, this version of 'ls' doesn't do any |
| 11 | * file sorting, and only has the most essential command line switches |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 12 | * (i.e., the ones I couldn't live without :-) All features which involve |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 13 | * linking in substantial chunks of libc can be disabled. |
| 14 | * |
| 15 | * Although I don't really want to add new features to this program to |
| 16 | * keep it small, I *am* interested to receive bug fixes and ways to make |
| 17 | * it more portable. |
| 18 | * |
| 19 | * KNOWN BUGS: |
Denys Vlasenko | 87c150c | 2009-10-03 01:14:15 +0200 | [diff] [blame] | 20 | * 1. hidden files can make column width too large |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 21 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | * NON-OPTIMAL BEHAVIOUR: |
| 23 | * 1. autowidth reads directories twice |
| 24 | * 2. if you do a short directory listing without filetype characters |
| 25 | * appended, there's no need to stat each one |
| 26 | * PORTABILITY: |
| 27 | * 1. requires lstat (BSD) - how do you do it without? |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 28 | * |
| 29 | * [2009-03] |
| 30 | * ls sorts listing now, and supports almost all options. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | */ |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame^] | 32 | |
| 33 | //usage:#define ls_trivial_usage |
| 34 | //usage: "[-1AaCxd" |
| 35 | //usage: IF_FEATURE_LS_FOLLOWLINKS("L") |
| 36 | //usage: IF_FEATURE_LS_RECURSIVE("R") |
| 37 | //usage: IF_FEATURE_LS_FILETYPES("Fp") "lins" |
| 38 | //usage: IF_FEATURE_LS_TIMESTAMPS("e") |
| 39 | //usage: IF_FEATURE_HUMAN_READABLE("h") |
| 40 | //usage: IF_FEATURE_LS_SORTFILES("rSXv") |
| 41 | //usage: IF_FEATURE_LS_TIMESTAMPS("ctu") |
| 42 | //usage: IF_SELINUX("kKZ") "]" |
| 43 | //usage: IF_FEATURE_AUTOWIDTH(" -w WIDTH") " [FILE]..." |
| 44 | //usage:#define ls_full_usage "\n\n" |
| 45 | //usage: "List directory contents\n" |
| 46 | //usage: "\nOptions:" |
| 47 | //usage: "\n -1 List in a single column" |
| 48 | //usage: "\n -A Don't list . and .." |
| 49 | //usage: "\n -a Don't hide entries starting with ." |
| 50 | //usage: "\n -C List by columns" |
| 51 | //usage: "\n -x List by lines" |
| 52 | //usage: "\n -d List directory entries instead of contents" |
| 53 | //usage: IF_FEATURE_LS_FOLLOWLINKS( |
| 54 | //usage: "\n -L List entries pointed to by symlinks" |
| 55 | //usage: ) |
| 56 | //usage: IF_FEATURE_LS_RECURSIVE( |
| 57 | //usage: "\n -R Recurse" |
| 58 | //usage: ) |
| 59 | //usage: IF_FEATURE_LS_FILETYPES( |
| 60 | //usage: "\n -F Append indicator (one of */=@|) to entries" |
| 61 | //usage: "\n -p Append indicator (one of /=@|) to entries" |
| 62 | //usage: ) |
| 63 | //usage: "\n -l Long listing format" |
| 64 | //usage: "\n -i List inode numbers" |
| 65 | //usage: "\n -n List numeric UIDs and GIDs instead of names" |
| 66 | //usage: "\n -s List the size of each file, in blocks" |
| 67 | //usage: IF_FEATURE_LS_TIMESTAMPS( |
| 68 | //usage: "\n -e List full date and time" |
| 69 | //usage: ) |
| 70 | //usage: IF_FEATURE_HUMAN_READABLE( |
| 71 | //usage: "\n -h List sizes in human readable format (1K 243M 2G)" |
| 72 | //usage: ) |
| 73 | //usage: IF_FEATURE_LS_SORTFILES( |
| 74 | //usage: "\n -r Sort in reverse order" |
| 75 | //usage: "\n -S Sort by file size" |
| 76 | //usage: "\n -X Sort by extension" |
| 77 | //usage: "\n -v Sort by version" |
| 78 | //usage: ) |
| 79 | //usage: IF_FEATURE_LS_TIMESTAMPS( |
| 80 | //usage: "\n -c With -l: sort by ctime" |
| 81 | //usage: "\n -t With -l: sort by modification time" |
| 82 | //usage: "\n -u With -l: sort by access time" |
| 83 | //usage: ) |
| 84 | //usage: IF_SELINUX( |
| 85 | //usage: "\n -k List security context" |
| 86 | //usage: "\n -K List security context in long format" |
| 87 | //usage: "\n -Z List security context and permission" |
| 88 | //usage: ) |
| 89 | //usage: IF_FEATURE_AUTOWIDTH( |
| 90 | //usage: "\n -w N Assume the terminal is N columns wide" |
| 91 | //usage: ) |
| 92 | //usage: IF_FEATURE_LS_COLOR( |
| 93 | //usage: "\n --color[={always,never,auto}] Control coloring" |
| 94 | //usage: ) |
| 95 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 96 | #include "libbb.h" |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 97 | #include "unicode.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 100 | /* This is a NOEXEC applet. Be very careful! */ |
| 101 | |
Eric Andersen | f1142c5 | 2001-02-20 06:16:29 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 245f91b | 2009-03-09 22:37:23 +0000 | [diff] [blame] | 103 | #if ENABLE_FTPD |
| 104 | /* ftpd uses ls, and without timestamps Mozilla won't understand |
| 105 | * ftpd's LIST output. |
| 106 | */ |
| 107 | # undef CONFIG_FEATURE_LS_TIMESTAMPS |
| 108 | # undef ENABLE_FEATURE_LS_TIMESTAMPS |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 109 | # undef IF_FEATURE_LS_TIMESTAMPS |
| 110 | # undef IF_NOT_FEATURE_LS_TIMESTAMPS |
Denis Vlasenko | 245f91b | 2009-03-09 22:37:23 +0000 | [diff] [blame] | 111 | # define CONFIG_FEATURE_LS_TIMESTAMPS 1 |
| 112 | # define ENABLE_FEATURE_LS_TIMESTAMPS 1 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 113 | # define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__ |
| 114 | # define IF_NOT_FEATURE_LS_TIMESTAMPS(...) |
Denis Vlasenko | 245f91b | 2009-03-09 22:37:23 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
| 117 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 118 | enum { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 119 | TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 120 | |
| 121 | /* what is the overall style of the listing */ |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 122 | STYLE_COLUMNAR = 1 << 21, /* many records per line */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 123 | STYLE_LONG = 2 << 21, /* one record per line, extended info */ |
| 124 | STYLE_SINGLE = 3 << 21, /* one record per line */ |
| 125 | STYLE_MASK = STYLE_SINGLE, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 126 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 127 | /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */ |
| 128 | /* what file information will be listed */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 129 | LIST_INO = 1 << 0, |
| 130 | LIST_BLOCKS = 1 << 1, |
| 131 | LIST_MODEBITS = 1 << 2, |
| 132 | LIST_NLINKS = 1 << 3, |
| 133 | LIST_ID_NAME = 1 << 4, |
| 134 | LIST_ID_NUMERIC = 1 << 5, |
| 135 | LIST_CONTEXT = 1 << 6, |
| 136 | LIST_SIZE = 1 << 7, |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 137 | //LIST_DEV = 1 << 8, - unused, synonym to LIST_SIZE |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 138 | LIST_DATE_TIME = 1 << 9, |
| 139 | LIST_FULLTIME = 1 << 10, |
| 140 | LIST_FILENAME = 1 << 11, |
| 141 | LIST_SYMLINK = 1 << 12, |
| 142 | LIST_FILETYPE = 1 << 13, |
| 143 | LIST_EXEC = 1 << 14, |
| 144 | LIST_MASK = (LIST_EXEC << 1) - 1, |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 145 | |
| 146 | /* what files will be displayed */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 147 | DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */ |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 148 | DISP_HIDDEN = 1 << 16, /* show filenames starting with . */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 149 | DISP_DOT = 1 << 17, /* show . and .. */ |
| 150 | DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */ |
| 151 | DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */ |
| 152 | DISP_ROWS = 1 << 20, /* print across rows */ |
| 153 | DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1), |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 154 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 155 | /* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */ |
| 156 | SORT_FORWARD = 0, /* sort in reverse order */ |
| 157 | SORT_REVERSE = 1 << 27, /* sort in reverse order */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 159 | SORT_NAME = 0, /* sort by file name */ |
| 160 | SORT_SIZE = 1 << 28, /* sort by file size */ |
| 161 | SORT_ATIME = 2 << 28, /* sort by last access time */ |
| 162 | SORT_CTIME = 3 << 28, /* sort by last change time */ |
| 163 | SORT_MTIME = 4 << 28, /* sort by last modification time */ |
| 164 | SORT_VERSION = 5 << 28, /* sort by version */ |
| 165 | SORT_EXT = 6 << 28, /* sort by file name extension */ |
| 166 | SORT_DIR = 7 << 28, /* sort by file or directory */ |
| 167 | SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 168 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 169 | /* which of the three times will be used */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 170 | TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 171 | TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 172 | TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS, |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 174 | FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 176 | LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 177 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 178 | LIST_SHORT = LIST_FILENAME, |
| 179 | LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \ |
| 180 | LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK, |
| 181 | |
| 182 | SPLIT_DIR = 1, |
| 183 | SPLIT_FILE = 0, |
| 184 | SPLIT_SUBDIR = 2, |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 185 | }; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 187 | /* "[-]Cadil1", POSIX mandated options, busybox always supports */ |
| 188 | /* "[-]gnsx", POSIX non-mandated options, busybox always supports */ |
| 189 | /* "[-]Q" GNU option? busybox always supports */ |
| 190 | /* "[-]Ak" GNU options, busybox always supports */ |
| 191 | /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */ |
| 192 | /* "[-]p", POSIX non-mandated options, busybox optionally supports */ |
| 193 | /* "[-]SXvThw", GNU options, busybox optionally supports */ |
| 194 | /* "[-]K", SELinux mandated options, busybox optionally supports */ |
| 195 | /* "[-]e", I think we made this one up */ |
| 196 | static const char ls_options[] ALIGN1 = |
| 197 | "Cadil1gnsxQAk" /* 13 opts, total 13 */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 198 | IF_FEATURE_LS_TIMESTAMPS("cetu") /* 4, 17 */ |
| 199 | IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 21 */ |
| 200 | IF_FEATURE_LS_FILETYPES("Fp") /* 2, 23 */ |
| 201 | IF_FEATURE_LS_FOLLOWLINKS("L") /* 1, 24 */ |
| 202 | IF_FEATURE_LS_RECURSIVE("R") /* 1, 25 */ |
| 203 | IF_FEATURE_HUMAN_READABLE("h") /* 1, 26 */ |
Denys Vlasenko | 5508363 | 2009-07-02 14:25:51 +0200 | [diff] [blame] | 204 | IF_SELINUX("KZ") /* 2, 28 */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 205 | IF_FEATURE_AUTOWIDTH("T:w:") /* 2, 30 */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 206 | ; |
| 207 | enum { |
| 208 | //OPT_C = (1 << 0), |
| 209 | //OPT_a = (1 << 1), |
| 210 | //OPT_d = (1 << 2), |
| 211 | //OPT_i = (1 << 3), |
| 212 | //OPT_l = (1 << 4), |
| 213 | //OPT_1 = (1 << 5), |
| 214 | OPT_g = (1 << 6), |
| 215 | //OPT_n = (1 << 7), |
| 216 | //OPT_s = (1 << 8), |
| 217 | //OPT_x = (1 << 9), |
| 218 | OPT_Q = (1 << 10), |
| 219 | //OPT_A = (1 << 11), |
| 220 | //OPT_k = (1 << 12), |
Denys Vlasenko | 163d864 | 2010-12-19 06:16:28 +0100 | [diff] [blame] | 221 | OPTBIT_F = 13 |
Denys Vlasenko | 5508363 | 2009-07-02 14:25:51 +0200 | [diff] [blame] | 222 | + 4 * ENABLE_FEATURE_LS_TIMESTAMPS |
Denys Vlasenko | 163d864 | 2010-12-19 06:16:28 +0100 | [diff] [blame] | 223 | + 4 * ENABLE_FEATURE_LS_SORTFILES, |
| 224 | OPTBIT_color = OPTBIT_F |
Denys Vlasenko | 5508363 | 2009-07-02 14:25:51 +0200 | [diff] [blame] | 225 | + 2 * ENABLE_FEATURE_LS_FILETYPES |
| 226 | + 1 * ENABLE_FEATURE_LS_FOLLOWLINKS |
| 227 | + 1 * ENABLE_FEATURE_LS_RECURSIVE |
| 228 | + 1 * ENABLE_FEATURE_HUMAN_READABLE |
| 229 | + 2 * ENABLE_SELINUX |
| 230 | + 2 * ENABLE_FEATURE_AUTOWIDTH, |
Denys Vlasenko | 163d864 | 2010-12-19 06:16:28 +0100 | [diff] [blame] | 231 | OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES, |
Denys Vlasenko | 5508363 | 2009-07-02 14:25:51 +0200 | [diff] [blame] | 232 | OPT_color = 1 << OPTBIT_color, |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 235 | /* TODO: simple toggles may be stored as OPT_xxx bits instead */ |
| 236 | static const unsigned opt_flags[] = { |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 237 | LIST_SHORT | STYLE_COLUMNAR, /* C */ |
| 238 | DISP_HIDDEN | DISP_DOT, /* a */ |
| 239 | DISP_NOLIST, /* d */ |
| 240 | LIST_INO, /* i */ |
| 241 | LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */ |
| 242 | LIST_SHORT | STYLE_SINGLE, /* 1 */ |
| 243 | 0, /* g (don't show owner) - handled via OPT_g */ |
| 244 | LIST_ID_NUMERIC, /* n */ |
| 245 | LIST_BLOCKS, /* s */ |
| 246 | LIST_SHORT | DISP_ROWS | STYLE_COLUMNAR, /* x */ |
| 247 | 0, /* Q (quote filename) - handled via OPT_Q */ |
| 248 | DISP_HIDDEN, /* A */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 249 | ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */ |
| 250 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
| 251 | TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */ |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 252 | LIST_FULLTIME, /* e */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 253 | ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */ |
| 254 | TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */ |
| 255 | #endif |
| 256 | #if ENABLE_FEATURE_LS_SORTFILES |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 257 | SORT_SIZE, /* S */ |
| 258 | SORT_EXT, /* X */ |
| 259 | SORT_REVERSE, /* r */ |
| 260 | SORT_VERSION, /* v */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #if ENABLE_FEATURE_LS_FILETYPES |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 263 | LIST_FILETYPE | LIST_EXEC, /* F */ |
| 264 | LIST_FILETYPE, /* p */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 265 | #endif |
| 266 | #if ENABLE_FEATURE_LS_FOLLOWLINKS |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 267 | FOLLOW_LINKS, /* L */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #if ENABLE_FEATURE_LS_RECURSIVE |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 270 | DISP_RECURSIVE, /* R */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 271 | #endif |
| 272 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 273 | LS_DISP_HR, /* h */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 274 | #endif |
| 275 | #if ENABLE_SELINUX |
| 276 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ |
| 277 | #endif |
| 278 | #if ENABLE_SELINUX |
| 279 | LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */ |
| 280 | #endif |
| 281 | (1U<<31) |
| 282 | /* options after Z are not processed through opt_flags: |
| 283 | * T, w - ignored |
| 284 | */ |
| 285 | }; |
| 286 | |
| 287 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 288 | /* |
| 289 | * a directory entry and its stat info are stored here |
| 290 | */ |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 291 | struct dnode { |
| 292 | const char *name; /* the dir entry name */ |
| 293 | const char *fullname; /* the dir entry name */ |
| 294 | struct dnode *next; /* point at the next node */ |
| 295 | smallint fname_allocated; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 296 | struct stat dstat; /* the file stat info */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 297 | IF_SELINUX(security_context_t sid;) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 298 | }; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 299 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 300 | struct globals { |
| 301 | #if ENABLE_FEATURE_LS_COLOR |
| 302 | smallint show_color; |
| 303 | #endif |
Denis Vlasenko | 4a689e9 | 2008-06-18 16:38:22 +0000 | [diff] [blame] | 304 | smallint exit_code; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 305 | unsigned all_fmt; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 306 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 307 | unsigned terminal_width; // = TERMINAL_WIDTH; |
| 308 | #endif |
| 309 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
| 310 | /* Do time() just once. Saves one syscall per file for "ls -l" */ |
| 311 | time_t current_time_t; |
| 312 | #endif |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 313 | } FIX_ALIASING; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 314 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 315 | #if ENABLE_FEATURE_LS_COLOR |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 316 | # define show_color (G.show_color ) |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 317 | #else |
| 318 | enum { show_color = 0 }; |
| 319 | #endif |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 320 | #define exit_code (G.exit_code ) |
| 321 | #define all_fmt (G.all_fmt ) |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 322 | #if ENABLE_FEATURE_AUTOWIDTH |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 323 | # define terminal_width (G.terminal_width) |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 324 | #else |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 325 | enum { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 326 | terminal_width = TERMINAL_WIDTH, |
| 327 | }; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 328 | #endif |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 329 | #define current_time_t (G.current_time_t) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 330 | #define INIT_G() do { \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 331 | /* we have to zero it out because of NOEXEC */ \ |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 332 | memset(&G, 0, sizeof(G)); \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 333 | IF_FEATURE_AUTOWIDTH(terminal_width = TERMINAL_WIDTH;) \ |
| 334 | IF_FEATURE_LS_TIMESTAMPS(time(¤t_time_t);) \ |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 335 | } while (0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 336 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 338 | static struct dnode *my_stat(const char *fullname, const char *name, int force_follow) |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 339 | { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 340 | struct stat dstat; |
| 341 | struct dnode *cur; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 342 | IF_SELINUX(security_context_t sid = NULL;) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 2110aa9 | 2007-02-28 23:14:06 +0000 | [diff] [blame] | 344 | if ((all_fmt & FOLLOW_LINKS) || force_follow) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 345 | #if ENABLE_SELINUX |
Bernhard Reutner-Fischer | d2c306e | 2006-05-29 12:10:23 +0000 | [diff] [blame] | 346 | if (is_selinux_enabled()) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 347 | getfilecon(fullname, &sid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 348 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 349 | #endif |
Bernhard Reutner-Fischer | d2c306e | 2006-05-29 12:10:23 +0000 | [diff] [blame] | 350 | if (stat(fullname, &dstat)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 351 | bb_simple_perror_msg(fullname); |
Denis Vlasenko | 4a689e9 | 2008-06-18 16:38:22 +0000 | [diff] [blame] | 352 | exit_code = EXIT_FAILURE; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 353 | return 0; |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 354 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 355 | } else { |
| 356 | #if ENABLE_SELINUX |
| 357 | if (is_selinux_enabled()) { |
Denis Vlasenko | 2110aa9 | 2007-02-28 23:14:06 +0000 | [diff] [blame] | 358 | lgetfilecon(fullname, &sid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 359 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 360 | #endif |
Bernhard Reutner-Fischer | d2c306e | 2006-05-29 12:10:23 +0000 | [diff] [blame] | 361 | if (lstat(fullname, &dstat)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 362 | bb_simple_perror_msg(fullname); |
Denis Vlasenko | 4a689e9 | 2008-06-18 16:38:22 +0000 | [diff] [blame] | 363 | exit_code = EXIT_FAILURE; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 364 | return 0; |
| 365 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 366 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 367 | |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 368 | cur = xmalloc(sizeof(*cur)); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 369 | cur->fullname = fullname; |
| 370 | cur->name = name; |
| 371 | cur->dstat = dstat; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 372 | IF_SELINUX(cur->sid = sid;) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 373 | return cur; |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 376 | /* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket |
| 377 | * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file |
| 378 | * 3/7:multiplexed char/block device) |
| 379 | * and we use 0 for unknown and 15 for executables (see below) */ |
| 380 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 381 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
| 382 | #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) |
| 383 | /* 036 black foreground 050 black background |
| 384 | 037 red foreground 051 red background |
| 385 | 040 green foreground 052 green background |
| 386 | 041 brown foreground 053 brown background |
| 387 | 042 blue foreground 054 blue background |
| 388 | 043 magenta (purple) foreground 055 magenta background |
| 389 | 044 cyan (light blue) foreground 056 cyan background |
| 390 | 045 gray foreground 057 white background |
| 391 | */ |
| 392 | #define COLOR(mode) ( \ |
| 393 | /*un fi chr dir blk file link sock exe */ \ |
| 394 | "\037\043\043\045\042\045\043\043\000\045\044\045\043\045\045\040" \ |
| 395 | [TYPEINDEX(mode)]) |
| 396 | /* Select normal (0) [actually "reset all"] or bold (1) |
| 397 | * (other attributes are 2:dim 4:underline 5:blink 7:reverse, |
| 398 | * let's use 7 for "impossible" types, just for fun) |
| 399 | * Note: coreutils 6.9 uses inverted red for setuid binaries. |
| 400 | */ |
| 401 | #define ATTR(mode) ( \ |
| 402 | /*un fi chr dir blk file link sock exe */ \ |
| 403 | "\01\00\01\07\01\07\01\07\00\07\01\07\01\07\07\01" \ |
| 404 | [TYPEINDEX(mode)]) |
| 405 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 406 | #if ENABLE_FEATURE_LS_COLOR |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 407 | /* mode of zero is interpreted as "unknown" (stat failed) */ |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 408 | static char fgcolor(mode_t mode) |
| 409 | { |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 410 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 411 | return COLOR(0xF000); /* File is executable ... */ |
| 412 | return COLOR(mode); |
| 413 | } |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 414 | static char bold(mode_t mode) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 415 | { |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 416 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 417 | return ATTR(0xF000); /* File is executable ... */ |
| 418 | return ATTR(mode); |
| 419 | } |
| 420 | #endif |
| 421 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 422 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 423 | static char append_char(mode_t mode) |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 424 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 425 | if (!(all_fmt & LIST_FILETYPE)) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 426 | return '\0'; |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 427 | if (S_ISDIR(mode)) |
| 428 | return '/'; |
| 429 | if (!(all_fmt & LIST_EXEC)) |
| 430 | return '\0'; |
| 431 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 432 | return '*'; |
| 433 | return APPCHAR(mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 434 | } |
| 435 | #endif |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 436 | |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 437 | static unsigned count_dirs(struct dnode **dn, int which) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 438 | { |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 439 | unsigned dirs, all; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 440 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 441 | if (!dn) |
| 442 | return 0; |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 443 | |
| 444 | dirs = all = 0; |
| 445 | for (; *dn; dn++) { |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 446 | const char *name; |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 447 | |
| 448 | all++; |
| 449 | if (!S_ISDIR((*dn)->dstat.st_mode)) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 450 | continue; |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 451 | name = (*dn)->name; |
| 452 | if (which != SPLIT_SUBDIR /* if not requested to skip . / .. */ |
| 453 | /* or if it's not . or .. */ |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 454 | || name[0] != '.' || (name[1] && (name[1] != '.' || name[2])) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 455 | ) { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 456 | dirs++; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 457 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 458 | } |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 459 | return which != SPLIT_FILE ? dirs : all - dirs; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 462 | /* get memory to hold an array of pointers */ |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 463 | static struct dnode **dnalloc(unsigned num) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 464 | { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 465 | if (num < 1) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 466 | return NULL; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 467 | |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 468 | num++; /* so that we have terminating NULL */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 469 | return xzalloc(num * sizeof(struct dnode *)); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 472 | #if ENABLE_FEATURE_LS_RECURSIVE |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 473 | static void dfree(struct dnode **dnp) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 474 | { |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 475 | unsigned i; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 476 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 477 | if (dnp == NULL) |
| 478 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 479 | |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 480 | for (i = 0; dnp[i]; i++) { |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 481 | struct dnode *cur = dnp[i]; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 482 | if (cur->fname_allocated) |
Denys Vlasenko | ffd4774 | 2009-10-03 11:42:33 +0200 | [diff] [blame] | 483 | free((char*)cur->fullname); |
| 484 | free(cur); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 485 | } |
Denys Vlasenko | ffd4774 | 2009-10-03 11:42:33 +0200 | [diff] [blame] | 486 | free(dnp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 487 | } |
Rob Landley | c44bc98 | 2006-05-28 01:19:06 +0000 | [diff] [blame] | 488 | #else |
Denis Vlasenko | 2405ad6 | 2007-01-19 21:24:17 +0000 | [diff] [blame] | 489 | #define dfree(...) ((void)0) |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 490 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 491 | |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 492 | /* Returns NULL-terminated malloced vector of pointers (or NULL) */ |
| 493 | static struct dnode **splitdnarray(struct dnode **dn, int which) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 494 | { |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 495 | unsigned dncnt, d; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 496 | struct dnode **dnp; |
| 497 | |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 498 | if (dn == NULL) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 499 | return NULL; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 500 | |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 501 | /* count how many dirs or files there are */ |
| 502 | dncnt = count_dirs(dn, which); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 503 | |
| 504 | /* allocate a file array and a dir array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 505 | dnp = dnalloc(dncnt); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 506 | |
| 507 | /* copy the entrys into the file or dir array */ |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 508 | for (d = 0; *dn; dn++) { |
| 509 | if (S_ISDIR((*dn)->dstat.st_mode)) { |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 510 | const char *name; |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 511 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 512 | if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) |
| 513 | continue; |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 514 | name = (*dn)->name; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 515 | if ((which & SPLIT_DIR) |
| 516 | || name[0]!='.' || (name[1] && (name[1]!='.' || name[2])) |
| 517 | ) { |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 518 | dnp[d++] = *dn; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 519 | } |
| 520 | } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) { |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 521 | dnp[d++] = *dn; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 524 | return dnp; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 527 | #if ENABLE_FEATURE_LS_SORTFILES |
Rob Landley | 425e758 | 2006-05-03 20:22:03 +0000 | [diff] [blame] | 528 | static int sortcmp(const void *a, const void *b) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 529 | { |
Rob Landley | 425e758 | 2006-05-03 20:22:03 +0000 | [diff] [blame] | 530 | struct dnode *d1 = *(struct dnode **)a; |
| 531 | struct dnode *d2 = *(struct dnode **)b; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 532 | unsigned sort_opts = all_fmt & SORT_MASK; |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 533 | off_t dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 534 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 535 | dif = 0; /* assume SORT_NAME */ |
| 536 | // TODO: use pre-initialized function pointer |
| 537 | // instead of branch forest |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 538 | if (sort_opts == SORT_SIZE) { |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 539 | dif = (d2->dstat.st_size - d1->dstat.st_size); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 540 | } else if (sort_opts == SORT_ATIME) { |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 541 | dif = (d2->dstat.st_atime - d1->dstat.st_atime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 542 | } else if (sort_opts == SORT_CTIME) { |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 543 | dif = (d2->dstat.st_ctime - d1->dstat.st_ctime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 544 | } else if (sort_opts == SORT_MTIME) { |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 545 | dif = (d2->dstat.st_mtime - d1->dstat.st_mtime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 546 | } else if (sort_opts == SORT_DIR) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 547 | dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 548 | /* } else if (sort_opts == SORT_VERSION) { */ |
| 549 | /* } else if (sort_opts == SORT_EXT) { */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 550 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 551 | if (dif == 0) { |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 552 | /* sort by name, or tie_breaker for other sorts */ |
| 553 | if (ENABLE_LOCALE_SUPPORT) |
| 554 | dif = strcoll(d1->name, d2->name); |
| 555 | else |
| 556 | dif = strcmp(d1->name, d2->name); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 559 | /* Make dif fit into an int */ |
| 560 | if (sizeof(dif) > sizeof(int)) { |
Denys Vlasenko | 6b01b71 | 2010-01-24 22:52:21 +0100 | [diff] [blame] | 561 | enum { BITS_TO_SHIFT = 8 * (sizeof(dif) - sizeof(int)) }; |
| 562 | /* shift leaving only "int" worth of bits */ |
| 563 | if (dif != 0) { |
| 564 | dif = 1 | (int)((uoff_t)dif >> BITS_TO_SHIFT); |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 565 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 566 | } |
Denys Vlasenko | 7be97c5 | 2010-01-18 13:02:27 +0100 | [diff] [blame] | 567 | |
| 568 | return (all_fmt & SORT_REVERSE) ? -(int)dif : (int)dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Rob Landley | 425e758 | 2006-05-03 20:22:03 +0000 | [diff] [blame] | 571 | static void dnsort(struct dnode **dn, int size) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 572 | { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 573 | qsort(dn, size, sizeof(*dn), sortcmp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 574 | } |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 575 | #else |
Denis Vlasenko | 2405ad6 | 2007-01-19 21:24:17 +0000 | [diff] [blame] | 576 | #define dnsort(dn, size) ((void)0) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 577 | #endif |
| 578 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 579 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 580 | static unsigned calc_name_len(const char *name) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 581 | { |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 582 | unsigned len; |
| 583 | uni_stat_t uni_stat; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 584 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 585 | // TODO: quote tab as \t, etc, if -Q |
| 586 | name = printable_string(&uni_stat, name); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 587 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 588 | if (!(option_mask32 & OPT_Q)) { |
| 589 | return uni_stat.unicode_width; |
| 590 | } |
| 591 | |
| 592 | len = 2 + uni_stat.unicode_width; |
| 593 | while (*name) { |
| 594 | if (*name == '"' || *name == '\\') { |
| 595 | len++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 596 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 597 | name++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 598 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 599 | return len; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 602 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 603 | /* Return the number of used columns. |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 604 | * Note that only STYLE_COLUMNAR uses return value. |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 605 | * STYLE_SINGLE and STYLE_LONG don't care. |
| 606 | * coreutils 7.2 also supports: |
| 607 | * ls -b (--escape) = octal escapes (although it doesn't look like working) |
| 608 | * ls -N (--literal) = not escape at all |
Denys Vlasenko | 0683d4d | 2009-10-03 10:53:36 +0200 | [diff] [blame] | 609 | */ |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 610 | static unsigned print_name(const char *name) |
Denys Vlasenko | 87c150c | 2009-10-03 01:14:15 +0200 | [diff] [blame] | 611 | { |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 612 | unsigned len; |
| 613 | uni_stat_t uni_stat; |
| 614 | |
| 615 | // TODO: quote tab as \t, etc, if -Q |
| 616 | name = printable_string(&uni_stat, name); |
| 617 | |
| 618 | if (!(option_mask32 & OPT_Q)) { |
| 619 | fputs(name, stdout); |
| 620 | return uni_stat.unicode_width; |
Denys Vlasenko | 87c150c | 2009-10-03 01:14:15 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 623 | len = 2 + uni_stat.unicode_width; |
| 624 | putchar('"'); |
| 625 | while (*name) { |
| 626 | if (*name == '"' || *name == '\\') { |
| 627 | putchar('\\'); |
| 628 | len++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 629 | } |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 630 | putchar(*name); |
| 631 | name++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 632 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 633 | putchar('"'); |
| 634 | return len; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 637 | /* Return the number of used columns. |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 638 | * Note that only STYLE_COLUMNAR uses return value, |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 639 | * STYLE_SINGLE and STYLE_LONG don't care. |
| 640 | */ |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 641 | static NOINLINE unsigned list_single(const struct dnode *dn) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 642 | { |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 643 | unsigned column = 0; |
Denis Vlasenko | 4d3a812 | 2009-03-27 17:22:00 +0000 | [diff] [blame] | 644 | char *lpath = lpath; /* for compiler */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 645 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 646 | struct stat info; |
| 647 | char append; |
| 648 | #endif |
| 649 | |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 650 | /* Never happens: |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 651 | if (dn->fullname == NULL) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 652 | return 0; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 653 | */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 654 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 655 | #if ENABLE_FEATURE_LS_FILETYPES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 656 | append = append_char(dn->dstat.st_mode); |
| 657 | #endif |
| 658 | |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 659 | /* Do readlink early, so that if it fails, error message |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 660 | * does not appear *inside* the "ls -l" line */ |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 661 | if (all_fmt & LIST_SYMLINK) |
| 662 | if (S_ISLNK(dn->dstat.st_mode)) |
| 663 | lpath = xmalloc_readlink_or_warn(dn->fullname); |
| 664 | |
| 665 | if (all_fmt & LIST_INO) |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 666 | column += printf("%7llu ", (long long) dn->dstat.st_ino); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 667 | if (all_fmt & LIST_BLOCKS) |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 668 | column += printf("%4"OFF_FMT"u ", (off_t) (dn->dstat.st_blocks >> 1)); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 669 | if (all_fmt & LIST_MODEBITS) |
| 670 | column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); |
| 671 | if (all_fmt & LIST_NLINKS) |
| 672 | column += printf("%4lu ", (long) dn->dstat.st_nlink); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 673 | #if ENABLE_FEATURE_LS_USERNAME |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 674 | if (all_fmt & LIST_ID_NAME) { |
| 675 | if (option_mask32 & OPT_g) { |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 676 | column += printf("%-8.8s ", |
Denys Vlasenko | 4909fec | 2010-11-06 00:46:57 +0100 | [diff] [blame] | 677 | get_cached_groupname(dn->dstat.st_gid)); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 678 | } else { |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 679 | column += printf("%-8.8s %-8.8s ", |
Denis Vlasenko | 2405ad6 | 2007-01-19 21:24:17 +0000 | [diff] [blame] | 680 | get_cached_username(dn->dstat.st_uid), |
| 681 | get_cached_groupname(dn->dstat.st_gid)); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 684 | #endif |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 685 | if (all_fmt & LIST_ID_NUMERIC) { |
| 686 | if (option_mask32 & OPT_g) |
Denys Vlasenko | 4909fec | 2010-11-06 00:46:57 +0100 | [diff] [blame] | 687 | column += printf("%-8u ", (int) dn->dstat.st_gid); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 688 | else |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 689 | column += printf("%-8u %-8u ", |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 690 | (int) dn->dstat.st_uid, |
| 691 | (int) dn->dstat.st_gid); |
| 692 | } |
| 693 | if (all_fmt & (LIST_SIZE /*|LIST_DEV*/ )) { |
| 694 | if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) { |
| 695 | column += printf("%4u, %3u ", |
| 696 | (int) major(dn->dstat.st_rdev), |
| 697 | (int) minor(dn->dstat.st_rdev)); |
| 698 | } else { |
| 699 | if (all_fmt & LS_DISP_HR) { |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 700 | column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ", |
Denys Vlasenko | 0bf44d0 | 2009-10-13 01:25:09 +0200 | [diff] [blame] | 701 | /* print st_size, show one fractional, use suffixes */ |
| 702 | make_human_readable_str(dn->dstat.st_size, 1, 0) |
| 703 | ); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 704 | } else { |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 705 | column += printf("%9"OFF_FMT"u ", (off_t) dn->dstat.st_size); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 706 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 709 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 710 | if (all_fmt & (LIST_FULLTIME|LIST_DATE_TIME)) { |
| 711 | char *filetime; |
| 712 | time_t ttime = dn->dstat.st_mtime; |
| 713 | if (all_fmt & TIME_ACCESS) |
| 714 | ttime = dn->dstat.st_atime; |
| 715 | if (all_fmt & TIME_CHANGE) |
| 716 | ttime = dn->dstat.st_ctime; |
| 717 | filetime = ctime(&ttime); |
| 718 | /* filetime's format: "Wed Jun 30 21:49:08 1993\n" */ |
| 719 | if (all_fmt & LIST_FULLTIME) |
| 720 | column += printf("%.24s ", filetime); |
| 721 | else { /* LIST_DATE_TIME */ |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 722 | /* current_time_t ~== time(NULL) */ |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 723 | time_t age = current_time_t - ttime; |
| 724 | printf("%.6s ", filetime + 4); /* "Jun 30" */ |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 725 | if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) { |
| 726 | /* hh:mm if less than 6 months old */ |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 727 | printf("%.5s ", filetime + 11); |
| 728 | } else { /* year. buggy if year > 9999 ;) */ |
| 729 | printf(" %.4s ", filetime + 20); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 730 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 731 | column += 13; |
| 732 | } |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 733 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 734 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 735 | #if ENABLE_SELINUX |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 736 | if (all_fmt & LIST_CONTEXT) { |
| 737 | column += printf("%-32s ", dn->sid ? dn->sid : "unknown"); |
| 738 | freecon(dn->sid); |
| 739 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 740 | #endif |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 741 | if (all_fmt & LIST_FILENAME) { |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 742 | #if ENABLE_FEATURE_LS_COLOR |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 743 | if (show_color) { |
| 744 | info.st_mode = 0; /* for fgcolor() */ |
| 745 | lstat(dn->fullname, &info); |
| 746 | printf("\033[%u;%um", bold(info.st_mode), |
| 747 | fgcolor(info.st_mode)); |
| 748 | } |
| 749 | #endif |
| 750 | column += print_name(dn->name); |
| 751 | if (show_color) { |
| 752 | printf("\033[0m"); |
| 753 | } |
| 754 | } |
| 755 | if (all_fmt & LIST_SYMLINK) { |
| 756 | if (S_ISLNK(dn->dstat.st_mode) && lpath) { |
| 757 | printf(" -> "); |
| 758 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
| 759 | #if ENABLE_FEATURE_LS_COLOR |
| 760 | info.st_mode = 0; /* for fgcolor() */ |
| 761 | #endif |
| 762 | if (stat(dn->fullname, &info) == 0) { |
| 763 | append = append_char(info.st_mode); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 764 | } |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 765 | #endif |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 766 | #if ENABLE_FEATURE_LS_COLOR |
| 767 | if (show_color) { |
| 768 | printf("\033[%u;%um", bold(info.st_mode), |
| 769 | fgcolor(info.st_mode)); |
| 770 | } |
| 771 | #endif |
| 772 | column += print_name(lpath) + 4; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 773 | if (show_color) { |
| 774 | printf("\033[0m"); |
| 775 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 776 | free(lpath); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 779 | #if ENABLE_FEATURE_LS_FILETYPES |
| 780 | if (all_fmt & LIST_FILETYPE) { |
| 781 | if (append) { |
| 782 | putchar(append); |
| 783 | column++; |
| 784 | } |
| 785 | } |
| 786 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 787 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 788 | return column; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 791 | static void showfiles(struct dnode **dn, unsigned nfiles) |
| 792 | { |
| 793 | unsigned i, ncols, nrows, row, nc; |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame^] | 794 | unsigned column; |
| 795 | unsigned nexttab; |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 796 | unsigned column_width = 0; /* used only by STYLE_COLUMNAR */ |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 797 | |
| 798 | if (all_fmt & STYLE_LONG) { /* STYLE_LONG or STYLE_SINGLE */ |
| 799 | ncols = 1; |
| 800 | } else { |
| 801 | /* find the longest file name, use that as the column width */ |
| 802 | for (i = 0; dn[i]; i++) { |
| 803 | int len = calc_name_len(dn[i]->name); |
| 804 | if (column_width < len) |
| 805 | column_width = len; |
| 806 | } |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame^] | 807 | column_width += 1 + |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 808 | IF_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + ) |
| 809 | ((all_fmt & LIST_INO) ? 8 : 0) + |
| 810 | ((all_fmt & LIST_BLOCKS) ? 5 : 0); |
| 811 | ncols = (int) (terminal_width / column_width); |
| 812 | } |
| 813 | |
| 814 | if (ncols > 1) { |
| 815 | nrows = nfiles / ncols; |
| 816 | if (nrows * ncols < nfiles) |
| 817 | nrows++; /* round up fractionals */ |
| 818 | } else { |
| 819 | nrows = nfiles; |
| 820 | ncols = 1; |
| 821 | } |
| 822 | |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame^] | 823 | column = 0; |
| 824 | nexttab = 0; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 825 | for (row = 0; row < nrows; row++) { |
| 826 | for (nc = 0; nc < ncols; nc++) { |
| 827 | /* reach into the array based on the column and row */ |
| 828 | if (all_fmt & DISP_ROWS) |
| 829 | i = (row * ncols) + nc; /* display across row */ |
| 830 | else |
| 831 | i = (nc * nrows) + row; /* display by column */ |
| 832 | if (i < nfiles) { |
| 833 | if (column > 0) { |
| 834 | nexttab -= column; |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame^] | 835 | printf("%*s ", nexttab, ""); |
| 836 | column += nexttab + 1; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 837 | } |
| 838 | nexttab = column + column_width; |
| 839 | column += list_single(dn[i]); |
| 840 | } |
| 841 | } |
| 842 | putchar('\n'); |
| 843 | column = 0; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | |
| 848 | #if ENABLE_DESKTOP |
| 849 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/ls.html |
| 850 | * If any of the -l, -n, -s options is specified, each list |
| 851 | * of files within the directory shall be preceded by a |
| 852 | * status line indicating the number of file system blocks |
| 853 | * occupied by files in the directory in 512-byte units if |
| 854 | * the -k option is not specified, or 1024-byte units if the |
| 855 | * -k option is specified, rounded up to the next integral |
| 856 | * number of units. |
| 857 | */ |
| 858 | /* by Jorgen Overgaard (jorgen AT antistaten.se) */ |
| 859 | static off_t calculate_blocks(struct dnode **dn) |
| 860 | { |
| 861 | uoff_t blocks = 1; |
| 862 | if (dn) { |
| 863 | while (*dn) { |
| 864 | /* st_blocks is in 512 byte blocks */ |
| 865 | blocks += (*dn)->dstat.st_blocks; |
| 866 | dn++; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* Even though standard says use 512 byte blocks, coreutils use 1k */ |
| 871 | /* Actually, we round up by calculating (blocks + 1) / 2, |
| 872 | * "+ 1" was done when we initialized blocks to 1 */ |
| 873 | return blocks >> 1; |
| 874 | } |
| 875 | #endif |
| 876 | |
| 877 | |
| 878 | static struct dnode **list_dir(const char *, unsigned *); |
| 879 | |
| 880 | static void showdirs(struct dnode **dn, int first) |
| 881 | { |
| 882 | unsigned nfiles; |
| 883 | unsigned dndirs; |
| 884 | struct dnode **subdnp; |
| 885 | struct dnode **dnd; |
| 886 | |
| 887 | /* Never happens: |
| 888 | if (dn == NULL || ndirs < 1) { |
| 889 | return; |
| 890 | } |
| 891 | */ |
| 892 | |
| 893 | for (; *dn; dn++) { |
| 894 | if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) { |
| 895 | if (!first) |
| 896 | bb_putchar('\n'); |
| 897 | first = 0; |
| 898 | printf("%s:\n", (*dn)->fullname); |
| 899 | } |
| 900 | subdnp = list_dir((*dn)->fullname, &nfiles); |
| 901 | #if ENABLE_DESKTOP |
| 902 | if ((all_fmt & STYLE_MASK) == STYLE_LONG) |
| 903 | printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp)); |
| 904 | #endif |
| 905 | if (nfiles > 0) { |
| 906 | /* list all files at this level */ |
| 907 | dnsort(subdnp, nfiles); |
| 908 | showfiles(subdnp, nfiles); |
| 909 | if (ENABLE_FEATURE_LS_RECURSIVE |
| 910 | && (all_fmt & DISP_RECURSIVE) |
| 911 | ) { |
| 912 | /* recursive - list the sub-dirs */ |
| 913 | dnd = splitdnarray(subdnp, SPLIT_SUBDIR); |
| 914 | dndirs = count_dirs(subdnp, SPLIT_SUBDIR); |
| 915 | if (dndirs > 0) { |
| 916 | dnsort(dnd, dndirs); |
| 917 | showdirs(dnd, 0); |
| 918 | /* free the array of dnode pointers to the dirs */ |
| 919 | free(dnd); |
| 920 | } |
| 921 | } |
| 922 | /* free the dnodes and the fullname mem */ |
| 923 | dfree(subdnp); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | |
| 929 | /* Returns NULL-terminated malloced vector of pointers (or NULL) */ |
| 930 | static struct dnode **list_dir(const char *path, unsigned *nfiles_p) |
| 931 | { |
| 932 | struct dnode *dn, *cur, **dnp; |
| 933 | struct dirent *entry; |
| 934 | DIR *dir; |
| 935 | unsigned i, nfiles; |
| 936 | |
| 937 | /* Never happens: |
| 938 | if (path == NULL) |
| 939 | return NULL; |
| 940 | */ |
| 941 | |
| 942 | *nfiles_p = 0; |
| 943 | dir = warn_opendir(path); |
| 944 | if (dir == NULL) { |
| 945 | exit_code = EXIT_FAILURE; |
| 946 | return NULL; /* could not open the dir */ |
| 947 | } |
| 948 | dn = NULL; |
| 949 | nfiles = 0; |
| 950 | while ((entry = readdir(dir)) != NULL) { |
| 951 | char *fullname; |
| 952 | |
| 953 | /* are we going to list the file- it may be . or .. or a hidden file */ |
| 954 | if (entry->d_name[0] == '.') { |
| 955 | if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2])) |
| 956 | && !(all_fmt & DISP_DOT) |
| 957 | ) { |
| 958 | continue; |
| 959 | } |
| 960 | if (!(all_fmt & DISP_HIDDEN)) |
| 961 | continue; |
| 962 | } |
| 963 | fullname = concat_path_file(path, entry->d_name); |
| 964 | cur = my_stat(fullname, bb_basename(fullname), 0); |
| 965 | if (!cur) { |
| 966 | free(fullname); |
| 967 | continue; |
| 968 | } |
| 969 | cur->fname_allocated = 1; |
| 970 | cur->next = dn; |
| 971 | dn = cur; |
| 972 | nfiles++; |
| 973 | } |
| 974 | closedir(dir); |
| 975 | |
| 976 | if (dn == NULL) |
| 977 | return NULL; |
| 978 | |
| 979 | /* now that we know how many files there are |
| 980 | * allocate memory for an array to hold dnode pointers |
| 981 | */ |
| 982 | *nfiles_p = nfiles; |
| 983 | dnp = dnalloc(nfiles); |
| 984 | for (i = 0; /* i < nfiles - detected via !dn below */; i++) { |
| 985 | dnp[i] = dn; /* save pointer to node in array */ |
| 986 | dn = dn->next; |
| 987 | if (!dn) |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | return dnp; |
| 992 | } |
| 993 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 994 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 995 | int ls_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 996 | { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 997 | struct dnode **dnd; |
| 998 | struct dnode **dnf; |
| 999 | struct dnode **dnp; |
| 1000 | struct dnode *dn; |
| 1001 | struct dnode *cur; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 1002 | unsigned opt; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1003 | unsigned nfiles; |
| 1004 | unsigned dnfiles; |
| 1005 | unsigned dndirs; |
| 1006 | unsigned i; |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1007 | #if ENABLE_FEATURE_LS_COLOR |
| 1008 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ |
| 1009 | /* coreutils 6.10: |
| 1010 | * # ls --color=BOGUS |
| 1011 | * ls: invalid argument 'BOGUS' for '--color' |
| 1012 | * Valid arguments are: |
| 1013 | * 'always', 'yes', 'force' |
| 1014 | * 'never', 'no', 'none' |
| 1015 | * 'auto', 'tty', 'if-tty' |
| 1016 | * (and substrings: "--color=alwa" work too) |
| 1017 | */ |
| 1018 | static const char ls_longopts[] ALIGN1 = |
| 1019 | "color\0" Optional_argument "\xff"; /* no short equivalent */ |
| 1020 | static const char color_str[] ALIGN1 = |
| 1021 | "always\0""yes\0""force\0" |
| 1022 | "auto\0""tty\0""if-tty\0"; |
Denis Vlasenko | 51f1b6c | 2008-07-15 05:21:47 +0000 | [diff] [blame] | 1023 | /* need to initialize since --color has _an optional_ argument */ |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1024 | const char *color_opt = color_str; /* "always" */ |
| 1025 | #endif |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1026 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1027 | INIT_G(); |
Denis Vlasenko | e055443 | 2007-01-19 22:03:06 +0000 | [diff] [blame] | 1028 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 1029 | init_unicode(); |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 1030 | |
Rob Landley | 2b8a05a | 2006-06-20 17:43:01 +0000 | [diff] [blame] | 1031 | all_fmt = LIST_SHORT | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 1032 | (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD)); |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1033 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1034 | #if ENABLE_FEATURE_AUTOWIDTH |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1035 | /* obtain the terminal width */ |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 1036 | get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL); |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1037 | /* go one less... */ |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 1038 | terminal_width--; |
Eric Andersen | 6d68781 | 2003-11-04 23:16:48 +0000 | [diff] [blame] | 1039 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1040 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1041 | /* process options */ |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1042 | IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;) |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1043 | opt_complementary = |
| 1044 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: |
| 1045 | * in some pairs of opts, only last one takes effect: |
| 1046 | */ |
| 1047 | IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES("t-S:S-t")) /* time/size */ |
| 1048 | // ":H-L:L-H:" - we don't have -H |
| 1049 | // ":m-l:l-m:" - we don't have -m |
| 1050 | ":C-xl:x-Cl:l-xC" /* bycols/bylines/long */ |
| 1051 | ":C-1:1-C" /* bycols/oneline */ |
| 1052 | ":x-1:1-x" /* bylines/oneline (not in SuS, but in GNU coreutils 8.4) */ |
| 1053 | ":c-u:u-c" /* mtime/atime */ |
| 1054 | /* -T NUM, -w NUM: */ |
| 1055 | IF_FEATURE_AUTOWIDTH(":T+:w+"); |
| 1056 | opt = getopt32(argv, ls_options |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame^] | 1057 | IF_FEATURE_AUTOWIDTH(, NULL, &terminal_width) |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1058 | IF_FEATURE_LS_COLOR(, &color_opt) |
| 1059 | ); |
Eric Andersen | d07cf59 | 2004-02-05 13:52:03 +0000 | [diff] [blame] | 1060 | for (i = 0; opt_flags[i] != (1U<<31); i++) { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1061 | if (opt & (1 << i)) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1062 | unsigned flags = opt_flags[i]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1063 | |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 1064 | if (flags & STYLE_MASK) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1065 | all_fmt &= ~STYLE_MASK; |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 1066 | if (flags & SORT_MASK) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1067 | all_fmt &= ~SORT_MASK; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1068 | if (flags & TIME_MASK) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1069 | all_fmt &= ~TIME_MASK; |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 1070 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1071 | if (flags & LIST_CONTEXT) |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 1072 | all_fmt |= STYLE_SINGLE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1073 | all_fmt |= flags; |
| 1074 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1077 | #if ENABLE_FEATURE_LS_COLOR |
| 1078 | /* find color bit value - last position for short getopt */ |
| 1079 | if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) { |
| 1080 | char *p = getenv("LS_COLORS"); |
| 1081 | /* LS_COLORS is unset, or (not empty && not "none") ? */ |
Denis Vlasenko | 51f1b6c | 2008-07-15 05:21:47 +0000 | [diff] [blame] | 1082 | if (!p || (p[0] && strcmp(p, "none") != 0)) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1083 | show_color = 1; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1084 | } |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1085 | if (opt & OPT_color) { |
| 1086 | if (color_opt[0] == 'n') |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1087 | show_color = 0; |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1088 | else switch (index_in_substrings(color_str, color_opt)) { |
| 1089 | case 3: |
| 1090 | case 4: |
| 1091 | case 5: |
| 1092 | if (isatty(STDOUT_FILENO)) { |
| 1093 | case 0: |
| 1094 | case 1: |
| 1095 | case 2: |
| 1096 | show_color = 1; |
| 1097 | } |
| 1098 | } |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1099 | } |
| 1100 | #endif |
| 1101 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1102 | /* sort out which command line options take precedence */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1103 | if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1104 | all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 1105 | if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { |
| 1106 | if (all_fmt & TIME_CHANGE) |
| 1107 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME; |
| 1108 | if (all_fmt & TIME_ACCESS) |
| 1109 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME; |
| 1110 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1111 | if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */ |
| 1112 | all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1113 | if (ENABLE_FEATURE_LS_USERNAME) |
| 1114 | if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC)) |
| 1115 | all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1116 | |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1117 | /* choose a display format if one was not already specified by an option */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 1118 | if (!(all_fmt & STYLE_MASK)) |
Denys Vlasenko | d87815d | 2010-12-19 05:43:44 +0100 | [diff] [blame] | 1119 | all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNAR : STYLE_SINGLE); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1120 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1121 | argv += optind; |
| 1122 | if (!argv[0]) |
| 1123 | *--argv = (char*)"."; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1124 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1125 | if (argv[1]) |
| 1126 | all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1127 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1128 | /* stuff the command line file names into a dnode array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1129 | dn = NULL; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1130 | nfiles = 0; |
| 1131 | do { |
Denys Vlasenko | 163d864 | 2010-12-19 06:16:28 +0100 | [diff] [blame] | 1132 | /* NB: follow links on command line unless -l, -s or -F */ |
| 1133 | cur = my_stat(*argv, *argv, |
| 1134 | !((all_fmt & (STYLE_LONG|LIST_BLOCKS)) || (option_mask32 & OPT_F)) |
| 1135 | ); |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1136 | argv++; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 1137 | if (!cur) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1138 | continue; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1139 | cur->fname_allocated = 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1140 | cur->next = dn; |
| 1141 | dn = cur; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1142 | nfiles++; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1143 | } while (*argv); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1144 | |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1145 | /* nfiles _may_ be 0 here - try "ls doesnt_exist" */ |
| 1146 | if (nfiles == 0) |
| 1147 | return exit_code; |
| 1148 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1149 | /* now that we know how many files there are |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 1150 | * allocate memory for an array to hold dnode pointers |
| 1151 | */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1152 | dnp = dnalloc(nfiles); |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1153 | for (i = 0; /* i < nfiles - detected via !dn below */; i++) { |
| 1154 | dnp[i] = dn; /* save pointer to node in array */ |
| 1155 | dn = dn->next; |
| 1156 | if (!dn) |
| 1157 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1160 | if (all_fmt & DISP_NOLIST) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 1161 | dnsort(dnp, nfiles); |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1162 | showfiles(dnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1163 | } else { |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 1164 | dnd = splitdnarray(dnp, SPLIT_DIR); |
| 1165 | dnf = splitdnarray(dnp, SPLIT_FILE); |
| 1166 | dndirs = count_dirs(dnp, SPLIT_DIR); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1167 | dnfiles = nfiles - dndirs; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1168 | if (dnfiles > 0) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 1169 | dnsort(dnf, dnfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1170 | showfiles(dnf, dnfiles); |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 1171 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1172 | free(dnf); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1173 | } |
| 1174 | if (dndirs > 0) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 1175 | dnsort(dnd, dndirs); |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 1176 | showdirs(dnd, dnfiles == 0); |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 1177 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1178 | free(dnd); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 1181 | if (ENABLE_FEATURE_CLEAN_UP) |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 1182 | dfree(dnp); |
Denis Vlasenko | 4a689e9 | 2008-06-18 16:38:22 +0000 | [diff] [blame] | 1183 | return exit_code; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1184 | } |