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