blob: e8c3e0490ef1f767e10b3341052606825634fd46 [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 Vlasenkoe6a2f4c2016-04-21 16:26:30 +020096#include "common_bufsiz.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020097#include "unicode.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000098
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +000099
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000100/* This is a NOEXEC applet. Be very careful! */
101
Eric Andersenf1142c52001-02-20 06:16:29 +0000102
Denis Vlasenko245f91b2009-03-09 22:37:23 +0000103#if ENABLE_FTPD
104/* ftpd uses ls, and without timestamps Mozilla won't understand
105 * ftpd's LIST output.
106 */
107# undef CONFIG_FEATURE_LS_TIMESTAMPS
108# undef ENABLE_FEATURE_LS_TIMESTAMPS
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000109# undef IF_FEATURE_LS_TIMESTAMPS
110# undef IF_NOT_FEATURE_LS_TIMESTAMPS
Denis Vlasenko245f91b2009-03-09 22:37:23 +0000111# define CONFIG_FEATURE_LS_TIMESTAMPS 1
112# define ENABLE_FEATURE_LS_TIMESTAMPS 1
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000113# define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__
114# define IF_NOT_FEATURE_LS_TIMESTAMPS(...)
Denis Vlasenko245f91b2009-03-09 22:37:23 +0000115#endif
116
117
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000118enum {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000119TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000120
Denys Vlasenko982aa262010-12-19 21:54:39 +0100121SPLIT_FILE = 0,
Denys Vlasenko2a816392011-05-13 17:28:09 +0200122SPLIT_DIR = 1,
Denys Vlasenko982aa262010-12-19 21:54:39 +0100123SPLIT_SUBDIR = 2,
124
Denys Vlasenko2a816392011-05-13 17:28:09 +0200125/* Bits in G.all_fmt: */
Eric Andersencc8ed391999-10-05 16:24:54 +0000126
Eric Andersen11c65522000-09-07 17:24:47 +0000127/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
128/* what file information will be listed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000129LIST_INO = 1 << 0,
130LIST_BLOCKS = 1 << 1,
131LIST_MODEBITS = 1 << 2,
132LIST_NLINKS = 1 << 3,
133LIST_ID_NAME = 1 << 4,
134LIST_ID_NUMERIC = 1 << 5,
135LIST_CONTEXT = 1 << 6,
136LIST_SIZE = 1 << 7,
James Youngmana4bc10c2010-12-20 01:36:16 +0100137LIST_DATE_TIME = 1 << 8,
138LIST_FULLTIME = 1 << 9,
139LIST_SYMLINK = 1 << 10,
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200140LIST_FILETYPE = 1 << 11, /* show / suffix for dirs */
141LIST_CLASSIFY = 1 << 12, /* requires LIST_FILETYPE, also show *,|,@,= suffixes */
142LIST_MASK = (LIST_CLASSIFY << 1) - 1,
Eric Andersen11c65522000-09-07 17:24:47 +0000143
144/* what files will be displayed */
James Youngmana4bc10c2010-12-20 01:36:16 +0100145DISP_DIRNAME = 1 << 13, /* 2 or more items? label directories */
146DISP_HIDDEN = 1 << 14, /* show filenames starting with . */
147DISP_DOT = 1 << 15, /* show . and .. */
148DISP_NOLIST = 1 << 16, /* show directory as itself, not contents */
149DISP_RECURSIVE = 1 << 17, /* show directory and everything below it */
150DISP_ROWS = 1 << 18, /* print across rows */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000151DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152
Denys Vlasenko982aa262010-12-19 21:54:39 +0100153/* what is the overall style of the listing */
James Youngmana4bc10c2010-12-20 01:36:16 +0100154STYLE_COLUMNAR = 1 << 19, /* many records per line */
155STYLE_LONG = 2 << 19, /* one record per line, extended info */
156STYLE_SINGLE = 3 << 19, /* one record per line */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100157STYLE_MASK = STYLE_SINGLE,
Eric Andersencc8ed391999-10-05 16:24:54 +0000158
Eric Andersen11c65522000-09-07 17:24:47 +0000159/* which of the three times will be used */
James Youngmana4bc10c2010-12-20 01:36:16 +0100160TIME_CHANGE = (1 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100161TIME_ACCESS = (2 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
James Youngmana4bc10c2010-12-20 01:36:16 +0100162TIME_MASK = (3 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163
Denys Vlasenko982aa262010-12-19 21:54:39 +0100164/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
James Youngmana4bc10c2010-12-20 01:36:16 +0100165SORT_REVERSE = 1 << 23,
Eric Andersencc8ed391999-10-05 16:24:54 +0000166
Denys Vlasenko982aa262010-12-19 21:54:39 +0100167SORT_NAME = 0, /* sort by file name */
James Youngmana4bc10c2010-12-20 01:36:16 +0100168SORT_SIZE = 1 << 24, /* sort by file size */
169SORT_ATIME = 2 << 24, /* sort by last access time */
170SORT_CTIME = 3 << 24, /* sort by last change time */
171SORT_MTIME = 4 << 24, /* sort by last modification time */
172SORT_VERSION = 5 << 24, /* sort by version */
173SORT_EXT = 6 << 24, /* sort by file name extension */
174SORT_DIR = 7 << 24, /* sort by file or directory */
175SORT_MASK = (7 << 24) * ENABLE_FEATURE_LS_SORTFILES,
Eric Andersencc8ed391999-10-05 16:24:54 +0000176
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000177LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
James Youngmana4bc10c2010-12-20 01:36:16 +0100178 LIST_DATE_TIME | LIST_SYMLINK,
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000179};
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100181/* -Cadil1 Std options, busybox always supports */
182/* -gnsxA Std options, busybox always supports */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100183/* -Q GNU option, busybox always supports */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100184/* -k SELinux option, busybox always supports (ignores if !SELinux) */
185/* Std has -k which means "show sizes in kbytes" */
Denys Vlasenko8ea683d2011-06-13 02:24:18 +0200186/* -LHRctur Std options, busybox optionally supports */
187/* -Fp Std options, busybox optionally supports */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100188/* -SXvhTw GNU options, busybox optionally supports */
Denys Vlasenko8ea683d2011-06-13 02:24:18 +0200189/* -T WIDTH Ignored (we don't use tabs on output) */
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100190/* -KZ SELinux mandated options, busybox optionally supports */
191/* (coreutils 8.4 has no -K, remove it?) */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100192/* -e I think we made this one up (looks similar to GNU --full-time) */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100193/* We already used up all 32 bits, if we need to add more, candidates for removal: */
194/* -K, -T, -e (add --full-time instead) */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000195static const char ls_options[] ALIGN1 =
Denys Vlasenko982aa262010-12-19 21:54:39 +0100196 "Cadil1gnsxQAk" /* 13 opts, total 13 */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000197 IF_FEATURE_LS_TIMESTAMPS("cetu") /* 4, 17 */
198 IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 21 */
199 IF_FEATURE_LS_FILETYPES("Fp") /* 2, 23 */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100200 IF_FEATURE_LS_RECURSIVE("R") /* 1, 24 */
201 IF_SELINUX("KZ") /* 2, 26 */
202 IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 28 */
203 IF_FEATURE_HUMAN_READABLE("h") /* 1, 29 */
204 IF_FEATURE_AUTOWIDTH("T:w:") /* 2, 31 */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100205 /* with --color, we use all 32 bits */;
Denis Vlasenko248ce912009-03-03 14:09:04 +0000206enum {
207 //OPT_C = (1 << 0),
208 //OPT_a = (1 << 1),
209 //OPT_d = (1 << 2),
210 //OPT_i = (1 << 3),
211 //OPT_l = (1 << 4),
212 //OPT_1 = (1 << 5),
213 OPT_g = (1 << 6),
214 //OPT_n = (1 << 7),
215 //OPT_s = (1 << 8),
216 //OPT_x = (1 << 9),
217 OPT_Q = (1 << 10),
218 //OPT_A = (1 << 11),
219 //OPT_k = (1 << 12),
Denys Vlasenko982aa262010-12-19 21:54:39 +0100220
221 OPTBIT_c = 13,
222 OPTBIT_e,
223 OPTBIT_t,
224 OPTBIT_u,
225 OPTBIT_S = OPTBIT_c + 4 * ENABLE_FEATURE_LS_TIMESTAMPS,
226 OPTBIT_X, /* 18 */
227 OPTBIT_r,
228 OPTBIT_v,
229 OPTBIT_F = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES,
230 OPTBIT_p, /* 22 */
231 OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES,
232 OPTBIT_K = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE,
233 OPTBIT_Z, /* 25 */
234 OPTBIT_L = OPTBIT_K + 2 * ENABLE_SELINUX,
235 OPTBIT_H, /* 27 */
Denys Vlasenkocd387f22011-02-27 04:10:00 +0100236 OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS,
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100237 OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE,
Denys Vlasenko982aa262010-12-19 21:54:39 +0100238 OPTBIT_w, /* 30 */
239 OPTBIT_color = OPTBIT_T + 2 * ENABLE_FEATURE_AUTOWIDTH,
240
241 OPT_c = (1 << OPTBIT_c) * ENABLE_FEATURE_LS_TIMESTAMPS,
242 OPT_e = (1 << OPTBIT_e) * ENABLE_FEATURE_LS_TIMESTAMPS,
243 OPT_t = (1 << OPTBIT_t) * ENABLE_FEATURE_LS_TIMESTAMPS,
244 OPT_u = (1 << OPTBIT_u) * ENABLE_FEATURE_LS_TIMESTAMPS,
245 OPT_S = (1 << OPTBIT_S) * ENABLE_FEATURE_LS_SORTFILES,
246 OPT_X = (1 << OPTBIT_X) * ENABLE_FEATURE_LS_SORTFILES,
247 OPT_r = (1 << OPTBIT_r) * ENABLE_FEATURE_LS_SORTFILES,
248 OPT_v = (1 << OPTBIT_v) * ENABLE_FEATURE_LS_SORTFILES,
249 OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES,
250 OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES,
251 OPT_R = (1 << OPTBIT_R) * ENABLE_FEATURE_LS_RECURSIVE,
252 OPT_K = (1 << OPTBIT_K) * ENABLE_SELINUX,
253 OPT_Z = (1 << OPTBIT_Z) * ENABLE_SELINUX,
254 OPT_L = (1 << OPTBIT_L) * ENABLE_FEATURE_LS_FOLLOWLINKS,
255 OPT_H = (1 << OPTBIT_H) * ENABLE_FEATURE_LS_FOLLOWLINKS,
256 OPT_h = (1 << OPTBIT_h) * ENABLE_FEATURE_HUMAN_READABLE,
257 OPT_T = (1 << OPTBIT_T) * ENABLE_FEATURE_AUTOWIDTH,
258 OPT_w = (1 << OPTBIT_w) * ENABLE_FEATURE_AUTOWIDTH,
259 OPT_color = (1 << OPTBIT_color) * ENABLE_FEATURE_LS_COLOR,
Denis Vlasenko248ce912009-03-03 14:09:04 +0000260};
261
Denis Vlasenko248ce912009-03-03 14:09:04 +0000262/* TODO: simple toggles may be stored as OPT_xxx bits instead */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100263static const uint32_t opt_flags[] = {
Denys Vlasenko69675782013-01-14 01:34:48 +0100264 STYLE_COLUMNAR, /* C */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100265 DISP_HIDDEN | DISP_DOT, /* a */
266 DISP_NOLIST, /* d */
267 LIST_INO, /* i */
Denys Vlasenko982aa262010-12-19 21:54:39 +0100268 LIST_LONG | STYLE_LONG, /* l */
James Youngmana4bc10c2010-12-20 01:36:16 +0100269 STYLE_SINGLE, /* 1 */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100270 LIST_LONG | STYLE_LONG, /* g (don't show owner) - handled via OPT_g. assumes l */
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100271 LIST_ID_NUMERIC | LIST_LONG | STYLE_LONG, /* n (assumes l) */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100272 LIST_BLOCKS, /* s */
James Youngmana4bc10c2010-12-20 01:36:16 +0100273 DISP_ROWS | STYLE_COLUMNAR, /* x */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100274 0, /* Q (quote filename) - handled via OPT_Q */
275 DISP_HIDDEN, /* A */
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100276 ENABLE_SELINUX * (LIST_CONTEXT|STYLE_SINGLE), /* k (ignored if !SELINUX) */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000277#if ENABLE_FEATURE_LS_TIMESTAMPS
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100278 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100279 LIST_FULLTIME, /* e */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +0100280 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
281 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000282#endif
283#if ENABLE_FEATURE_LS_SORTFILES
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100284 SORT_SIZE, /* S */
285 SORT_EXT, /* X */
286 SORT_REVERSE, /* r */
287 SORT_VERSION, /* v */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000288#endif
289#if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200290 LIST_FILETYPE | LIST_CLASSIFY, /* F */
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100291 LIST_FILETYPE, /* p */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000292#endif
Denis Vlasenko248ce912009-03-03 14:09:04 +0000293#if ENABLE_FEATURE_LS_RECURSIVE
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100294 DISP_RECURSIVE, /* R */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000295#endif
Denis Vlasenko248ce912009-03-03 14:09:04 +0000296#if ENABLE_SELINUX
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100297 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME|STYLE_SINGLE, /* K */
298 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT|STYLE_SINGLE, /* Z */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000299#endif
Denys Vlasenko982aa262010-12-19 21:54:39 +0100300 (1U << 31)
301 /* options after Z are not processed through opt_flags */
Denis Vlasenko248ce912009-03-03 14:09:04 +0000302};
303
304
Eric Andersen11c65522000-09-07 17:24:47 +0000305/*
Denys Vlasenko2a816392011-05-13 17:28:09 +0200306 * a directory entry and its stat info
Eric Andersen11c65522000-09-07 17:24:47 +0000307 */
Denys Vlasenko76c7d952009-10-03 01:15:47 +0200308struct dnode {
Denys Vlasenko2a816392011-05-13 17:28:09 +0200309 const char *name; /* usually basename, but think "ls -l dir/file" */
310 const char *fullname; /* full name (usable for stat etc) */
311 struct dnode *dn_next; /* for linked list */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000312 IF_SELINUX(security_context_t sid;)
Denys Vlasenko2a816392011-05-13 17:28:09 +0200313 smallint fname_allocated;
314
315 /* Used to avoid re-doing [l]stat at printout stage
316 * if we already collected needed data in scan stage:
317 */
318 mode_t dn_mode_lstat; /* obtained with lstat, or 0 */
319 mode_t dn_mode_stat; /* obtained with stat, or 0 */
320
321// struct stat dstat;
322// struct stat is huge. We don't need it in full.
323// At least we don't need st_dev and st_blksize,
324// but there are invisible fields as well
325// (such as nanosecond-resolution timespamps)
326// and padding, which we also don't want to store.
327// We also can pre-parse dev_t dn_rdev (in glibc, it's huge).
Denys Vlasenko4029e212011-05-13 17:28:46 +0200328// On 32-bit uclibc: dnode size went from 112 to 84 bytes.
Denys Vlasenko2a816392011-05-13 17:28:09 +0200329//
330 /* Same names as in struct stat, but with dn_ instead of st_ pfx: */
331 mode_t dn_mode; /* obtained with lstat OR stat, depending on -L etc */
332 off_t dn_size;
333#if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES
334 time_t dn_atime;
335 time_t dn_mtime;
336 time_t dn_ctime;
337#endif
338 ino_t dn_ino;
339 blkcnt_t dn_blocks;
340 nlink_t dn_nlink;
341 uid_t dn_uid;
342 gid_t dn_gid;
343 int dn_rdev_maj;
344 int dn_rdev_min;
345// dev_t dn_dev;
346// blksize_t dn_blksize;
Eric Andersen11c65522000-09-07 17:24:47 +0000347};
Eric Andersen11c65522000-09-07 17:24:47 +0000348
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000349struct globals {
350#if ENABLE_FEATURE_LS_COLOR
351 smallint show_color;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200352# define G_show_color (G.show_color)
353#else
354# define G_show_color 0
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000355#endif
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000356 smallint exit_code;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000357 unsigned all_fmt;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000358#if ENABLE_FEATURE_AUTOWIDTH
Denys Vlasenko2a816392011-05-13 17:28:09 +0200359 unsigned terminal_width;
360# define G_terminal_width (G.terminal_width)
361#else
362# define G_terminal_width TERMINAL_WIDTH
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000363#endif
364#if ENABLE_FEATURE_LS_TIMESTAMPS
365 /* Do time() just once. Saves one syscall per file for "ls -l" */
366 time_t current_time_t;
367#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100368} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200369#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000370#define INIT_G() do { \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200371 /* we have to zero it out because of NOEXEC */ \
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000372 memset(&G, 0, sizeof(G)); \
Denys Vlasenko2a816392011-05-13 17:28:09 +0200373 IF_FEATURE_AUTOWIDTH(G_terminal_width = TERMINAL_WIDTH;) \
374 IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000375} while (0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000376
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000377
Denys Vlasenko4029e212011-05-13 17:28:46 +0200378/*** Output code ***/
Denys Vlasenko2a816392011-05-13 17:28:09 +0200379
Eric Andersen79565b62000-08-11 18:10:21 +0000380
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000381/* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket
382 * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file
383 * 3/7:multiplexed char/block device)
384 * and we use 0 for unknown and 15 for executables (see below) */
385#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200386/* un fi chr - dir - blk - file - link - sock - - exe */
387#define APPCHAR(mode) ("\0""|""\0""\0""/""\0""\0""\0""\0""\0""@""\0""=""\0""\0""\0" [TYPEINDEX(mode)])
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000388/* 036 black foreground 050 black background
389 037 red foreground 051 red background
390 040 green foreground 052 green background
391 041 brown foreground 053 brown background
392 042 blue foreground 054 blue background
393 043 magenta (purple) foreground 055 magenta background
394 044 cyan (light blue) foreground 056 cyan background
395 045 gray foreground 057 white background
396*/
397#define COLOR(mode) ( \
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200398 /*un fi chr - dir - blk - file - link - sock - - exe */ \
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000399 "\037\043\043\045\042\045\043\043\000\045\044\045\043\045\045\040" \
400 [TYPEINDEX(mode)])
401/* Select normal (0) [actually "reset all"] or bold (1)
402 * (other attributes are 2:dim 4:underline 5:blink 7:reverse,
403 * let's use 7 for "impossible" types, just for fun)
404 * Note: coreutils 6.9 uses inverted red for setuid binaries.
405 */
406#define ATTR(mode) ( \
Denys Vlasenko4ad95e62011-05-12 18:40:59 +0200407 /*un fi chr - dir - blk - file- link- sock- - exe */ \
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000408 "\01\00\01\07\01\07\01\07\00\07\01\07\01\07\07\01" \
409 [TYPEINDEX(mode)])
410
Denis Vlasenko5c759602006-10-28 12:37:16 +0000411#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000412/* mode of zero is interpreted as "unknown" (stat failed) */
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000413static char fgcolor(mode_t mode)
414{
Rob Landley9947a242006-06-15 22:11:10 +0000415 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000416 return COLOR(0xF000); /* File is executable ... */
417 return COLOR(mode);
418}
Denis Vlasenkoc1969f62009-03-18 22:39:34 +0000419static char bold(mode_t mode)
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000420{
Rob Landley9947a242006-06-15 22:11:10 +0000421 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000422 return ATTR(0xF000); /* File is executable ... */
423 return ATTR(mode);
424}
425#endif
426
Denys Vlasenko2a816392011-05-13 17:28:09 +0200427#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000428static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000429{
Denys Vlasenko2a816392011-05-13 17:28:09 +0200430 if (!(G.all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000431 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000432 if (S_ISDIR(mode))
433 return '/';
Denys Vlasenko2a816392011-05-13 17:28:09 +0200434 if (!(G.all_fmt & LIST_CLASSIFY))
Rob Landley9947a242006-06-15 22:11:10 +0000435 return '\0';
436 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000437 return '*';
438 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000439}
440#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000441
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100442static unsigned calc_name_len(const char *name)
Eric Andersen11c65522000-09-07 17:24:47 +0000443{
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100444 unsigned len;
445 uni_stat_t uni_stat;
Eric Andersen11c65522000-09-07 17:24:47 +0000446
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100447 // TODO: quote tab as \t, etc, if -Q
448 name = printable_string(&uni_stat, name);
Eric Andersen11c65522000-09-07 17:24:47 +0000449
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100450 if (!(option_mask32 & OPT_Q)) {
451 return uni_stat.unicode_width;
452 }
453
454 len = 2 + uni_stat.unicode_width;
455 while (*name) {
456 if (*name == '"' || *name == '\\') {
457 len++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000458 }
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100459 name++;
Eric Andersen11c65522000-09-07 17:24:47 +0000460 }
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100461 return len;
Eric Andersen11c65522000-09-07 17:24:47 +0000462}
463
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100464/* Return the number of used columns.
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100465 * Note that only STYLE_COLUMNAR uses return value.
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100466 * STYLE_SINGLE and STYLE_LONG don't care.
467 * coreutils 7.2 also supports:
468 * ls -b (--escape) = octal escapes (although it doesn't look like working)
469 * ls -N (--literal) = not escape at all
Denys Vlasenko0683d4d2009-10-03 10:53:36 +0200470 */
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100471static unsigned print_name(const char *name)
Denys Vlasenko87c150c2009-10-03 01:14:15 +0200472{
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100473 unsigned len;
474 uni_stat_t uni_stat;
475
476 // TODO: quote tab as \t, etc, if -Q
477 name = printable_string(&uni_stat, name);
478
479 if (!(option_mask32 & OPT_Q)) {
480 fputs(name, stdout);
481 return uni_stat.unicode_width;
Denys Vlasenko87c150c2009-10-03 01:14:15 +0200482 }
483
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100484 len = 2 + uni_stat.unicode_width;
485 putchar('"');
486 while (*name) {
487 if (*name == '"' || *name == '\\') {
488 putchar('\\');
489 len++;
Eric Andersen11c65522000-09-07 17:24:47 +0000490 }
Dan Fandrich77d48722010-09-07 23:38:28 -0700491 putchar(*name);
492 name++;
Eric Andersen11c65522000-09-07 17:24:47 +0000493 }
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100494 putchar('"');
495 return len;
Eric Andersen11c65522000-09-07 17:24:47 +0000496}
497
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100498/* Return the number of used columns.
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100499 * Note that only STYLE_COLUMNAR uses return value,
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100500 * STYLE_SINGLE and STYLE_LONG don't care.
501 */
Denys Vlasenko4029e212011-05-13 17:28:46 +0200502static NOINLINE unsigned display_single(const struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000503{
Denys Vlasenko76c7d952009-10-03 01:15:47 +0200504 unsigned column = 0;
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200505 char *lpath;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000506#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +0200507 struct stat statbuf;
Eric Andersen11c65522000-09-07 17:24:47 +0000508 char append;
509#endif
510
Denis Vlasenko5c759602006-10-28 12:37:16 +0000511#if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko2a816392011-05-13 17:28:09 +0200512 append = append_char(dn->dn_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000513#endif
514
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000515 /* Do readlink early, so that if it fails, error message
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100516 * does not appear *inside* the "ls -l" line */
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200517 lpath = NULL;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200518 if (G.all_fmt & LIST_SYMLINK)
519 if (S_ISLNK(dn->dn_mode))
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000520 lpath = xmalloc_readlink_or_warn(dn->fullname);
521
Denys Vlasenko2a816392011-05-13 17:28:09 +0200522 if (G.all_fmt & LIST_INO)
523 column += printf("%7llu ", (long long) dn->dn_ino);
Denys Vlasenko3b28dae2011-03-01 05:37:41 +0100524//TODO: -h should affect -s too:
Denys Vlasenko2a816392011-05-13 17:28:09 +0200525 if (G.all_fmt & LIST_BLOCKS)
526 column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1));
527 if (G.all_fmt & LIST_MODEBITS)
528 column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode));
529 if (G.all_fmt & LIST_NLINKS)
530 column += printf("%4lu ", (long) dn->dn_nlink);
531 if (G.all_fmt & LIST_ID_NUMERIC) {
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100532 if (option_mask32 & OPT_g)
Denys Vlasenko2a816392011-05-13 17:28:09 +0200533 column += printf("%-8u ", (int) dn->dn_gid);
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100534 else
535 column += printf("%-8u %-8u ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200536 (int) dn->dn_uid,
537 (int) dn->dn_gid);
Denys Vlasenko9f368e32011-02-28 12:16:10 +0100538 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000539#if ENABLE_FEATURE_LS_USERNAME
Denys Vlasenko2a816392011-05-13 17:28:09 +0200540 else if (G.all_fmt & LIST_ID_NAME) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000541 if (option_mask32 & OPT_g) {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100542 column += printf("%-8.8s ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200543 get_cached_groupname(dn->dn_gid));
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000544 } else {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100545 column += printf("%-8.8s %-8.8s ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200546 get_cached_username(dn->dn_uid),
547 get_cached_groupname(dn->dn_gid));
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000548 }
549 }
Eric Andersen11c65522000-09-07 17:24:47 +0000550#endif
Denys Vlasenko2a816392011-05-13 17:28:09 +0200551 if (G.all_fmt & LIST_SIZE) {
552 if (S_ISBLK(dn->dn_mode) || S_ISCHR(dn->dn_mode)) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000553 column += printf("%4u, %3u ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200554 dn->dn_rdev_maj,
555 dn->dn_rdev_min);
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000556 } else {
Denys Vlasenko982aa262010-12-19 21:54:39 +0100557 if (option_mask32 & OPT_h) {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100558 column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ",
Denys Vlasenko2a816392011-05-13 17:28:09 +0200559 /* print size, show one fractional, use suffixes */
560 make_human_readable_str(dn->dn_size, 1, 0)
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200561 );
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000562 } else {
Denys Vlasenko2a816392011-05-13 17:28:09 +0200563 column += printf("%9"OFF_FMT"u ", dn->dn_size);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000564 }
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000565 }
566 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000567#if ENABLE_FEATURE_LS_TIMESTAMPS
Denys Vlasenko2a816392011-05-13 17:28:09 +0200568 if (G.all_fmt & (LIST_FULLTIME|LIST_DATE_TIME)) {
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100569 char *filetime;
Denys Vlasenkoc5beaa02015-02-23 15:25:58 +0100570 const time_t *ttime = &dn->dn_mtime;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200571 if (G.all_fmt & TIME_ACCESS)
Denys Vlasenko0cb981c2015-02-18 11:29:07 +0100572 ttime = &dn->dn_atime;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200573 if (G.all_fmt & TIME_CHANGE)
Denys Vlasenko0cb981c2015-02-18 11:29:07 +0100574 ttime = &dn->dn_ctime;
575 filetime = ctime(ttime);
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100576 /* filetime's format: "Wed Jun 30 21:49:08 1993\n" */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200577 if (G.all_fmt & LIST_FULLTIME) { /* -e */
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100578 /* Note: coreutils 8.4 ls --full-time prints:
579 * 2009-07-13 17:49:27.000000000 +0200
580 */
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100581 column += printf("%.24s ", filetime);
Denys Vlasenko26d11b82011-02-28 12:38:08 +0100582 } else { /* LIST_DATE_TIME */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200583 /* G.current_time_t ~== time(NULL) */
Denys Vlasenko0cb981c2015-02-18 11:29:07 +0100584 time_t age = G.current_time_t - *ttime;
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000585 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
Denys Vlasenko8e92df12015-02-16 15:36:25 +0100586 /* less than 6 months old */
587 /* "mmm dd hh:mm " */
588 printf("%.12s ", filetime + 4);
589 } else {
590 /* "mmm dd yyyy " */
591 /* "mmm dd yyyyy " after year 9999 :) */
592 strchr(filetime + 20, '\n')[0] = ' ';
593 printf("%.7s%6s", filetime + 4, filetime + 20);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000594 }
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000595 column += 13;
596 }
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +0100597 }
Eric Andersen11c65522000-09-07 17:24:47 +0000598#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000599#if ENABLE_SELINUX
Denys Vlasenko2a816392011-05-13 17:28:09 +0200600 if (G.all_fmt & LIST_CONTEXT) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000601 column += printf("%-32s ", dn->sid ? dn->sid : "unknown");
602 freecon(dn->sid);
603 }
Eric Andersen9e480452003-07-03 10:07:04 +0000604#endif
James Youngmana4bc10c2010-12-20 01:36:16 +0100605
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000606#if ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +0200607 if (G_show_color) {
608 mode_t mode = dn->dn_mode_lstat;
609 if (!mode)
610 if (lstat(dn->fullname, &statbuf) == 0)
611 mode = statbuf.st_mode;
612 printf("\033[%u;%um", bold(mode), fgcolor(mode));
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000613 }
James Youngmana4bc10c2010-12-20 01:36:16 +0100614#endif
615 column += print_name(dn->name);
Denys Vlasenko2a816392011-05-13 17:28:09 +0200616 if (G_show_color) {
James Youngmana4bc10c2010-12-20 01:36:16 +0100617 printf("\033[0m");
618 }
619
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200620 if (lpath) {
621 printf(" -> ");
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000622#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +0200623 if ((G.all_fmt & LIST_FILETYPE) || G_show_color) {
624 mode_t mode = dn->dn_mode_stat;
625 if (!mode)
626 if (stat(dn->fullname, &statbuf) == 0)
627 mode = statbuf.st_mode;
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200628# if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko2a816392011-05-13 17:28:09 +0200629 append = append_char(mode);
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200630# endif
Denys Vlasenko2a816392011-05-13 17:28:09 +0200631# if ENABLE_FEATURE_LS_COLOR
632 if (G_show_color) {
633 printf("\033[%u;%um", bold(mode), fgcolor(mode));
634 }
635# endif
Eric Andersen11c65522000-09-07 17:24:47 +0000636 }
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200637#endif
638 column += print_name(lpath) + 4;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200639 free(lpath);
640 if (G_show_color) {
Denys Vlasenkod27ac292011-05-13 17:27:15 +0200641 printf("\033[0m");
642 }
Eric Andersen11c65522000-09-07 17:24:47 +0000643 }
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000644#if ENABLE_FEATURE_LS_FILETYPES
Denys Vlasenko2a816392011-05-13 17:28:09 +0200645 if (G.all_fmt & LIST_FILETYPE) {
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000646 if (append) {
647 putchar(append);
648 column++;
649 }
650 }
651#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000652
Glenn L McGrath4d001292003-01-06 01:11:50 +0000653 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000654}
655
Denys Vlasenko4029e212011-05-13 17:28:46 +0200656static void display_files(struct dnode **dn, unsigned nfiles)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100657{
658 unsigned i, ncols, nrows, row, nc;
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +0100659 unsigned column;
660 unsigned nexttab;
Denys Vlasenkod87815d2010-12-19 05:43:44 +0100661 unsigned column_width = 0; /* used only by STYLE_COLUMNAR */
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100662
Denys Vlasenko2a816392011-05-13 17:28:09 +0200663 if (G.all_fmt & STYLE_LONG) { /* STYLE_LONG or STYLE_SINGLE */
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100664 ncols = 1;
665 } else {
666 /* find the longest file name, use that as the column width */
667 for (i = 0; dn[i]; i++) {
668 int len = calc_name_len(dn[i]->name);
669 if (column_width < len)
670 column_width = len;
671 }
Denys Vlasenkoea351b92016-03-06 17:53:11 +0100672 column_width += 2 +
Denys Vlasenko2a816392011-05-13 17:28:09 +0200673 IF_SELINUX( ((G.all_fmt & LIST_CONTEXT) ? 33 : 0) + )
674 ((G.all_fmt & LIST_INO) ? 8 : 0) +
675 ((G.all_fmt & LIST_BLOCKS) ? 5 : 0);
676 ncols = (unsigned)G_terminal_width / column_width;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100677 }
678
679 if (ncols > 1) {
680 nrows = nfiles / ncols;
681 if (nrows * ncols < nfiles)
682 nrows++; /* round up fractionals */
683 } else {
684 nrows = nfiles;
685 ncols = 1;
686 }
687
Denys Vlasenko2f7d9e82010-12-19 07:06:44 +0100688 column = 0;
689 nexttab = 0;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100690 for (row = 0; row < nrows; row++) {
691 for (nc = 0; nc < ncols; nc++) {
692 /* reach into the array based on the column and row */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200693 if (G.all_fmt & DISP_ROWS)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100694 i = (row * ncols) + nc; /* display across row */
695 else
696 i = (nc * nrows) + row; /* display by column */
697 if (i < nfiles) {
698 if (column > 0) {
699 nexttab -= column;
Denys Vlasenkoea351b92016-03-06 17:53:11 +0100700 printf("%*s", nexttab, "");
701 column += nexttab;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100702 }
703 nexttab = column + column_width;
Denys Vlasenko4029e212011-05-13 17:28:46 +0200704 column += display_single(dn[i]);
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100705 }
706 }
707 putchar('\n');
708 column = 0;
709 }
710}
711
712
Denys Vlasenko4029e212011-05-13 17:28:46 +0200713/*** Dir scanning code ***/
714
715static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100716{
Denys Vlasenko4029e212011-05-13 17:28:46 +0200717 struct stat statbuf;
718 struct dnode *cur;
719
720 cur = xzalloc(sizeof(*cur));
721 cur->fullname = fullname;
722 cur->name = name;
723
724 if ((option_mask32 & OPT_L) || force_follow) {
725#if ENABLE_SELINUX
726 if (is_selinux_enabled()) {
Denys Vlasenko69675782013-01-14 01:34:48 +0100727 getfilecon(fullname, &cur->sid);
Denys Vlasenko4029e212011-05-13 17:28:46 +0200728 }
729#endif
730 if (stat(fullname, &statbuf)) {
731 bb_simple_perror_msg(fullname);
732 G.exit_code = EXIT_FAILURE;
733 free(cur);
734 return NULL;
735 }
736 cur->dn_mode_stat = statbuf.st_mode;
737 } else {
738#if ENABLE_SELINUX
739 if (is_selinux_enabled()) {
740 lgetfilecon(fullname, &cur->sid);
741 }
742#endif
743 if (lstat(fullname, &statbuf)) {
744 bb_simple_perror_msg(fullname);
745 G.exit_code = EXIT_FAILURE;
746 free(cur);
747 return NULL;
748 }
749 cur->dn_mode_lstat = statbuf.st_mode;
750 }
751
752 /* cur->dstat = statbuf: */
753 cur->dn_mode = statbuf.st_mode ;
754 cur->dn_size = statbuf.st_size ;
755#if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES
756 cur->dn_atime = statbuf.st_atime ;
757 cur->dn_mtime = statbuf.st_mtime ;
758 cur->dn_ctime = statbuf.st_ctime ;
759#endif
760 cur->dn_ino = statbuf.st_ino ;
761 cur->dn_blocks = statbuf.st_blocks;
762 cur->dn_nlink = statbuf.st_nlink ;
763 cur->dn_uid = statbuf.st_uid ;
764 cur->dn_gid = statbuf.st_gid ;
765 cur->dn_rdev_maj = major(statbuf.st_rdev);
766 cur->dn_rdev_min = minor(statbuf.st_rdev);
767
768 return cur;
769}
770
771static unsigned count_dirs(struct dnode **dn, int which)
772{
773 unsigned dirs, all;
774
775 if (!dn)
776 return 0;
777
778 dirs = all = 0;
779 for (; *dn; dn++) {
780 const char *name;
781
782 all++;
783 if (!S_ISDIR((*dn)->dn_mode))
784 continue;
785
786 name = (*dn)->name;
787 if (which != SPLIT_SUBDIR /* if not requested to skip . / .. */
788 /* or if it's not . or .. */
789 || name[0] != '.'
790 || (name[1] && (name[1] != '.' || name[2]))
791 ) {
792 dirs++;
793 }
794 }
795 return which != SPLIT_FILE ? dirs : all - dirs;
796}
797
798/* get memory to hold an array of pointers */
799static struct dnode **dnalloc(unsigned num)
800{
801 if (num < 1)
802 return NULL;
803
804 num++; /* so that we have terminating NULL */
805 return xzalloc(num * sizeof(struct dnode *));
806}
807
808#if ENABLE_FEATURE_LS_RECURSIVE
809static void dfree(struct dnode **dnp)
810{
811 unsigned i;
812
813 if (dnp == NULL)
814 return;
815
816 for (i = 0; dnp[i]; i++) {
817 struct dnode *cur = dnp[i];
818 if (cur->fname_allocated)
819 free((char*)cur->fullname);
820 free(cur);
821 }
822 free(dnp);
823}
824#else
825#define dfree(...) ((void)0)
826#endif
827
828/* Returns NULL-terminated malloced vector of pointers (or NULL) */
829static struct dnode **splitdnarray(struct dnode **dn, int which)
830{
831 unsigned dncnt, d;
832 struct dnode **dnp;
833
834 if (dn == NULL)
835 return NULL;
836
837 /* count how many dirs or files there are */
838 dncnt = count_dirs(dn, which);
839
840 /* allocate a file array and a dir array */
841 dnp = dnalloc(dncnt);
842
843 /* copy the entrys into the file or dir array */
844 for (d = 0; *dn; dn++) {
845 if (S_ISDIR((*dn)->dn_mode)) {
846 const char *name;
847
848 if (which == SPLIT_FILE)
849 continue;
850
851 name = (*dn)->name;
852 if ((which & SPLIT_DIR) /* any dir... */
853 /* ... or not . or .. */
854 || name[0] != '.'
855 || (name[1] && (name[1] != '.' || name[2]))
856 ) {
857 dnp[d++] = *dn;
858 }
859 } else
860 if (which == SPLIT_FILE) {
861 dnp[d++] = *dn;
862 }
863 }
864 return dnp;
865}
866
867#if ENABLE_FEATURE_LS_SORTFILES
868static int sortcmp(const void *a, const void *b)
869{
870 struct dnode *d1 = *(struct dnode **)a;
871 struct dnode *d2 = *(struct dnode **)b;
872 unsigned sort_opts = G.all_fmt & SORT_MASK;
873 off_t dif;
874
875 dif = 0; /* assume SORT_NAME */
876 // TODO: use pre-initialized function pointer
877 // instead of branch forest
878 if (sort_opts == SORT_SIZE) {
879 dif = (d2->dn_size - d1->dn_size);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200880 } else
881 if (sort_opts == SORT_ATIME) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200882 dif = (d2->dn_atime - d1->dn_atime);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200883 } else
884 if (sort_opts == SORT_CTIME) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200885 dif = (d2->dn_ctime - d1->dn_ctime);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200886 } else
887 if (sort_opts == SORT_MTIME) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200888 dif = (d2->dn_mtime - d1->dn_mtime);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200889 } else
890 if (sort_opts == SORT_DIR) {
Denys Vlasenko4029e212011-05-13 17:28:46 +0200891 dif = S_ISDIR(d2->dn_mode) - S_ISDIR(d1->dn_mode);
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200892 } else
Denys Vlasenko1e18a012011-06-21 17:12:52 +0200893#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200894 if (sort_opts == SORT_VERSION) {
895 dif = strverscmp(d1->name, d2->name);
896 } else
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200897#endif
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200898 if (sort_opts == SORT_EXT) {
899 dif = strcmp(strchrnul(d1->name, '.'), strchrnul(d2->name, '.'));
Denys Vlasenko4029e212011-05-13 17:28:46 +0200900 }
901 if (dif == 0) {
Denys Vlasenkof194cc12011-06-13 02:13:42 +0200902 /* sort by name, use as tie breaker for other sorts */
Denys Vlasenko4029e212011-05-13 17:28:46 +0200903 if (ENABLE_LOCALE_SUPPORT)
904 dif = strcoll(d1->name, d2->name);
905 else
906 dif = strcmp(d1->name, d2->name);
907 }
908
909 /* Make dif fit into an int */
910 if (sizeof(dif) > sizeof(int)) {
911 enum { BITS_TO_SHIFT = 8 * (sizeof(dif) - sizeof(int)) };
912 /* shift leaving only "int" worth of bits */
913 if (dif != 0) {
914 dif = 1 | (int)((uoff_t)dif >> BITS_TO_SHIFT);
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100915 }
916 }
917
Denys Vlasenko4029e212011-05-13 17:28:46 +0200918 return (G.all_fmt & SORT_REVERSE) ? -(int)dif : (int)dif;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100919}
Denys Vlasenko4029e212011-05-13 17:28:46 +0200920
921static void dnsort(struct dnode **dn, int size)
922{
923 qsort(dn, size, sizeof(*dn), sortcmp);
924}
Denys Vlasenko8dd29da2011-05-13 17:55:08 +0200925
926static void sort_and_display_files(struct dnode **dn, unsigned nfiles)
927{
928 dnsort(dn, nfiles);
929 display_files(dn, nfiles);
930}
Denys Vlasenko4029e212011-05-13 17:28:46 +0200931#else
Denys Vlasenko8dd29da2011-05-13 17:55:08 +0200932# define dnsort(dn, size) ((void)0)
933# define sort_and_display_files(dn, nfiles) display_files(dn, nfiles)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100934#endif
935
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100936/* Returns NULL-terminated malloced vector of pointers (or NULL) */
Denys Vlasenko4029e212011-05-13 17:28:46 +0200937static struct dnode **scan_one_dir(const char *path, unsigned *nfiles_p)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100938{
939 struct dnode *dn, *cur, **dnp;
940 struct dirent *entry;
941 DIR *dir;
942 unsigned i, nfiles;
943
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100944 *nfiles_p = 0;
945 dir = warn_opendir(path);
946 if (dir == NULL) {
Denys Vlasenko2a816392011-05-13 17:28:09 +0200947 G.exit_code = EXIT_FAILURE;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100948 return NULL; /* could not open the dir */
949 }
950 dn = NULL;
951 nfiles = 0;
952 while ((entry = readdir(dir)) != NULL) {
953 char *fullname;
954
955 /* are we going to list the file- it may be . or .. or a hidden file */
956 if (entry->d_name[0] == '.') {
957 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
Denys Vlasenko2a816392011-05-13 17:28:09 +0200958 && !(G.all_fmt & DISP_DOT)
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100959 ) {
960 continue;
961 }
Denys Vlasenko2a816392011-05-13 17:28:09 +0200962 if (!(G.all_fmt & DISP_HIDDEN))
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100963 continue;
964 }
965 fullname = concat_path_file(path, entry->d_name);
966 cur = my_stat(fullname, bb_basename(fullname), 0);
967 if (!cur) {
968 free(fullname);
969 continue;
970 }
971 cur->fname_allocated = 1;
Denys Vlasenko2a816392011-05-13 17:28:09 +0200972 cur->dn_next = dn;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100973 dn = cur;
974 nfiles++;
975 }
976 closedir(dir);
977
978 if (dn == NULL)
979 return NULL;
980
981 /* now that we know how many files there are
982 * allocate memory for an array to hold dnode pointers
983 */
984 *nfiles_p = nfiles;
985 dnp = dnalloc(nfiles);
986 for (i = 0; /* i < nfiles - detected via !dn below */; i++) {
987 dnp[i] = dn; /* save pointer to node in array */
Denys Vlasenko2a816392011-05-13 17:28:09 +0200988 dn = dn->dn_next;
Denys Vlasenkod8528b82010-01-31 05:15:38 +0100989 if (!dn)
990 break;
991 }
992
993 return dnp;
994}
995
Denys Vlasenko4029e212011-05-13 17:28:46 +0200996#if ENABLE_DESKTOP
997/* http://www.opengroup.org/onlinepubs/9699919799/utilities/ls.html
998 * If any of the -l, -n, -s options is specified, each list
999 * of files within the directory shall be preceded by a
1000 * status line indicating the number of file system blocks
1001 * occupied by files in the directory in 512-byte units if
1002 * the -k option is not specified, or 1024-byte units if the
1003 * -k option is specified, rounded up to the next integral
1004 * number of units.
1005 */
1006/* by Jorgen Overgaard (jorgen AT antistaten.se) */
1007static off_t calculate_blocks(struct dnode **dn)
1008{
1009 uoff_t blocks = 1;
1010 if (dn) {
1011 while (*dn) {
1012 /* st_blocks is in 512 byte blocks */
1013 blocks += (*dn)->dn_blocks;
1014 dn++;
1015 }
1016 }
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001017
Denys Vlasenko4029e212011-05-13 17:28:46 +02001018 /* Even though standard says use 512 byte blocks, coreutils use 1k */
1019 /* Actually, we round up by calculating (blocks + 1) / 2,
1020 * "+ 1" was done when we initialized blocks to 1 */
1021 return blocks >> 1;
1022}
1023#endif
1024
1025static void scan_and_display_dirs_recur(struct dnode **dn, int first)
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001026{
1027 unsigned nfiles;
1028 struct dnode **subdnp;
1029
1030 for (; *dn; dn++) {
Denys Vlasenko2a816392011-05-13 17:28:09 +02001031 if (G.all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001032 if (!first)
1033 bb_putchar('\n');
1034 first = 0;
1035 printf("%s:\n", (*dn)->fullname);
1036 }
Denys Vlasenko4029e212011-05-13 17:28:46 +02001037 subdnp = scan_one_dir((*dn)->fullname, &nfiles);
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001038#if ENABLE_DESKTOP
Denys Vlasenkofca0ee52014-02-27 15:40:26 +01001039 if ((G.all_fmt & STYLE_MASK) == STYLE_LONG || (G.all_fmt & LIST_BLOCKS))
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001040 printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
1041#endif
1042 if (nfiles > 0) {
1043 /* list all files at this level */
Denys Vlasenko8dd29da2011-05-13 17:55:08 +02001044 sort_and_display_files(subdnp, nfiles);
Denys Vlasenko4029e212011-05-13 17:28:46 +02001045
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001046 if (ENABLE_FEATURE_LS_RECURSIVE
Denys Vlasenko2a816392011-05-13 17:28:09 +02001047 && (G.all_fmt & DISP_RECURSIVE)
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001048 ) {
1049 struct dnode **dnd;
1050 unsigned dndirs;
1051 /* recursive - list the sub-dirs */
1052 dnd = splitdnarray(subdnp, SPLIT_SUBDIR);
1053 dndirs = count_dirs(subdnp, SPLIT_SUBDIR);
1054 if (dndirs > 0) {
1055 dnsort(dnd, dndirs);
Denys Vlasenko4029e212011-05-13 17:28:46 +02001056 scan_and_display_dirs_recur(dnd, 0);
Denys Vlasenko66ca2412011-05-13 17:27:36 +02001057 /* free the array of dnode pointers to the dirs */
1058 free(dnd);
1059 }
1060 }
1061 /* free the dnodes and the fullname mem */
1062 dfree(subdnp);
1063 }
1064 }
1065}
1066
1067
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001068int ls_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +00001069{
Glenn L McGrath792cae52004-01-18 05:15:16 +00001070 struct dnode **dnd;
1071 struct dnode **dnf;
1072 struct dnode **dnp;
1073 struct dnode *dn;
1074 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001075 unsigned opt;
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001076 unsigned nfiles;
1077 unsigned dnfiles;
1078 unsigned dndirs;
1079 unsigned i;
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001080#if ENABLE_FEATURE_LS_COLOR
1081 /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
1082 /* coreutils 6.10:
1083 * # ls --color=BOGUS
1084 * ls: invalid argument 'BOGUS' for '--color'
1085 * Valid arguments are:
1086 * 'always', 'yes', 'force'
1087 * 'never', 'no', 'none'
1088 * 'auto', 'tty', 'if-tty'
1089 * (and substrings: "--color=alwa" work too)
1090 */
1091 static const char ls_longopts[] ALIGN1 =
1092 "color\0" Optional_argument "\xff"; /* no short equivalent */
1093 static const char color_str[] ALIGN1 =
1094 "always\0""yes\0""force\0"
1095 "auto\0""tty\0""if-tty\0";
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +00001096 /* need to initialize since --color has _an optional_ argument */
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001097 const char *color_opt = color_str; /* "always" */
1098#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +00001099
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001100 INIT_G();
Denis Vlasenkoe0554432007-01-19 22:03:06 +00001101
Denys Vlasenko28055022010-01-04 20:49:58 +01001102 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001103
Denys Vlasenko9f368e32011-02-28 12:16:10 +01001104 if (ENABLE_FEATURE_LS_SORTFILES)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001105 G.all_fmt = SORT_NAME;
Glenn L McGrath792cae52004-01-18 05:15:16 +00001106
Denis Vlasenko5c759602006-10-28 12:37:16 +00001107#if ENABLE_FEATURE_AUTOWIDTH
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001108 /* obtain the terminal width */
Denys Vlasenko641caae2015-10-23 01:44:22 +02001109 G_terminal_width = get_terminal_width(STDIN_FILENO);
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001110 /* go one less... */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001111 G_terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +00001112#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001113
Eric Andersen11c65522000-09-07 17:24:47 +00001114 /* process options */
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001115 IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;)
Denys Vlasenkof3137462010-12-19 05:05:34 +01001116 opt_complementary =
Denys Vlasenko407ab2a2010-12-19 16:29:08 +01001117 /* -e implies -l */
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +02001118 IF_FEATURE_LS_TIMESTAMPS("el")
Denys Vlasenkof3137462010-12-19 05:05:34 +01001119 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html:
1120 * in some pairs of opts, only last one takes effect:
1121 */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +01001122 IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES(":t-S:S-t")) /* time/size */
Denys Vlasenko982aa262010-12-19 21:54:39 +01001123 // ":m-l:l-m" - we don't have -m
1124 IF_FEATURE_LS_FOLLOWLINKS(":H-L:L-H")
Denys Vlasenkof3137462010-12-19 05:05:34 +01001125 ":C-xl:x-Cl:l-xC" /* bycols/bylines/long */
1126 ":C-1:1-C" /* bycols/oneline */
1127 ":x-1:1-x" /* bylines/oneline (not in SuS, but in GNU coreutils 8.4) */
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +02001128 IF_FEATURE_LS_TIMESTAMPS(":c-u:u-c") /* mtime/atime */
Denys Vlasenko407ab2a2010-12-19 16:29:08 +01001129 /* -w NUM: */
1130 IF_FEATURE_AUTOWIDTH(":w+");
Denys Vlasenkof3137462010-12-19 05:05:34 +01001131 opt = getopt32(argv, ls_options
Denys Vlasenko2a816392011-05-13 17:28:09 +02001132 IF_FEATURE_AUTOWIDTH(, NULL, &G_terminal_width)
Denys Vlasenkof3137462010-12-19 05:05:34 +01001133 IF_FEATURE_LS_COLOR(, &color_opt)
1134 );
Denys Vlasenko982aa262010-12-19 21:54:39 +01001135 for (i = 0; opt_flags[i] != (1U << 31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +00001136 if (opt & (1 << i)) {
Denys Vlasenko982aa262010-12-19 21:54:39 +01001137 uint32_t flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001138
Denys Vlasenkod87815d2010-12-19 05:43:44 +01001139 if (flags & STYLE_MASK)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001140 G.all_fmt &= ~STYLE_MASK;
Denys Vlasenkod87815d2010-12-19 05:43:44 +01001141 if (flags & SORT_MASK)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001142 G.all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +00001143 if (flags & TIME_MASK)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001144 G.all_fmt &= ~TIME_MASK;
Denys Vlasenkod87815d2010-12-19 05:43:44 +01001145
Denys Vlasenko2a816392011-05-13 17:28:09 +02001146 G.all_fmt |= flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +00001147 }
Eric Andersen11c65522000-09-07 17:24:47 +00001148 }
1149
Denis Vlasenko5c759602006-10-28 12:37:16 +00001150#if ENABLE_FEATURE_LS_COLOR
Denys Vlasenko2a816392011-05-13 17:28:09 +02001151 /* set G_show_color = 1/0 */
Denis Vlasenko5c759602006-10-28 12:37:16 +00001152 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
1153 char *p = getenv("LS_COLORS");
1154 /* LS_COLORS is unset, or (not empty && not "none") ? */
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +00001155 if (!p || (p[0] && strcmp(p, "none") != 0))
Denys Vlasenko2a816392011-05-13 17:28:09 +02001156 G_show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +00001157 }
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001158 if (opt & OPT_color) {
1159 if (color_opt[0] == 'n')
Denys Vlasenko2a816392011-05-13 17:28:09 +02001160 G_show_color = 0;
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001161 else switch (index_in_substrings(color_str, color_opt)) {
1162 case 3:
1163 case 4:
1164 case 5:
1165 if (isatty(STDOUT_FILENO)) {
1166 case 0:
1167 case 1:
1168 case 2:
Denys Vlasenko2a816392011-05-13 17:28:09 +02001169 G_show_color = 1;
Denys Vlasenkoae05dd42009-07-03 12:22:19 +02001170 }
1171 }
Paul Fox156dc412005-08-01 19:33:30 +00001172 }
1173#endif
1174
Eric Andersen11c65522000-09-07 17:24:47 +00001175 /* sort out which command line options take precedence */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001176 if (ENABLE_FEATURE_LS_RECURSIVE && (G.all_fmt & DISP_NOLIST))
1177 G.all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +00001178 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
Denys Vlasenko2a816392011-05-13 17:28:09 +02001179 if (G.all_fmt & TIME_CHANGE)
1180 G.all_fmt = (G.all_fmt & ~SORT_MASK) | SORT_CTIME;
1181 if (G.all_fmt & TIME_ACCESS)
1182 G.all_fmt = (G.all_fmt & ~SORT_MASK) | SORT_ATIME;
Rob Landleyea224be2006-06-18 20:20:07 +00001183 }
Denys Vlasenko2a816392011-05-13 17:28:09 +02001184 if ((G.all_fmt & STYLE_MASK) != STYLE_LONG) /* not -l? */
1185 G.all_fmt &= ~(LIST_ID_NUMERIC|LIST_ID_NAME|LIST_FULLTIME);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001186
Denys Vlasenkof3137462010-12-19 05:05:34 +01001187 /* choose a display format if one was not already specified by an option */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001188 if (!(G.all_fmt & STYLE_MASK))
1189 G.all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNAR : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +00001190
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001191 argv += optind;
1192 if (!argv[0])
1193 *--argv = (char*)".";
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001194
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001195 if (argv[1])
Denys Vlasenko2a816392011-05-13 17:28:09 +02001196 G.all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001197
Denis Vlasenko5c759602006-10-28 12:37:16 +00001198 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001199 dn = NULL;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001200 nfiles = 0;
1201 do {
Denys Vlasenko163d8642010-12-19 06:16:28 +01001202 cur = my_stat(*argv, *argv,
Denys Vlasenko982aa262010-12-19 21:54:39 +01001203 /* follow links on command line unless -l, -s or -F: */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001204 !((G.all_fmt & STYLE_MASK) == STYLE_LONG
1205 || (G.all_fmt & LIST_BLOCKS)
Denys Vlasenkoea684c62011-03-12 03:12:36 +01001206 || (option_mask32 & OPT_F)
1207 )
Denys Vlasenko982aa262010-12-19 21:54:39 +01001208 /* ... or if -H: */
1209 || (option_mask32 & OPT_H)
Denys Vlasenkod27ac292011-05-13 17:27:15 +02001210 /* ... or if -L, but my_stat always follows links if -L */
Denys Vlasenko163d8642010-12-19 06:16:28 +01001211 );
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001212 argv++;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001213 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001214 continue;
Denys Vlasenko2a816392011-05-13 17:28:09 +02001215 /*cur->fname_allocated = 0; - already is */
1216 cur->dn_next = dn;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001217 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001218 nfiles++;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +00001219 } while (*argv);
Eric Andersen11c65522000-09-07 17:24:47 +00001220
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001221 /* nfiles _may_ be 0 here - try "ls doesnt_exist" */
1222 if (nfiles == 0)
Denys Vlasenko2a816392011-05-13 17:28:09 +02001223 return G.exit_code;
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001224
Eric Andersen11c65522000-09-07 17:24:47 +00001225 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +00001226 * allocate memory for an array to hold dnode pointers
1227 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001228 dnp = dnalloc(nfiles);
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001229 for (i = 0; /* i < nfiles - detected via !dn below */; i++) {
1230 dnp[i] = dn; /* save pointer to node in array */
Denys Vlasenko2a816392011-05-13 17:28:09 +02001231 dn = dn->dn_next;
Denys Vlasenko76c7d952009-10-03 01:15:47 +02001232 if (!dn)
1233 break;
Eric Andersen11c65522000-09-07 17:24:47 +00001234 }
1235
Denys Vlasenko2a816392011-05-13 17:28:09 +02001236 if (G.all_fmt & DISP_NOLIST) {
Denys Vlasenko8dd29da2011-05-13 17:55:08 +02001237 sort_and_display_files(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001238 } else {
Denys Vlasenkocae409c2009-10-03 11:43:48 +02001239 dnd = splitdnarray(dnp, SPLIT_DIR);
1240 dnf = splitdnarray(dnp, SPLIT_FILE);
1241 dndirs = count_dirs(dnp, SPLIT_DIR);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001242 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001243 if (dnfiles > 0) {
Denys Vlasenko8dd29da2011-05-13 17:55:08 +02001244 sort_and_display_files(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +00001245 if (ENABLE_FEATURE_CLEAN_UP)
1246 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +00001247 }
1248 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +00001249 dnsort(dnd, dndirs);
Denys Vlasenko4029e212011-05-13 17:28:46 +02001250 scan_and_display_dirs_recur(dnd, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +00001251 if (ENABLE_FEATURE_CLEAN_UP)
1252 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +00001253 }
1254 }
Denys Vlasenko2a816392011-05-13 17:28:09 +02001255
Rob Landley26314862006-05-02 19:46:52 +00001256 if (ENABLE_FEATURE_CLEAN_UP)
Denys Vlasenkocae409c2009-10-03 11:43:48 +02001257 dfree(dnp);
Denys Vlasenko2a816392011-05-13 17:28:09 +02001258 return G.exit_code;
Eric Andersencc8ed391999-10-05 16:24:54 +00001259}