blob: f11eb4358176ea05b530132d7b4bd099b2c0e82a [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen9d3aba71999-10-06 09:04:55 +00002/*
Eric Andersencc8ed391999-10-05 16:24:54 +00003 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
Eric Andersen11c65522000-09-07 17:24:47 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +00006 */
7
Denis Vlasenko5e4fda02009-03-08 23:46:48 +00008/* [date unknown. Perhaps before year 2000]
Eric Andersencc8ed391999-10-05 16:24:54 +00009 * To achieve a small memory footprint, this version of 'ls' doesn't do any
10 * file sorting, and only has the most essential command line switches
Eric Andersen77d92682001-05-23 20:32:09 +000011 * (i.e., the ones I couldn't live without :-) All features which involve
Eric Andersencc8ed391999-10-05 16:24:54 +000012 * linking in substantial chunks of libc can be disabled.
13 *
14 * Although I don't really want to add new features to this program to
15 * keep it small, I *am* interested to receive bug fixes and ways to make
16 * it more portable.
17 *
18 * KNOWN BUGS:
Denys Vlasenko87c150c2009-10-03 01:14:15 +020019 * 1. hidden files can make column width too large
Erik Andersen9ffdaa62000-02-11 21:55:04 +000020 *
Eric Andersencc8ed391999-10-05 16:24:54 +000021 * NON-OPTIMAL BEHAVIOUR:
22 * 1. autowidth reads directories twice
23 * 2. if you do a short directory listing without filetype characters
24 * appended, there's no need to stat each one
25 * PORTABILITY:
26 * 1. requires lstat (BSD) - how do you do it without?
Denis Vlasenko5e4fda02009-03-08 23:46:48 +000027 *
28 * [2009-03]
29 * ls sorts listing now, and supports almost all options.
Eric Andersencc8ed391999-10-05 16:24:54 +000030 */
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010031
32//usage:#define ls_trivial_usage
33//usage: "[-1AaCxd"
Denys Vlasenko982aa262010-12-19 21:54:39 +010034//usage: IF_FEATURE_LS_FOLLOWLINKS("LH")
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010035//usage: IF_FEATURE_LS_RECURSIVE("R")
36//usage: IF_FEATURE_LS_FILETYPES("Fp") "lins"
37//usage: IF_FEATURE_LS_TIMESTAMPS("e")
38//usage: IF_FEATURE_HUMAN_READABLE("h")
39//usage: IF_FEATURE_LS_SORTFILES("rSXv")
40//usage: IF_FEATURE_LS_TIMESTAMPS("ctu")
41//usage: IF_SELINUX("kKZ") "]"
Denys Vlasenko279a7ac2011-05-12 18:44:51 +020042//usage: IF_FEATURE_AUTOWIDTH(" [-w WIDTH]") " [FILE]..."
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010043//usage:#define ls_full_usage "\n\n"
44//usage: "List directory contents\n"
Denys Vlasenkofa9126e2011-04-03 01:27:49 +020045//usage: "\n -1 One column output"
46//usage: "\n -a Include entries which start with ."
47//usage: "\n -A Like -a, but exclude . and .."
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010048//usage: "\n -C List by columns"
49//usage: "\n -x List by lines"
50//usage: "\n -d List directory entries instead of contents"
51//usage: IF_FEATURE_LS_FOLLOWLINKS(
Denys Vlasenko982aa262010-12-19 21:54:39 +010052//usage: "\n -L Follow symlinks"
Denys Vlasenko4ad95e62011-05-12 18:40:59 +020053//usage: "\n -H Follow symlinks on command line"
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010054//usage: )
55//usage: IF_FEATURE_LS_RECURSIVE(
56//usage: "\n -R Recurse"
57//usage: )
58//usage: IF_FEATURE_LS_FILETYPES(
Denys Vlasenko4ad95e62011-05-12 18:40:59 +020059//usage: "\n -p Append / to dir entries"
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010060//usage: "\n -F Append indicator (one of */=@|) to entries"
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010061//usage: )
62//usage: "\n -l Long listing format"
63//usage: "\n -i List inode numbers"
64//usage: "\n -n List numeric UIDs and GIDs instead of names"
Denys Vlasenkofa9126e2011-04-03 01:27:49 +020065//usage: "\n -s List allocated blocks"
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010066//usage: IF_FEATURE_LS_TIMESTAMPS(
67//usage: "\n -e List full date and time"
68//usage: )
69//usage: IF_FEATURE_HUMAN_READABLE(
70//usage: "\n -h List sizes in human readable format (1K 243M 2G)"
71//usage: )
72//usage: IF_FEATURE_LS_SORTFILES(
73//usage: "\n -r Sort in reverse order"
Denys Vlasenkofa9126e2011-04-03 01:27:49 +020074//usage: "\n -S Sort by size"
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010075//usage: "\n -X Sort by extension"
76//usage: "\n -v Sort by version"
77//usage: )
78//usage: IF_FEATURE_LS_TIMESTAMPS(
79//usage: "\n -c With -l: sort by ctime"
Denys Vlasenkofa9126e2011-04-03 01:27:49 +020080//usage: "\n -t With -l: sort by mtime"
81//usage: "\n -u With -l: sort by atime"
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +010082//usage: )
83//usage: IF_SELINUX(
84//usage: "\n -k List security context"
85//usage: "\n -K List security context in long format"
86//usage: "\n -Z List security context and permission"
87//usage: )
88//usage: IF_FEATURE_AUTOWIDTH(
89//usage: "\n -w N Assume the terminal is N columns wide"
90//usage: )
91//usage: IF_FEATURE_LS_COLOR(
92//usage: "\n --color[={always,never,auto}] Control coloring"
93//usage: )
94
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000095#include "libbb.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020096#include "unicode.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000097
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +000098
Denis Vlasenko99912ca2007-04-10 15:43:37 +000099/* This is a NOEXEC applet. Be very careful! */
100
Eric Andersenf1142c52001-02-20 06:16:29 +0000101
Denis Vlasenko245f91b2009-03-09 22:37:23 +0000102#if ENABLE_FTPD
103/* ftpd uses ls, and without timestamps Mozilla won't understand
104 * ftpd's LIST output.
105 */
106# undef CONFIG_FEATURE_LS_TIMESTAMPS
107# undef ENABLE_FEATURE_LS_TIMESTAMPS
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000108# undef IF_FEATURE_LS_TIMESTAMPS
109# undef IF_NOT_FEATURE_LS_TIMESTAMPS
Denis Vlasenko245f91b2009-03-09 22:37:23 +0000110# define CONFIG_FEATURE_LS_TIMESTAMPS 1
111# define ENABLE_FEATURE_LS_TIMESTAMPS 1
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000112# define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__
113# define IF_NOT_FEATURE_LS_TIMESTAMPS(...)
Denis Vlasenko245f91b2009-03-09 22:37:23 +0000114#endif
115
116
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000117enum {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000118TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000119
Denys Vlasenko982aa262010-12-19 21:54:39 +0100120SPLIT_FILE = 0,
Denys Vlasenko2a816392011-05-13 17:28:09 +0200121SPLIT_DIR = 1,
Denys Vlasenko982aa262010-12-19 21:54:39 +0100122SPLIT_SUBDIR = 2,
123
Denys Vlasenko2a816392011-05-13 17:28:09 +0200124/* Bits in G.all_fmt: */
Eric Andersencc8ed391999-10-05 16:24:54 +0000125
Eric Andersen11c65522000-09-07 17:24:47 +0000126/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
127/* what file information will be listed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000128LIST_INO = 1 << 0,
129LIST_BLOCKS = 1 << 1,
130LIST_MODEBITS = 1 << 2,
131LIST_NLINKS = 1 << 3,
132LIST_ID_NAME = 1 << 4,
133LIST_ID_NUMERIC = 1 << 5,
134LIST_CONTEXT = 1 << 6,
135LIST_SIZE = 1 << 7,
James Youngmana4bc10c2010-12-20 01:36:16 +0100136LIST_DATE_TIME = 1 << 8,
137LIST_FULLTIME = 1 << 9,
138LIST_SYMLINK = 1 << 10,
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200139LIST_FILETYPE = 1 << 11, /* show / suffix for dirs */
140LIST_CLASSIFY = 1 << 12, /* requires LIST_FILETYPE, also show *,|,@,= suffixes */
141LIST_MASK = (LIST_CLASSIFY << 1) - 1,
Eric Andersen11c65522000-09-07 17:24:47 +0000142
143/* what files will be displayed */
James Youngmana4bc10c2010-12-20 01:36:16 +0100144DISP_DIRNAME = 1 << 13, /* 2 or more items? label directories */
145DISP_HIDDEN = 1 << 14, /* show filenames starting with . */
146DISP_DOT = 1 << 15, /* show . and .. */
147DISP_NOLIST = 1 << 16, /* show directory as itself, not contents */
148DISP_RECURSIVE = 1 << 17, /* show directory and everything below it */
149DISP_ROWS = 1 << 18, /* print across rows */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000150DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151
Denys Vlasenko982aa262010-12-19 21:54:39 +0100152/* what is the overall style of the listing */
James Youngmana4bc10c2010-12-20 01:36:16 +0100153STYLE_COLUMNAR = 1 << 19, /* many records per line */
154STYLE_LONG = 2 << 19, /* one record per line, extended info */
155STYLE_SINGLE = 3 << 19, /* one record per line */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100156STYLE_MASK = STYLE_SINGLE,
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Eric Andersen11c65522000-09-07 17:24:47 +0000158/* which of the three times will be used */
James Youngmana4bc10c2010-12-20 01:36:16 +0100159TIME_CHANGE = (1 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100160TIME_ACCESS = (2 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
James Youngmana4bc10c2010-12-20 01:36:16 +0100161TIME_MASK = (3 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162
Denys Vlasenko982aa262010-12-19 21:54:39 +0100163/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
James Youngmana4bc10c2010-12-20 01:36:16 +0100164SORT_REVERSE = 1 << 23,
Eric Andersencc8ed391999-10-05 16:24:54 +0000165
Denys Vlasenko982aa262010-12-19 21:54:39 +0100166SORT_NAME = 0, /* sort by file name */
James Youngmana4bc10c2010-12-20 01:36:16 +0100167SORT_SIZE = 1 << 24, /* sort by file size */
168SORT_ATIME = 2 << 24, /* sort by last access time */
169SORT_CTIME = 3 << 24, /* sort by last change time */
170SORT_MTIME = 4 << 24, /* sort by last modification time */
171SORT_VERSION = 5 << 24, /* sort by version */
172SORT_EXT = 6 << 24, /* sort by file name extension */
173SORT_DIR = 7 << 24, /* sort by file or directory */
174SORT_MASK = (7 << 24) * ENABLE_FEATURE_LS_SORTFILES,
Eric Andersencc8ed391999-10-05 16:24:54 +0000175
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000176LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
James Youngmana4bc10c2010-12-20 01:36:16 +0100177 LIST_DATE_TIME | LIST_SYMLINK,
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000178};
Eric Andersencc8ed391999-10-05 16:24:54 +0000179
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100180/* -Cadil1 Std options, busybox always supports */
181/* -gnsxA Std options, busybox always supports */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100182/* -Q GNU option, busybox always supports */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100183/* -k SELinux option, busybox always supports (ignores if !SELinux) */
184/* Std has -k which means "show sizes in kbytes" */
Denys Vlasenko8ea683d2011-06-13 02:24:18 +0200185/* -LHRctur Std options, busybox optionally supports */
186/* -Fp Std options, busybox optionally supports */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100187/* -SXvhTw GNU options, busybox optionally supports */
Denys Vlasenko8ea683d2011-06-13 02:24:18 +0200188/* -T WIDTH Ignored (we don't use tabs on output) */
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100189/* -KZ SELinux mandated options, busybox optionally supports */
190/* (coreutils 8.4 has no -K, remove it?) */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100191/* -e I think we made this one up (looks similar to GNU --full-time) */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100192/* We already used up all 32 bits, if we need to add more, candidates for removal: */
193/* -K, -T, -e (add --full-time instead) */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000194static const char ls_options[] ALIGN1 =
Denys Vlasenko982aa262010-12-19 21:54:39 +0100195 "Cadil1gnsxQAk" /* 13 opts, total 13 */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000196 IF_FEATURE_LS_TIMESTAMPS("cetu") /* 4, 17 */
197 IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 21 */
198 IF_FEATURE_LS_FILETYPES("Fp") /* 2, 23 */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100199 IF_FEATURE_LS_RECURSIVE("R") /* 1, 24 */
200 IF_SELINUX("KZ") /* 2, 26 */
201 IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 28 */
202 IF_FEATURE_HUMAN_READABLE("h") /* 1, 29 */
203 IF_FEATURE_AUTOWIDTH("T:w:") /* 2, 31 */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100204 /* with --color, we use all 32 bits */;
Denis Vlasenko248ce912009-03-03 14:09:04 +0000205enum {
206 //OPT_C = (1 << 0),
207 //OPT_a = (1 << 1),
208 //OPT_d = (1 << 2),
209 //OPT_i = (1 << 3),
210 //OPT_l = (1 << 4),
211 //OPT_1 = (1 << 5),
212 OPT_g = (1 << 6),
213 //OPT_n = (1 << 7),
214 //OPT_s = (1 << 8),
215 //OPT_x = (1 << 9),
216 OPT_Q = (1 << 10),
217 //OPT_A = (1 << 11),
218 //OPT_k = (1 << 12),
Denys Vlasenko982aa262010-12-19 21:54:39 +0100219
220 OPTBIT_c = 13,
221 OPTBIT_e,
222 OPTBIT_t,
223 OPTBIT_u,
224 OPTBIT_S = OPTBIT_c + 4 * ENABLE_FEATURE_LS_TIMESTAMPS,
225 OPTBIT_X, /* 18 */
226 OPTBIT_r,
227 OPTBIT_v,
228 OPTBIT_F = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES,
229 OPTBIT_p, /* 22 */
230 OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES,
231 OPTBIT_K = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE,
232 OPTBIT_Z, /* 25 */
233 OPTBIT_L = OPTBIT_K + 2 * ENABLE_SELINUX,
234 OPTBIT_H, /* 27 */
Denys Vlasenkocd387f22011-02-27 04:10:00 +0100235 OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS,
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100236 OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE,
Denys Vlasenko982aa262010-12-19 21:54:39 +0100237 OPTBIT_w, /* 30 */
238 OPTBIT_color = OPTBIT_T + 2 * ENABLE_FEATURE_AUTOWIDTH,
239
240 OPT_c = (1 << OPTBIT_c) * ENABLE_FEATURE_LS_TIMESTAMPS,
241 OPT_e = (1 << OPTBIT_e) * ENABLE_FEATURE_LS_TIMESTAMPS,
242 OPT_t = (1 << OPTBIT_t) * ENABLE_FEATURE_LS_TIMESTAMPS,
243 OPT_u = (1 << OPTBIT_u) * ENABLE_FEATURE_LS_TIMESTAMPS,
244 OPT_S = (1 << OPTBIT_S) * ENABLE_FEATURE_LS_SORTFILES,
245 OPT_X = (1 << OPTBIT_X) * ENABLE_FEATURE_LS_SORTFILES,
246 OPT_r = (1 << OPTBIT_r) * ENABLE_FEATURE_LS_SORTFILES,
247 OPT_v = (1 << OPTBIT_v) * ENABLE_FEATURE_LS_SORTFILES,
248 OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES,
249 OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES,
250 OPT_R = (1 << OPTBIT_R) * ENABLE_FEATURE_LS_RECURSIVE,
251 OPT_K = (1 << OPTBIT_K) * ENABLE_SELINUX,
252 OPT_Z = (1 << OPTBIT_Z) * ENABLE_SELINUX,
253 OPT_L = (1 << OPTBIT_L) * ENABLE_FEATURE_LS_FOLLOWLINKS,
254 OPT_H = (1 << OPTBIT_H) * ENABLE_FEATURE_LS_FOLLOWLINKS,
255 OPT_h = (1 << OPTBIT_h) * ENABLE_FEATURE_HUMAN_READABLE,
256 OPT_T = (1 << OPTBIT_T) * ENABLE_FEATURE_AUTOWIDTH,
257 OPT_w = (1 << OPTBIT_w) * ENABLE_FEATURE_AUTOWIDTH,
258 OPT_color = (1 << OPTBIT_color) * ENABLE_FEATURE_LS_COLOR,
Denis Vlasenko248ce912009-03-03 14:09:04 +0000259};
260
Denis Vlasenko248ce912009-03-03 14:09:04 +0000261/* TODO: simple toggles may be stored as OPT_xxx bits instead */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100262static const uint32_t opt_flags[] = {
James Youngmana4bc10c2010-12-20 01:36:16 +0100263 STYLE_COLUMNAR, /* C */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100264 DISP_HIDDEN | DISP_DOT, /* a */
265 DISP_NOLIST, /* d */
266 LIST_INO, /* i */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100267 LIST_LONG | STYLE_LONG, /* l */
James Youngmana4bc10c2010-12-20 01:36:16 +0100268 STYLE_SINGLE, /* 1 */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100269 LIST_LONG | STYLE_LONG, /* g (don't show owner) - handled via OPT_g. assumes l */
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100270 LIST_ID_NUMERIC | LIST_LONG | STYLE_LONG, /* n (assumes l) */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100271 LIST_BLOCKS, /* s */
James Youngmana4bc10c2010-12-20 01:36:16 +0100272 DISP_ROWS | STYLE_COLUMNAR, /* x */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100273 0, /* Q (quote filename) - handled via OPT_Q */
274 DISP_HIDDEN, /* A */
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100275 ENABLE_SELINUX * (LIST_CONTEXT|STYLE_SINGLE), /* k (ignored if !SELINUX) */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000276#if ENABLE_FEATURE_LS_TIMESTAMPS
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100277 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100278 LIST_FULLTIME, /* e */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100279 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
280 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000281#endif
282#if ENABLE_FEATURE_LS_SORTFILES
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100283 SORT_SIZE, /* S */
284 SORT_EXT, /* X */
285 SORT_REVERSE, /* r */
286 SORT_VERSION, /* v */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000287#endif
288#if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200289 LIST_FILETYPE | LIST_CLASSIFY, /* F */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100290 LIST_FILETYPE, /* p */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000291#endif
Denis Vlasenko248ce912009-03-03 14:09:04 +0000292#if ENABLE_FEATURE_LS_RECURSIVE
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100293 DISP_RECURSIVE, /* R */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000294#endif
Denis Vlasenko248ce912009-03-03 14:09:04 +0000295#if ENABLE_SELINUX
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100296 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME|STYLE_SINGLE, /* K */
297 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT|STYLE_SINGLE, /* Z */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000298#endif
Denys Vlasenko982aa262010-12-19 21:54:39 +0100299 (1U << 31)
300 /* options after Z are not processed through opt_flags */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000301};
302
303
Eric Andersen11c65522000-09-07 17:24:47 +0000304/*
Denys Vlasenko2a816392011-05-13 17:28:09 +0200305 * a directory entry and its stat info
Eric Andersen11c65522000-09-07 17:24:47 +0000306 */
Denys Vlasenko76c7d952009-10-03 01:15:47 +0200307struct dnode {
Denys Vlasenko2a816392011-05-13 17:28:09 +0200308 const char *name; /* usually basename, but think "ls -l dir/file" */
309 const char *fullname; /* full name (usable for stat etc) */
310 struct dnode *dn_next; /* for linked list */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000311 IF_SELINUX(security_context_t sid;)
Denys Vlasenko2a816392011-05-13 17:28:09 +0200312 smallint fname_allocated;
313
314 /* Used to avoid re-doing [l]stat at printout stage
315 * if we already collected needed data in scan stage:
316 */
317 mode_t dn_mode_lstat; /* obtained with lstat, or 0 */
318 mode_t dn_mode_stat; /* obtained with stat, or 0 */
319
320// struct stat dstat;
321// struct stat is huge. We don't need it in full.
322// At least we don't need st_dev and st_blksize,
323// but there are invisible fields as well
324// (such as nanosecond-resolution timespamps)
325// and padding, which we also don't want to store.
326// We also can pre-parse dev_t dn_rdev (in glibc, it's huge).
Denys Vlasenko4029e212011-05-13 17:28:46 +0200327// On 32-bit uclibc: dnode size went from 112 to 84 bytes.
Denys Vlasenko2a816392011-05-13 17:28:09 +0200328//
329 /* Same names as in struct stat, but with dn_ instead of st_ pfx: */
330 mode_t dn_mode; /* obtained with lstat OR stat, depending on -L etc */
331 off_t dn_size;
332#if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES
333 time_t dn_atime;
334 time_t dn_mtime;
335 time_t dn_ctime;
336#endif
337 ino_t dn_ino;
338 blkcnt_t dn_blocks;
339 nlink_t dn_nlink;
340 uid_t dn_uid;
341 gid_t dn_gid;
342 int dn_rdev_maj;
343 int dn_rdev_min;
344// dev_t dn_dev;
345// blksize_t dn_blksize;
Eric Andersen11c65522000-09-07 17:24:47 +0000346};
Eric Andersen11c65522000-09-07 17:24:47 +0000347
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000348struct globals {
349#if ENABLE_FEATURE_LS_COLOR
350 smallint show_color;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200351# define G_show_color (G.show_color)
352#else
353# define G_show_color 0
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000354#endif
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000355 smallint exit_code;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000356 unsigned all_fmt;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000357#if ENABLE_FEATURE_AUTOWIDTH
Denys Vlasenko2a816392011-05-13 17:28:09 +0200358 unsigned terminal_width;
359# define G_terminal_width (G.terminal_width)
360#else
361# define G_terminal_width TERMINAL_WIDTH
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000362#endif
363#if ENABLE_FEATURE_LS_TIMESTAMPS
364 /* Do time() just once. Saves one syscall per file for "ls -l" */
365 time_t current_time_t;
366#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100367} FIX_ALIASING;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000368#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000369#define INIT_G() do { \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200370 /* we have to zero it out because of NOEXEC */ \
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000371 memset(&G, 0, sizeof(G)); \
Denys Vlasenko2a816392011-05-13 17:28:09 +0200372 IF_FEATURE_AUTOWIDTH(G_terminal_width = TERMINAL_WIDTH;) \
373 IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000374} while (0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000375
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000376
Denys Vlasenko4029e212011-05-13 17:28:46 +0200377/*** Output code ***/
Denys Vlasenko2a816392011-05-13 17:28:09 +0200378
Eric Andersen79565b62000-08-11 18:10:21 +0000379
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000380/* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket
381 * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file
382 * 3/7:multiplexed char/block device)
383 * and we use 0 for unknown and 15 for executables (see below) */
384#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200385/* un fi chr - dir - blk - file - link - sock - - exe */
386#define APPCHAR(mode) ("\0""|""\0""\0""/""\0""\0""\0""\0""\0""@""\0""=""\0""\0""\0" [TYPEINDEX(mode)])
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000387/* 036 black foreground 050 black background
388 037 red foreground 051 red background
389 040 green foreground 052 green background
390 041 brown foreground 053 brown background
391 042 blue foreground 054 blue background
392 043 magenta (purple) foreground 055 magenta background
393 044 cyan (light blue) foreground 056 cyan background
394 045 gray foreground 057 white background
395*/
396#define COLOR(mode) ( \
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200397 /*un fi chr - dir - blk - file - link - sock - - exe */ \
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000398 "\037\043\043\045\042\045\043\043\000\045\044\045\043\045\045\040" \
399 [TYPEINDEX(mode)])
400/* Select normal (0) [actually "reset all"] or bold (1)
401 * (other attributes are 2:dim 4:underline 5:blink 7:reverse,
402 * let's use 7 for "impossible" types, just for fun)
403 * Note: coreutils 6.9 uses inverted red for setuid binaries.
404 */
405#define ATTR(mode) ( \
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200406 /*un fi chr - dir - blk - file- link- sock- - exe */ \
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000407 "\01\00\01\07\01\07\01\07\00\07\01\07\01\07\07\01" \
408 [TYPEINDEX(mode)])
409
Denis Vlasenko5c759602006-10-28 12:37:16 +0000410#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000411/* mode of zero is interpreted as "unknown" (stat failed) */
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000412static char fgcolor(mode_t mode)
413{
Rob Landley9947a242006-06-15 22:11:10 +0000414 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000415 return COLOR(0xF000); /* File is executable ... */
416 return COLOR(mode);
417}
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000418static char bold(mode_t mode)
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000419{
Rob Landley9947a242006-06-15 22:11:10 +0000420 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000421 return ATTR(0xF000); /* File is executable ... */
422 return ATTR(mode);
423}
424#endif
425
Denys Vlasenko2a816392011-05-13 17:28:09 +0200426#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000427static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000428{
Denys Vlasenko2a816392011-05-13 17:28:09 +0200429 if (!(G.all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000430 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000431 if (S_ISDIR(mode))
432 return '/';
Denys Vlasenko2a816392011-05-13 17:28:09 +0200433 if (!(G.all_fmt & LIST_CLASSIFY))
Rob Landley9947a242006-06-15 22:11:10 +0000434 return '\0';
435 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000436 return '*';
437 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000438}
439#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000440
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100441static unsigned calc_name_len(const char *name)
Eric Andersen11c65522000-09-07 17:24:47 +0000442{
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100443 unsigned len;
444 uni_stat_t uni_stat;
Eric Andersen11c65522000-09-07 17:24:47 +0000445
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100446 // TODO: quote tab as \t, etc, if -Q
447 name = printable_string(&uni_stat, name);
Eric Andersen11c65522000-09-07 17:24:47 +0000448
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100449 if (!(option_mask32 & OPT_Q)) {
450 return uni_stat.unicode_width;
451 }
452
453 len = 2 + uni_stat.unicode_width;
454 while (*name) {
455 if (*name == '"' || *name == '\\') {
456 len++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000457 }
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100458 name++;
Eric Andersen11c65522000-09-07 17:24:47 +0000459 }
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100460 return len;
Eric Andersen11c65522000-09-07 17:24:47 +0000461}
462
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100463/* Return the number of used columns.
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100464 * Note that only STYLE_COLUMNAR uses return value.
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100465 * STYLE_SINGLE and STYLE_LONG don't care.
466 * coreutils 7.2 also supports:
467 * ls -b (--escape) = octal escapes (although it doesn't look like working)
468 * ls -N (--literal) = not escape at all
Denys Vlasenko0683d4d2009-10-03 10:53:36 +0200469 */
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100470static unsigned print_name(const char *name)
Denys Vlasenko87c150c2009-10-03 01:14:15 +0200471{
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100472 unsigned len;
473 uni_stat_t uni_stat;
474
475 // TODO: quote tab as \t, etc, if -Q
476 name = printable_string(&uni_stat, name);
477
478 if (!(option_mask32 & OPT_Q)) {
479 fputs(name, stdout);
480 return uni_stat.unicode_width;
Denys Vlasenko87c150c2009-10-03 01:14:15 +0200481 }
482
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100483 len = 2 + uni_stat.unicode_width;
484 putchar('"');
485 while (*name) {
486 if (*name == '"' || *name == '\\') {
487 putchar('\\');
488 len++;
Eric Andersen11c65522000-09-07 17:24:47 +0000489 }
Dan Fandrich77d48722010-09-07 23:38:28 -0700490 putchar(*name);
491 name++;
Eric Andersen11c65522000-09-07 17:24:47 +0000492 }
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100493 putchar('"');
494 return len;
Eric Andersen11c65522000-09-07 17:24:47 +0000495}
496
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100497/* Return the number of used columns.
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100498 * Note that only STYLE_COLUMNAR uses return value,
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100499 * STYLE_SINGLE and STYLE_LONG don't care.
500 */
Denys Vlasenko4029e212011-05-13 17:28:46 +0200501static NOINLINE unsigned display_single(const struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000502{
Denys Vlasenko76c7d952009-10-03 01:15:47 +0200503 unsigned column = 0;
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200504 char *lpath;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000505#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +0200506 struct stat statbuf;
Eric Andersen11c65522000-09-07 17:24:47 +0000507 char append;
508#endif
509
Denis Vlasenko5c759602006-10-28 12:37:16 +0000510#if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko2a816392011-05-13 17:28:09 +0200511 append = append_char(dn->dn_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000512#endif
513
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000514 /* Do readlink early, so that if it fails, error message
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100515 * does not appear *inside* the "ls -l" line */
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200516 lpath = NULL;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200517 if (G.all_fmt & LIST_SYMLINK)
518 if (S_ISLNK(dn->dn_mode))
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000519 lpath = xmalloc_readlink_or_warn(dn->fullname);
520
Denys Vlasenko2a816392011-05-13 17:28:09 +0200521 if (G.all_fmt & LIST_INO)
522 column += printf("%7llu ", (long long) dn->dn_ino);
Denys Vlasenko3b28dae2011-03-01 05:37:41 +0100523//TODO: -h should affect -s too:
Denys Vlasenko2a816392011-05-13 17:28:09 +0200524 if (G.all_fmt & LIST_BLOCKS)
525 column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1));
526 if (G.all_fmt & LIST_MODEBITS)
527 column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode));
528 if (G.all_fmt & LIST_NLINKS)
529 column += printf("%4lu ", (long) dn->dn_nlink);
530 if (G.all_fmt & LIST_ID_NUMERIC) {
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100531 if (option_mask32 & OPT_g)
Denys Vlasenko2a816392011-05-13 17:28:09 +0200532 column += printf("%-8u ", (int) dn->dn_gid);
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100533 else
534 column += printf("%-8u %-8u ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200535 (int) dn->dn_uid,
536 (int) dn->dn_gid);
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100537 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000538#if ENABLE_FEATURE_LS_USERNAME
Denys Vlasenko2a816392011-05-13 17:28:09 +0200539 else if (G.all_fmt & LIST_ID_NAME) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000540 if (option_mask32 & OPT_g) {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100541 column += printf("%-8.8s ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200542 get_cached_groupname(dn->dn_gid));
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000543 } else {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100544 column += printf("%-8.8s %-8.8s ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200545 get_cached_username(dn->dn_uid),
546 get_cached_groupname(dn->dn_gid));
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000547 }
548 }
Eric Andersen11c65522000-09-07 17:24:47 +0000549#endif
Denys Vlasenko2a816392011-05-13 17:28:09 +0200550 if (G.all_fmt & LIST_SIZE) {
551 if (S_ISBLK(dn->dn_mode) || S_ISCHR(dn->dn_mode)) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000552 column += printf("%4u, %3u ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200553 dn->dn_rdev_maj,
554 dn->dn_rdev_min);
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000555 } else {
Denys Vlasenko982aa262010-12-19 21:54:39 +0100556 if (option_mask32 & OPT_h) {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100557 column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200558 /* print size, show one fractional, use suffixes */
559 make_human_readable_str(dn->dn_size, 1, 0)
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200560 );
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000561 } else {
Denys Vlasenko2a816392011-05-13 17:28:09 +0200562 column += printf("%9"OFF_FMT"u ", dn->dn_size);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000563 }
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000564 }
565 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000566#if ENABLE_FEATURE_LS_TIMESTAMPS
Denys Vlasenko2a816392011-05-13 17:28:09 +0200567 if (G.all_fmt & (LIST_FULLTIME|LIST_DATE_TIME)) {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100568 char *filetime;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200569 time_t ttime = dn->dn_mtime;
570 if (G.all_fmt & TIME_ACCESS)
571 ttime = dn->dn_atime;
572 if (G.all_fmt & TIME_CHANGE)
573 ttime = dn->dn_ctime;
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100574 filetime = ctime(&ttime);
575 /* filetime's format: "Wed Jun 30 21:49:08 1993\n" */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200576 if (G.all_fmt & LIST_FULLTIME) { /* -e */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100577 /* Note: coreutils 8.4 ls --full-time prints:
578 * 2009-07-13 17:49:27.000000000 +0200
579 */
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100580 column += printf("%.24s ", filetime);
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100581 } else { /* LIST_DATE_TIME */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200582 /* G.current_time_t ~== time(NULL) */
583 time_t age = G.current_time_t - ttime;
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100584 printf("%.6s ", filetime + 4); /* "Jun 30" */
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000585 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
586 /* hh:mm if less than 6 months old */
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100587 printf("%.5s ", filetime + 11);
588 } else { /* year. buggy if year > 9999 ;) */
589 printf(" %.4s ", filetime + 20);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000590 }
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000591 column += 13;
592 }
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100593 }
Eric Andersen11c65522000-09-07 17:24:47 +0000594#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000595#if ENABLE_SELINUX
Denys Vlasenko2a816392011-05-13 17:28:09 +0200596 if (G.all_fmt & LIST_CONTEXT) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000597 column += printf("%-32s ", dn->sid ? dn->sid : "unknown");
598 freecon(dn->sid);
599 }
Eric Andersen9e480452003-07-03 10:07:04 +0000600#endif
James Youngmana4bc10c2010-12-20 01:36:16 +0100601
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000602#if ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +0200603 if (G_show_color) {
604 mode_t mode = dn->dn_mode_lstat;
605 if (!mode)
606 if (lstat(dn->fullname, &statbuf) == 0)
607 mode = statbuf.st_mode;
608 printf("\033[%u;%um", bold(mode), fgcolor(mode));
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000609 }
James Youngmana4bc10c2010-12-20 01:36:16 +0100610#endif
611 column += print_name(dn->name);
Denys Vlasenko2a816392011-05-13 17:28:09 +0200612 if (G_show_color) {
James Youngmana4bc10c2010-12-20 01:36:16 +0100613 printf("\033[0m");
614 }
615
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200616 if (lpath) {
617 printf(" -> ");
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000618#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +0200619 if ((G.all_fmt & LIST_FILETYPE) || G_show_color) {
620 mode_t mode = dn->dn_mode_stat;
621 if (!mode)
622 if (stat(dn->fullname, &statbuf) == 0)
623 mode = statbuf.st_mode;
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200624# if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko2a816392011-05-13 17:28:09 +0200625 append = append_char(mode);
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200626# endif
Denys Vlasenko2a816392011-05-13 17:28:09 +0200627# if ENABLE_FEATURE_LS_COLOR
628 if (G_show_color) {
629 printf("\033[%u;%um", bold(mode), fgcolor(mode));
630 }
631# endif
Eric Andersen11c65522000-09-07 17:24:47 +0000632 }
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200633#endif
634 column += print_name(lpath) + 4;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200635 free(lpath);
636 if (G_show_color) {
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200637 printf("\033[0m");
638 }
Eric Andersen11c65522000-09-07 17:24:47 +0000639 }
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000640#if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko2a816392011-05-13 17:28:09 +0200641 if (G.all_fmt & LIST_FILETYPE) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000642 if (append) {
643 putchar(append);
644 column++;
645 }
646 }
647#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000648
Glenn L McGrath4d001292003-01-06 01:11:50 +0000649 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000650}
651
Denys Vlasenko4029e212011-05-13 17:28:46 +0200652static void display_files(struct dnode **dn, unsigned nfiles)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100653{
654 unsigned i, ncols, nrows, row, nc;
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +0100655 unsigned column;
656 unsigned nexttab;
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100657 unsigned column_width = 0; /* used only by STYLE_COLUMNAR */
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100658
Denys Vlasenko2a816392011-05-13 17:28:09 +0200659 if (G.all_fmt & STYLE_LONG) { /* STYLE_LONG or STYLE_SINGLE */
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100660 ncols = 1;
661 } else {
662 /* find the longest file name, use that as the column width */
663 for (i = 0; dn[i]; i++) {
664 int len = calc_name_len(dn[i]->name);
665 if (column_width < len)
666 column_width = len;
667 }
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +0100668 column_width += 1 +
Denys Vlasenko2a816392011-05-13 17:28:09 +0200669 IF_SELINUX( ((G.all_fmt & LIST_CONTEXT) ? 33 : 0) + )
670 ((G.all_fmt & LIST_INO) ? 8 : 0) +
671 ((G.all_fmt & LIST_BLOCKS) ? 5 : 0);
672 ncols = (unsigned)G_terminal_width / column_width;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100673 }
674
675 if (ncols > 1) {
676 nrows = nfiles / ncols;
677 if (nrows * ncols < nfiles)
678 nrows++; /* round up fractionals */
679 } else {
680 nrows = nfiles;
681 ncols = 1;
682 }
683
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +0100684 column = 0;
685 nexttab = 0;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100686 for (row = 0; row < nrows; row++) {
687 for (nc = 0; nc < ncols; nc++) {
688 /* reach into the array based on the column and row */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200689 if (G.all_fmt & DISP_ROWS)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100690 i = (row * ncols) + nc; /* display across row */
691 else
692 i = (nc * nrows) + row; /* display by column */
693 if (i < nfiles) {
694 if (column > 0) {
695 nexttab -= column;
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +0100696 printf("%*s ", nexttab, "");
697 column += nexttab + 1;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100698 }
699 nexttab = column + column_width;
Denys Vlasenko4029e212011-05-13 17:28:46 +0200700 column += display_single(dn[i]);
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100701 }
702 }
703 putchar('\n');
704 column = 0;
705 }
706}
707
708
Denys Vlasenko4029e212011-05-13 17:28:46 +0200709/*** Dir scanning code ***/
710
711static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100712{
Denys Vlasenko4029e212011-05-13 17:28:46 +0200713 struct stat statbuf;
714 struct dnode *cur;
715
716 cur = xzalloc(sizeof(*cur));
717 cur->fullname = fullname;
718 cur->name = name;
719
720 if ((option_mask32 & OPT_L) || force_follow) {
721#if ENABLE_SELINUX
722 if (is_selinux_enabled()) {
723 getfilecon(fullname, &cur->sid);
724 }
725#endif
726 if (stat(fullname, &statbuf)) {
727 bb_simple_perror_msg(fullname);
728 G.exit_code = EXIT_FAILURE;
729 free(cur);
730 return NULL;
731 }
732 cur->dn_mode_stat = statbuf.st_mode;
733 } else {
734#if ENABLE_SELINUX
735 if (is_selinux_enabled()) {
736 lgetfilecon(fullname, &cur->sid);
737 }
738#endif
739 if (lstat(fullname, &statbuf)) {
740 bb_simple_perror_msg(fullname);
741 G.exit_code = EXIT_FAILURE;
742 free(cur);
743 return NULL;
744 }
745 cur->dn_mode_lstat = statbuf.st_mode;
746 }
747
748 /* cur->dstat = statbuf: */
749 cur->dn_mode = statbuf.st_mode ;
750 cur->dn_size = statbuf.st_size ;
751#if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES
752 cur->dn_atime = statbuf.st_atime ;
753 cur->dn_mtime = statbuf.st_mtime ;
754 cur->dn_ctime = statbuf.st_ctime ;
755#endif
756 cur->dn_ino = statbuf.st_ino ;
757 cur->dn_blocks = statbuf.st_blocks;
758 cur->dn_nlink = statbuf.st_nlink ;
759 cur->dn_uid = statbuf.st_uid ;
760 cur->dn_gid = statbuf.st_gid ;
761 cur->dn_rdev_maj = major(statbuf.st_rdev);
762 cur->dn_rdev_min = minor(statbuf.st_rdev);
763
764 return cur;
765}
766
767static unsigned count_dirs(struct dnode **dn, int which)
768{
769 unsigned dirs, all;
770
771 if (!dn)
772 return 0;
773
774 dirs = all = 0;
775 for (; *dn; dn++) {
776 const char *name;
777
778 all++;
779 if (!S_ISDIR((*dn)->dn_mode))
780 continue;
781
782 name = (*dn)->name;
783 if (which != SPLIT_SUBDIR /* if not requested to skip . / .. */
784 /* or if it's not . or .. */
785 || name[0] != '.'
786 || (name[1] && (name[1] != '.' || name[2]))
787 ) {
788 dirs++;
789 }
790 }
791 return which != SPLIT_FILE ? dirs : all - dirs;
792}
793
794/* get memory to hold an array of pointers */
795static struct dnode **dnalloc(unsigned num)
796{
797 if (num < 1)
798 return NULL;
799
800 num++; /* so that we have terminating NULL */
801 return xzalloc(num * sizeof(struct dnode *));
802}
803
804#if ENABLE_FEATURE_LS_RECURSIVE
805static void dfree(struct dnode **dnp)
806{
807 unsigned i;
808
809 if (dnp == NULL)
810 return;
811
812 for (i = 0; dnp[i]; i++) {
813 struct dnode *cur = dnp[i];
814 if (cur->fname_allocated)
815 free((char*)cur->fullname);
816 free(cur);
817 }
818 free(dnp);
819}
820#else
821#define dfree(...) ((void)0)
822#endif
823
824/* Returns NULL-terminated malloced vector of pointers (or NULL) */
825static struct dnode **splitdnarray(struct dnode **dn, int which)
826{
827 unsigned dncnt, d;
828 struct dnode **dnp;
829
830 if (dn == NULL)
831 return NULL;
832
833 /* count how many dirs or files there are */
834 dncnt = count_dirs(dn, which);
835
836 /* allocate a file array and a dir array */
837 dnp = dnalloc(dncnt);
838
839 /* copy the entrys into the file or dir array */
840 for (d = 0; *dn; dn++) {
841 if (S_ISDIR((*dn)->dn_mode)) {
842 const char *name;
843
844 if (which == SPLIT_FILE)
845 continue;
846
847 name = (*dn)->name;
848 if ((which & SPLIT_DIR) /* any dir... */
849 /* ... or not . or .. */
850 || name[0] != '.'
851 || (name[1] && (name[1] != '.' || name[2]))
852 ) {
853 dnp[d++] = *dn;
854 }
855 } else
856 if (which == SPLIT_FILE) {
857 dnp[d++] = *dn;
858 }
859 }
860 return dnp;
861}
862
863#if ENABLE_FEATURE_LS_SORTFILES
864static int sortcmp(const void *a, const void *b)
865{
866 struct dnode *d1 = *(struct dnode **)a;
867 struct dnode *d2 = *(struct dnode **)b;
868 unsigned sort_opts = G.all_fmt & SORT_MASK;
869 off_t dif;
870
871 dif = 0; /* assume SORT_NAME */
872 // TODO: use pre-initialized function pointer
873 // instead of branch forest
874 if (sort_opts == SORT_SIZE) {
875 dif = (d2->dn_size - d1->dn_size);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200876 } else
877 if (sort_opts == SORT_ATIME) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200878 dif = (d2->dn_atime - d1->dn_atime);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200879 } else
880 if (sort_opts == SORT_CTIME) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200881 dif = (d2->dn_ctime - d1->dn_ctime);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200882 } else
883 if (sort_opts == SORT_MTIME) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200884 dif = (d2->dn_mtime - d1->dn_mtime);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200885 } else
886 if (sort_opts == SORT_DIR) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200887 dif = S_ISDIR(d2->dn_mode) - S_ISDIR(d1->dn_mode);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200888 } else
Denys Vlasenko1e18a012011-06-21 17:12:52 +0200889#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200890 if (sort_opts == SORT_VERSION) {
891 dif = strverscmp(d1->name, d2->name);
892 } else
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200893#endif
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200894 if (sort_opts == SORT_EXT) {
895 dif = strcmp(strchrnul(d1->name, '.'), strchrnul(d2->name, '.'));
Denys Vlasenko4029e212011-05-13 17:28:46 +0200896 }
897 if (dif == 0) {
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200898 /* sort by name, use as tie breaker for other sorts */
Denys Vlasenko4029e212011-05-13 17:28:46 +0200899 if (ENABLE_LOCALE_SUPPORT)
900 dif = strcoll(d1->name, d2->name);
901 else
902 dif = strcmp(d1->name, d2->name);
903 }
904
905 /* Make dif fit into an int */
906 if (sizeof(dif) > sizeof(int)) {
907 enum { BITS_TO_SHIFT = 8 * (sizeof(dif) - sizeof(int)) };
908 /* shift leaving only "int" worth of bits */
909 if (dif != 0) {
910 dif = 1 | (int)((uoff_t)dif >> BITS_TO_SHIFT);
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100911 }
912 }
913
Denys Vlasenko4029e212011-05-13 17:28:46 +0200914 return (G.all_fmt & SORT_REVERSE) ? -(int)dif : (int)dif;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100915}
Denys Vlasenko4029e212011-05-13 17:28:46 +0200916
917static void dnsort(struct dnode **dn, int size)
918{
919 qsort(dn, size, sizeof(*dn), sortcmp);
920}
Denys Vlasenko8dd29da2011-05-13 17:55:08 +0200921
922static void sort_and_display_files(struct dnode **dn, unsigned nfiles)
923{
924 dnsort(dn, nfiles);
925 display_files(dn, nfiles);
926}
Denys Vlasenko4029e212011-05-13 17:28:46 +0200927#else
Denys Vlasenko8dd29da2011-05-13 17:55:08 +0200928# define dnsort(dn, size) ((void)0)
929# define sort_and_display_files(dn, nfiles) display_files(dn, nfiles)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100930#endif
931
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100932/* Returns NULL-terminated malloced vector of pointers (or NULL) */
Denys Vlasenko4029e212011-05-13 17:28:46 +0200933static struct dnode **scan_one_dir(const char *path, unsigned *nfiles_p)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100934{
935 struct dnode *dn, *cur, **dnp;
936 struct dirent *entry;
937 DIR *dir;
938 unsigned i, nfiles;
939
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100940 *nfiles_p = 0;
941 dir = warn_opendir(path);
942 if (dir == NULL) {
Denys Vlasenko2a816392011-05-13 17:28:09 +0200943 G.exit_code = EXIT_FAILURE;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100944 return NULL; /* could not open the dir */
945 }
946 dn = NULL;
947 nfiles = 0;
948 while ((entry = readdir(dir)) != NULL) {
949 char *fullname;
950
951 /* are we going to list the file- it may be . or .. or a hidden file */
952 if (entry->d_name[0] == '.') {
953 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
Denys Vlasenko2a816392011-05-13 17:28:09 +0200954 && !(G.all_fmt & DISP_DOT)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100955 ) {
956 continue;
957 }
Denys Vlasenko2a816392011-05-13 17:28:09 +0200958 if (!(G.all_fmt & DISP_HIDDEN))
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100959 continue;
960 }
961 fullname = concat_path_file(path, entry->d_name);
962 cur = my_stat(fullname, bb_basename(fullname), 0);
963 if (!cur) {
964 free(fullname);
965 continue;
966 }
967 cur->fname_allocated = 1;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200968 cur->dn_next = dn;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100969 dn = cur;
970 nfiles++;
971 }
972 closedir(dir);
973
974 if (dn == NULL)
975 return NULL;
976
977 /* now that we know how many files there are
978 * allocate memory for an array to hold dnode pointers
979 */
980 *nfiles_p = nfiles;
981 dnp = dnalloc(nfiles);
982 for (i = 0; /* i < nfiles - detected via !dn below */; i++) {
983 dnp[i] = dn; /* save pointer to node in array */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200984 dn = dn->dn_next;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100985 if (!dn)
986 break;
987 }
988
989 return dnp;
990}
991
Denys Vlasenko4029e212011-05-13 17:28:46 +0200992#if ENABLE_DESKTOP
993/* http://www.opengroup.org/onlinepubs/9699919799/utilities/ls.html
994 * If any of the -l, -n, -s options is specified, each list
995 * of files within the directory shall be preceded by a
996 * status line indicating the number of file system blocks
997 * occupied by files in the directory in 512-byte units if
998 * the -k option is not specified, or 1024-byte units if the
999 * -k option is specified, rounded up to the next integral
1000 * number of units.
1001 */
1002/* by Jorgen Overgaard (jorgen AT antistaten.se) */
1003static off_t calculate_blocks(struct dnode **dn)
1004{
1005 uoff_t blocks = 1;
1006 if (dn) {
1007 while (*dn) {
1008 /* st_blocks is in 512 byte blocks */
1009 blocks += (*dn)->dn_blocks;
1010 dn++;
1011 }
1012 }
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001013
Denys Vlasenko4029e212011-05-13 17:28:46 +02001014 /* Even though standard says use 512 byte blocks, coreutils use 1k */
1015 /* Actually, we round up by calculating (blocks + 1) / 2,
1016 * "+ 1" was done when we initialized blocks to 1 */
1017 return blocks >> 1;
1018}
1019#endif
1020
1021static void scan_and_display_dirs_recur(struct dnode **dn, int first)
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001022{
1023 unsigned nfiles;
1024 struct dnode **subdnp;
1025
1026 for (; *dn; dn++) {
Denys Vlasenko2a816392011-05-13 17:28:09 +02001027 if (G.all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001028 if (!first)
1029 bb_putchar('\n');
1030 first = 0;
1031 printf("%s:\n", (*dn)->fullname);
1032 }
Denys Vlasenko4029e212011-05-13 17:28:46 +02001033 subdnp = scan_one_dir((*dn)->fullname, &nfiles);
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001034#if ENABLE_DESKTOP
Denys Vlasenko2a816392011-05-13 17:28:09 +02001035 if ((G.all_fmt & STYLE_MASK) == STYLE_LONG)
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001036 printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
1037#endif
1038 if (nfiles > 0) {
1039 /* list all files at this level */
Denys Vlasenko8dd29da2011-05-13 17:55:08 +02001040 sort_and_display_files(subdnp, nfiles);
Denys Vlasenko4029e212011-05-13 17:28:46 +02001041
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001042 if (ENABLE_FEATURE_LS_RECURSIVE
Denys Vlasenko2a816392011-05-13 17:28:09 +02001043 && (G.all_fmt & DISP_RECURSIVE)
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001044 ) {
1045 struct dnode **dnd;
1046 unsigned dndirs;
1047 /* recursive - list the sub-dirs */
1048 dnd = splitdnarray(subdnp, SPLIT_SUBDIR);
1049 dndirs = count_dirs(subdnp, SPLIT_SUBDIR);
1050 if (dndirs > 0) {
1051 dnsort(dnd, dndirs);
Denys Vlasenko4029e212011-05-13 17:28:46 +02001052 scan_and_display_dirs_recur(dnd, 0);
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001053 /* free the array of dnode pointers to the dirs */
1054 free(dnd);
1055 }
1056 }
1057 /* free the dnodes and the fullname mem */
1058 dfree(subdnp);
1059 }
1060 }
1061}
1062
1063
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001064int ls_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +00001065{
Glenn L McGrath792cae52004-01-18 05:15:16 +00001066 struct dnode **dnd;
1067 struct dnode **dnf;
1068 struct dnode **dnp;
1069 struct dnode *dn;
1070 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001071 unsigned opt;
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001072 unsigned nfiles;
1073 unsigned dnfiles;
1074 unsigned dndirs;
1075 unsigned i;
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001076#if ENABLE_FEATURE_LS_COLOR
1077 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
1078 /* coreutils 6.10:
1079 * # ls --color=BOGUS
1080 * ls: invalid argument 'BOGUS' for '--color'
1081 * Valid arguments are:
1082 * 'always', 'yes', 'force'
1083 * 'never', 'no', 'none'
1084 * 'auto', 'tty', 'if-tty'
1085 * (and substrings: "--color=alwa" work too)
1086 */
1087 static const char ls_longopts[] ALIGN1 =
1088 "color\0" Optional_argument "\xff"; /* no short equivalent */
1089 static const char color_str[] ALIGN1 =
1090 "always\0""yes\0""force\0"
1091 "auto\0""tty\0""if-tty\0";
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +00001092 /* need to initialize since --color has _an optional_ argument */
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001093 const char *color_opt = color_str; /* "always" */
1094#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +00001095
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001096 INIT_G();
Denis Vlasenkoe0554432007-01-19 22:03:06 +00001097
Denys Vlasenko28055022010-01-04 20:49:58 +01001098 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001099
Denys Vlasenko9f368e32011-02-28 12:16:10 +01001100 if (ENABLE_FEATURE_LS_SORTFILES)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001101 G.all_fmt = SORT_NAME;
Glenn L McGrath792cae52004-01-18 05:15:16 +00001102
Denis Vlasenko5c759602006-10-28 12:37:16 +00001103#if ENABLE_FEATURE_AUTOWIDTH
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001104 /* obtain the terminal width */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001105 get_terminal_width_height(STDIN_FILENO, &G_terminal_width, NULL);
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001106 /* go one less... */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001107 G_terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +00001108#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001109
Eric Andersen11c65522000-09-07 17:24:47 +00001110 /* process options */
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001111 IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;)
Denys Vlasenkof3137462010-12-19 05:05:34 +01001112 opt_complementary =
Denys Vlasenko407ab2a2010-12-19 16:29:08 +01001113 /* -e implies -l */
1114 "el"
Denys Vlasenkof3137462010-12-19 05:05:34 +01001115 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html:
1116 * in some pairs of opts, only last one takes effect:
1117 */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +01001118 IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES(":t-S:S-t")) /* time/size */
Denys Vlasenko982aa262010-12-19 21:54:39 +01001119 // ":m-l:l-m" - we don't have -m
1120 IF_FEATURE_LS_FOLLOWLINKS(":H-L:L-H")
Denys Vlasenkof3137462010-12-19 05:05:34 +01001121 ":C-xl:x-Cl:l-xC" /* bycols/bylines/long */
1122 ":C-1:1-C" /* bycols/oneline */
1123 ":x-1:1-x" /* bylines/oneline (not in SuS, but in GNU coreutils 8.4) */
1124 ":c-u:u-c" /* mtime/atime */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +01001125 /* -w NUM: */
1126 IF_FEATURE_AUTOWIDTH(":w+");
Denys Vlasenkof3137462010-12-19 05:05:34 +01001127 opt = getopt32(argv, ls_options
Denys Vlasenko2a816392011-05-13 17:28:09 +02001128 IF_FEATURE_AUTOWIDTH(, NULL, &G_terminal_width)
Denys Vlasenkof3137462010-12-19 05:05:34 +01001129 IF_FEATURE_LS_COLOR(, &color_opt)
1130 );
Denys Vlasenko982aa262010-12-19 21:54:39 +01001131 for (i = 0; opt_flags[i] != (1U << 31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +00001132 if (opt & (1 << i)) {
Denys Vlasenko982aa262010-12-19 21:54:39 +01001133 uint32_t flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001134
Denys Vlasenkod87815d2010-12-19 05:43:44 +01001135 if (flags & STYLE_MASK)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001136 G.all_fmt &= ~STYLE_MASK;
Denys Vlasenkod87815d2010-12-19 05:43:44 +01001137 if (flags & SORT_MASK)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001138 G.all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +00001139 if (flags & TIME_MASK)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001140 G.all_fmt &= ~TIME_MASK;
Denys Vlasenkod87815d2010-12-19 05:43:44 +01001141
Denys Vlasenko2a816392011-05-13 17:28:09 +02001142 G.all_fmt |= flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +00001143 }
Eric Andersen11c65522000-09-07 17:24:47 +00001144 }
1145
Denis Vlasenko5c759602006-10-28 12:37:16 +00001146#if ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +02001147 /* set G_show_color = 1/0 */
Denis Vlasenko5c759602006-10-28 12:37:16 +00001148 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
1149 char *p = getenv("LS_COLORS");
1150 /* LS_COLORS is unset, or (not empty && not "none") ? */
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +00001151 if (!p || (p[0] && strcmp(p, "none") != 0))
Denys Vlasenko2a816392011-05-13 17:28:09 +02001152 G_show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +00001153 }
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001154 if (opt & OPT_color) {
1155 if (color_opt[0] == 'n')
Denys Vlasenko2a816392011-05-13 17:28:09 +02001156 G_show_color = 0;
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001157 else switch (index_in_substrings(color_str, color_opt)) {
1158 case 3:
1159 case 4:
1160 case 5:
1161 if (isatty(STDOUT_FILENO)) {
1162 case 0:
1163 case 1:
1164 case 2:
Denys Vlasenko2a816392011-05-13 17:28:09 +02001165 G_show_color = 1;
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001166 }
1167 }
Paul Fox156dc412005-08-01 19:33:30 +00001168 }
1169#endif
1170
Eric Andersen11c65522000-09-07 17:24:47 +00001171 /* sort out which command line options take precedence */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001172 if (ENABLE_FEATURE_LS_RECURSIVE && (G.all_fmt & DISP_NOLIST))
1173 G.all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +00001174 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
Denys Vlasenko2a816392011-05-13 17:28:09 +02001175 if (G.all_fmt & TIME_CHANGE)
1176 G.all_fmt = (G.all_fmt & ~SORT_MASK) | SORT_CTIME;
1177 if (G.all_fmt & TIME_ACCESS)
1178 G.all_fmt = (G.all_fmt & ~SORT_MASK) | SORT_ATIME;
Rob Landleyea224be2006-06-18 20:20:07 +00001179 }
Denys Vlasenko2a816392011-05-13 17:28:09 +02001180 if ((G.all_fmt & STYLE_MASK) != STYLE_LONG) /* not -l? */
1181 G.all_fmt &= ~(LIST_ID_NUMERIC|LIST_ID_NAME|LIST_FULLTIME);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001182
Denys Vlasenkof3137462010-12-19 05:05:34 +01001183 /* choose a display format if one was not already specified by an option */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001184 if (!(G.all_fmt & STYLE_MASK))
1185 G.all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNAR : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +00001186
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001187 argv += optind;
1188 if (!argv[0])
1189 *--argv = (char*)".";
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001190
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001191 if (argv[1])
Denys Vlasenko2a816392011-05-13 17:28:09 +02001192 G.all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001193
Denis Vlasenko5c759602006-10-28 12:37:16 +00001194 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001195 dn = NULL;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001196 nfiles = 0;
1197 do {
Denys Vlasenko163d8642010-12-19 06:16:28 +01001198 cur = my_stat(*argv, *argv,
Denys Vlasenko982aa262010-12-19 21:54:39 +01001199 /* follow links on command line unless -l, -s or -F: */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001200 !((G.all_fmt & STYLE_MASK) == STYLE_LONG
1201 || (G.all_fmt & LIST_BLOCKS)
Denys Vlasenkoea684c62011-03-12 03:12:36 +01001202 || (option_mask32 & OPT_F)
1203 )
Denys Vlasenko982aa262010-12-19 21:54:39 +01001204 /* ... or if -H: */
1205 || (option_mask32 & OPT_H)
Denys Vlasenkod27ac292011-05-13 17:27:15 +02001206 /* ... or if -L, but my_stat always follows links if -L */
Denys Vlasenko163d8642010-12-19 06:16:28 +01001207 );
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001208 argv++;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001209 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001210 continue;
Denys Vlasenko2a816392011-05-13 17:28:09 +02001211 /*cur->fname_allocated = 0; - already is */
1212 cur->dn_next = dn;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001213 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001214 nfiles++;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001215 } while (*argv);
Eric Andersen11c65522000-09-07 17:24:47 +00001216
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001217 /* nfiles _may_ be 0 here - try "ls doesnt_exist" */
1218 if (nfiles == 0)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001219 return G.exit_code;
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001220
Eric Andersen11c65522000-09-07 17:24:47 +00001221 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +00001222 * allocate memory for an array to hold dnode pointers
1223 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001224 dnp = dnalloc(nfiles);
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001225 for (i = 0; /* i < nfiles - detected via !dn below */; i++) {
1226 dnp[i] = dn; /* save pointer to node in array */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001227 dn = dn->dn_next;
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001228 if (!dn)
1229 break;
Eric Andersen11c65522000-09-07 17:24:47 +00001230 }
1231
Denys Vlasenko2a816392011-05-13 17:28:09 +02001232 if (G.all_fmt & DISP_NOLIST) {
Denys Vlasenko8dd29da2011-05-13 17:55:08 +02001233 sort_and_display_files(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001234 } else {
Denys Vlasenkocae409c2009-10-03 11:43:48 +02001235 dnd = splitdnarray(dnp, SPLIT_DIR);
1236 dnf = splitdnarray(dnp, SPLIT_FILE);
1237 dndirs = count_dirs(dnp, SPLIT_DIR);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001238 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001239 if (dnfiles > 0) {
Denys Vlasenko8dd29da2011-05-13 17:55:08 +02001240 sort_and_display_files(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +00001241 if (ENABLE_FEATURE_CLEAN_UP)
1242 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +00001243 }
1244 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +00001245 dnsort(dnd, dndirs);
Denys Vlasenko4029e212011-05-13 17:28:46 +02001246 scan_and_display_dirs_recur(dnd, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +00001247 if (ENABLE_FEATURE_CLEAN_UP)
1248 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +00001249 }
1250 }
Denys Vlasenko2a816392011-05-13 17:28:09 +02001251
Rob Landley26314862006-05-02 19:46:52 +00001252 if (ENABLE_FEATURE_CLEAN_UP)
Denys Vlasenkocae409c2009-10-03 11:43:48 +02001253 dfree(dnp);
Denys Vlasenko2a816392011-05-13 17:28:09 +02001254 return G.exit_code;
Eric Andersencc8ed391999-10-05 16:24:54 +00001255}