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