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 | * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com> |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 7 | /* [date unknown. Perhaps before year 2000] |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | * To achieve a small memory footprint, this version of 'ls' doesn't do any |
| 9 | * file sorting, and only has the most essential command line switches |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 10 | * (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] | 11 | * linking in substantial chunks of libc can be disabled. |
| 12 | * |
| 13 | * Although I don't really want to add new features to this program to |
| 14 | * keep it small, I *am* interested to receive bug fixes and ways to make |
| 15 | * it more portable. |
| 16 | * |
| 17 | * KNOWN BUGS: |
Denys Vlasenko | 87c150c | 2009-10-03 01:14:15 +0200 | [diff] [blame] | 18 | * 1. hidden files can make column width too large |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 19 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | * NON-OPTIMAL BEHAVIOUR: |
| 21 | * 1. autowidth reads directories twice |
| 22 | * 2. if you do a short directory listing without filetype characters |
| 23 | * appended, there's no need to stat each one |
| 24 | * PORTABILITY: |
| 25 | * 1. requires lstat (BSD) - how do you do it without? |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 26 | * |
| 27 | * [2009-03] |
| 28 | * ls sorts listing now, and supports almost all options. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 30 | //config:config LS |
| 31 | //config: bool "ls" |
| 32 | //config: default y |
| 33 | //config: help |
| 34 | //config: ls is used to list the contents of directories. |
| 35 | //config: |
| 36 | //config:config FEATURE_LS_FILETYPES |
| 37 | //config: bool "Enable filetyping options (-p and -F)" |
| 38 | //config: default y |
| 39 | //config: depends on LS |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 40 | //config: |
| 41 | //config:config FEATURE_LS_FOLLOWLINKS |
| 42 | //config: bool "Enable symlinks dereferencing (-L)" |
| 43 | //config: default y |
| 44 | //config: depends on LS |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 45 | //config: |
| 46 | //config:config FEATURE_LS_RECURSIVE |
| 47 | //config: bool "Enable recursion (-R)" |
| 48 | //config: default y |
| 49 | //config: depends on LS |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 50 | //config: |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 51 | //config:config FEATURE_LS_WIDTH |
| 52 | //config: bool "Enable -w WIDTH and window size autodetection" |
| 53 | //config: default y |
| 54 | //config: depends on LS |
| 55 | //config: |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 56 | //config:config FEATURE_LS_SORTFILES |
| 57 | //config: bool "Sort the file names" |
| 58 | //config: default y |
| 59 | //config: depends on LS |
| 60 | //config: help |
| 61 | //config: Allow ls to sort file names alphabetically. |
| 62 | //config: |
| 63 | //config:config FEATURE_LS_TIMESTAMPS |
| 64 | //config: bool "Show file timestamps" |
| 65 | //config: default y |
| 66 | //config: depends on LS |
| 67 | //config: help |
| 68 | //config: Allow ls to display timestamps for files. |
| 69 | //config: |
| 70 | //config:config FEATURE_LS_USERNAME |
| 71 | //config: bool "Show username/groupnames" |
| 72 | //config: default y |
| 73 | //config: depends on LS |
| 74 | //config: help |
| 75 | //config: Allow ls to display username/groupname for files. |
| 76 | //config: |
| 77 | //config:config FEATURE_LS_COLOR |
| 78 | //config: bool "Allow use of color to identify file types" |
| 79 | //config: default y |
| 80 | //config: depends on LS && LONG_OPTS |
| 81 | //config: help |
| 82 | //config: This enables the --color option to ls. |
| 83 | //config: |
| 84 | //config:config FEATURE_LS_COLOR_IS_DEFAULT |
| 85 | //config: bool "Produce colored ls output by default" |
| 86 | //config: default y |
| 87 | //config: depends on FEATURE_LS_COLOR |
| 88 | //config: help |
| 89 | //config: Saying yes here will turn coloring on by default, |
| 90 | //config: even if no "--color" option is given to the ls command. |
| 91 | //config: This is not recommended, since the colors are not |
| 92 | //config: configurable, and the output may not be legible on |
| 93 | //config: many output screens. |
| 94 | |
| 95 | //applet:IF_LS(APPLET_NOEXEC(ls, ls, BB_DIR_BIN, BB_SUID_DROP, ls)) |
| 96 | |
| 97 | //kbuild:lib-$(CONFIG_LS) += ls.o |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 98 | |
| 99 | //usage:#define ls_trivial_usage |
| 100 | //usage: "[-1AaCxd" |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 101 | //usage: IF_FEATURE_LS_FOLLOWLINKS("LH") |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 102 | //usage: IF_FEATURE_LS_RECURSIVE("R") |
| 103 | //usage: IF_FEATURE_LS_FILETYPES("Fp") "lins" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 104 | //usage: IF_FEATURE_HUMAN_READABLE("h") |
| 105 | //usage: IF_FEATURE_LS_SORTFILES("rSXv") |
| 106 | //usage: IF_FEATURE_LS_TIMESTAMPS("ctu") |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 107 | //usage: IF_SELINUX("kZ") "]" |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 108 | //usage: IF_FEATURE_LS_WIDTH(" [-w WIDTH]") " [FILE]..." |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 109 | //usage:#define ls_full_usage "\n\n" |
| 110 | //usage: "List directory contents\n" |
Denys Vlasenko | fa9126e | 2011-04-03 01:27:49 +0200 | [diff] [blame] | 111 | //usage: "\n -1 One column output" |
| 112 | //usage: "\n -a Include entries which start with ." |
| 113 | //usage: "\n -A Like -a, but exclude . and .." |
Denys Vlasenko | 92c6898 | 2017-01-23 20:21:14 +0100 | [diff] [blame] | 114 | ////usage: "\n -C List by columns" - don't show, this is a default anyway |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 115 | //usage: "\n -x List by lines" |
| 116 | //usage: "\n -d List directory entries instead of contents" |
| 117 | //usage: IF_FEATURE_LS_FOLLOWLINKS( |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 118 | //usage: "\n -L Follow symlinks" |
Denys Vlasenko | 4ad95e6 | 2011-05-12 18:40:59 +0200 | [diff] [blame] | 119 | //usage: "\n -H Follow symlinks on command line" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 120 | //usage: ) |
| 121 | //usage: IF_FEATURE_LS_RECURSIVE( |
| 122 | //usage: "\n -R Recurse" |
| 123 | //usage: ) |
| 124 | //usage: IF_FEATURE_LS_FILETYPES( |
Denys Vlasenko | 4ad95e6 | 2011-05-12 18:40:59 +0200 | [diff] [blame] | 125 | //usage: "\n -p Append / to dir entries" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 126 | //usage: "\n -F Append indicator (one of */=@|) to entries" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 127 | //usage: ) |
| 128 | //usage: "\n -l Long listing format" |
| 129 | //usage: "\n -i List inode numbers" |
| 130 | //usage: "\n -n List numeric UIDs and GIDs instead of names" |
Denys Vlasenko | fa9126e | 2011-04-03 01:27:49 +0200 | [diff] [blame] | 131 | //usage: "\n -s List allocated blocks" |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 132 | //usage: IF_FEATURE_LS_TIMESTAMPS( |
Denys Vlasenko | e1f90d1 | 2017-01-22 22:02:19 +0100 | [diff] [blame] | 133 | //usage: "\n -lc List ctime" |
| 134 | //usage: "\n -lu List atime" |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 135 | //usage: ) |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 136 | //usage: IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS( |
| 137 | //usage: "\n --full-time List full date and time" |
| 138 | //usage: )) |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 139 | //usage: IF_FEATURE_HUMAN_READABLE( |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 140 | //usage: "\n -h Human readable sizes (1K 243M 2G)" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 141 | //usage: ) |
| 142 | //usage: IF_FEATURE_LS_SORTFILES( |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 143 | //usage: IF_LONG_OPTS( |
| 144 | //usage: "\n --group-directories-first" |
| 145 | //usage: ) |
Denys Vlasenko | fa9126e | 2011-04-03 01:27:49 +0200 | [diff] [blame] | 146 | //usage: "\n -S Sort by size" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 147 | //usage: "\n -X Sort by extension" |
| 148 | //usage: "\n -v Sort by version" |
| 149 | //usage: ) |
| 150 | //usage: IF_FEATURE_LS_TIMESTAMPS( |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 151 | //usage: "\n -t Sort by mtime" |
| 152 | //usage: "\n -tc Sort by ctime" |
| 153 | //usage: "\n -tu Sort by atime" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 154 | //usage: ) |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 155 | //usage: "\n -r Reverse sort order" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 156 | //usage: IF_SELINUX( |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 157 | //usage: "\n -Z List security context and permission" |
| 158 | //usage: ) |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 159 | //usage: IF_FEATURE_LS_WIDTH( |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 160 | //usage: "\n -w N Format N columns wide" |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 161 | //usage: ) |
| 162 | //usage: IF_FEATURE_LS_COLOR( |
| 163 | //usage: "\n --color[={always,never,auto}] Control coloring" |
| 164 | //usage: ) |
| 165 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 166 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 167 | #include "common_bufsiz.h" |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 168 | #include "unicode.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 169 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 170 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 171 | /* This is a NOEXEC applet. Be very careful! */ |
| 172 | |
Eric Andersen | f1142c5 | 2001-02-20 06:16:29 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | 245f91b | 2009-03-09 22:37:23 +0000 | [diff] [blame] | 174 | #if ENABLE_FTPD |
| 175 | /* ftpd uses ls, and without timestamps Mozilla won't understand |
| 176 | * ftpd's LIST output. |
| 177 | */ |
| 178 | # undef CONFIG_FEATURE_LS_TIMESTAMPS |
| 179 | # undef ENABLE_FEATURE_LS_TIMESTAMPS |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 180 | # undef IF_FEATURE_LS_TIMESTAMPS |
| 181 | # undef IF_NOT_FEATURE_LS_TIMESTAMPS |
Denis Vlasenko | 245f91b | 2009-03-09 22:37:23 +0000 | [diff] [blame] | 182 | # define CONFIG_FEATURE_LS_TIMESTAMPS 1 |
| 183 | # define ENABLE_FEATURE_LS_TIMESTAMPS 1 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 184 | # define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__ |
| 185 | # define IF_NOT_FEATURE_LS_TIMESTAMPS(...) |
Denis Vlasenko | 245f91b | 2009-03-09 22:37:23 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
| 188 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 189 | enum { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 190 | TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 191 | |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 192 | SPLIT_FILE = 0, |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 193 | SPLIT_DIR = 1, |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 194 | SPLIT_SUBDIR = 2, |
| 195 | |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 196 | /* Bits in G.all_fmt: */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 197 | /* what files will be displayed */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 198 | DISP_DIRNAME = 1 << 9, /* 2 or more items? label directories */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 199 | }; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 200 | |
Denys Vlasenko | e1f90d1 | 2017-01-22 22:02:19 +0100 | [diff] [blame] | 201 | /* -Cadi1l Std options, busybox always supports */ |
Denys Vlasenko | 407ab2a | 2010-12-19 16:29:08 +0100 | [diff] [blame] | 202 | /* -gnsxA Std options, busybox always supports */ |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 203 | /* -Q GNU option, busybox always supports */ |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 204 | /* -k Std option, busybox always supports (by ignoring) */ |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 205 | /* It means "for -s, show sizes in kbytes" */ |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 206 | /* Seems to only affect "POSIXLY_CORRECT=1 ls -sk" */ |
| 207 | /* since otherwise -s shows kbytes anyway */ |
Denys Vlasenko | 8ea683d | 2011-06-13 02:24:18 +0200 | [diff] [blame] | 208 | /* -LHRctur Std options, busybox optionally supports */ |
| 209 | /* -Fp Std options, busybox optionally supports */ |
Denys Vlasenko | 407ab2a | 2010-12-19 16:29:08 +0100 | [diff] [blame] | 210 | /* -SXvhTw GNU options, busybox optionally supports */ |
Denys Vlasenko | 8ea683d | 2011-06-13 02:24:18 +0200 | [diff] [blame] | 211 | /* -T WIDTH Ignored (we don't use tabs on output) */ |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 212 | /* -Z SELinux mandated option, busybox optionally supports */ |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 213 | static const char ls_options[] ALIGN1 = |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 214 | "Cadi1lgnsxAk" /* 12 opts, total 12 */ |
| 215 | IF_FEATURE_LS_FILETYPES("Fp") /* 2, 14 */ |
| 216 | IF_FEATURE_LS_RECURSIVE("R") /* 1, 15 */ |
| 217 | IF_SELINUX("Z") /* 1, 16 */ |
| 218 | "Q" /* 1, 17 */ |
| 219 | IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 20 */ |
| 220 | IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 24 */ |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 221 | IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 26 */ |
| 222 | IF_FEATURE_HUMAN_READABLE("h") /* 1, 27 */ |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 223 | IF_FEATURE_LS_WIDTH("T:w:") /* 2, 29 */ |
| 224 | ; |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 225 | enum { |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 226 | OPT_C = (1 << 0), |
Denys Vlasenko | f5bd6f6 | 2017-01-23 18:23:19 +0100 | [diff] [blame] | 227 | OPT_a = (1 << 1), |
Denys Vlasenko | 4cde4cc | 2017-01-23 20:08:22 +0100 | [diff] [blame] | 228 | OPT_d = (1 << 2), |
Denys Vlasenko | b60686c | 2017-01-23 18:34:11 +0100 | [diff] [blame] | 229 | OPT_i = (1 << 3), |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 230 | OPT_1 = (1 << 4), |
Denys Vlasenko | e1f90d1 | 2017-01-22 22:02:19 +0100 | [diff] [blame] | 231 | OPT_l = (1 << 5), |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 232 | OPT_g = (1 << 6), |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 233 | OPT_n = (1 << 7), |
Denys Vlasenko | 5d43ddc | 2017-01-23 18:43:43 +0100 | [diff] [blame] | 234 | OPT_s = (1 << 8), |
Denys Vlasenko | 92c6898 | 2017-01-23 20:21:14 +0100 | [diff] [blame] | 235 | OPT_x = (1 << 9), |
Denys Vlasenko | f5bd6f6 | 2017-01-23 18:23:19 +0100 | [diff] [blame] | 236 | OPT_A = (1 << 10), |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 237 | //OPT_k = (1 << 11), |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 238 | |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 239 | OPTBIT_F = 12, |
| 240 | OPTBIT_p, /* 13 */ |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 241 | OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES, |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 242 | OPTBIT_Z = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE, |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 243 | OPTBIT_Q = OPTBIT_Z + 1 * ENABLE_SELINUX, |
| 244 | OPTBIT_c, /* 17 */ |
| 245 | OPTBIT_t, /* 18 */ |
| 246 | OPTBIT_u, /* 19 */ |
| 247 | OPTBIT_S = OPTBIT_c + 3 * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 248 | OPTBIT_X, /* 21 */ |
| 249 | OPTBIT_r, /* 22 */ |
| 250 | OPTBIT_v, /* 23 */ |
| 251 | OPTBIT_L = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES, |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 252 | OPTBIT_H, /* 25 */ |
Denys Vlasenko | cd387f2 | 2011-02-27 04:10:00 +0100 | [diff] [blame] | 253 | OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS, |
Denys Vlasenko | 9f368e3 | 2011-02-28 12:16:10 +0100 | [diff] [blame] | 254 | OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE, |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 255 | OPTBIT_w, /* 28 */ |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 256 | OPTBIT_full_time = OPTBIT_T + 2 * ENABLE_FEATURE_LS_WIDTH, |
| 257 | OPTBIT_dirs_first, |
| 258 | OPTBIT_color, /* 31 */ |
| 259 | /* with long opts, we use all 32 bits */ |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 260 | |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 261 | OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES, |
| 262 | OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES, |
| 263 | OPT_R = (1 << OPTBIT_R) * ENABLE_FEATURE_LS_RECURSIVE, |
| 264 | OPT_Z = (1 << OPTBIT_Z) * ENABLE_SELINUX, |
| 265 | OPT_Q = (1 << OPTBIT_Q), |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 266 | OPT_c = (1 << OPTBIT_c) * ENABLE_FEATURE_LS_TIMESTAMPS, |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 267 | OPT_t = (1 << OPTBIT_t) * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 268 | OPT_u = (1 << OPTBIT_u) * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 269 | OPT_S = (1 << OPTBIT_S) * ENABLE_FEATURE_LS_SORTFILES, |
| 270 | OPT_X = (1 << OPTBIT_X) * ENABLE_FEATURE_LS_SORTFILES, |
| 271 | OPT_r = (1 << OPTBIT_r) * ENABLE_FEATURE_LS_SORTFILES, |
| 272 | OPT_v = (1 << OPTBIT_v) * ENABLE_FEATURE_LS_SORTFILES, |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 273 | OPT_L = (1 << OPTBIT_L) * ENABLE_FEATURE_LS_FOLLOWLINKS, |
| 274 | OPT_H = (1 << OPTBIT_H) * ENABLE_FEATURE_LS_FOLLOWLINKS, |
| 275 | OPT_h = (1 << OPTBIT_h) * ENABLE_FEATURE_HUMAN_READABLE, |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 276 | OPT_T = (1 << OPTBIT_T) * ENABLE_FEATURE_LS_WIDTH, |
| 277 | OPT_w = (1 << OPTBIT_w) * ENABLE_FEATURE_LS_WIDTH, |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 278 | OPT_full_time = (1 << OPTBIT_full_time ) * ENABLE_LONG_OPTS, |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 279 | OPT_dirs_first = (1 << OPTBIT_dirs_first) * ENABLE_LONG_OPTS, |
| 280 | OPT_color = (1 << OPTBIT_color ) * ENABLE_FEATURE_LS_COLOR, |
Denis Vlasenko | 248ce91 | 2009-03-03 14:09:04 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 283 | /* |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 284 | * a directory entry and its stat info |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 285 | */ |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 286 | struct dnode { |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 287 | const char *name; /* usually basename, but think "ls -l dir/file" */ |
| 288 | const char *fullname; /* full name (usable for stat etc) */ |
| 289 | struct dnode *dn_next; /* for linked list */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 290 | IF_SELINUX(security_context_t sid;) |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 291 | smallint fname_allocated; |
| 292 | |
| 293 | /* Used to avoid re-doing [l]stat at printout stage |
| 294 | * if we already collected needed data in scan stage: |
| 295 | */ |
| 296 | mode_t dn_mode_lstat; /* obtained with lstat, or 0 */ |
| 297 | mode_t dn_mode_stat; /* obtained with stat, or 0 */ |
| 298 | |
| 299 | // struct stat dstat; |
| 300 | // struct stat is huge. We don't need it in full. |
| 301 | // At least we don't need st_dev and st_blksize, |
| 302 | // but there are invisible fields as well |
| 303 | // (such as nanosecond-resolution timespamps) |
| 304 | // and padding, which we also don't want to store. |
| 305 | // We also can pre-parse dev_t dn_rdev (in glibc, it's huge). |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 306 | // On 32-bit uclibc: dnode size went from 112 to 84 bytes. |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 307 | // |
| 308 | /* Same names as in struct stat, but with dn_ instead of st_ pfx: */ |
| 309 | mode_t dn_mode; /* obtained with lstat OR stat, depending on -L etc */ |
| 310 | off_t dn_size; |
| 311 | #if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 312 | time_t dn_time; |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 313 | #endif |
| 314 | ino_t dn_ino; |
| 315 | blkcnt_t dn_blocks; |
| 316 | nlink_t dn_nlink; |
| 317 | uid_t dn_uid; |
| 318 | gid_t dn_gid; |
| 319 | int dn_rdev_maj; |
| 320 | int dn_rdev_min; |
| 321 | // dev_t dn_dev; |
| 322 | // blksize_t dn_blksize; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 323 | }; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 324 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 325 | struct globals { |
| 326 | #if ENABLE_FEATURE_LS_COLOR |
| 327 | smallint show_color; |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 328 | # define G_show_color (G.show_color) |
| 329 | #else |
| 330 | # define G_show_color 0 |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 331 | #endif |
Denis Vlasenko | 4a689e9 | 2008-06-18 16:38:22 +0000 | [diff] [blame] | 332 | smallint exit_code; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 333 | unsigned all_fmt; |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 334 | #if ENABLE_FEATURE_LS_WIDTH |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 335 | unsigned terminal_width; |
| 336 | # define G_terminal_width (G.terminal_width) |
| 337 | #else |
| 338 | # define G_terminal_width TERMINAL_WIDTH |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 339 | #endif |
| 340 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
| 341 | /* Do time() just once. Saves one syscall per file for "ls -l" */ |
| 342 | time_t current_time_t; |
| 343 | #endif |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 344 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 345 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 346 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 347 | setup_common_bufsiz(); \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 348 | /* we have to zero it out because of NOEXEC */ \ |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 349 | memset(&G, 0, sizeof(G)); \ |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 350 | IF_FEATURE_LS_WIDTH(G_terminal_width = TERMINAL_WIDTH;) \ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 351 | IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \ |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 352 | } while (0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 353 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 354 | |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 355 | /*** Output code ***/ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 356 | |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 357 | |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 358 | /* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket |
| 359 | * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file |
| 360 | * 3/7:multiplexed char/block device) |
| 361 | * and we use 0 for unknown and 15 for executables (see below) */ |
| 362 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
Denys Vlasenko | 4ad95e6 | 2011-05-12 18:40:59 +0200 | [diff] [blame] | 363 | /* un fi chr - dir - blk - file - link - sock - - exe */ |
| 364 | #define APPCHAR(mode) ("\0""|""\0""\0""/""\0""\0""\0""\0""\0""@""\0""=""\0""\0""\0" [TYPEINDEX(mode)]) |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 365 | /* 036 black foreground 050 black background |
| 366 | 037 red foreground 051 red background |
| 367 | 040 green foreground 052 green background |
| 368 | 041 brown foreground 053 brown background |
| 369 | 042 blue foreground 054 blue background |
| 370 | 043 magenta (purple) foreground 055 magenta background |
| 371 | 044 cyan (light blue) foreground 056 cyan background |
| 372 | 045 gray foreground 057 white background |
| 373 | */ |
| 374 | #define COLOR(mode) ( \ |
Denys Vlasenko | 4ad95e6 | 2011-05-12 18:40:59 +0200 | [diff] [blame] | 375 | /*un fi chr - dir - blk - file - link - sock - - exe */ \ |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 376 | "\037\043\043\045\042\045\043\043\000\045\044\045\043\045\045\040" \ |
| 377 | [TYPEINDEX(mode)]) |
| 378 | /* Select normal (0) [actually "reset all"] or bold (1) |
| 379 | * (other attributes are 2:dim 4:underline 5:blink 7:reverse, |
| 380 | * let's use 7 for "impossible" types, just for fun) |
| 381 | * Note: coreutils 6.9 uses inverted red for setuid binaries. |
| 382 | */ |
| 383 | #define ATTR(mode) ( \ |
Denys Vlasenko | 4ad95e6 | 2011-05-12 18:40:59 +0200 | [diff] [blame] | 384 | /*un fi chr - dir - blk - file- link- sock- - exe */ \ |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 385 | "\01\00\01\07\01\07\01\07\00\07\01\07\01\07\07\01" \ |
| 386 | [TYPEINDEX(mode)]) |
| 387 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 388 | #if ENABLE_FEATURE_LS_COLOR |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 389 | /* mode of zero is interpreted as "unknown" (stat failed) */ |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 390 | static char fgcolor(mode_t mode) |
| 391 | { |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 392 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 393 | return COLOR(0xF000); /* File is executable ... */ |
| 394 | return COLOR(mode); |
| 395 | } |
Denis Vlasenko | c1969f6 | 2009-03-18 22:39:34 +0000 | [diff] [blame] | 396 | static char bold(mode_t mode) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 397 | { |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 398 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 399 | return ATTR(0xF000); /* File is executable ... */ |
| 400 | return ATTR(mode); |
| 401 | } |
| 402 | #endif |
| 403 | |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 404 | #if ENABLE_FEATURE_LS_FILETYPES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 405 | static char append_char(mode_t mode) |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 406 | { |
Denys Vlasenko | 96d9c5b | 2017-01-23 19:56:13 +0100 | [diff] [blame] | 407 | if (!(option_mask32 & (OPT_F|OPT_p))) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 408 | return '\0'; |
Denys Vlasenko | 96d9c5b | 2017-01-23 19:56:13 +0100 | [diff] [blame] | 409 | |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 410 | if (S_ISDIR(mode)) |
| 411 | return '/'; |
Denys Vlasenko | 96d9c5b | 2017-01-23 19:56:13 +0100 | [diff] [blame] | 412 | if (!(option_mask32 & OPT_F)) |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 413 | return '\0'; |
| 414 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 415 | return '*'; |
| 416 | return APPCHAR(mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 417 | } |
| 418 | #endif |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 419 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 420 | static unsigned calc_name_len(const char *name) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 421 | { |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 422 | unsigned len; |
| 423 | uni_stat_t uni_stat; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 424 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 425 | // TODO: quote tab as \t, etc, if -Q |
| 426 | name = printable_string(&uni_stat, name); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 427 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 428 | if (!(option_mask32 & OPT_Q)) { |
| 429 | return uni_stat.unicode_width; |
| 430 | } |
| 431 | |
| 432 | len = 2 + uni_stat.unicode_width; |
| 433 | while (*name) { |
| 434 | if (*name == '"' || *name == '\\') { |
| 435 | len++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 436 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 437 | name++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 438 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 439 | return len; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 442 | /* Return the number of used columns. |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 443 | * Note that only columnar output uses return value. |
| 444 | * -l and -1 modes don't care. |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 445 | * coreutils 7.2 also supports: |
| 446 | * ls -b (--escape) = octal escapes (although it doesn't look like working) |
| 447 | * ls -N (--literal) = not escape at all |
Denys Vlasenko | 0683d4d | 2009-10-03 10:53:36 +0200 | [diff] [blame] | 448 | */ |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 449 | static unsigned print_name(const char *name) |
Denys Vlasenko | 87c150c | 2009-10-03 01:14:15 +0200 | [diff] [blame] | 450 | { |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 451 | unsigned len; |
| 452 | uni_stat_t uni_stat; |
| 453 | |
| 454 | // TODO: quote tab as \t, etc, if -Q |
| 455 | name = printable_string(&uni_stat, name); |
| 456 | |
| 457 | if (!(option_mask32 & OPT_Q)) { |
| 458 | fputs(name, stdout); |
| 459 | return uni_stat.unicode_width; |
Denys Vlasenko | 87c150c | 2009-10-03 01:14:15 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 462 | len = 2 + uni_stat.unicode_width; |
| 463 | putchar('"'); |
| 464 | while (*name) { |
| 465 | if (*name == '"' || *name == '\\') { |
| 466 | putchar('\\'); |
| 467 | len++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 468 | } |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 469 | putchar(*name); |
| 470 | name++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 471 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 472 | putchar('"'); |
| 473 | return len; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 476 | /* Return the number of used columns. |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 477 | * Note that only columnar output uses return value, |
| 478 | * -l and -1 modes don't care. |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 479 | */ |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 480 | static NOINLINE unsigned display_single(const struct dnode *dn) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 481 | { |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 482 | unsigned column = 0; |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 483 | char *lpath; |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 484 | int opt; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 485 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 486 | struct stat statbuf; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 487 | char append; |
| 488 | #endif |
| 489 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 490 | #if ENABLE_FEATURE_LS_FILETYPES |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 491 | append = append_char(dn->dn_mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 492 | #endif |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 493 | opt = option_mask32; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 494 | |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 495 | /* Do readlink early, so that if it fails, error message |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 496 | * does not appear *inside* the "ls -l" line */ |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 497 | lpath = NULL; |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 498 | if (opt & OPT_l) |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 499 | if (S_ISLNK(dn->dn_mode)) |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 500 | lpath = xmalloc_readlink_or_warn(dn->fullname); |
| 501 | |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 502 | if (opt & OPT_i) /* show inode# */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 503 | column += printf("%7llu ", (long long) dn->dn_ino); |
Denys Vlasenko | 3b28dae | 2011-03-01 05:37:41 +0100 | [diff] [blame] | 504 | //TODO: -h should affect -s too: |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 505 | if (opt & OPT_s) /* show allocated blocks */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 506 | column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1)); |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 507 | if (opt & OPT_l) { |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 508 | /* long listing: show mode */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 509 | column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode)); |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 510 | /* long listing: show number of links */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 511 | column += printf("%4lu ", (long) dn->dn_nlink); |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 512 | /* long listing: show user/group */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 513 | if (opt & OPT_n) { |
| 514 | if (opt & OPT_g) |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 515 | column += printf("%-8u ", (int) dn->dn_gid); |
| 516 | else |
| 517 | column += printf("%-8u %-8u ", |
| 518 | (int) dn->dn_uid, |
| 519 | (int) dn->dn_gid); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 520 | } |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 521 | #if ENABLE_FEATURE_LS_USERNAME |
| 522 | else { |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 523 | if (opt & OPT_g) { |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 524 | column += printf("%-8.8s ", |
| 525 | get_cached_groupname(dn->dn_gid)); |
| 526 | } else { |
| 527 | column += printf("%-8.8s %-8.8s ", |
| 528 | get_cached_username(dn->dn_uid), |
| 529 | get_cached_groupname(dn->dn_gid)); |
| 530 | } |
| 531 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 532 | #endif |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 533 | #if ENABLE_SELINUX |
Denys Vlasenko | 51b01fd | 2017-01-23 19:42:12 +0100 | [diff] [blame] | 534 | } |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 535 | if (opt & OPT_Z) { |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 536 | column += printf("%-32s ", dn->sid ? dn->sid : "?"); |
| 537 | freecon(dn->sid); |
| 538 | } |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 539 | if (opt & OPT_l) { |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 540 | #endif |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 541 | /* long listing: show size */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 542 | if (S_ISBLK(dn->dn_mode) || S_ISCHR(dn->dn_mode)) { |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 543 | column += printf("%4u, %3u ", |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 544 | dn->dn_rdev_maj, |
| 545 | dn->dn_rdev_min); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 546 | } else { |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 547 | if (opt & OPT_h) { |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 548 | column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ", |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 549 | /* print size, show one fractional, use suffixes */ |
| 550 | make_human_readable_str(dn->dn_size, 1, 0) |
Denys Vlasenko | 0bf44d0 | 2009-10-13 01:25:09 +0200 | [diff] [blame] | 551 | ); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 552 | } else { |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 553 | column += printf("%9"OFF_FMT"u ", dn->dn_size); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 554 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 555 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 556 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 557 | /* long listing: show {m,c,a}time */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 558 | if (opt & OPT_full_time) { /* --full-time */ |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 559 | /* coreutils 8.4 ls --full-time prints: |
Denys Vlasenko | 26d11b8 | 2011-02-28 12:38:08 +0100 | [diff] [blame] | 560 | * 2009-07-13 17:49:27.000000000 +0200 |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 561 | * we don't show fractional seconds. |
Denys Vlasenko | 26d11b8 | 2011-02-28 12:38:08 +0100 | [diff] [blame] | 562 | */ |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 563 | char buf[sizeof("YYYY-mm-dd HH:MM:SS TIMEZONE")]; |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 564 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %z", |
| 565 | localtime(&dn->dn_time)); |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 566 | column += printf("%s ", buf); |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 567 | } else { /* ordinary time format */ |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 568 | /* G.current_time_t is ~== time(NULL) */ |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 569 | char *filetime = ctime(&dn->dn_time); |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 570 | /* filetime's format: "Wed Jun 30 21:49:08 1993\n" */ |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 571 | time_t age = G.current_time_t - dn->dn_time; |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 572 | if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) { |
Denys Vlasenko | 8e92df1 | 2015-02-16 15:36:25 +0100 | [diff] [blame] | 573 | /* less than 6 months old */ |
| 574 | /* "mmm dd hh:mm " */ |
| 575 | printf("%.12s ", filetime + 4); |
| 576 | } else { |
| 577 | /* "mmm dd yyyy " */ |
| 578 | /* "mmm dd yyyyy " after year 9999 :) */ |
| 579 | strchr(filetime + 20, '\n')[0] = ' '; |
| 580 | printf("%.7s%6s", filetime + 4, filetime + 20); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 581 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 582 | column += 13; |
| 583 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 584 | #endif |
Denys Vlasenko | 2c3131d | 2017-01-23 19:05:11 +0100 | [diff] [blame] | 585 | } |
James Youngman | a4bc10c | 2010-12-20 01:36:16 +0100 | [diff] [blame] | 586 | |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 587 | #if ENABLE_FEATURE_LS_COLOR |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 588 | if (G_show_color) { |
| 589 | mode_t mode = dn->dn_mode_lstat; |
| 590 | if (!mode) |
| 591 | if (lstat(dn->fullname, &statbuf) == 0) |
| 592 | mode = statbuf.st_mode; |
| 593 | printf("\033[%u;%um", bold(mode), fgcolor(mode)); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 594 | } |
James Youngman | a4bc10c | 2010-12-20 01:36:16 +0100 | [diff] [blame] | 595 | #endif |
| 596 | column += print_name(dn->name); |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 597 | if (G_show_color) { |
James Youngman | a4bc10c | 2010-12-20 01:36:16 +0100 | [diff] [blame] | 598 | printf("\033[0m"); |
| 599 | } |
| 600 | |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 601 | if (lpath) { |
| 602 | printf(" -> "); |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 603 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 604 | if ((opt & (OPT_F|OPT_p)) |
Denys Vlasenko | 96d9c5b | 2017-01-23 19:56:13 +0100 | [diff] [blame] | 605 | || G_show_color |
| 606 | ) { |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 607 | mode_t mode = dn->dn_mode_stat; |
| 608 | if (!mode) |
| 609 | if (stat(dn->fullname, &statbuf) == 0) |
| 610 | mode = statbuf.st_mode; |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 611 | # if ENABLE_FEATURE_LS_FILETYPES |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 612 | append = append_char(mode); |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 613 | # endif |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 614 | # if ENABLE_FEATURE_LS_COLOR |
| 615 | if (G_show_color) { |
| 616 | printf("\033[%u;%um", bold(mode), fgcolor(mode)); |
| 617 | } |
| 618 | # endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 619 | } |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 620 | #endif |
| 621 | column += print_name(lpath) + 4; |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 622 | free(lpath); |
| 623 | if (G_show_color) { |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 624 | printf("\033[0m"); |
| 625 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 626 | } |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 627 | #if ENABLE_FEATURE_LS_FILETYPES |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 628 | if (opt & (OPT_F|OPT_p)) { |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 629 | if (append) { |
| 630 | putchar(append); |
| 631 | column++; |
| 632 | } |
| 633 | } |
| 634 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 635 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 636 | return column; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 639 | static void display_files(struct dnode **dn, unsigned nfiles) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 640 | { |
| 641 | unsigned i, ncols, nrows, row, nc; |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 642 | unsigned column; |
| 643 | unsigned nexttab; |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 644 | unsigned column_width = 0; /* used only by coulmnal output */ |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 645 | |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 646 | if (option_mask32 & (OPT_l|OPT_1)) { |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 647 | ncols = 1; |
| 648 | } else { |
| 649 | /* find the longest file name, use that as the column width */ |
| 650 | for (i = 0; dn[i]; i++) { |
| 651 | int len = calc_name_len(dn[i]->name); |
| 652 | if (column_width < len) |
| 653 | column_width = len; |
| 654 | } |
Denys Vlasenko | 5d43ddc | 2017-01-23 18:43:43 +0100 | [diff] [blame] | 655 | column_width += 2 |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 656 | + ((option_mask32 & OPT_Z) ? 33 : 0) /* context width */ |
Denys Vlasenko | 5d43ddc | 2017-01-23 18:43:43 +0100 | [diff] [blame] | 657 | + ((option_mask32 & OPT_i) ? 8 : 0) /* inode# width */ |
| 658 | + ((option_mask32 & OPT_s) ? 5 : 0) /* "alloc block" width */ |
| 659 | ; |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 660 | ncols = (unsigned)G_terminal_width / column_width; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | if (ncols > 1) { |
| 664 | nrows = nfiles / ncols; |
| 665 | if (nrows * ncols < nfiles) |
| 666 | nrows++; /* round up fractionals */ |
| 667 | } else { |
| 668 | nrows = nfiles; |
| 669 | ncols = 1; |
| 670 | } |
| 671 | |
Denys Vlasenko | 2f7d9e8 | 2010-12-19 07:06:44 +0100 | [diff] [blame] | 672 | column = 0; |
| 673 | nexttab = 0; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 674 | for (row = 0; row < nrows; row++) { |
| 675 | for (nc = 0; nc < ncols; nc++) { |
| 676 | /* reach into the array based on the column and row */ |
Denys Vlasenko | 92c6898 | 2017-01-23 20:21:14 +0100 | [diff] [blame] | 677 | if (option_mask32 & OPT_x) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 678 | i = (row * ncols) + nc; /* display across row */ |
| 679 | else |
| 680 | i = (nc * nrows) + row; /* display by column */ |
| 681 | if (i < nfiles) { |
| 682 | if (column > 0) { |
| 683 | nexttab -= column; |
Denys Vlasenko | ea351b9 | 2016-03-06 17:53:11 +0100 | [diff] [blame] | 684 | printf("%*s", nexttab, ""); |
| 685 | column += nexttab; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 686 | } |
| 687 | nexttab = column + column_width; |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 688 | column += display_single(dn[i]); |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | putchar('\n'); |
| 692 | column = 0; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 697 | /*** Dir scanning code ***/ |
| 698 | |
| 699 | static struct dnode *my_stat(const char *fullname, const char *name, int force_follow) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 700 | { |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 701 | struct stat statbuf; |
| 702 | struct dnode *cur; |
| 703 | |
| 704 | cur = xzalloc(sizeof(*cur)); |
| 705 | cur->fullname = fullname; |
| 706 | cur->name = name; |
| 707 | |
| 708 | if ((option_mask32 & OPT_L) || force_follow) { |
| 709 | #if ENABLE_SELINUX |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 710 | if (option_mask32 & OPT_Z) { |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 711 | getfilecon(fullname, &cur->sid); |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 712 | } |
| 713 | #endif |
| 714 | if (stat(fullname, &statbuf)) { |
| 715 | bb_simple_perror_msg(fullname); |
| 716 | G.exit_code = EXIT_FAILURE; |
| 717 | free(cur); |
| 718 | return NULL; |
| 719 | } |
| 720 | cur->dn_mode_stat = statbuf.st_mode; |
| 721 | } else { |
| 722 | #if ENABLE_SELINUX |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 723 | if (option_mask32 & OPT_Z) { |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 724 | lgetfilecon(fullname, &cur->sid); |
| 725 | } |
| 726 | #endif |
| 727 | if (lstat(fullname, &statbuf)) { |
| 728 | bb_simple_perror_msg(fullname); |
| 729 | G.exit_code = EXIT_FAILURE; |
| 730 | free(cur); |
| 731 | return NULL; |
| 732 | } |
| 733 | cur->dn_mode_lstat = statbuf.st_mode; |
| 734 | } |
| 735 | |
| 736 | /* cur->dstat = statbuf: */ |
| 737 | cur->dn_mode = statbuf.st_mode ; |
| 738 | cur->dn_size = statbuf.st_size ; |
| 739 | #if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 740 | cur->dn_time = statbuf.st_mtime ; |
| 741 | if (option_mask32 & OPT_u) |
| 742 | cur->dn_time = statbuf.st_atime; |
| 743 | if (option_mask32 & OPT_c) |
| 744 | cur->dn_time = statbuf.st_ctime; |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 745 | #endif |
| 746 | cur->dn_ino = statbuf.st_ino ; |
| 747 | cur->dn_blocks = statbuf.st_blocks; |
| 748 | cur->dn_nlink = statbuf.st_nlink ; |
| 749 | cur->dn_uid = statbuf.st_uid ; |
| 750 | cur->dn_gid = statbuf.st_gid ; |
| 751 | cur->dn_rdev_maj = major(statbuf.st_rdev); |
| 752 | cur->dn_rdev_min = minor(statbuf.st_rdev); |
| 753 | |
| 754 | return cur; |
| 755 | } |
| 756 | |
| 757 | static unsigned count_dirs(struct dnode **dn, int which) |
| 758 | { |
| 759 | unsigned dirs, all; |
| 760 | |
| 761 | if (!dn) |
| 762 | return 0; |
| 763 | |
| 764 | dirs = all = 0; |
| 765 | for (; *dn; dn++) { |
| 766 | const char *name; |
| 767 | |
| 768 | all++; |
| 769 | if (!S_ISDIR((*dn)->dn_mode)) |
| 770 | continue; |
| 771 | |
| 772 | name = (*dn)->name; |
| 773 | if (which != SPLIT_SUBDIR /* if not requested to skip . / .. */ |
| 774 | /* or if it's not . or .. */ |
| 775 | || name[0] != '.' |
| 776 | || (name[1] && (name[1] != '.' || name[2])) |
| 777 | ) { |
| 778 | dirs++; |
| 779 | } |
| 780 | } |
| 781 | return which != SPLIT_FILE ? dirs : all - dirs; |
| 782 | } |
| 783 | |
| 784 | /* get memory to hold an array of pointers */ |
| 785 | static struct dnode **dnalloc(unsigned num) |
| 786 | { |
| 787 | if (num < 1) |
| 788 | return NULL; |
| 789 | |
| 790 | num++; /* so that we have terminating NULL */ |
| 791 | return xzalloc(num * sizeof(struct dnode *)); |
| 792 | } |
| 793 | |
| 794 | #if ENABLE_FEATURE_LS_RECURSIVE |
| 795 | static void dfree(struct dnode **dnp) |
| 796 | { |
| 797 | unsigned i; |
| 798 | |
| 799 | if (dnp == NULL) |
| 800 | return; |
| 801 | |
| 802 | for (i = 0; dnp[i]; i++) { |
| 803 | struct dnode *cur = dnp[i]; |
| 804 | if (cur->fname_allocated) |
| 805 | free((char*)cur->fullname); |
| 806 | free(cur); |
| 807 | } |
| 808 | free(dnp); |
| 809 | } |
| 810 | #else |
| 811 | #define dfree(...) ((void)0) |
| 812 | #endif |
| 813 | |
| 814 | /* Returns NULL-terminated malloced vector of pointers (or NULL) */ |
| 815 | static struct dnode **splitdnarray(struct dnode **dn, int which) |
| 816 | { |
| 817 | unsigned dncnt, d; |
| 818 | struct dnode **dnp; |
| 819 | |
| 820 | if (dn == NULL) |
| 821 | return NULL; |
| 822 | |
| 823 | /* count how many dirs or files there are */ |
| 824 | dncnt = count_dirs(dn, which); |
| 825 | |
| 826 | /* allocate a file array and a dir array */ |
| 827 | dnp = dnalloc(dncnt); |
| 828 | |
| 829 | /* copy the entrys into the file or dir array */ |
| 830 | for (d = 0; *dn; dn++) { |
| 831 | if (S_ISDIR((*dn)->dn_mode)) { |
| 832 | const char *name; |
| 833 | |
| 834 | if (which == SPLIT_FILE) |
| 835 | continue; |
| 836 | |
| 837 | name = (*dn)->name; |
| 838 | if ((which & SPLIT_DIR) /* any dir... */ |
| 839 | /* ... or not . or .. */ |
| 840 | || name[0] != '.' |
| 841 | || (name[1] && (name[1] != '.' || name[2])) |
| 842 | ) { |
| 843 | dnp[d++] = *dn; |
| 844 | } |
| 845 | } else |
| 846 | if (which == SPLIT_FILE) { |
| 847 | dnp[d++] = *dn; |
| 848 | } |
| 849 | } |
| 850 | return dnp; |
| 851 | } |
| 852 | |
| 853 | #if ENABLE_FEATURE_LS_SORTFILES |
| 854 | static int sortcmp(const void *a, const void *b) |
| 855 | { |
| 856 | struct dnode *d1 = *(struct dnode **)a; |
| 857 | struct dnode *d2 = *(struct dnode **)b; |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 858 | unsigned opt = option_mask32; |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 859 | off_t dif; |
| 860 | |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 861 | dif = 0; /* assume sort by name */ |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 862 | // TODO: use pre-initialized function pointer |
| 863 | // instead of branch forest |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 864 | if (opt & OPT_dirs_first) { |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 865 | dif = S_ISDIR(d2->dn_mode) - S_ISDIR(d1->dn_mode); |
| 866 | if (dif != 0) |
| 867 | goto maybe_invert_and_ret; |
| 868 | } |
| 869 | |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 870 | if (opt & OPT_S) { /* sort by size */ |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 871 | dif = (d2->dn_size - d1->dn_size); |
Denys Vlasenko | f194cc1 | 2011-06-13 02:13:42 +0200 | [diff] [blame] | 872 | } else |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 873 | if (opt & OPT_t) { /* sort by time */ |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 874 | dif = (d2->dn_time - d1->dn_time); |
Denys Vlasenko | f194cc1 | 2011-06-13 02:13:42 +0200 | [diff] [blame] | 875 | } else |
Denys Vlasenko | 1e18a01 | 2011-06-21 17:12:52 +0200 | [diff] [blame] | 876 | #if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1 |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 877 | if (opt & OPT_v) { /* sort by version */ |
Denys Vlasenko | f194cc1 | 2011-06-13 02:13:42 +0200 | [diff] [blame] | 878 | dif = strverscmp(d1->name, d2->name); |
| 879 | } else |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 880 | #endif |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 881 | if (opt & OPT_X) { /* sort by extension */ |
Denys Vlasenko | f194cc1 | 2011-06-13 02:13:42 +0200 | [diff] [blame] | 882 | dif = strcmp(strchrnul(d1->name, '.'), strchrnul(d2->name, '.')); |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 883 | } |
| 884 | if (dif == 0) { |
Denys Vlasenko | f194cc1 | 2011-06-13 02:13:42 +0200 | [diff] [blame] | 885 | /* sort by name, use as tie breaker for other sorts */ |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 886 | if (ENABLE_LOCALE_SUPPORT) |
| 887 | dif = strcoll(d1->name, d2->name); |
| 888 | else |
| 889 | dif = strcmp(d1->name, d2->name); |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 890 | } else { |
| 891 | /* Make dif fit into an int */ |
| 892 | if (sizeof(dif) > sizeof(int)) { |
| 893 | enum { BITS_TO_SHIFT = 8 * (sizeof(dif) - sizeof(int)) }; |
| 894 | /* shift leaving only "int" worth of bits */ |
| 895 | /* (this requires dif != 0, and here it is nonzero) */ |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 896 | dif = 1 | (int)((uoff_t)dif >> BITS_TO_SHIFT); |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 897 | } |
| 898 | } |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 899 | maybe_invert_and_ret: |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 900 | return (opt & OPT_r) ? -(int)dif : (int)dif; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 901 | } |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 902 | |
| 903 | static void dnsort(struct dnode **dn, int size) |
| 904 | { |
| 905 | qsort(dn, size, sizeof(*dn), sortcmp); |
| 906 | } |
Denys Vlasenko | 8dd29da | 2011-05-13 17:55:08 +0200 | [diff] [blame] | 907 | |
| 908 | static void sort_and_display_files(struct dnode **dn, unsigned nfiles) |
| 909 | { |
| 910 | dnsort(dn, nfiles); |
| 911 | display_files(dn, nfiles); |
| 912 | } |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 913 | #else |
Denys Vlasenko | 8dd29da | 2011-05-13 17:55:08 +0200 | [diff] [blame] | 914 | # define dnsort(dn, size) ((void)0) |
| 915 | # define sort_and_display_files(dn, nfiles) display_files(dn, nfiles) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 916 | #endif |
| 917 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 918 | /* Returns NULL-terminated malloced vector of pointers (or NULL) */ |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 919 | static struct dnode **scan_one_dir(const char *path, unsigned *nfiles_p) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 920 | { |
| 921 | struct dnode *dn, *cur, **dnp; |
| 922 | struct dirent *entry; |
| 923 | DIR *dir; |
| 924 | unsigned i, nfiles; |
| 925 | |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 926 | *nfiles_p = 0; |
| 927 | dir = warn_opendir(path); |
| 928 | if (dir == NULL) { |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 929 | G.exit_code = EXIT_FAILURE; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 930 | return NULL; /* could not open the dir */ |
| 931 | } |
| 932 | dn = NULL; |
| 933 | nfiles = 0; |
| 934 | while ((entry = readdir(dir)) != NULL) { |
| 935 | char *fullname; |
| 936 | |
| 937 | /* are we going to list the file- it may be . or .. or a hidden file */ |
| 938 | if (entry->d_name[0] == '.') { |
Denys Vlasenko | f5bd6f6 | 2017-01-23 18:23:19 +0100 | [diff] [blame] | 939 | if (!(option_mask32 & (OPT_a|OPT_A))) |
| 940 | continue; /* skip all dotfiles if no -a/-A */ |
| 941 | if (!(option_mask32 & OPT_a) |
| 942 | && (!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2])) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 943 | ) { |
Denys Vlasenko | f5bd6f6 | 2017-01-23 18:23:19 +0100 | [diff] [blame] | 944 | continue; /* if only -A, skip . and .. but show other dotfiles */ |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 945 | } |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 946 | } |
| 947 | fullname = concat_path_file(path, entry->d_name); |
| 948 | cur = my_stat(fullname, bb_basename(fullname), 0); |
| 949 | if (!cur) { |
| 950 | free(fullname); |
| 951 | continue; |
| 952 | } |
| 953 | cur->fname_allocated = 1; |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 954 | cur->dn_next = dn; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 955 | dn = cur; |
| 956 | nfiles++; |
| 957 | } |
| 958 | closedir(dir); |
| 959 | |
| 960 | if (dn == NULL) |
| 961 | return NULL; |
| 962 | |
| 963 | /* now that we know how many files there are |
| 964 | * allocate memory for an array to hold dnode pointers |
| 965 | */ |
| 966 | *nfiles_p = nfiles; |
| 967 | dnp = dnalloc(nfiles); |
| 968 | for (i = 0; /* i < nfiles - detected via !dn below */; i++) { |
| 969 | dnp[i] = dn; /* save pointer to node in array */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 970 | dn = dn->dn_next; |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 971 | if (!dn) |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | return dnp; |
| 976 | } |
| 977 | |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 978 | #if ENABLE_DESKTOP |
| 979 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/ls.html |
| 980 | * If any of the -l, -n, -s options is specified, each list |
| 981 | * of files within the directory shall be preceded by a |
| 982 | * status line indicating the number of file system blocks |
| 983 | * occupied by files in the directory in 512-byte units if |
| 984 | * the -k option is not specified, or 1024-byte units if the |
| 985 | * -k option is specified, rounded up to the next integral |
| 986 | * number of units. |
| 987 | */ |
| 988 | /* by Jorgen Overgaard (jorgen AT antistaten.se) */ |
| 989 | static off_t calculate_blocks(struct dnode **dn) |
| 990 | { |
| 991 | uoff_t blocks = 1; |
| 992 | if (dn) { |
| 993 | while (*dn) { |
| 994 | /* st_blocks is in 512 byte blocks */ |
| 995 | blocks += (*dn)->dn_blocks; |
| 996 | dn++; |
| 997 | } |
| 998 | } |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 999 | |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 1000 | /* Even though standard says use 512 byte blocks, coreutils use 1k */ |
| 1001 | /* Actually, we round up by calculating (blocks + 1) / 2, |
| 1002 | * "+ 1" was done when we initialized blocks to 1 */ |
| 1003 | return blocks >> 1; |
| 1004 | } |
| 1005 | #endif |
| 1006 | |
| 1007 | static void scan_and_display_dirs_recur(struct dnode **dn, int first) |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1008 | { |
| 1009 | unsigned nfiles; |
| 1010 | struct dnode **subdnp; |
| 1011 | |
| 1012 | for (; *dn; dn++) { |
Denys Vlasenko | 4cde4cc | 2017-01-23 20:08:22 +0100 | [diff] [blame] | 1013 | if ((G.all_fmt & DISP_DIRNAME) |
| 1014 | || (option_mask32 & OPT_R) |
| 1015 | ) { |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1016 | if (!first) |
| 1017 | bb_putchar('\n'); |
| 1018 | first = 0; |
| 1019 | printf("%s:\n", (*dn)->fullname); |
| 1020 | } |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 1021 | subdnp = scan_one_dir((*dn)->fullname, &nfiles); |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1022 | #if ENABLE_DESKTOP |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1023 | if (option_mask32 & (OPT_s|OPT_l)) { |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1024 | printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp)); |
Denys Vlasenko | 5d43ddc | 2017-01-23 18:43:43 +0100 | [diff] [blame] | 1025 | } |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1026 | #endif |
| 1027 | if (nfiles > 0) { |
| 1028 | /* list all files at this level */ |
Denys Vlasenko | 8dd29da | 2011-05-13 17:55:08 +0200 | [diff] [blame] | 1029 | sort_and_display_files(subdnp, nfiles); |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 1030 | |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1031 | if (ENABLE_FEATURE_LS_RECURSIVE |
Denys Vlasenko | 4cde4cc | 2017-01-23 20:08:22 +0100 | [diff] [blame] | 1032 | && (option_mask32 & OPT_R) |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1033 | ) { |
| 1034 | struct dnode **dnd; |
| 1035 | unsigned dndirs; |
| 1036 | /* recursive - list the sub-dirs */ |
| 1037 | dnd = splitdnarray(subdnp, SPLIT_SUBDIR); |
| 1038 | dndirs = count_dirs(subdnp, SPLIT_SUBDIR); |
| 1039 | if (dndirs > 0) { |
| 1040 | dnsort(dnd, dndirs); |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 1041 | scan_and_display_dirs_recur(dnd, 0); |
Denys Vlasenko | 66ca241 | 2011-05-13 17:27:36 +0200 | [diff] [blame] | 1042 | /* free the array of dnode pointers to the dirs */ |
| 1043 | free(dnd); |
| 1044 | } |
| 1045 | } |
| 1046 | /* free the dnodes and the fullname mem */ |
| 1047 | dfree(subdnp); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1053 | int ls_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1054 | { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1055 | struct dnode **dnd; |
| 1056 | struct dnode **dnf; |
| 1057 | struct dnode **dnp; |
| 1058 | struct dnode *dn; |
| 1059 | struct dnode *cur; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 1060 | unsigned opt; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1061 | unsigned nfiles; |
| 1062 | unsigned dnfiles; |
| 1063 | unsigned dndirs; |
| 1064 | unsigned i; |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1065 | #if ENABLE_FEATURE_LS_COLOR |
| 1066 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ |
| 1067 | /* coreutils 6.10: |
| 1068 | * # ls --color=BOGUS |
| 1069 | * ls: invalid argument 'BOGUS' for '--color' |
| 1070 | * Valid arguments are: |
| 1071 | * 'always', 'yes', 'force' |
| 1072 | * 'never', 'no', 'none' |
| 1073 | * 'auto', 'tty', 'if-tty' |
| 1074 | * (and substrings: "--color=alwa" work too) |
| 1075 | */ |
| 1076 | static const char ls_longopts[] ALIGN1 = |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 1077 | "full-time\0" No_argument "\xff" |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 1078 | "group-directories-first\0" No_argument "\xfe" |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 1079 | "color\0" Optional_argument "\xfd" |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 1080 | ; |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1081 | static const char color_str[] ALIGN1 = |
| 1082 | "always\0""yes\0""force\0" |
| 1083 | "auto\0""tty\0""if-tty\0"; |
Denis Vlasenko | 51f1b6c | 2008-07-15 05:21:47 +0000 | [diff] [blame] | 1084 | /* need to initialize since --color has _an optional_ argument */ |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1085 | const char *color_opt = color_str; /* "always" */ |
| 1086 | #endif |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1087 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1088 | INIT_G(); |
Denis Vlasenko | e055443 | 2007-01-19 22:03:06 +0000 | [diff] [blame] | 1089 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 1090 | init_unicode(); |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 1091 | |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 1092 | #if ENABLE_FEATURE_LS_WIDTH |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1093 | /* obtain the terminal width */ |
Denys Vlasenko | 641caae | 2015-10-23 01:44:22 +0200 | [diff] [blame] | 1094 | G_terminal_width = get_terminal_width(STDIN_FILENO); |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1095 | /* go one less... */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1096 | G_terminal_width--; |
Eric Andersen | 6d68781 | 2003-11-04 23:16:48 +0000 | [diff] [blame] | 1097 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1098 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1099 | /* process options */ |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1100 | IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;) |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1101 | opt_complementary = |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1102 | /* -n and -g imply -l */ |
| 1103 | "nl:gl" |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 1104 | /* --full-time implies -l */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1105 | IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS(":\xff""l")) |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1106 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: |
| 1107 | * in some pairs of opts, only last one takes effect: |
| 1108 | */ |
Denys Vlasenko | 407ab2a | 2010-12-19 16:29:08 +0100 | [diff] [blame] | 1109 | IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES(":t-S:S-t")) /* time/size */ |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 1110 | // ":m-l:l-m" - we don't have -m |
| 1111 | IF_FEATURE_LS_FOLLOWLINKS(":H-L:L-H") |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1112 | ":C-xl:x-Cl:l-xC" /* bycols/bylines/long */ |
| 1113 | ":C-1:1-C" /* bycols/oneline */ |
| 1114 | ":x-1:1-x" /* bylines/oneline (not in SuS, but in GNU coreutils 8.4) */ |
Denys Vlasenko | b47b3ce | 2011-08-10 00:51:29 +0200 | [diff] [blame] | 1115 | IF_FEATURE_LS_TIMESTAMPS(":c-u:u-c") /* mtime/atime */ |
Denys Vlasenko | 407ab2a | 2010-12-19 16:29:08 +0100 | [diff] [blame] | 1116 | /* -w NUM: */ |
Denys Vlasenko | ed15dde | 2017-01-11 16:35:52 +0100 | [diff] [blame] | 1117 | IF_FEATURE_LS_WIDTH(":w+"); |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1118 | opt = getopt32(argv, ls_options |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1119 | IF_FEATURE_LS_WIDTH(, /*-T*/ NULL, /*-w*/ &G_terminal_width) |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1120 | IF_FEATURE_LS_COLOR(, &color_opt) |
| 1121 | ); |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 1122 | #if 0 /* option bits debug */ |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 1123 | bb_error_msg("opt:0x%08x l:%x H:%x color:%x dirs:%x", opt, OPT_l, OPT_H, OPT_color, OPT_dirs_first); |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 1124 | if (opt & OPT_c ) bb_error_msg("-c"); |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 1125 | if (opt & OPT_l ) bb_error_msg("-l"); |
Denys Vlasenko | 194b2eb | 2017-01-22 17:32:20 +0100 | [diff] [blame] | 1126 | if (opt & OPT_H ) bb_error_msg("-H"); |
| 1127 | if (opt & OPT_color ) bb_error_msg("--color"); |
| 1128 | if (opt & OPT_dirs_first) bb_error_msg("--group-directories-first"); |
| 1129 | if (opt & OPT_full_time ) bb_error_msg("--full-time"); |
| 1130 | exit(0); |
| 1131 | #endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1132 | |
Denys Vlasenko | a1cbaca | 2017-01-23 19:30:14 +0100 | [diff] [blame] | 1133 | #if ENABLE_SELINUX |
| 1134 | if (opt & OPT_Z) { |
| 1135 | if (!is_selinux_enabled()) |
| 1136 | option_mask32 &= ~OPT_Z; |
| 1137 | } |
| 1138 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1139 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1140 | #if ENABLE_FEATURE_LS_COLOR |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1141 | /* set G_show_color = 1/0 */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1142 | if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) { |
| 1143 | char *p = getenv("LS_COLORS"); |
| 1144 | /* LS_COLORS is unset, or (not empty && not "none") ? */ |
Denis Vlasenko | 51f1b6c | 2008-07-15 05:21:47 +0000 | [diff] [blame] | 1145 | if (!p || (p[0] && strcmp(p, "none") != 0)) |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1146 | G_show_color = 1; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1147 | } |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1148 | if (opt & OPT_color) { |
| 1149 | if (color_opt[0] == 'n') |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1150 | G_show_color = 0; |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1151 | else switch (index_in_substrings(color_str, color_opt)) { |
| 1152 | case 3: |
| 1153 | case 4: |
| 1154 | case 5: |
| 1155 | if (isatty(STDOUT_FILENO)) { |
| 1156 | case 0: |
| 1157 | case 1: |
| 1158 | case 2: |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1159 | G_show_color = 1; |
Denys Vlasenko | ae05dd4 | 2009-07-03 12:22:19 +0200 | [diff] [blame] | 1160 | } |
| 1161 | } |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1162 | } |
| 1163 | #endif |
| 1164 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1165 | /* sort out which command line options take precedence */ |
Denys Vlasenko | 4cde4cc | 2017-01-23 20:08:22 +0100 | [diff] [blame] | 1166 | if (ENABLE_FEATURE_LS_RECURSIVE && (opt & OPT_d)) |
| 1167 | option_mask32 &= ~OPT_R; /* no recurse if listing only dir */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1168 | if (!(opt & OPT_l)) { /* not -l? */ |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 1169 | if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { |
| 1170 | /* when to sort by time? -t[cu] sorts by time even with -l */ |
| 1171 | /* (this is achieved by opt_flags[] element for -t) */ |
| 1172 | /* without -l, bare -c or -u enable sort too */ |
| 1173 | /* (with -l, bare -c or -u just select which time to show) */ |
| 1174 | if (opt & (OPT_c|OPT_u)) { |
Denys Vlasenko | 11540a8 | 2017-01-23 18:01:48 +0100 | [diff] [blame] | 1175 | option_mask32 |= OPT_t; |
Denys Vlasenko | f580baf | 2017-01-22 19:02:57 +0100 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1179 | |
Denys Vlasenko | f313746 | 2010-12-19 05:05:34 +0100 | [diff] [blame] | 1180 | /* choose a display format if one was not already specified by an option */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1181 | if (!(option_mask32 & (OPT_l|OPT_1|OPT_x|OPT_C))) |
| 1182 | option_mask32 |= (isatty(STDOUT_FILENO) ? OPT_C : OPT_1); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1183 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1184 | argv += optind; |
| 1185 | if (!argv[0]) |
| 1186 | *--argv = (char*)"."; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1187 | |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1188 | if (argv[1]) |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1189 | G.all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1190 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 1191 | /* stuff the command line file names into a dnode array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1192 | dn = NULL; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1193 | nfiles = 0; |
| 1194 | do { |
Denys Vlasenko | 163d864 | 2010-12-19 06:16:28 +0100 | [diff] [blame] | 1195 | cur = my_stat(*argv, *argv, |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 1196 | /* follow links on command line unless -l, -s or -F: */ |
Denys Vlasenko | ccc1699 | 2017-01-23 20:43:06 +0100 | [diff] [blame^] | 1197 | !(option_mask32 & (OPT_l|OPT_s|OPT_F)) |
Denys Vlasenko | 982aa26 | 2010-12-19 21:54:39 +0100 | [diff] [blame] | 1198 | /* ... or if -H: */ |
| 1199 | || (option_mask32 & OPT_H) |
Denys Vlasenko | d27ac29 | 2011-05-13 17:27:15 +0200 | [diff] [blame] | 1200 | /* ... or if -L, but my_stat always follows links if -L */ |
Denys Vlasenko | 163d864 | 2010-12-19 06:16:28 +0100 | [diff] [blame] | 1201 | ); |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1202 | argv++; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 1203 | if (!cur) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1204 | continue; |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1205 | /*cur->fname_allocated = 0; - already is */ |
| 1206 | cur->dn_next = dn; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1207 | dn = cur; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1208 | nfiles++; |
Denis Vlasenko | c7497ea | 2008-06-13 11:16:09 +0000 | [diff] [blame] | 1209 | } while (*argv); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1210 | |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1211 | /* nfiles _may_ be 0 here - try "ls doesnt_exist" */ |
| 1212 | if (nfiles == 0) |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1213 | return G.exit_code; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1214 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1215 | /* now that we know how many files there are |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 1216 | * allocate memory for an array to hold dnode pointers |
| 1217 | */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1218 | dnp = dnalloc(nfiles); |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1219 | for (i = 0; /* i < nfiles - detected via !dn below */; i++) { |
| 1220 | dnp[i] = dn; /* save pointer to node in array */ |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1221 | dn = dn->dn_next; |
Denys Vlasenko | 76c7d95 | 2009-10-03 01:15:47 +0200 | [diff] [blame] | 1222 | if (!dn) |
| 1223 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Denys Vlasenko | 4cde4cc | 2017-01-23 20:08:22 +0100 | [diff] [blame] | 1226 | if (option_mask32 & OPT_d) { |
Denys Vlasenko | 8dd29da | 2011-05-13 17:55:08 +0200 | [diff] [blame] | 1227 | sort_and_display_files(dnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1228 | } else { |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 1229 | dnd = splitdnarray(dnp, SPLIT_DIR); |
| 1230 | dnf = splitdnarray(dnp, SPLIT_FILE); |
| 1231 | dndirs = count_dirs(dnp, SPLIT_DIR); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1232 | dnfiles = nfiles - dndirs; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1233 | if (dnfiles > 0) { |
Denys Vlasenko | 8dd29da | 2011-05-13 17:55:08 +0200 | [diff] [blame] | 1234 | sort_and_display_files(dnf, dnfiles); |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 1235 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1236 | free(dnf); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1237 | } |
| 1238 | if (dndirs > 0) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 1239 | dnsort(dnd, dndirs); |
Denys Vlasenko | 4029e21 | 2011-05-13 17:28:46 +0200 | [diff] [blame] | 1240 | scan_and_display_dirs_recur(dnd, dnfiles == 0); |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 1241 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1242 | free(dnd); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1245 | |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 1246 | if (ENABLE_FEATURE_CLEAN_UP) |
Denys Vlasenko | cae409c | 2009-10-03 11:43:48 +0200 | [diff] [blame] | 1247 | dfree(dnp); |
Denys Vlasenko | 2a81639 | 2011-05-13 17:28:09 +0200 | [diff] [blame] | 1248 | return G.exit_code; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1249 | } |