blob: 2b292555740edab62d69baa30fc3389caf96518d [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 * tiny-ls.c version 0.1.0: A minimalist 'ls'
4 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
Eric Andersen11c65522000-09-07 17:24:47 +00005 *
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +00006 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 */
8
9/*
10 * To achieve a small memory footprint, this version of 'ls' doesn't do any
11 * file sorting, and only has the most essential command line switches
Eric Andersen77d92682001-05-23 20:32:09 +000012 * (i.e., the ones I couldn't live without :-) All features which involve
Eric Andersencc8ed391999-10-05 16:24:54 +000013 * linking in substantial chunks of libc can be disabled.
14 *
15 * Although I don't really want to add new features to this program to
16 * keep it small, I *am* interested to receive bug fixes and ways to make
17 * it more portable.
18 *
19 * KNOWN BUGS:
Erik Andersen9ffdaa62000-02-11 21:55:04 +000020 * 1. ls -l of a directory doesn't give "total <blocks>" header
21 * 2. ls of a symlink to a directory doesn't list directory contents
22 * 3. hidden files can make column width too large
23 *
Eric Andersencc8ed391999-10-05 16:24:54 +000024 * NON-OPTIMAL BEHAVIOUR:
25 * 1. autowidth reads directories twice
26 * 2. if you do a short directory listing without filetype characters
27 * appended, there's no need to stat each one
28 * PORTABILITY:
29 * 1. requires lstat (BSD) - how do you do it without?
30 */
31
Rob Landleyd921b2e2006-08-03 15:41:12 +000032#include <getopt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000034
35/* This is a NOEXEC applet. Be very careful! */
36
Eric Andersenf1142c52001-02-20 06:16:29 +000037
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000038enum {
Manuel Novoa III cad53642003-03-19 09:13:01 +000039
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000040TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
41COLUMN_GAP = 2, /* includes the file type char */
42
43/* what is the overall style of the listing */
44STYLE_COLUMNS = 1 << 21, /* fill columns */
45STYLE_LONG = 2 << 21, /* one record per line, extended info */
46STYLE_SINGLE = 3 << 21, /* one record per line */
47STYLE_MASK = STYLE_SINGLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Eric Andersen11c65522000-09-07 17:24:47 +000049/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
50/* what file information will be listed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000051LIST_INO = 1 << 0,
52LIST_BLOCKS = 1 << 1,
53LIST_MODEBITS = 1 << 2,
54LIST_NLINKS = 1 << 3,
55LIST_ID_NAME = 1 << 4,
56LIST_ID_NUMERIC = 1 << 5,
57LIST_CONTEXT = 1 << 6,
58LIST_SIZE = 1 << 7,
59LIST_DEV = 1 << 8,
60LIST_DATE_TIME = 1 << 9,
61LIST_FULLTIME = 1 << 10,
62LIST_FILENAME = 1 << 11,
63LIST_SYMLINK = 1 << 12,
64LIST_FILETYPE = 1 << 13,
65LIST_EXEC = 1 << 14,
66LIST_MASK = (LIST_EXEC << 1) - 1,
Eric Andersen11c65522000-09-07 17:24:47 +000067
68/* what files will be displayed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000069DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
Denis Vlasenko656f7462006-10-28 13:02:55 +000070DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000071DISP_DOT = 1 << 17, /* show . and .. */
72DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
73DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
74DISP_ROWS = 1 << 20, /* print across rows */
75DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
Manuel Novoa III cad53642003-03-19 09:13:01 +000076
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000077/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
78SORT_FORWARD = 0, /* sort in reverse order */
79SORT_REVERSE = 1 << 27, /* sort in reverse order */
Eric Andersen11c65522000-09-07 17:24:47 +000080
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000081SORT_NAME = 0, /* sort by file name */
82SORT_SIZE = 1 << 28, /* sort by file size */
83SORT_ATIME = 2 << 28, /* sort by last access time */
84SORT_CTIME = 3 << 28, /* sort by last change time */
85SORT_MTIME = 4 << 28, /* sort by last modification time */
86SORT_VERSION = 5 << 28, /* sort by version */
87SORT_EXT = 6 << 28, /* sort by file name extension */
88SORT_DIR = 7 << 28, /* sort by file or directory */
89SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
Eric Andersencc8ed391999-10-05 16:24:54 +000090
Eric Andersen11c65522000-09-07 17:24:47 +000091/* which of the three times will be used */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000092TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
93TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
94TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
Manuel Novoa III cad53642003-03-19 09:13:01 +000095
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000096FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
Eric Andersencc8ed391999-10-05 16:24:54 +000097
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000098LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000099
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000100LIST_SHORT = LIST_FILENAME,
101LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
102 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
103
104SPLIT_DIR = 1,
105SPLIT_FILE = 0,
106SPLIT_SUBDIR = 2,
107
108};
Eric Andersencc8ed391999-10-05 16:24:54 +0000109
Eric Andersen11c65522000-09-07 17:24:47 +0000110#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
111#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000112#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
113#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
114 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
115#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
116 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000117
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000118/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000119#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000120static smallint show_color;
Paul Fox156dc412005-08-01 19:33:30 +0000121/* long option entry used only for --color, which has no short option
Denis Vlasenko656f7462006-10-28 13:02:55 +0000122 * equivalent */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000123static const char ls_color_opt[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000124 "color\0" Optional_argument "\xff" /* no short equivalent */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000125 ;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000126#else
127enum { show_color = 0 };
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000128#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129
Eric Andersen11c65522000-09-07 17:24:47 +0000130/*
131 * a directory entry and its stat info are stored here
132 */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000133struct dnode { /* the basic node */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000134 const char *name; /* the dir entry name */
135 const char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000136 int allocated;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000137 struct stat dstat; /* the file stat info */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000138 USE_SELINUX(security_context_t sid;)
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000139 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000140};
141typedef struct dnode dnode_t;
142
Glenn L McGrath4d001292003-01-06 01:11:50 +0000143static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000144static struct dnode **dnalloc(int);
145static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000146
Denis Vlasenko5c759602006-10-28 12:37:16 +0000147static unsigned all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000148
Denis Vlasenko5c759602006-10-28 12:37:16 +0000149#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko13858992006-10-08 12:49:22 +0000150static unsigned tabstops = COLUMN_GAP;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000151static unsigned terminal_width = TERMINAL_WIDTH;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000152#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000153enum {
154 tabstops = COLUMN_GAP,
155 terminal_width = TERMINAL_WIDTH,
156};
Eric Andersen11c65522000-09-07 17:24:47 +0000157#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000158
Matt Kraai33fdae52000-10-13 17:59:43 +0000159static int status = EXIT_SUCCESS;
160
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000161static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000162{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000163 struct stat dstat;
164 struct dnode *cur;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000165 USE_SELINUX(security_context_t sid = NULL;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000166
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000167 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000168#if ENABLE_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000169 if (is_selinux_enabled()) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000170 getfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000171 }
Eric Andersen9e480452003-07-03 10:07:04 +0000172#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000173 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000175 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000176 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000177 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000178 } else {
179#if ENABLE_SELINUX
180 if (is_selinux_enabled()) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000181 lgetfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000182 }
Eric Andersen9e480452003-07-03 10:07:04 +0000183#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000184 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000185 bb_perror_msg("%s", fullname);
186 status = EXIT_FAILURE;
187 return 0;
188 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000189 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000190
Denis Vlasenko5c759602006-10-28 12:37:16 +0000191 cur = xmalloc(sizeof(struct dnode));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000192 cur->fullname = fullname;
193 cur->name = name;
194 cur->dstat = dstat;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000195 USE_SELINUX(cur->sid = sid;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000196 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000197}
198
Denis Vlasenko5c759602006-10-28 12:37:16 +0000199#if ENABLE_FEATURE_LS_COLOR
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000200static char fgcolor(mode_t mode)
201{
202 /* Check wheter the file is existing (if so, color it red!) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000203 if (errno == ENOENT)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000204 return '\037';
Rob Landley9947a242006-06-15 22:11:10 +0000205 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000206 return COLOR(0xF000); /* File is executable ... */
207 return COLOR(mode);
208}
209
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000210static char bgcolor(mode_t mode)
211{
Rob Landley9947a242006-06-15 22:11:10 +0000212 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000213 return ATTR(0xF000); /* File is executable ... */
214 return ATTR(mode);
215}
216#endif
217
Denis Vlasenko5c759602006-10-28 12:37:16 +0000218#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000219static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000220{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000222 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000223 if (S_ISDIR(mode))
224 return '/';
225 if (!(all_fmt & LIST_EXEC))
226 return '\0';
227 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000228 return '*';
229 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000230}
231#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000232
Denis Vlasenko5c759602006-10-28 12:37:16 +0000233#define countdirs(A, B) count_dirs((A), (B), 1)
234#define countsubdirs(A, B) count_dirs((A), (B), 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000236{
237 int i, dirs;
238
Denis Vlasenko5c759602006-10-28 12:37:16 +0000239 if (!dn)
240 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000241 dirs = 0;
242 for (i = 0; i < nfiles; i++) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000243 const char *name;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000244 if (!S_ISDIR(dn[i]->dstat.st_mode))
245 continue;
246 name = dn[i]->name;
247 if (notsubdirs
248 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
249 ) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000250 dirs++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000251 }
Eric Andersen11c65522000-09-07 17:24:47 +0000252 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000253 return dirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000254}
255
Eric Andersen3e6ff902001-03-09 21:24:12 +0000256static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000257{
258 int nfiles;
259 struct dnode *cur;
260
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000261 if (dnp == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000262 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000263 nfiles = 0;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000264 for (cur = dnp[0]; cur->next; cur = cur->next)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000265 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000266 nfiles++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000267 return nfiles;
Eric Andersen11c65522000-09-07 17:24:47 +0000268}
269
270/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000271static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000272{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000273 if (num < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000274 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000275
Denis Vlasenko5c759602006-10-28 12:37:16 +0000276 return xzalloc(num * sizeof(struct dnode *));
Eric Andersen11c65522000-09-07 17:24:47 +0000277}
278
Denis Vlasenko5c759602006-10-28 12:37:16 +0000279#if ENABLE_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000280static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000281{
Rob Landley26314862006-05-02 19:46:52 +0000282 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000283
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000284 if (dnp == NULL)
285 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000286
Rob Landley26314862006-05-02 19:46:52 +0000287 for (i = 0; i < nfiles; i++) {
288 struct dnode *cur = dnp[i];
Denis Vlasenko5c759602006-10-28 12:37:16 +0000289 if (cur->allocated)
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000290 free((char*)cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000291 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000292 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000293 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000294}
Rob Landleyc44bc982006-05-28 01:19:06 +0000295#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000296#define dfree(...) ((void)0)
Eric Andersen20aab262001-07-19 22:28:02 +0000297#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000298
Eric Andersen3e6ff902001-03-09 21:24:12 +0000299static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000300{
301 int dncnt, i, d;
302 struct dnode **dnp;
303
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000304 if (dn == NULL || nfiles < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000305 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000306
307 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000308 if (which == SPLIT_SUBDIR)
309 dncnt = countsubdirs(dn, nfiles);
310 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000311 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000312 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000313 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000314 }
Eric Andersen11c65522000-09-07 17:24:47 +0000315
316 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000317 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000318
319 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000320 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000321 if (S_ISDIR(dn[i]->dstat.st_mode)) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000322 const char *name;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000323 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
324 continue;
325 name = dn[i]->name;
326 if ((which & SPLIT_DIR)
327 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
328 ) {
329 dnp[d++] = dn[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000330 }
331 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
332 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000333 }
334 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000335 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000336}
337
Denis Vlasenko5c759602006-10-28 12:37:16 +0000338#if ENABLE_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000339static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000340{
Rob Landley425e7582006-05-03 20:22:03 +0000341 struct dnode *d1 = *(struct dnode **)a;
342 struct dnode *d2 = *(struct dnode **)b;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000343 unsigned sort_opts = all_fmt & SORT_MASK;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000344 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000345
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000346 dif = 0; /* assume SORT_NAME */
347 // TODO: use pre-initialized function pointer
348 // instead of branch forest
Eric Andersen11c65522000-09-07 17:24:47 +0000349 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000350 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000351 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000352 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000353 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000354 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000355 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000356 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000357 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000358 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000359 /* } else if (sort_opts == SORT_VERSION) { */
360 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000361 }
362
Eric Andersen11c65522000-09-07 17:24:47 +0000363 if (dif == 0) {
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000364 /* sort by name - may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000365 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
366 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000367 }
368
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000369 if (all_fmt & SORT_REVERSE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000370 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000371 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000372 return dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000373}
374
Rob Landley425e7582006-05-03 20:22:03 +0000375static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000376{
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000377 qsort(dn, size, sizeof(*dn), sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000378}
Rob Landleyea224be2006-06-18 20:20:07 +0000379#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000380#define dnsort(dn, size) ((void)0)
Eric Andersen11c65522000-09-07 17:24:47 +0000381#endif
382
Rob Landleyea224be2006-06-18 20:20:07 +0000383
Eric Andersen3e6ff902001-03-09 21:24:12 +0000384static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000385{
386 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000387 int column = 0;
388 int nexttab = 0;
389 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000390
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000391 if (dn == NULL || nfiles < 1)
392 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000393
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000394 if (all_fmt & STYLE_LONG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000395 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000396 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000397 /* find the longest file name, use that as the column width */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000398 for (i = 0; i < nfiles; i++) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000399 int len = strlen(dn[i]->name);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000400 if (column_width < len)
401 column_width = len;
402 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000403 column_width += tabstops +
404 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
405 ((all_fmt & LIST_INO) ? 8 : 0) +
406 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000407 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000408 }
409
Eric Andersene57d54b2001-01-30 18:03:11 +0000410 if (ncols > 1) {
411 nrows = nfiles / ncols;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000412 if (nrows * ncols < nfiles)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000413 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000414 } else {
415 nrows = nfiles;
416 ncols = 1;
417 }
Eric Andersen11c65522000-09-07 17:24:47 +0000418
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000419 for (row = 0; row < nrows; row++) {
420 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000421 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000422 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000423 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000424 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000425 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000426 if (column > 0) {
427 nexttab -= column;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000428 printf("%*s", nexttab, "");
429 column += nexttab;
430 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000431 nexttab = column + column_width;
432 column += list_single(dn[i]);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000433 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000434 }
435 putchar('\n');
436 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000437 }
Eric Andersen11c65522000-09-07 17:24:47 +0000438}
439
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000440
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000441static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000442{
443 int i, nfiles;
444 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000445 int dndirs;
446 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000447
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000448 if (dn == NULL || ndirs < 1)
449 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000450
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000451 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000452 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000453 if (!first)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000454 puts("");
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000455 first = 0;
456 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000457 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000458 subdnp = list_dir(dn[i]->fullname);
459 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000460 if (nfiles > 0) {
461 /* list all files at this level */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000462 dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000463 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000464 if (ENABLE_FEATURE_LS_RECURSIVE) {
465 if (all_fmt & DISP_RECURSIVE) {
466 /* recursive- list the sub-dirs */
467 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
468 dndirs = countsubdirs(subdnp, nfiles);
469 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000470 dnsort(dnd, dndirs);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000471 showdirs(dnd, dndirs, 0);
472 /* free the array of dnode pointers to the dirs */
473 free(dnd);
474 }
Eric Andersen11c65522000-09-07 17:24:47 +0000475 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000476 /* free the dnodes and the fullname mem */
477 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000478 }
Eric Andersen11c65522000-09-07 17:24:47 +0000479 }
480 }
481}
482
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000483
Glenn L McGrath4d001292003-01-06 01:11:50 +0000484static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000485{
486 struct dnode *dn, *cur, **dnp;
487 struct dirent *entry;
488 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000489 int i, nfiles;
490
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000491 if (path == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000492 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000493
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000494 dn = NULL;
495 nfiles = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000496 dir = warn_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000497 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000498 status = EXIT_FAILURE;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000499 return NULL; /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000500 }
501 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000502 char *fullname;
503
Eric Andersen11c65522000-09-07 17:24:47 +0000504 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000505 if (entry->d_name[0] == '.') {
Denis Vlasenko16abcd92007-04-13 23:59:52 +0000506 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
507 && !(all_fmt & DISP_DOT)
508 ) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000509 continue;
Denis Vlasenko16abcd92007-04-13 23:59:52 +0000510 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000511 if (!(all_fmt & DISP_HIDDEN))
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000512 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000513 }
514 fullname = concat_path_file(path, entry->d_name);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000515 cur = my_stat(fullname, bb_basename(fullname), 0);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000516 if (!cur) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000517 free(fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000518 continue;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000519 }
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000520 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000521 cur->next = dn;
522 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000523 nfiles++;
524 }
525 closedir(dir);
526
527 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000528 * allocate memory for an array to hold dnode pointers
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000529 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000530 if (dn == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000531 return NULL;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000532 dnp = dnalloc(nfiles);
533 for (i = 0, cur = dn; i < nfiles; i++) {
534 dnp[i] = cur; /* save pointer to node in array */
535 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000536 }
537
Denis Vlasenko5c759602006-10-28 12:37:16 +0000538 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000539}
540
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000541
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000542#if ENABLE_FEATURE_LS_TIMESTAMPS
543/* Do time() just once. Saves one syscall per file for "ls -l" */
544/* Initialized in main() */
545static time_t current_time_t;
546#endif
547
Eric Andersen3e6ff902001-03-09 21:24:12 +0000548static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000549{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000550 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000551
Denis Vlasenko5c759602006-10-28 12:37:16 +0000552#if ENABLE_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000553 char *filetime;
554 time_t ttime, age;
555#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000556#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000557 struct stat info;
558 char append;
559#endif
560
Glenn L McGrath4d001292003-01-06 01:11:50 +0000561 if (dn->fullname == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000562 return 0;
Eric Andersen11c65522000-09-07 17:24:47 +0000563
Denis Vlasenko5c759602006-10-28 12:37:16 +0000564#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000565 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000566 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000567 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000568 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000569 ttime = dn->dstat.st_ctime;
570 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000571#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000572#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000573 append = append_char(dn->dstat.st_mode);
574#endif
575
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000576 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000577 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000578 case LIST_INO:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000579 column += printf("%7ld ", (long) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000580 break;
581 case LIST_BLOCKS:
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000582 column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000583 break;
584 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000585 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000586 break;
587 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000588 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000589 break;
590 case LIST_ID_NAME:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000591#if ENABLE_FEATURE_LS_USERNAME
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000592 printf("%-8.8s %-8.8s",
593 get_cached_username(dn->dstat.st_uid),
594 get_cached_groupname(dn->dstat.st_gid));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000595 column += 17;
596 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000597#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000598 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000599 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000600 break;
601 case LIST_SIZE:
602 case LIST_DEV:
603 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000604 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
605 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000606 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000607 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000608 column += printf("%9s ",
Denis Vlasenko5c759602006-10-28 12:37:16 +0000609 make_human_readable_str(dn->dstat.st_size, 1, 0));
610 } else {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000611 column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000612 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000613 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000614 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000615#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000616 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000617 printf("%24.24s ", filetime);
618 column += 25;
619 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000620 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000621 if ((all_fmt & LIST_FULLTIME) == 0) {
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000622 /* current_time_t ~== time(NULL) */
623 age = current_time_t - ttime;
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000624 printf("%6.6s ", filetime + 4);
625 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
626 /* hh:mm if less than 6 months old */
627 printf("%5.5s ", filetime + 11);
628 } else {
629 printf(" %4.4s ", filetime + 20);
630 }
631 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000632 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000633 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000634#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000635#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000636 case LIST_CONTEXT:
637 {
Rob Landley60158cb2005-05-03 06:25:50 +0000638 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000639 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000640
Rob Landley60158cb2005-05-03 06:25:50 +0000641 if (dn->sid) {
Denis Vlasenko656f7462006-10-28 13:02:55 +0000642 /* I assume sid initilized with NULL */
643 len = strlen(dn->sid) + 1;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000644 safe_strncpy(context, dn->sid, len);
645 freecon(dn->sid);
646 } else {
647 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000648 }
649 printf("%-32s ", context);
650 column += MAX(33, len);
651 }
652 break;
653#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000654 case LIST_FILENAME:
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000655 errno = 0;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000656#if ENABLE_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000657 if (show_color && !lstat(dn->fullname, &info)) {
658 printf("\033[%d;%dm", bgcolor(info.st_mode),
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000659 fgcolor(info.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000660 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000661#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000662 column += printf("%s", dn->name);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000663 if (show_color) {
664 printf("\033[0m");
665 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000666 break;
667 case LIST_SYMLINK:
668 if (S_ISLNK(dn->dstat.st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000669 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000670 if (!lpath) break;
671 printf(" -> ");
672#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
673 if (!stat(dn->fullname, &info)) {
674 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000675 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000676#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000677#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000678 if (show_color) {
679 errno = 0;
680 printf("\033[%d;%dm", bgcolor(info.st_mode),
681 fgcolor(info.st_mode));
682 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000683#endif
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000684 column += printf("%s", lpath) + 4;
685 if (show_color) {
686 printf("\033[0m");
687 }
688 free(lpath);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000689 }
690 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000691#if ENABLE_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000692 case LIST_FILETYPE:
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000693 if (append) {
694 putchar(append);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000695 column++;
696 }
697 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000698#endif
699 }
700 }
701
Glenn L McGrath4d001292003-01-06 01:11:50 +0000702 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000703}
704
Glenn L McGrath792cae52004-01-18 05:15:16 +0000705/* "[-]Cadil1", POSIX mandated options, busybox always supports */
706/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
707/* "[-]Ak" GNU options, busybox always supports */
708/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
709/* "[-]p", POSIX non-mandated options, busybox optionally supports */
710/* "[-]SXvThw", GNU options, busybox optionally supports */
711/* "[-]K", SELinux mandated options, busybox optionally supports */
712/* "[-]e", I think we made this one up */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000713static const char ls_options[] ALIGN1 =
714 "Cadil1gnsxAk"
Denis Vlasenko5c759602006-10-28 12:37:16 +0000715 USE_FEATURE_LS_TIMESTAMPS("cetu")
716 USE_FEATURE_LS_SORTFILES("SXrv")
717 USE_FEATURE_LS_FILETYPES("Fp")
718 USE_FEATURE_LS_FOLLOWLINKS("L")
719 USE_FEATURE_LS_RECURSIVE("R")
720 USE_FEATURE_HUMAN_READABLE("h")
721 USE_SELINUX("K")
Denis Vlasenko49622d72007-03-10 16:58:49 +0000722 USE_FEATURE_AUTOWIDTH("T:w:")
723 USE_SELINUX("Z");
Glenn L McGrath792cae52004-01-18 05:15:16 +0000724
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000725enum {
726 LIST_MASK_TRIGGER = 0,
727 STYLE_MASK_TRIGGER = STYLE_MASK,
728 DISP_MASK_TRIGGER = DISP_ROWS,
729 SORT_MASK_TRIGGER = SORT_MASK,
730};
Manuel Novoa III cad53642003-03-19 09:13:01 +0000731
732static const unsigned opt_flags[] = {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000733 LIST_SHORT | STYLE_COLUMNS, /* C */
734 DISP_HIDDEN | DISP_DOT, /* a */
735 DISP_NOLIST, /* d */
736 LIST_INO, /* i */
737 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
738 LIST_SHORT | STYLE_SINGLE, /* 1 */
739 0, /* g - ingored */
740 LIST_ID_NUMERIC, /* n */
741 LIST_BLOCKS, /* s */
742 DISP_ROWS, /* x */
743 DISP_HIDDEN, /* A */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000744 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000745#if ENABLE_FEATURE_LS_TIMESTAMPS
746 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
747 LIST_FULLTIME, /* e */
748 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
749 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000751#if ENABLE_FEATURE_LS_SORTFILES
752 SORT_SIZE, /* S */
753 SORT_EXT, /* X */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000754 SORT_REVERSE, /* r */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000755 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000756#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000757#if ENABLE_FEATURE_LS_FILETYPES
758 LIST_FILETYPE | LIST_EXEC, /* F */
759 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000760#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000761#if ENABLE_FEATURE_LS_FOLLOWLINKS
762 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000763#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000764#if ENABLE_FEATURE_LS_RECURSIVE
765 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000766#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000767#if ENABLE_FEATURE_HUMAN_READABLE
768 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000769#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000770#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000771 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
772#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000773#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000774 0, 0, /* T, w - ignored */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000775#endif
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000776#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000777 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
778#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000779 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000780};
781
782
Denis Vlasenko97fd6d82007-03-19 20:59:20 +0000783/* THIS IS A "SAFE" APPLET, main() MAY BE CALLED INTERNALLY FROM SHELL */
784/* BE CAREFUL! */
785
Denis Vlasenko06af2162007-02-03 17:28:39 +0000786int ls_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000787int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000788{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000789 struct dnode **dnd;
790 struct dnode **dnf;
791 struct dnode **dnp;
792 struct dnode *dn;
793 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000794 unsigned opt;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000795 int nfiles = 0;
796 int dnfiles;
797 int dndirs;
798 int oi;
799 int ac;
800 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000801 char **av;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000802 USE_FEATURE_AUTOWIDTH(char *tabstops_str = NULL;)
803 USE_FEATURE_AUTOWIDTH(char *terminal_width_str = NULL;)
804 USE_FEATURE_LS_COLOR(char *color_opt;)
Glenn L McGrath792cae52004-01-18 05:15:16 +0000805
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000806#if ENABLE_FEATURE_LS_TIMESTAMPS
807 time(&current_time_t);
808#endif
809
Rob Landley2b8a05a2006-06-20 17:43:01 +0000810 all_fmt = LIST_SHORT |
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000811 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000812
Denis Vlasenko5c759602006-10-28 12:37:16 +0000813#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000814 /* Obtain the terminal width */
Denis Vlasenkof893da82007-08-09 08:27:24 +0000815 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000816 /* Go one less... */
817 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000818#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000819
Eric Andersen11c65522000-09-07 17:24:47 +0000820 /* process options */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000821 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000822#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000823 opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000824 USE_FEATURE_LS_COLOR(, &color_opt));
Denis Vlasenko5c759602006-10-28 12:37:16 +0000825 if (tabstops_str)
Denis Vlasenko13858992006-10-08 12:49:22 +0000826 tabstops = xatou(tabstops_str);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000827 if (terminal_width_str)
Denis Vlasenko13858992006-10-08 12:49:22 +0000828 terminal_width = xatou(terminal_width_str);
Glenn L McGrath792cae52004-01-18 05:15:16 +0000829#else
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000830 opt = getopt32(argc, argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000831#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000832 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000833 if (opt & (1 << i)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000834 unsigned flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000835
Denis Vlasenko5c759602006-10-28 12:37:16 +0000836 if (flags & LIST_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 all_fmt &= ~LIST_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000838 if (flags & STYLE_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000839 all_fmt &= ~STYLE_MASK;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000840 if (flags & SORT_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000841 all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000842 if (flags & DISP_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000843 all_fmt &= ~DISP_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000844 if (flags & TIME_MASK)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000845 all_fmt &= ~TIME_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000846 if (flags & LIST_CONTEXT)
Eric Andersen9e480452003-07-03 10:07:04 +0000847 all_fmt |= STYLE_SINGLE;
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000848 /* huh?? opt cannot be 'l' */
849 //if (LS_DISP_HR && opt == 'l')
850 // all_fmt &= ~LS_DISP_HR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000851 all_fmt |= flags;
852 }
Eric Andersen11c65522000-09-07 17:24:47 +0000853 }
854
Denis Vlasenko5c759602006-10-28 12:37:16 +0000855#if ENABLE_FEATURE_LS_COLOR
856 /* find color bit value - last position for short getopt */
857 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
858 char *p = getenv("LS_COLORS");
859 /* LS_COLORS is unset, or (not empty && not "none") ? */
860 if (!p || (p[0] && strcmp(p, "none")))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000861 show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000862 }
863 if (opt & (1 << i)) { /* next flag after short options */
864 if (!color_opt || !strcmp("always", color_opt))
865 show_color = 1;
866 else if (color_opt && !strcmp("never", color_opt))
867 show_color = 0;
868 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
869 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000870 }
871#endif
872
Eric Andersen11c65522000-09-07 17:24:47 +0000873 /* sort out which command line options take precedence */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000874 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000875 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +0000876 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
877 if (all_fmt & TIME_CHANGE)
878 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
879 if (all_fmt & TIME_ACCESS)
880 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
881 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000882 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
883 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000884 if (ENABLE_FEATURE_LS_USERNAME)
885 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
886 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000887
Eric Andersen11c65522000-09-07 17:24:47 +0000888 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +0000889 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +0000890 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +0000891
892 /*
893 * when there are no cmd line args we have to supply a default "." arg.
894 * we will create a second argv array, "av" that will hold either
895 * our created "." arg, or the real cmd line args. The av array
896 * just holds the pointers- we don't move the date the pointers
897 * point to.
898 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000899 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +0000900 if (ac < 1) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000901 static const char *const dotdir[] = { "." };
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000902
903 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000904 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +0000905 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000906 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +0000907 }
908
909 /* now, everything is in the av array */
910 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000911 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +0000912
Denis Vlasenko5c759602006-10-28 12:37:16 +0000913 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000914 dn = NULL;
915 for (oi = 0; oi < ac; oi++) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000916 /* ls w/o -l follows links on command line */
917 cur = my_stat(av[oi], av[oi], !(all_fmt & STYLE_LONG));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000918 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000919 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000920 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000921 cur->next = dn;
922 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000923 nfiles++;
924 }
925
926 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000927 * allocate memory for an array to hold dnode pointers
928 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000929 dnp = dnalloc(nfiles);
930 for (i = 0, cur = dn; i < nfiles; i++) {
931 dnp[i] = cur; /* save pointer to node in array */
932 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000933 }
934
Manuel Novoa III cad53642003-03-19 09:13:01 +0000935 if (all_fmt & DISP_NOLIST) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000936 dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000937 if (nfiles > 0)
938 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000939 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000940 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
941 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
942 dndirs = countdirs(dnp, nfiles);
943 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000944 if (dnfiles > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000945 dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000946 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +0000947 if (ENABLE_FEATURE_CLEAN_UP)
948 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +0000949 }
950 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000951 dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000952 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +0000953 if (ENABLE_FEATURE_CLEAN_UP)
954 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +0000955 }
956 }
Rob Landley26314862006-05-02 19:46:52 +0000957 if (ENABLE_FEATURE_CLEAN_UP)
958 dfree(dnp, nfiles);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000959 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000960}