blob: 8aa05507dce89f5135e3ac88b7d31dff0293d73f [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
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +000034#if ENABLE_FEATURE_ASSUME_UNICODE
35#include <wchar.h>
36#endif
37
Denis Vlasenko99912ca2007-04-10 15:43:37 +000038/* This is a NOEXEC applet. Be very careful! */
39
Eric Andersenf1142c52001-02-20 06:16:29 +000040
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000041enum {
Manuel Novoa III cad53642003-03-19 09:13:01 +000042
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000043TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
44COLUMN_GAP = 2, /* includes the file type char */
45
46/* what is the overall style of the listing */
47STYLE_COLUMNS = 1 << 21, /* fill columns */
48STYLE_LONG = 2 << 21, /* one record per line, extended info */
49STYLE_SINGLE = 3 << 21, /* one record per line */
50STYLE_MASK = STYLE_SINGLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Eric Andersen11c65522000-09-07 17:24:47 +000052/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
53/* what file information will be listed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000054LIST_INO = 1 << 0,
55LIST_BLOCKS = 1 << 1,
56LIST_MODEBITS = 1 << 2,
57LIST_NLINKS = 1 << 3,
58LIST_ID_NAME = 1 << 4,
59LIST_ID_NUMERIC = 1 << 5,
60LIST_CONTEXT = 1 << 6,
61LIST_SIZE = 1 << 7,
62LIST_DEV = 1 << 8,
63LIST_DATE_TIME = 1 << 9,
64LIST_FULLTIME = 1 << 10,
65LIST_FILENAME = 1 << 11,
66LIST_SYMLINK = 1 << 12,
67LIST_FILETYPE = 1 << 13,
68LIST_EXEC = 1 << 14,
69LIST_MASK = (LIST_EXEC << 1) - 1,
Eric Andersen11c65522000-09-07 17:24:47 +000070
71/* what files will be displayed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000072DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
Denis Vlasenko656f7462006-10-28 13:02:55 +000073DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000074DISP_DOT = 1 << 17, /* show . and .. */
75DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
76DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
77DISP_ROWS = 1 << 20, /* print across rows */
78DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
Manuel Novoa III cad53642003-03-19 09:13:01 +000079
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000080/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
81SORT_FORWARD = 0, /* sort in reverse order */
82SORT_REVERSE = 1 << 27, /* sort in reverse order */
Eric Andersen11c65522000-09-07 17:24:47 +000083
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000084SORT_NAME = 0, /* sort by file name */
85SORT_SIZE = 1 << 28, /* sort by file size */
86SORT_ATIME = 2 << 28, /* sort by last access time */
87SORT_CTIME = 3 << 28, /* sort by last change time */
88SORT_MTIME = 4 << 28, /* sort by last modification time */
89SORT_VERSION = 5 << 28, /* sort by version */
90SORT_EXT = 6 << 28, /* sort by file name extension */
91SORT_DIR = 7 << 28, /* sort by file or directory */
92SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Eric Andersen11c65522000-09-07 17:24:47 +000094/* which of the three times will be used */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000095TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
96TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
97TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
Manuel Novoa III cad53642003-03-19 09:13:01 +000098
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000099FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
Eric Andersencc8ed391999-10-05 16:24:54 +0000100
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000101LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
Eric Andersencc8ed391999-10-05 16:24:54 +0000102
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000103LIST_SHORT = LIST_FILENAME,
104LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
105 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
106
107SPLIT_DIR = 1,
108SPLIT_FILE = 0,
109SPLIT_SUBDIR = 2,
110
111};
Eric Andersencc8ed391999-10-05 16:24:54 +0000112
Eric Andersen11c65522000-09-07 17:24:47 +0000113#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
114#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000115#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
116#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
117 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
118#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
119 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000120
Eric Andersen11c65522000-09-07 17:24:47 +0000121/*
122 * a directory entry and its stat info are stored here
123 */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000124struct dnode { /* the basic node */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000125 const char *name; /* the dir entry name */
126 const char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000127 int allocated;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000128 struct stat dstat; /* the file stat info */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000129 USE_SELINUX(security_context_t sid;)
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000130 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000131};
Eric Andersen11c65522000-09-07 17:24:47 +0000132
Glenn L McGrath4d001292003-01-06 01:11:50 +0000133static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000134static struct dnode **dnalloc(int);
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000135static int list_single(const struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000136
Eric Andersen11c65522000-09-07 17:24:47 +0000137
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000138struct globals {
139#if ENABLE_FEATURE_LS_COLOR
140 smallint show_color;
141#endif
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000142 smallint exit_code;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000143 unsigned all_fmt;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000144#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000145 unsigned tabstops; // = COLUMN_GAP;
146 unsigned terminal_width; // = TERMINAL_WIDTH;
147#endif
148#if ENABLE_FEATURE_LS_TIMESTAMPS
149 /* Do time() just once. Saves one syscall per file for "ls -l" */
150 time_t current_time_t;
151#endif
152};
153#define G (*(struct globals*)&bb_common_bufsiz1)
154#if ENABLE_FEATURE_LS_COLOR
155#define show_color (G.show_color )
156#else
157enum { show_color = 0 };
158#endif
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000159#define exit_code (G.exit_code )
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000160#define all_fmt (G.all_fmt )
161#if ENABLE_FEATURE_AUTOWIDTH
162#define tabstops (G.tabstops )
163#define terminal_width (G.terminal_width)
Eric Andersenf5d5e772001-01-24 23:34:48 +0000164#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000165enum {
166 tabstops = COLUMN_GAP,
167 terminal_width = TERMINAL_WIDTH,
168};
Eric Andersen11c65522000-09-07 17:24:47 +0000169#endif
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000170#define current_time_t (G.current_time_t)
171/* memset: we have to zero it out because of NOEXEC */
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000172#define INIT_G() do { \
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000173 memset(&G, 0, sizeof(G)); \
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +0000174 USE_FEATURE_AUTOWIDTH(tabstops = COLUMN_GAP;) \
175 USE_FEATURE_AUTOWIDTH(terminal_width = TERMINAL_WIDTH;) \
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000176 USE_FEATURE_LS_TIMESTAMPS(time(&current_time_t);) \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000177} while (0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000178
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000179
180#if ENABLE_FEATURE_ASSUME_UNICODE
181/* libbb candidate */
182static size_t mbstrlen(const char *string)
183{
184 size_t width = mbsrtowcs(NULL /*dest*/, &string,
185 MAXINT(size_t) /*len*/, NULL /*state*/);
186 if (width == (size_t)-1)
187 return strlen(string);
188 return width;
189}
190#else
191#define mbstrlen(string) strlen(string)
192#endif
193
Matt Kraai33fdae52000-10-13 17:59:43 +0000194
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000195static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000196{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000197 struct stat dstat;
198 struct dnode *cur;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000199 USE_SELINUX(security_context_t sid = NULL;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000200
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000201 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000202#if ENABLE_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000203 if (is_selinux_enabled()) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000204 getfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000205 }
Eric Andersen9e480452003-07-03 10:07:04 +0000206#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000207 if (stat(fullname, &dstat)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000208 bb_simple_perror_msg(fullname);
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000209 exit_code = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000210 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000211 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000212 } else {
213#if ENABLE_SELINUX
214 if (is_selinux_enabled()) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000215 lgetfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000216 }
Eric Andersen9e480452003-07-03 10:07:04 +0000217#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000218 if (lstat(fullname, &dstat)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000219 bb_simple_perror_msg(fullname);
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000220 exit_code = EXIT_FAILURE;
Eric Andersen9e480452003-07-03 10:07:04 +0000221 return 0;
222 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000223 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000224
Denis Vlasenko5c759602006-10-28 12:37:16 +0000225 cur = xmalloc(sizeof(struct dnode));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000226 cur->fullname = fullname;
227 cur->name = name;
228 cur->dstat = dstat;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000229 USE_SELINUX(cur->sid = sid;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000230 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000231}
232
Denis Vlasenko5c759602006-10-28 12:37:16 +0000233#if ENABLE_FEATURE_LS_COLOR
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000234static char fgcolor(mode_t mode)
235{
236 /* Check wheter the file is existing (if so, color it red!) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000237 if (errno == ENOENT)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000238 return '\037';
Rob Landley9947a242006-06-15 22:11:10 +0000239 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000240 return COLOR(0xF000); /* File is executable ... */
241 return COLOR(mode);
242}
243
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000244static char bgcolor(mode_t mode)
245{
Rob Landley9947a242006-06-15 22:11:10 +0000246 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000247 return ATTR(0xF000); /* File is executable ... */
248 return ATTR(mode);
249}
250#endif
251
Denis Vlasenko5c759602006-10-28 12:37:16 +0000252#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000253static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000254{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000256 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000257 if (S_ISDIR(mode))
258 return '/';
259 if (!(all_fmt & LIST_EXEC))
260 return '\0';
261 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000262 return '*';
263 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000264}
265#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000266
Denis Vlasenko5c759602006-10-28 12:37:16 +0000267#define countdirs(A, B) count_dirs((A), (B), 1)
268#define countsubdirs(A, B) count_dirs((A), (B), 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000269static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000270{
271 int i, dirs;
272
Denis Vlasenko5c759602006-10-28 12:37:16 +0000273 if (!dn)
274 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000275 dirs = 0;
276 for (i = 0; i < nfiles; i++) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000277 const char *name;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000278 if (!S_ISDIR(dn[i]->dstat.st_mode))
279 continue;
280 name = dn[i]->name;
281 if (notsubdirs
282 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
283 ) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000284 dirs++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000285 }
Eric Andersen11c65522000-09-07 17:24:47 +0000286 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000287 return dirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000288}
289
Eric Andersen3e6ff902001-03-09 21:24:12 +0000290static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000291{
292 int nfiles;
293 struct dnode *cur;
294
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000295 if (dnp == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000296 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000297 nfiles = 0;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000298 for (cur = dnp[0]; cur->next; cur = cur->next)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000299 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000300 nfiles++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000301 return nfiles;
Eric Andersen11c65522000-09-07 17:24:47 +0000302}
303
304/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000305static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000306{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000307 if (num < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000308 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000309
Denis Vlasenko5c759602006-10-28 12:37:16 +0000310 return xzalloc(num * sizeof(struct dnode *));
Eric Andersen11c65522000-09-07 17:24:47 +0000311}
312
Denis Vlasenko5c759602006-10-28 12:37:16 +0000313#if ENABLE_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000314static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000315{
Rob Landley26314862006-05-02 19:46:52 +0000316 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000317
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000318 if (dnp == NULL)
319 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000320
Rob Landley26314862006-05-02 19:46:52 +0000321 for (i = 0; i < nfiles; i++) {
322 struct dnode *cur = dnp[i];
Denis Vlasenko5c759602006-10-28 12:37:16 +0000323 if (cur->allocated)
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000324 free((char*)cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000325 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000326 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000327 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000328}
Rob Landleyc44bc982006-05-28 01:19:06 +0000329#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000330#define dfree(...) ((void)0)
Eric Andersen20aab262001-07-19 22:28:02 +0000331#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000332
Eric Andersen3e6ff902001-03-09 21:24:12 +0000333static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000334{
335 int dncnt, i, d;
336 struct dnode **dnp;
337
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000338 if (dn == NULL || nfiles < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000339 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000340
341 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000342 if (which == SPLIT_SUBDIR)
343 dncnt = countsubdirs(dn, nfiles);
344 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000345 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000346 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000347 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000348 }
Eric Andersen11c65522000-09-07 17:24:47 +0000349
350 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000351 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000352
353 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000354 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000355 if (S_ISDIR(dn[i]->dstat.st_mode)) {
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000356 const char *name;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000357 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
358 continue;
359 name = dn[i]->name;
360 if ((which & SPLIT_DIR)
361 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
362 ) {
363 dnp[d++] = dn[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000364 }
365 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
366 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000367 }
368 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000369 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000370}
371
Denis Vlasenko5c759602006-10-28 12:37:16 +0000372#if ENABLE_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000373static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000374{
Rob Landley425e7582006-05-03 20:22:03 +0000375 struct dnode *d1 = *(struct dnode **)a;
376 struct dnode *d2 = *(struct dnode **)b;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000377 unsigned sort_opts = all_fmt & SORT_MASK;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000378 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000379
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000380 dif = 0; /* assume SORT_NAME */
381 // TODO: use pre-initialized function pointer
382 // instead of branch forest
Eric Andersen11c65522000-09-07 17:24:47 +0000383 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000384 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000385 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000386 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000387 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000388 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000389 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000390 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000391 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000392 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000393 /* } else if (sort_opts == SORT_VERSION) { */
394 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000395 }
396
Eric Andersen11c65522000-09-07 17:24:47 +0000397 if (dif == 0) {
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000398 /* sort by name - may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000399 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
400 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000401 }
402
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000403 if (all_fmt & SORT_REVERSE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000404 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000405 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000406 return dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000407}
408
Rob Landley425e7582006-05-03 20:22:03 +0000409static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000410{
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000411 qsort(dn, size, sizeof(*dn), sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000412}
Rob Landleyea224be2006-06-18 20:20:07 +0000413#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000414#define dnsort(dn, size) ((void)0)
Eric Andersen11c65522000-09-07 17:24:47 +0000415#endif
416
Rob Landleyea224be2006-06-18 20:20:07 +0000417
Eric Andersen3e6ff902001-03-09 21:24:12 +0000418static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000419{
420 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000421 int column = 0;
422 int nexttab = 0;
423 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000424
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000425 if (dn == NULL || nfiles < 1)
426 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000427
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000428 if (all_fmt & STYLE_LONG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000429 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000430 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000431 /* find the longest file name, use that as the column width */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000432 for (i = 0; i < nfiles; i++) {
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000433 int len = mbstrlen(dn[i]->name);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000434 if (column_width < len)
435 column_width = len;
436 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000437 column_width += tabstops +
438 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
439 ((all_fmt & LIST_INO) ? 8 : 0) +
440 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000441 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000442 }
443
Eric Andersene57d54b2001-01-30 18:03:11 +0000444 if (ncols > 1) {
445 nrows = nfiles / ncols;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000446 if (nrows * ncols < nfiles)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000447 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000448 } else {
449 nrows = nfiles;
450 ncols = 1;
451 }
Eric Andersen11c65522000-09-07 17:24:47 +0000452
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000453 for (row = 0; row < nrows; row++) {
454 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000455 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000456 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000457 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000458 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000459 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000460 if (column > 0) {
461 nexttab -= column;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000462 printf("%*s", nexttab, "");
463 column += nexttab;
464 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000465 nexttab = column + column_width;
466 column += list_single(dn[i]);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000467 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000468 }
469 putchar('\n');
470 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000471 }
Eric Andersen11c65522000-09-07 17:24:47 +0000472}
473
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000474
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000475static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000476{
477 int i, nfiles;
478 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000479 int dndirs;
480 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000481
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000482 if (dn == NULL || ndirs < 1)
483 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000484
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000485 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000486 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000487 if (!first)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000488 bb_putchar('\n');
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000489 first = 0;
490 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000491 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000492 subdnp = list_dir(dn[i]->fullname);
493 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000494 if (nfiles > 0) {
495 /* list all files at this level */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000496 dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000497 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000498 if (ENABLE_FEATURE_LS_RECURSIVE) {
499 if (all_fmt & DISP_RECURSIVE) {
500 /* recursive- list the sub-dirs */
501 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
502 dndirs = countsubdirs(subdnp, nfiles);
503 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000504 dnsort(dnd, dndirs);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000505 showdirs(dnd, dndirs, 0);
506 /* free the array of dnode pointers to the dirs */
507 free(dnd);
508 }
Eric Andersen11c65522000-09-07 17:24:47 +0000509 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000510 /* free the dnodes and the fullname mem */
511 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000512 }
Eric Andersen11c65522000-09-07 17:24:47 +0000513 }
514 }
515}
516
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000517
Glenn L McGrath4d001292003-01-06 01:11:50 +0000518static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000519{
520 struct dnode *dn, *cur, **dnp;
521 struct dirent *entry;
522 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000523 int i, nfiles;
524
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000525 if (path == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000526 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000527
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000528 dn = NULL;
529 nfiles = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000530 dir = warn_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000531 if (dir == NULL) {
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000532 exit_code = EXIT_FAILURE;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000533 return NULL; /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000534 }
535 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000536 char *fullname;
537
Eric Andersen11c65522000-09-07 17:24:47 +0000538 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000539 if (entry->d_name[0] == '.') {
Denis Vlasenko16abcd92007-04-13 23:59:52 +0000540 if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
541 && !(all_fmt & DISP_DOT)
542 ) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000543 continue;
Denis Vlasenko16abcd92007-04-13 23:59:52 +0000544 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000545 if (!(all_fmt & DISP_HIDDEN))
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000546 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000547 }
548 fullname = concat_path_file(path, entry->d_name);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000549 cur = my_stat(fullname, bb_basename(fullname), 0);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000550 if (!cur) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000551 free(fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000552 continue;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000553 }
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000554 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000555 cur->next = dn;
556 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000557 nfiles++;
558 }
559 closedir(dir);
560
561 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000562 * allocate memory for an array to hold dnode pointers
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000563 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000564 if (dn == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000565 return NULL;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000566 dnp = dnalloc(nfiles);
567 for (i = 0, cur = dn; i < nfiles; i++) {
568 dnp[i] = cur; /* save pointer to node in array */
569 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000570 }
571
Denis Vlasenko5c759602006-10-28 12:37:16 +0000572 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000573}
574
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000575
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000576static int list_single(const struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000577{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000578 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000579
Denis Vlasenko5c759602006-10-28 12:37:16 +0000580#if ENABLE_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000581 char *filetime;
582 time_t ttime, age;
583#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000584#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000585 struct stat info;
586 char append;
587#endif
588
Glenn L McGrath4d001292003-01-06 01:11:50 +0000589 if (dn->fullname == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000590 return 0;
Eric Andersen11c65522000-09-07 17:24:47 +0000591
Denis Vlasenko5c759602006-10-28 12:37:16 +0000592#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000593 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000594 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000595 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000596 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000597 ttime = dn->dstat.st_ctime;
598 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000599#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000600#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000601 append = append_char(dn->dstat.st_mode);
602#endif
603
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000604 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000605 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000606 case LIST_INO:
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000607 column += printf("%7lu ", (long) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000608 break;
609 case LIST_BLOCKS:
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000610 column += printf("%4"OFF_FMT"u ", (off_t) dn->dstat.st_blocks >> 1);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000611 break;
612 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000613 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000614 break;
615 case LIST_NLINKS:
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000616 column += printf("%4lu ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000617 break;
618 case LIST_ID_NAME:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000619#if ENABLE_FEATURE_LS_USERNAME
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000620 printf("%-8.8s %-8.8s",
621 get_cached_username(dn->dstat.st_uid),
622 get_cached_groupname(dn->dstat.st_gid));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000623 column += 17;
624 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000625#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000626 case LIST_ID_NUMERIC:
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000627 column += printf("%-8u %-8u", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000628 break;
629 case LIST_SIZE:
630 case LIST_DEV:
631 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000632 column += printf("%4u, %3u ", (int) major(dn->dstat.st_rdev),
Eric Andersen4f807a82004-07-26 09:11:12 +0000633 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000634 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000635 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000636 column += printf("%9s ",
Denis Vlasenko5c759602006-10-28 12:37:16 +0000637 make_human_readable_str(dn->dstat.st_size, 1, 0));
638 } else {
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000639 column += printf("%9"OFF_FMT"u ", (off_t) dn->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000640 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000641 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000642 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000643#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000644 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000645 printf("%24.24s ", filetime);
646 column += 25;
647 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000648 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000649 if ((all_fmt & LIST_FULLTIME) == 0) {
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000650 /* current_time_t ~== time(NULL) */
651 age = current_time_t - ttime;
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000652 printf("%6.6s ", filetime + 4);
653 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
654 /* hh:mm if less than 6 months old */
655 printf("%5.5s ", filetime + 11);
656 } else {
657 printf(" %4.4s ", filetime + 20);
658 }
659 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000660 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000661 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000662#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000663#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000664 case LIST_CONTEXT:
665 {
Rob Landley60158cb2005-05-03 06:25:50 +0000666 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000667 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000668
Rob Landley60158cb2005-05-03 06:25:50 +0000669 if (dn->sid) {
Denis Vlasenko656f7462006-10-28 13:02:55 +0000670 /* I assume sid initilized with NULL */
671 len = strlen(dn->sid) + 1;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000672 safe_strncpy(context, dn->sid, len);
673 freecon(dn->sid);
674 } else {
675 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000676 }
677 printf("%-32s ", context);
678 column += MAX(33, len);
679 }
680 break;
681#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000682 case LIST_FILENAME:
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000683 errno = 0;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000684#if ENABLE_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000685 if (show_color && !lstat(dn->fullname, &info)) {
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000686 printf("\033[%u;%um", bgcolor(info.st_mode),
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000687 fgcolor(info.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000688 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000689#endif
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000690#if ENABLE_FEATURE_ASSUME_UNICODE
691 printf("%s", dn->name);
692 column += mbstrlen(dn->name);
693#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000694 column += printf("%s", dn->name);
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000695#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000696 if (show_color) {
697 printf("\033[0m");
698 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000699 break;
700 case LIST_SYMLINK:
701 if (S_ISLNK(dn->dstat.st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000702 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000703 if (!lpath) break;
704 printf(" -> ");
705#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
706 if (!stat(dn->fullname, &info)) {
707 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000708 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000709#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000710#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000711 if (show_color) {
712 errno = 0;
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000713 printf("\033[%u;%um", bgcolor(info.st_mode),
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000714 fgcolor(info.st_mode));
715 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000716#endif
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000717 column += printf("%s", lpath) + 4;
718 if (show_color) {
719 printf("\033[0m");
720 }
721 free(lpath);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000722 }
723 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000724#if ENABLE_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000725 case LIST_FILETYPE:
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000726 if (append) {
727 putchar(append);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000728 column++;
729 }
730 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000731#endif
732 }
733 }
734
Glenn L McGrath4d001292003-01-06 01:11:50 +0000735 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000736}
737
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000738
Glenn L McGrath792cae52004-01-18 05:15:16 +0000739/* "[-]Cadil1", POSIX mandated options, busybox always supports */
740/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
741/* "[-]Ak" GNU options, busybox always supports */
742/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
743/* "[-]p", POSIX non-mandated options, busybox optionally supports */
744/* "[-]SXvThw", GNU options, busybox optionally supports */
745/* "[-]K", SELinux mandated options, busybox optionally supports */
746/* "[-]e", I think we made this one up */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000747static const char ls_options[] ALIGN1 =
748 "Cadil1gnsxAk"
Denis Vlasenko5c759602006-10-28 12:37:16 +0000749 USE_FEATURE_LS_TIMESTAMPS("cetu")
750 USE_FEATURE_LS_SORTFILES("SXrv")
751 USE_FEATURE_LS_FILETYPES("Fp")
752 USE_FEATURE_LS_FOLLOWLINKS("L")
753 USE_FEATURE_LS_RECURSIVE("R")
754 USE_FEATURE_HUMAN_READABLE("h")
755 USE_SELINUX("K")
Denis Vlasenko49622d72007-03-10 16:58:49 +0000756 USE_FEATURE_AUTOWIDTH("T:w:")
757 USE_SELINUX("Z");
Glenn L McGrath792cae52004-01-18 05:15:16 +0000758
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000759enum {
760 LIST_MASK_TRIGGER = 0,
761 STYLE_MASK_TRIGGER = STYLE_MASK,
762 DISP_MASK_TRIGGER = DISP_ROWS,
763 SORT_MASK_TRIGGER = SORT_MASK,
764};
Manuel Novoa III cad53642003-03-19 09:13:01 +0000765
766static const unsigned opt_flags[] = {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000767 LIST_SHORT | STYLE_COLUMNS, /* C */
768 DISP_HIDDEN | DISP_DOT, /* a */
769 DISP_NOLIST, /* d */
770 LIST_INO, /* i */
771 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
772 LIST_SHORT | STYLE_SINGLE, /* 1 */
773 0, /* g - ingored */
774 LIST_ID_NUMERIC, /* n */
775 LIST_BLOCKS, /* s */
776 DISP_ROWS, /* x */
777 DISP_HIDDEN, /* A */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000778 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000779#if ENABLE_FEATURE_LS_TIMESTAMPS
780 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
781 LIST_FULLTIME, /* e */
782 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
783 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000784#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000785#if ENABLE_FEATURE_LS_SORTFILES
786 SORT_SIZE, /* S */
787 SORT_EXT, /* X */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000788 SORT_REVERSE, /* r */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000789 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000790#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000791#if ENABLE_FEATURE_LS_FILETYPES
792 LIST_FILETYPE | LIST_EXEC, /* F */
793 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000794#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000795#if ENABLE_FEATURE_LS_FOLLOWLINKS
796 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000797#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000798#if ENABLE_FEATURE_LS_RECURSIVE
799 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000800#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000801#if ENABLE_FEATURE_HUMAN_READABLE
802 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000803#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000804#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000805 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
806#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000807#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000808 0, 0, /* T, w - ignored */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000809#endif
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000810#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000811 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
812#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000813 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000814};
815
816
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000817/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
818#if ENABLE_FEATURE_LS_COLOR
819/* long option entry used only for --color, which has no short option
820 * equivalent */
821static const char ls_color_opt[] ALIGN1 =
822 "color\0" Optional_argument "\xff" /* no short equivalent */
823 ;
824#endif
825
Denis Vlasenko97fd6d82007-03-19 20:59:20 +0000826
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000827int ls_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000828int ls_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000829{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000830 struct dnode **dnd;
831 struct dnode **dnf;
832 struct dnode **dnp;
833 struct dnode *dn;
834 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000835 unsigned opt;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000836 int nfiles;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000837 int dnfiles;
838 int dndirs;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000839 int i;
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +0000840 /* need to initialize since --color has _an optional_ argument */
841 USE_FEATURE_LS_COLOR(const char *color_opt = "always";)
Glenn L McGrath792cae52004-01-18 05:15:16 +0000842
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000843 INIT_G();
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000844
Rob Landley2b8a05a2006-06-20 17:43:01 +0000845 all_fmt = LIST_SHORT |
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000846 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000847
Denis Vlasenko5c759602006-10-28 12:37:16 +0000848#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000849 /* Obtain the terminal width */
Denis Vlasenkof893da82007-08-09 08:27:24 +0000850 get_terminal_width_height(STDIN_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000851 /* Go one less... */
852 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000853#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000854
Eric Andersen11c65522000-09-07 17:24:47 +0000855 /* process options */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000856 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000857#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko1d426652008-03-17 09:09:09 +0000858 opt_complementary = "T+:w+"; /* -T N, -w N */
859 opt = getopt32(argv, ls_options, &tabstops, &terminal_width
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000860 USE_FEATURE_LS_COLOR(, &color_opt));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000861#else
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000862 opt = getopt32(argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000863#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000864 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000865 if (opt & (1 << i)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000866 unsigned flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000867
Denis Vlasenko5c759602006-10-28 12:37:16 +0000868 if (flags & LIST_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000869 all_fmt &= ~LIST_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000870 if (flags & STYLE_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000871 all_fmt &= ~STYLE_MASK;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000872 if (flags & SORT_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000873 all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000874 if (flags & DISP_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000875 all_fmt &= ~DISP_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000876 if (flags & TIME_MASK)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000877 all_fmt &= ~TIME_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000878 if (flags & LIST_CONTEXT)
Eric Andersen9e480452003-07-03 10:07:04 +0000879 all_fmt |= STYLE_SINGLE;
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000880 /* huh?? opt cannot be 'l' */
881 //if (LS_DISP_HR && opt == 'l')
882 // all_fmt &= ~LS_DISP_HR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000883 all_fmt |= flags;
884 }
Eric Andersen11c65522000-09-07 17:24:47 +0000885 }
886
Denis Vlasenko5c759602006-10-28 12:37:16 +0000887#if ENABLE_FEATURE_LS_COLOR
888 /* find color bit value - last position for short getopt */
889 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
890 char *p = getenv("LS_COLORS");
891 /* LS_COLORS is unset, or (not empty && not "none") ? */
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +0000892 if (!p || (p[0] && strcmp(p, "none") != 0))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000893 show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000894 }
895 if (opt & (1 << i)) { /* next flag after short options */
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +0000896 if (strcmp("always", color_opt) == 0)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000897 show_color = 1;
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +0000898 else if (strcmp("never", color_opt) == 0)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000899 show_color = 0;
Denis Vlasenko51f1b6c2008-07-15 05:21:47 +0000900 else if (strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
Denis Vlasenko5c759602006-10-28 12:37:16 +0000901 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000902 }
903#endif
904
Eric Andersen11c65522000-09-07 17:24:47 +0000905 /* sort out which command line options take precedence */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000906 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000907 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +0000908 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
909 if (all_fmt & TIME_CHANGE)
910 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
911 if (all_fmt & TIME_ACCESS)
912 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
913 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000914 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
915 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000916 if (ENABLE_FEATURE_LS_USERNAME)
917 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
918 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000919
Eric Andersen11c65522000-09-07 17:24:47 +0000920 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +0000921 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +0000922 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +0000923
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000924 argv += optind;
925 if (!argv[0])
926 *--argv = (char*)".";
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000927
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000928 if (argv[1])
929 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +0000930
Denis Vlasenko5c759602006-10-28 12:37:16 +0000931 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000932 dn = NULL;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000933 nfiles = 0;
934 do {
Denis Vlasenko11a6f9b2009-03-03 13:20:22 +0000935 /* NB: follow links on command line unless -l or -s */
936 cur = my_stat(*argv, *argv, !(all_fmt & (STYLE_LONG|LIST_BLOCKS)));
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000937 argv++;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000938 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000939 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000940 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000941 cur->next = dn;
942 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000943 nfiles++;
Denis Vlasenkoc7497ea2008-06-13 11:16:09 +0000944 } while (*argv);
Eric Andersen11c65522000-09-07 17:24:47 +0000945
946 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000947 * allocate memory for an array to hold dnode pointers
948 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000949 dnp = dnalloc(nfiles);
950 for (i = 0, cur = dn; i < nfiles; i++) {
951 dnp[i] = cur; /* save pointer to node in array */
952 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000953 }
954
Manuel Novoa III cad53642003-03-19 09:13:01 +0000955 if (all_fmt & DISP_NOLIST) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000956 dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000957 if (nfiles > 0)
958 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000959 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000960 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
961 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
962 dndirs = countdirs(dnp, nfiles);
963 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000964 if (dnfiles > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000965 dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000966 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +0000967 if (ENABLE_FEATURE_CLEAN_UP)
968 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +0000969 }
970 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000971 dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000972 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +0000973 if (ENABLE_FEATURE_CLEAN_UP)
974 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +0000975 }
976 }
Rob Landley26314862006-05-02 19:46:52 +0000977 if (ENABLE_FEATURE_CLEAN_UP)
978 dfree(dnp, nfiles);
Denis Vlasenko4a689e92008-06-18 16:38:22 +0000979 return exit_code;
Eric Andersencc8ed391999-10-05 16:24:54 +0000980}