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