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