blob: 9850866343b3f7a306e5434573def902874137fd [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000032#include "libbb.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000033
34/* This is a NOEXEC applet. Be very careful! */
35
Eric Andersenf1142c52001-02-20 06:16:29 +000036
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000037enum {
Manuel Novoa III cad53642003-03-19 09:13:01 +000038
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000039TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
40COLUMN_GAP = 2, /* includes the file type char */
41
42/* what is the overall style of the listing */
43STYLE_COLUMNS = 1 << 21, /* fill columns */
44STYLE_LONG = 2 << 21, /* one record per line, extended info */
45STYLE_SINGLE = 3 << 21, /* one record per line */
46STYLE_MASK = STYLE_SINGLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000047
Eric Andersen11c65522000-09-07 17:24:47 +000048/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
49/* what file information will be listed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000050LIST_INO = 1 << 0,
51LIST_BLOCKS = 1 << 1,
52LIST_MODEBITS = 1 << 2,
53LIST_NLINKS = 1 << 3,
54LIST_ID_NAME = 1 << 4,
55LIST_ID_NUMERIC = 1 << 5,
56LIST_CONTEXT = 1 << 6,
57LIST_SIZE = 1 << 7,
58LIST_DEV = 1 << 8,
59LIST_DATE_TIME = 1 << 9,
60LIST_FULLTIME = 1 << 10,
61LIST_FILENAME = 1 << 11,
62LIST_SYMLINK = 1 << 12,
63LIST_FILETYPE = 1 << 13,
64LIST_EXEC = 1 << 14,
65LIST_MASK = (LIST_EXEC << 1) - 1,
Eric Andersen11c65522000-09-07 17:24:47 +000066
67/* what files will be displayed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000068DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
Denis Vlasenko656f7462006-10-28 13:02:55 +000069DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000070DISP_DOT = 1 << 17, /* show . and .. */
71DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
72DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
73DISP_ROWS = 1 << 20, /* print across rows */
74DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
Manuel Novoa III cad53642003-03-19 09:13:01 +000075
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000076/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
77SORT_FORWARD = 0, /* sort in reverse order */
78SORT_REVERSE = 1 << 27, /* sort in reverse order */
Eric Andersen11c65522000-09-07 17:24:47 +000079
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000080SORT_NAME = 0, /* sort by file name */
81SORT_SIZE = 1 << 28, /* sort by file size */
82SORT_ATIME = 2 << 28, /* sort by last access time */
83SORT_CTIME = 3 << 28, /* sort by last change time */
84SORT_MTIME = 4 << 28, /* sort by last modification time */
85SORT_VERSION = 5 << 28, /* sort by version */
86SORT_EXT = 6 << 28, /* sort by file name extension */
87SORT_DIR = 7 << 28, /* sort by file or directory */
88SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
Eric Andersencc8ed391999-10-05 16:24:54 +000089
Eric Andersen11c65522000-09-07 17:24:47 +000090/* which of the three times will be used */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000091TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
92TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
93TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
Manuel Novoa III cad53642003-03-19 09:13:01 +000094
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000095FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
Eric Andersencc8ed391999-10-05 16:24:54 +000096
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000097LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000098
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000099LIST_SHORT = LIST_FILENAME,
100LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
101 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
102
103SPLIT_DIR = 1,
104SPLIT_FILE = 0,
105SPLIT_SUBDIR = 2,
106
107};
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
Eric Andersen11c65522000-09-07 17:24:47 +0000109#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
110#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000111#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
112#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
113 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
114#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
115 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000116
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000117/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000118#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000119static smallint show_color;
Paul Fox156dc412005-08-01 19:33:30 +0000120/* long option entry used only for --color, which has no short option
Denis Vlasenko656f7462006-10-28 13:02:55 +0000121 * equivalent */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000122static const char ls_color_opt[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000123 "color\0" Optional_argument "\xff" /* no short equivalent */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000124 ;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000125#else
126enum { show_color = 0 };
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000127#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128
Eric Andersen11c65522000-09-07 17:24:47 +0000129/*
130 * a directory entry and its stat info are stored here
131 */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000132struct dnode { /* the basic node */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000133 const char *name; /* the dir entry name */
134 const char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000135 int allocated;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000136 struct stat dstat; /* the file stat info */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000137 USE_SELINUX(security_context_t sid;)
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000138 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000139};
140typedef struct dnode dnode_t;
141
Glenn L McGrath4d001292003-01-06 01:11:50 +0000142static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000143static struct dnode **dnalloc(int);
144static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000145
Denis Vlasenko5c759602006-10-28 12:37:16 +0000146static unsigned all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000147
Denis Vlasenko5c759602006-10-28 12:37:16 +0000148#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko13858992006-10-08 12:49:22 +0000149static unsigned tabstops = COLUMN_GAP;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000150static unsigned terminal_width = TERMINAL_WIDTH;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000151#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000152enum {
153 tabstops = COLUMN_GAP,
154 terminal_width = TERMINAL_WIDTH,
155};
Eric Andersen11c65522000-09-07 17:24:47 +0000156#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Matt Kraai33fdae52000-10-13 17:59:43 +0000158static int status = EXIT_SUCCESS;
159
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000160static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000161{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000162 struct stat dstat;
163 struct dnode *cur;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000164 USE_SELINUX(security_context_t sid = NULL;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000165
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000166 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000167#if ENABLE_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000168 if (is_selinux_enabled()) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000169 getfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000170 }
Eric Andersen9e480452003-07-03 10:07:04 +0000171#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000172 if (stat(fullname, &dstat)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000173 bb_simple_perror_msg(fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000174 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000175 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000176 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000177 } else {
178#if ENABLE_SELINUX
179 if (is_selinux_enabled()) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000180 lgetfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000181 }
Eric Andersen9e480452003-07-03 10:07:04 +0000182#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000183 if (lstat(fullname, &dstat)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000184 bb_simple_perror_msg(fullname);
Eric Andersen9e480452003-07-03 10:07:04 +0000185 status = EXIT_FAILURE;
186 return 0;
187 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000188 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000189
Denis Vlasenko5c759602006-10-28 12:37:16 +0000190 cur = xmalloc(sizeof(struct dnode));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000191 cur->fullname = fullname;
192 cur->name = name;
193 cur->dstat = dstat;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000194 USE_SELINUX(cur->sid = sid;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000195 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000196}
197
Denis Vlasenko5c759602006-10-28 12:37:16 +0000198#if ENABLE_FEATURE_LS_COLOR
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000199static char fgcolor(mode_t mode)
200{
201 /* Check wheter the file is existing (if so, color it red!) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000202 if (errno == ENOENT)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000203 return '\037';
Rob Landley9947a242006-06-15 22:11:10 +0000204 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000205 return COLOR(0xF000); /* File is executable ... */
206 return COLOR(mode);
207}
208
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000209static char bgcolor(mode_t mode)
210{
Rob Landley9947a242006-06-15 22:11:10 +0000211 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000212 return ATTR(0xF000); /* File is executable ... */
213 return ATTR(mode);
214}
215#endif
216
Denis Vlasenko5c759602006-10-28 12:37:16 +0000217#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000218static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000219{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000221 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000222 if (S_ISDIR(mode))
223 return '/';
224 if (!(all_fmt & LIST_EXEC))
225 return '\0';
226 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000227 return '*';
228 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000229}
230#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000231
Denis Vlasenko5c759602006-10-28 12:37:16 +0000232#define countdirs(A, B) count_dirs((A), (B), 1)
233#define countsubdirs(A, B) count_dirs((A), (B), 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000235{
236 int i, dirs;
237
Denis Vlasenko5c759602006-10-28 12:37:16 +0000238 if (!dn)
239 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000240 dirs = 0;
241 for (i = 0; i < nfiles; i++) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000242 const char *name;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000243 if (!S_ISDIR(dn[i]->dstat.st_mode))
244 continue;
245 name = dn[i]->name;
246 if (notsubdirs
247 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
248 ) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000249 dirs++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000250 }
Eric Andersen11c65522000-09-07 17:24:47 +0000251 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000252 return dirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000253}
254
Eric Andersen3e6ff902001-03-09 21:24:12 +0000255static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000256{
257 int nfiles;
258 struct dnode *cur;
259
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000260 if (dnp == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000261 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000262 nfiles = 0;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000263 for (cur = dnp[0]; cur->next; cur = cur->next)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000264 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000265 nfiles++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000266 return nfiles;
Eric Andersen11c65522000-09-07 17:24:47 +0000267}
268
269/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000270static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000271{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000272 if (num < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000273 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000274
Denis Vlasenko5c759602006-10-28 12:37:16 +0000275 return xzalloc(num * sizeof(struct dnode *));
Eric Andersen11c65522000-09-07 17:24:47 +0000276}
277
Denis Vlasenko5c759602006-10-28 12:37:16 +0000278#if ENABLE_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000279static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000280{
Rob Landley26314862006-05-02 19:46:52 +0000281 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000282
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000283 if (dnp == NULL)
284 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000285
Rob Landley26314862006-05-02 19:46:52 +0000286 for (i = 0; i < nfiles; i++) {
287 struct dnode *cur = dnp[i];
Denis Vlasenko5c759602006-10-28 12:37:16 +0000288 if (cur->allocated)
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000289 free((char*)cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000290 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000291 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000292 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000293}
Rob Landleyc44bc982006-05-28 01:19:06 +0000294#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000295#define dfree(...) ((void)0)
Eric Andersen20aab262001-07-19 22:28:02 +0000296#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000297
Eric Andersen3e6ff902001-03-09 21:24:12 +0000298static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000299{
300 int dncnt, i, d;
301 struct dnode **dnp;
302
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000303 if (dn == NULL || nfiles < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000304 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000305
306 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000307 if (which == SPLIT_SUBDIR)
308 dncnt = countsubdirs(dn, nfiles);
309 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000310 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000311 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000312 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000313 }
Eric Andersen11c65522000-09-07 17:24:47 +0000314
315 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000316 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000317
318 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000319 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320 if (S_ISDIR(dn[i]->dstat.st_mode)) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000321 const char *name;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000322 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
323 continue;
324 name = dn[i]->name;
325 if ((which & SPLIT_DIR)
326 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
327 ) {
328 dnp[d++] = dn[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000329 }
330 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
331 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000332 }
333 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000334 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000335}
336
Denis Vlasenko5c759602006-10-28 12:37:16 +0000337#if ENABLE_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000338static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000339{
Rob Landley425e7582006-05-03 20:22:03 +0000340 struct dnode *d1 = *(struct dnode **)a;
341 struct dnode *d2 = *(struct dnode **)b;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000342 unsigned sort_opts = all_fmt & SORT_MASK;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000343 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000344
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000345 dif = 0; /* assume SORT_NAME */
346 // TODO: use pre-initialized function pointer
347 // instead of branch forest
Eric Andersen11c65522000-09-07 17:24:47 +0000348 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000349 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000350 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000351 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000352 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000353 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000354 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000355 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000356 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000357 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000358 /* } else if (sort_opts == SORT_VERSION) { */
359 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000360 }
361
Eric Andersen11c65522000-09-07 17:24:47 +0000362 if (dif == 0) {
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000363 /* sort by name - may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000364 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
365 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000366 }
367
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000368 if (all_fmt & SORT_REVERSE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000369 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000370 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000371 return dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000372}
373
Rob Landley425e7582006-05-03 20:22:03 +0000374static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000375{
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000376 qsort(dn, size, sizeof(*dn), sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000377}
Rob Landleyea224be2006-06-18 20:20:07 +0000378#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000379#define dnsort(dn, size) ((void)0)
Eric Andersen11c65522000-09-07 17:24:47 +0000380#endif
381
Rob Landleyea224be2006-06-18 20:20:07 +0000382
Eric Andersen3e6ff902001-03-09 21:24:12 +0000383static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000384{
385 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000386 int column = 0;
387 int nexttab = 0;
388 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000389
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000390 if (dn == NULL || nfiles < 1)
391 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000392
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000393 if (all_fmt & STYLE_LONG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000394 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000395 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000396 /* find the longest file name, use that as the column width */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000397 for (i = 0; i < nfiles; i++) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000398 int len = strlen(dn[i]->name);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000399 if (column_width < len)
400 column_width = len;
401 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000402 column_width += tabstops +
403 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
404 ((all_fmt & LIST_INO) ? 8 : 0) +
405 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000406 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000407 }
408
Eric Andersene57d54b2001-01-30 18:03:11 +0000409 if (ncols > 1) {
410 nrows = nfiles / ncols;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000411 if (nrows * ncols < nfiles)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000412 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000413 } else {
414 nrows = nfiles;
415 ncols = 1;
416 }
Eric Andersen11c65522000-09-07 17:24:47 +0000417
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000418 for (row = 0; row < nrows; row++) {
419 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000420 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000421 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000422 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000423 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000424 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000425 if (column > 0) {
426 nexttab -= column;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000427 printf("%*s", nexttab, "");
428 column += nexttab;
429 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000430 nexttab = column + column_width;
431 column += list_single(dn[i]);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000432 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000433 }
434 putchar('\n');
435 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000436 }
Eric Andersen11c65522000-09-07 17:24:47 +0000437}
438
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000439
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000440static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000441{
442 int i, nfiles;
443 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000444 int dndirs;
445 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000446
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000447 if (dn == NULL || ndirs < 1)
448 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000449
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000450 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000451 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000452 if (!first)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000453 bb_putchar('\n');
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000454 first = 0;
455 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000456 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000457 subdnp = list_dir(dn[i]->fullname);
458 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000459 if (nfiles > 0) {
460 /* list all files at this level */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000461 dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000462 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000463 if (ENABLE_FEATURE_LS_RECURSIVE) {
464 if (all_fmt & DISP_RECURSIVE) {
465 /* recursive- list the sub-dirs */
466 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
467 dndirs = countsubdirs(subdnp, nfiles);
468 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000469 dnsort(dnd, dndirs);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000470 showdirs(dnd, dndirs, 0);
471 /* free the array of dnode pointers to the dirs */
472 free(dnd);
473 }
Eric Andersen11c65522000-09-07 17:24:47 +0000474 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000475 /* free the dnodes and the fullname mem */
476 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000477 }
Eric Andersen11c65522000-09-07 17:24:47 +0000478 }
479 }
480}
481
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000482
Glenn L McGrath4d001292003-01-06 01:11:50 +0000483static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000484{
485 struct dnode *dn, *cur, **dnp;
486 struct dirent *entry;
487 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000488 int i, nfiles;
489
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000490 if (path == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000491 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000492
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000493 dn = NULL;
494 nfiles = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000495 dir = warn_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000496 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000497 status = EXIT_FAILURE;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000498 return NULL; /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000499 }
500 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000501 char *fullname;
502
Eric Andersen11c65522000-09-07 17:24:47 +0000503 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000504 if (entry->d_name[0] == '.') {
Denis Vlasenko16abcd92007-04-13 23:59:52 +0000505 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
506 && !(all_fmt & DISP_DOT)
507 ) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000508 continue;
Denis Vlasenko16abcd92007-04-13 23:59:52 +0000509 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000510 if (!(all_fmt & DISP_HIDDEN))
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000511 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000512 }
513 fullname = concat_path_file(path, entry->d_name);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000514 cur = my_stat(fullname, bb_basename(fullname), 0);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000515 if (!cur) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000516 free(fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000517 continue;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000518 }
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000519 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000520 cur->next = dn;
521 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000522 nfiles++;
523 }
524 closedir(dir);
525
526 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000527 * allocate memory for an array to hold dnode pointers
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000528 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000529 if (dn == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000530 return NULL;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000531 dnp = dnalloc(nfiles);
532 for (i = 0, cur = dn; i < nfiles; i++) {
533 dnp[i] = cur; /* save pointer to node in array */
534 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000535 }
536
Denis Vlasenko5c759602006-10-28 12:37:16 +0000537 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000538}
539
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000540
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000541#if ENABLE_FEATURE_LS_TIMESTAMPS
542/* Do time() just once. Saves one syscall per file for "ls -l" */
543/* Initialized in main() */
544static time_t current_time_t;
545#endif
546
Eric Andersen3e6ff902001-03-09 21:24:12 +0000547static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000548{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000549 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000550
Denis Vlasenko5c759602006-10-28 12:37:16 +0000551#if ENABLE_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000552 char *filetime;
553 time_t ttime, age;
554#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000555#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000556 struct stat info;
557 char append;
558#endif
559
Glenn L McGrath4d001292003-01-06 01:11:50 +0000560 if (dn->fullname == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000561 return 0;
Eric Andersen11c65522000-09-07 17:24:47 +0000562
Denis Vlasenko5c759602006-10-28 12:37:16 +0000563#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000564 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000565 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000566 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000567 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000568 ttime = dn->dstat.st_ctime;
569 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000570#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000571#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000572 append = append_char(dn->dstat.st_mode);
573#endif
574
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000575 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000576 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000577 case LIST_INO:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000578 column += printf("%7ld ", (long) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000579 break;
580 case LIST_BLOCKS:
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000581 column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000582 break;
583 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000584 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000585 break;
586 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000587 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000588 break;
589 case LIST_ID_NAME:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000590#if ENABLE_FEATURE_LS_USERNAME
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000591 printf("%-8.8s %-8.8s",
592 get_cached_username(dn->dstat.st_uid),
593 get_cached_groupname(dn->dstat.st_gid));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000594 column += 17;
595 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000596#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000597 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000598 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000599 break;
600 case LIST_SIZE:
601 case LIST_DEV:
602 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000603 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
604 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000605 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000606 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000607 column += printf("%9s ",
Denis Vlasenko5c759602006-10-28 12:37:16 +0000608 make_human_readable_str(dn->dstat.st_size, 1, 0));
609 } else {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000610 column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000611 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000612 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000613 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000614#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000615 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000616 printf("%24.24s ", filetime);
617 column += 25;
618 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000619 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000620 if ((all_fmt & LIST_FULLTIME) == 0) {
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000621 /* current_time_t ~== time(NULL) */
622 age = current_time_t - ttime;
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000623 printf("%6.6s ", filetime + 4);
624 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
625 /* hh:mm if less than 6 months old */
626 printf("%5.5s ", filetime + 11);
627 } else {
628 printf(" %4.4s ", filetime + 20);
629 }
630 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000631 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000632 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000633#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000634#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000635 case LIST_CONTEXT:
636 {
Rob Landley60158cb2005-05-03 06:25:50 +0000637 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000638 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000639
Rob Landley60158cb2005-05-03 06:25:50 +0000640 if (dn->sid) {
Denis Vlasenko656f7462006-10-28 13:02:55 +0000641 /* I assume sid initilized with NULL */
642 len = strlen(dn->sid) + 1;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000643 safe_strncpy(context, dn->sid, len);
644 freecon(dn->sid);
645 } else {
646 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000647 }
648 printf("%-32s ", context);
649 column += MAX(33, len);
650 }
651 break;
652#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000653 case LIST_FILENAME:
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000654 errno = 0;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000655#if ENABLE_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000656 if (show_color && !lstat(dn->fullname, &info)) {
657 printf("\033[%d;%dm", bgcolor(info.st_mode),
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000658 fgcolor(info.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000659 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000660#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000661 column += printf("%s", dn->name);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000662 if (show_color) {
663 printf("\033[0m");
664 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000665 break;
666 case LIST_SYMLINK:
667 if (S_ISLNK(dn->dstat.st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000668 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000669 if (!lpath) break;
670 printf(" -> ");
671#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
672 if (!stat(dn->fullname, &info)) {
673 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000674 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000675#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000676#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000677 if (show_color) {
678 errno = 0;
679 printf("\033[%d;%dm", bgcolor(info.st_mode),
680 fgcolor(info.st_mode));
681 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000682#endif
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000683 column += printf("%s", lpath) + 4;
684 if (show_color) {
685 printf("\033[0m");
686 }
687 free(lpath);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000688 }
689 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000690#if ENABLE_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000691 case LIST_FILETYPE:
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000692 if (append) {
693 putchar(append);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000694 column++;
695 }
696 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000697#endif
698 }
699 }
700
Glenn L McGrath4d001292003-01-06 01:11:50 +0000701 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000702}
703
Glenn L McGrath792cae52004-01-18 05:15:16 +0000704/* "[-]Cadil1", POSIX mandated options, busybox always supports */
705/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
706/* "[-]Ak" GNU options, busybox always supports */
707/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
708/* "[-]p", POSIX non-mandated options, busybox optionally supports */
709/* "[-]SXvThw", GNU options, busybox optionally supports */
710/* "[-]K", SELinux mandated options, busybox optionally supports */
711/* "[-]e", I think we made this one up */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000712static const char ls_options[] ALIGN1 =
713 "Cadil1gnsxAk"
Denis Vlasenko5c759602006-10-28 12:37:16 +0000714 USE_FEATURE_LS_TIMESTAMPS("cetu")
715 USE_FEATURE_LS_SORTFILES("SXrv")
716 USE_FEATURE_LS_FILETYPES("Fp")
717 USE_FEATURE_LS_FOLLOWLINKS("L")
718 USE_FEATURE_LS_RECURSIVE("R")
719 USE_FEATURE_HUMAN_READABLE("h")
720 USE_SELINUX("K")
Denis Vlasenko49622d72007-03-10 16:58:49 +0000721 USE_FEATURE_AUTOWIDTH("T:w:")
722 USE_SELINUX("Z");
Glenn L McGrath792cae52004-01-18 05:15:16 +0000723
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000724enum {
725 LIST_MASK_TRIGGER = 0,
726 STYLE_MASK_TRIGGER = STYLE_MASK,
727 DISP_MASK_TRIGGER = DISP_ROWS,
728 SORT_MASK_TRIGGER = SORT_MASK,
729};
Manuel Novoa III cad53642003-03-19 09:13:01 +0000730
731static const unsigned opt_flags[] = {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000732 LIST_SHORT | STYLE_COLUMNS, /* C */
733 DISP_HIDDEN | DISP_DOT, /* a */
734 DISP_NOLIST, /* d */
735 LIST_INO, /* i */
736 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
737 LIST_SHORT | STYLE_SINGLE, /* 1 */
738 0, /* g - ingored */
739 LIST_ID_NUMERIC, /* n */
740 LIST_BLOCKS, /* s */
741 DISP_ROWS, /* x */
742 DISP_HIDDEN, /* A */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000743 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000744#if ENABLE_FEATURE_LS_TIMESTAMPS
745 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
746 LIST_FULLTIME, /* e */
747 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
748 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000749#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000750#if ENABLE_FEATURE_LS_SORTFILES
751 SORT_SIZE, /* S */
752 SORT_EXT, /* X */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000753 SORT_REVERSE, /* r */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000754 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000755#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000756#if ENABLE_FEATURE_LS_FILETYPES
757 LIST_FILETYPE | LIST_EXEC, /* F */
758 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000759#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000760#if ENABLE_FEATURE_LS_FOLLOWLINKS
761 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000762#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000763#if ENABLE_FEATURE_LS_RECURSIVE
764 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000765#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000766#if ENABLE_FEATURE_HUMAN_READABLE
767 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000768#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000769#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000770 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
771#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000772#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000773 0, 0, /* T, w - ignored */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000774#endif
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000775#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000776 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
777#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000778 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000779};
780
781
Denis Vlasenko97fd6d82007-03-19 20:59:20 +0000782/* THIS IS A "SAFE" APPLET, main() MAY BE CALLED INTERNALLY FROM SHELL */
783/* BE CAREFUL! */
784
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000785int ls_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000786int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000787{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000788 struct dnode **dnd;
789 struct dnode **dnf;
790 struct dnode **dnp;
791 struct dnode *dn;
792 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000793 unsigned opt;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000794 int nfiles = 0;
795 int dnfiles;
796 int dndirs;
797 int oi;
798 int ac;
799 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000800 char **av;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000801 USE_FEATURE_LS_COLOR(char *color_opt;)
Glenn L McGrath792cae52004-01-18 05:15:16 +0000802
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000803#if ENABLE_FEATURE_LS_TIMESTAMPS
804 time(&current_time_t);
805#endif
806
Rob Landley2b8a05a2006-06-20 17:43:01 +0000807 all_fmt = LIST_SHORT |
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000808 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000809
Denis Vlasenko5c759602006-10-28 12:37:16 +0000810#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000811 /* Obtain the terminal width */
Denis Vlasenkof893da82007-08-09 08:27:24 +0000812 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000813 /* Go one less... */
814 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000815#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000816
Eric Andersen11c65522000-09-07 17:24:47 +0000817 /* process options */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000818 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000819#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko1d426652008-03-17 09:09:09 +0000820 opt_complementary = "T+:w+"; /* -T N, -w N */
821 opt = getopt32(argv, ls_options, &tabstops, &terminal_width
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000822 USE_FEATURE_LS_COLOR(, &color_opt));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000823#else
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000824 opt = getopt32(argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000825#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000826 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000827 if (opt & (1 << i)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000828 unsigned flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000829
Denis Vlasenko5c759602006-10-28 12:37:16 +0000830 if (flags & LIST_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000831 all_fmt &= ~LIST_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000832 if (flags & STYLE_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000833 all_fmt &= ~STYLE_MASK;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000834 if (flags & SORT_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000835 all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000836 if (flags & DISP_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 all_fmt &= ~DISP_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000838 if (flags & TIME_MASK)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000839 all_fmt &= ~TIME_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000840 if (flags & LIST_CONTEXT)
Eric Andersen9e480452003-07-03 10:07:04 +0000841 all_fmt |= STYLE_SINGLE;
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000842 /* huh?? opt cannot be 'l' */
843 //if (LS_DISP_HR && opt == 'l')
844 // all_fmt &= ~LS_DISP_HR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000845 all_fmt |= flags;
846 }
Eric Andersen11c65522000-09-07 17:24:47 +0000847 }
848
Denis Vlasenko5c759602006-10-28 12:37:16 +0000849#if ENABLE_FEATURE_LS_COLOR
850 /* find color bit value - last position for short getopt */
851 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
852 char *p = getenv("LS_COLORS");
853 /* LS_COLORS is unset, or (not empty && not "none") ? */
854 if (!p || (p[0] && strcmp(p, "none")))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000855 show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000856 }
857 if (opt & (1 << i)) { /* next flag after short options */
858 if (!color_opt || !strcmp("always", color_opt))
859 show_color = 1;
860 else if (color_opt && !strcmp("never", color_opt))
861 show_color = 0;
862 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
863 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000864 }
865#endif
866
Eric Andersen11c65522000-09-07 17:24:47 +0000867 /* sort out which command line options take precedence */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000868 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000869 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +0000870 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
871 if (all_fmt & TIME_CHANGE)
872 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
873 if (all_fmt & TIME_ACCESS)
874 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
875 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000876 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
877 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000878 if (ENABLE_FEATURE_LS_USERNAME)
879 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
880 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000881
Eric Andersen11c65522000-09-07 17:24:47 +0000882 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +0000883 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +0000884 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +0000885
886 /*
887 * when there are no cmd line args we have to supply a default "." arg.
888 * we will create a second argv array, "av" that will hold either
889 * our created "." arg, or the real cmd line args. The av array
890 * just holds the pointers- we don't move the date the pointers
891 * point to.
892 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000893 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +0000894 if (ac < 1) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000895 static const char *const dotdir[] = { "." };
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000896
897 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000898 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +0000899 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000900 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +0000901 }
902
903 /* now, everything is in the av array */
904 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000905 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +0000906
Denis Vlasenko5c759602006-10-28 12:37:16 +0000907 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000908 dn = NULL;
909 for (oi = 0; oi < ac; oi++) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000910 /* ls w/o -l follows links on command line */
911 cur = my_stat(av[oi], av[oi], !(all_fmt & STYLE_LONG));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000912 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000913 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000914 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000915 cur->next = dn;
916 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000917 nfiles++;
918 }
919
920 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000921 * allocate memory for an array to hold dnode pointers
922 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000923 dnp = dnalloc(nfiles);
924 for (i = 0, cur = dn; i < nfiles; i++) {
925 dnp[i] = cur; /* save pointer to node in array */
926 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000927 }
928
Manuel Novoa III cad53642003-03-19 09:13:01 +0000929 if (all_fmt & DISP_NOLIST) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000930 dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000931 if (nfiles > 0)
932 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000933 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000934 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
935 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
936 dndirs = countdirs(dnp, nfiles);
937 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000938 if (dnfiles > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000939 dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000940 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +0000941 if (ENABLE_FEATURE_CLEAN_UP)
942 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +0000943 }
944 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000945 dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000946 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +0000947 if (ENABLE_FEATURE_CLEAN_UP)
948 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +0000949 }
950 }
Rob Landley26314862006-05-02 19:46:52 +0000951 if (ENABLE_FEATURE_CLEAN_UP)
952 dfree(dnp, nfiles);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000953 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000954}