blob: f31a496822b0b83d5b8c084516ef07921eb068d6 [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
Eric Andersene57d54b2001-01-30 18:03:11 +000032enum {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +000033 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +000034 COLUMN_GAP = 2, /* includes the file type char */
Eric Andersene57d54b2001-01-30 18:03:11 +000035};
36
Eric Andersencc8ed391999-10-05 16:24:54 +000037/************************************************************************/
38
Rob Landleyea224be2006-06-18 20:20:07 +000039#include "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000040#include <getopt.h>
Eric Andersenf1142c52001-02-20 06:16:29 +000041
Eric Andersen11c65522000-09-07 17:24:47 +000042/* what is the overall style of the listing */
Manuel Novoa III cad53642003-03-19 09:13:01 +000043#define STYLE_COLUMNS (1U<<21) /* fill columns */
44#define STYLE_LONG (2U<<21) /* one record per line, extended info */
45#define STYLE_SINGLE (3U<<21) /* one record per line */
46
47#define STYLE_MASK STYLE_SINGLE
48#define STYLE_ONE_RECORD_FLAG STYLE_LONG
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Eric Andersen11c65522000-09-07 17:24:47 +000050/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
51/* what file information will be listed */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000052#define LIST_INO (1U<<0)
53#define LIST_BLOCKS (1U<<1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000054#define LIST_MODEBITS (1U<<2)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000055#define LIST_NLINKS (1U<<3)
Manuel Novoa III cad53642003-03-19 09:13:01 +000056#define LIST_ID_NAME (1U<<4)
57#define LIST_ID_NUMERIC (1U<<5)
Eric Andersen9e480452003-07-03 10:07:04 +000058#define LIST_CONTEXT (1U<<6)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000059#define LIST_SIZE (1U<<7)
60#define LIST_DEV (1U<<8)
Eric Andersen9e480452003-07-03 10:07:04 +000061#define LIST_DATE_TIME (1U<<9)
62#define LIST_FULLTIME (1U<<10)
63#define LIST_FILENAME (1U<<11)
64#define LIST_SYMLINK (1U<<12)
65#define LIST_FILETYPE (1U<<13)
66#define LIST_EXEC (1U<<14)
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
68#define LIST_MASK ((LIST_EXEC << 1) - 1)
Eric Andersen11c65522000-09-07 17:24:47 +000069
70/* what files will be displayed */
Manuel Novoa III cad53642003-03-19 09:13:01 +000071#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000072#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
73#define DISP_DOT (1U<<17) /* show . and .. */
74#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
Manuel Novoa III cad53642003-03-19 09:13:01 +000075#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000076#define DISP_ROWS (1U<<20) /* print across rows */
Manuel Novoa III cad53642003-03-19 09:13:01 +000077
Rob Landley9947a242006-06-15 22:11:10 +000078#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1))
Eric Andersen11c65522000-09-07 17:24:47 +000079
Rob Landleyea224be2006-06-18 20:20:07 +000080// CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +000081/* how will the files be sorted */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000082#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
Manuel Novoa III cad53642003-03-19 09:13:01 +000083#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
84
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000085#define SORT_NAME 0 /* sort by file name */
Manuel Novoa III cad53642003-03-19 09:13:01 +000086#define SORT_SIZE (1U<<28) /* sort by file size */
87#define SORT_ATIME (2U<<28) /* sort by last access time */
88#define SORT_CTIME (3U<<28) /* sort by last change time */
89#define SORT_MTIME (4U<<28) /* sort by last modification time */
90#define SORT_VERSION (5U<<28) /* sort by version */
91#define SORT_EXT (6U<<28) /* sort by file name extension */
92#define SORT_DIR (7U<<28) /* sort by file or directory */
93
94#define SORT_MASK (7U<<28)
Eric Andersencc8ed391999-10-05 16:24:54 +000095
Eric Andersen11c65522000-09-07 17:24:47 +000096/* which of the three times will be used */
Rob Landley2b8a05a2006-06-20 17:43:01 +000097#define TIME_CHANGE ((1U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
98#define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS)
99#define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100
Denis Vlasenko5c759602006-10-28 12:37:16 +0000101#if ENABLE_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102#define FOLLOW_LINKS (1U<<25)
103#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000104#if ENABLE_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105#define LS_DISP_HR (1U<<26)
Eric Andersencc8ed391999-10-05 16:24:54 +0000106#endif
107
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000108#define LIST_SHORT (LIST_FILENAME)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000109//#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000110#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
111 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000112//#define LIST_ILONG (LIST_INO | LIST_LONG)
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114#define SPLIT_DIR 1
115#define SPLIT_FILE 0
116#define SPLIT_SUBDIR 2
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Eric Andersen11c65522000-09-07 17:24:47 +0000118#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
119#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000120
Eric Andersend598d412002-04-27 09:19:39 +0000121#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000122# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
Eric Andersen11c65522000-09-07 17:24:47 +0000123#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000124
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000125/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000126#if ENABLE_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000127
Denis Vlasenko5c759602006-10-28 12:37:16 +0000128static int show_color;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000129
Paul Fox156dc412005-08-01 19:33:30 +0000130/* long option entry used only for --color, which has no short option
131 * equivalent. */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000132static const struct option ls_color_opt[] = {
133 { "color", optional_argument, NULL, 1 },
134 { NULL, 0, NULL, 0 }
Paul Fox156dc412005-08-01 19:33:30 +0000135};
136
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000137#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000138 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000139#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000140 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Denis Vlasenko5c759602006-10-28 12:37:16 +0000141#else
142enum { show_color = 0 };
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000143#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144
Eric Andersen11c65522000-09-07 17:24:47 +0000145/*
146 * a directory entry and its stat info are stored here
147 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000148struct dnode { /* the basic node */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000149 char *name; /* the dir entry name */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000150 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000151 int allocated;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000152 struct stat dstat; /* the file stat info */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000153 USE_SELINUX(security_context_t sid;)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000154 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000155};
156typedef struct dnode dnode_t;
157
Glenn L McGrath4d001292003-01-06 01:11:50 +0000158static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000159static struct dnode **dnalloc(int);
160static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000161
Denis Vlasenko5c759602006-10-28 12:37:16 +0000162static unsigned all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000163
Denis Vlasenko5c759602006-10-28 12:37:16 +0000164#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko13858992006-10-08 12:49:22 +0000165static unsigned tabstops = COLUMN_GAP;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000166static unsigned terminal_width = TERMINAL_WIDTH;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000167#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000168enum {
169 tabstops = COLUMN_GAP,
170 terminal_width = TERMINAL_WIDTH,
171};
Eric Andersen11c65522000-09-07 17:24:47 +0000172#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000173
Matt Kraai33fdae52000-10-13 17:59:43 +0000174static int status = EXIT_SUCCESS;
175
Glenn L McGrath4d001292003-01-06 01:11:50 +0000176static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000177{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000178 struct stat dstat;
179 struct dnode *cur;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000180 USE_SELINUX(security_context_t sid = NULL;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000181
Denis Vlasenko5c759602006-10-28 12:37:16 +0000182 if (ENABLE_FEATURE_LS_FOLLOWLINKS && (all_fmt & FOLLOW_LINKS)) {
183#if ENABLE_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000184 if (is_selinux_enabled()) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000185 getfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000186 }
Eric Andersen9e480452003-07-03 10:07:04 +0000187#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000188 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000190 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000191 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000192 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000193 } else {
194#if ENABLE_SELINUX
195 if (is_selinux_enabled()) {
196 lgetfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000197 }
Eric Andersen9e480452003-07-03 10:07:04 +0000198#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000199 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000200 bb_perror_msg("%s", fullname);
201 status = EXIT_FAILURE;
202 return 0;
203 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000204 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000205
Denis Vlasenko5c759602006-10-28 12:37:16 +0000206 cur = xmalloc(sizeof(struct dnode));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000207 cur->fullname = fullname;
208 cur->name = name;
209 cur->dstat = dstat;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000210 USE_SELINUX(cur->sid = sid;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000211 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000212}
213
Eric Andersen11c65522000-09-07 17:24:47 +0000214/*----------------------------------------------------------------------*/
Denis Vlasenko5c759602006-10-28 12:37:16 +0000215#if ENABLE_FEATURE_LS_COLOR
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000216static char fgcolor(mode_t mode)
217{
218 /* Check wheter the file is existing (if so, color it red!) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000219 if (errno == ENOENT)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000220 return '\037';
Rob Landley9947a242006-06-15 22:11:10 +0000221 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000222 return COLOR(0xF000); /* File is executable ... */
223 return COLOR(mode);
224}
225
226/*----------------------------------------------------------------------*/
227static char bgcolor(mode_t mode)
228{
Rob Landley9947a242006-06-15 22:11:10 +0000229 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000230 return ATTR(0xF000); /* File is executable ... */
231 return ATTR(mode);
232}
233#endif
234
235/*----------------------------------------------------------------------*/
Denis Vlasenko5c759602006-10-28 12:37:16 +0000236#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000237static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000238{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000239 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000240 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000241 if (S_ISDIR(mode))
242 return '/';
243 if (!(all_fmt & LIST_EXEC))
244 return '\0';
245 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000246 return '*';
247 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000248}
249#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000250
Eric Andersen11c65522000-09-07 17:24:47 +0000251/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000252
Denis Vlasenko5c759602006-10-28 12:37:16 +0000253#define countdirs(A, B) count_dirs((A), (B), 1)
254#define countsubdirs(A, B) count_dirs((A), (B), 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255
256static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000257{
258 int i, dirs;
259
Denis Vlasenko5c759602006-10-28 12:37:16 +0000260 if (!dn)
261 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000262 dirs = 0;
263 for (i = 0; i < nfiles; i++) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000264 char *name;
265 if (!S_ISDIR(dn[i]->dstat.st_mode))
266 continue;
267 name = dn[i]->name;
268 if (notsubdirs
269 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
270 ) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000271 dirs++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000272 }
Eric Andersen11c65522000-09-07 17:24:47 +0000273 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000274 return dirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000275}
276
Eric Andersen3e6ff902001-03-09 21:24:12 +0000277static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000278{
279 int nfiles;
280 struct dnode *cur;
281
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000282 if (dnp == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000283 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000284 nfiles = 0;
285 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
286 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000287 nfiles++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000288 return nfiles;
Eric Andersen11c65522000-09-07 17:24:47 +0000289}
290
291/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000292static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000293{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000294 if (num < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000295 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000296
Denis Vlasenko5c759602006-10-28 12:37:16 +0000297 return xzalloc(num * sizeof(struct dnode *));
Eric Andersen11c65522000-09-07 17:24:47 +0000298}
299
Denis Vlasenko5c759602006-10-28 12:37:16 +0000300#if ENABLE_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000301static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000302{
Rob Landley26314862006-05-02 19:46:52 +0000303 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000304
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000305 if (dnp == NULL)
306 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000307
Rob Landley26314862006-05-02 19:46:52 +0000308 for (i = 0; i < nfiles; i++) {
309 struct dnode *cur = dnp[i];
Denis Vlasenko5c759602006-10-28 12:37:16 +0000310 if (cur->allocated)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000311 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000312 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000313 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000314 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000315}
Rob Landleyc44bc982006-05-28 01:19:06 +0000316#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000317#define dfree(...) do {} while(0)
Eric Andersen20aab262001-07-19 22:28:02 +0000318#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000319
Eric Andersen3e6ff902001-03-09 21:24:12 +0000320static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000321{
322 int dncnt, i, d;
323 struct dnode **dnp;
324
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000325 if (dn == NULL || nfiles < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000326 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000327
328 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000329 if (which == SPLIT_SUBDIR)
330 dncnt = countsubdirs(dn, nfiles);
331 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000332 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000333 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000334 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000335 }
Eric Andersen11c65522000-09-07 17:24:47 +0000336
337 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000338 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000339
340 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000341 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000342 if (S_ISDIR(dn[i]->dstat.st_mode)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000343 char *name;
344 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
345 continue;
346 name = dn[i]->name;
347 if ((which & SPLIT_DIR)
348 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
349 ) {
350 dnp[d++] = dn[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000351 }
352 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
353 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000354 }
355 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000356 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000357}
358
359/*----------------------------------------------------------------------*/
Denis Vlasenko5c759602006-10-28 12:37:16 +0000360#if ENABLE_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000361static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000362{
Rob Landley425e7582006-05-03 20:22:03 +0000363 struct dnode *d1 = *(struct dnode **)a;
364 struct dnode *d2 = *(struct dnode **)b;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000365 unsigned sort_opts = all_fmt & SORT_MASK;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000366 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000367
Manuel Novoa III cad53642003-03-19 09:13:01 +0000368 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000369 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000370 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000371 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000372 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000373 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000374 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000375 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000376 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000377 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000378 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000379 /* } else if (sort_opts == SORT_VERSION) { */
380 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000381 }
382
Eric Andersen11c65522000-09-07 17:24:47 +0000383 if (dif == 0) {
384 /* sort by name- may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000385 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
386 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000387 }
388
Manuel Novoa III cad53642003-03-19 09:13:01 +0000389 if (all_fmt & SORT_ORDER_REVERSE) {
390 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000391 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000392 return dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000393}
394
395/*----------------------------------------------------------------------*/
Rob Landley425e7582006-05-03 20:22:03 +0000396static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000397{
Rob Landley425e7582006-05-03 20:22:03 +0000398 qsort(dn, size, sizeof *dn, sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000399}
Rob Landleyea224be2006-06-18 20:20:07 +0000400#else
401#define sortcmp(a, b) 0
Denis Vlasenko5c759602006-10-28 12:37:16 +0000402#define dnsort(dn, size) do {} while(0)
Eric Andersen11c65522000-09-07 17:24:47 +0000403#endif
404
Rob Landleyea224be2006-06-18 20:20:07 +0000405
Eric Andersen11c65522000-09-07 17:24:47 +0000406/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000407static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000408{
409 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000410 int column = 0;
411 int nexttab = 0;
412 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000413
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000414 if (dn == NULL || nfiles < 1)
415 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000416
Manuel Novoa III cad53642003-03-19 09:13:01 +0000417 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000418 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000419 } else {
420 /* find the longest file name- use that as the column width */
421 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000422 int len = strlen(dn[i]->name) +
Denis Vlasenko5c759602006-10-28 12:37:16 +0000423#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000424 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
425#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000426 ((all_fmt & LIST_INO) ? 8 : 0) +
427 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
428 if (column_width < len)
429 column_width = len;
430 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000431 column_width += tabstops;
432 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000433 }
434
Eric Andersene57d54b2001-01-30 18:03:11 +0000435 if (ncols > 1) {
436 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000437 if ((nrows * ncols) < nfiles)
438 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000439 } else {
440 nrows = nfiles;
441 ncols = 1;
442 }
Eric Andersen11c65522000-09-07 17:24:47 +0000443
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000444 for (row = 0; row < nrows; row++) {
445 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000446 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000447 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000448 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000449 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000450 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000451 if (column > 0) {
452 nexttab -= column;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000453 printf("%*s", nexttab, "");
454 column += nexttab;
455 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000456 nexttab = column + column_width;
457 column += list_single(dn[i]);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000458 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000459 }
460 putchar('\n');
461 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000462 }
Eric Andersen11c65522000-09-07 17:24:47 +0000463}
464
465/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000466static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000467{
468 int i, nfiles;
469 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000470 int dndirs;
471 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000472
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000473 if (dn == NULL || ndirs < 1)
474 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000475
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000476 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000477 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000478 if (!first)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000479 puts("");
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000480 first = 0;
481 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000482 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000483 subdnp = list_dir(dn[i]->fullname);
484 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000485 if (nfiles > 0) {
486 /* list all files at this level */
Rob Landleyea224be2006-06-18 20:20:07 +0000487 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000488 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000489 if (ENABLE_FEATURE_LS_RECURSIVE) {
490 if (all_fmt & DISP_RECURSIVE) {
491 /* recursive- list the sub-dirs */
492 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
493 dndirs = countsubdirs(subdnp, nfiles);
494 if (dndirs > 0) {
495 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
496 showdirs(dnd, dndirs, 0);
497 /* free the array of dnode pointers to the dirs */
498 free(dnd);
499 }
Eric Andersen11c65522000-09-07 17:24:47 +0000500 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000501 /* free the dnodes and the fullname mem */
502 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000503 }
Eric Andersen11c65522000-09-07 17:24:47 +0000504 }
505 }
506}
507
508/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000509static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000510{
511 struct dnode *dn, *cur, **dnp;
512 struct dirent *entry;
513 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000514 int i, nfiles;
515
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000516 if (path == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000517 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000518
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000519 dn = NULL;
520 nfiles = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000521 dir = warn_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000522 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000523 status = EXIT_FAILURE;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000524 return NULL; /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000525 }
526 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000527 char *fullname;
528
Eric Andersen11c65522000-09-07 17:24:47 +0000529 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000530 if (entry->d_name[0] == '.') {
531 if ((entry->d_name[1] == 0 || (
532 entry->d_name[1] == '.'
533 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000534 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000535 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000536 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000537 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000538 }
539 fullname = concat_path_file(path, entry->d_name);
540 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000541 if (!cur) {
542 // FIXME: free(fullname); ?
Eric Andersen11c65522000-09-07 17:24:47 +0000543 continue;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000544 }
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000545 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000546 cur->next = dn;
547 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000548 nfiles++;
549 }
550 closedir(dir);
551
552 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000553 ** allocate memory for an array to hold dnode pointers
554 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000555 if (dn == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000556 return NULL;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000557 dnp = dnalloc(nfiles);
558 for (i = 0, cur = dn; i < nfiles; i++) {
559 dnp[i] = cur; /* save pointer to node in array */
560 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000561 }
562
Denis Vlasenko5c759602006-10-28 12:37:16 +0000563 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000564}
565
566/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000567static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000568{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000569 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000570
Denis Vlasenko5c759602006-10-28 12:37:16 +0000571#if ENABLE_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000572 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000573#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000574#if ENABLE_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000575 char *filetime;
576 time_t ttime, age;
577#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000578#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000579 struct stat info;
580 char append;
581#endif
582
Glenn L McGrath4d001292003-01-06 01:11:50 +0000583 if (dn->fullname == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000584 return 0;
Eric Andersen11c65522000-09-07 17:24:47 +0000585
Denis Vlasenko5c759602006-10-28 12:37:16 +0000586#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000587 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000588 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000589 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000590 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000591 ttime = dn->dstat.st_ctime;
592 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000593#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000594#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000595 append = append_char(dn->dstat.st_mode);
596#endif
597
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000598 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000599 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000600 case LIST_INO:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000601 column += printf("%7ld ", (long) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000602 break;
603 case LIST_BLOCKS:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000604 column += printf("%4"OFF_FMT" ", (off_t) dn->dstat.st_blocks >> 1);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000605 break;
606 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000607 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000608 break;
609 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000610 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000611 break;
612 case LIST_ID_NAME:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000613#if ENABLE_FEATURE_LS_USERNAME
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000614 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000615 printf("%-8.8s ", scratch);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000616 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000617 printf("%-8.8s", scratch);
618 column += 17;
619 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000620#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000621 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000622 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000623 break;
624 case LIST_SIZE:
625 case LIST_DEV:
626 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000627 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
628 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000629 } else {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000630 if (ENABLE_FEATURE_HUMAN_READABLE && (all_fmt & LS_DISP_HR)) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000631 column += printf("%9s ",
Denis Vlasenko5c759602006-10-28 12:37:16 +0000632 make_human_readable_str(dn->dstat.st_size, 1, 0));
633 } else {
634 column += printf("%9"OFF_FMT" ", (off_t) dn->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000635 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000636 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000637 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000638#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000639 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000640 printf("%24.24s ", filetime);
641 column += 25;
642 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000643 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000644 if ((all_fmt & LIST_FULLTIME) == 0) {
645 age = time(NULL) - ttime;
646 printf("%6.6s ", filetime + 4);
647 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
648 /* hh:mm if less than 6 months old */
649 printf("%5.5s ", filetime + 11);
650 } else {
651 printf(" %4.4s ", filetime + 20);
652 }
653 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000654 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000655 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000656#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000657#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000658 case LIST_CONTEXT:
659 {
Rob Landley60158cb2005-05-03 06:25:50 +0000660 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000661 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000662
Rob Landley60158cb2005-05-03 06:25:50 +0000663 if (dn->sid) {
664 /* I assume sid initilized with NULL */
665 len = strlen(dn->sid)+1;
666 safe_strncpy(context, dn->sid, len);
667 freecon(dn->sid);
668 }else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000669 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000670 }
671 printf("%-32s ", context);
672 column += MAX(33, len);
673 }
674 break;
675#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000676 case LIST_FILENAME:
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000677 errno = 0;
678 if (show_color && !lstat(dn->fullname, &info)) {
679 printf("\033[%d;%dm", bgcolor(info.st_mode),
680 fgcolor(info.st_mode));
681 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000682 column += printf("%s", dn->name);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000683 if (show_color) {
684 printf("\033[0m");
685 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000686 break;
687 case LIST_SYMLINK:
688 if (S_ISLNK(dn->dstat.st_mode)) {
689 char *lpath = xreadlink(dn->fullname);
690
691 if (lpath) {
692 printf(" -> ");
693#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
694 if (!stat(dn->fullname, &info)) {
695 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000696 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000697#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000698 if (show_color) {
699 errno = 0;
700 printf("\033[%d;%dm", bgcolor(info.st_mode),
701 fgcolor(info.st_mode));
702 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000703 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000704 if (show_color) {
705 printf("\033[0m");
706 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000707 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000708 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000709 }
710 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000711#if ENABLE_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000712 case LIST_FILETYPE:
713 if (append != '\0') {
714 printf("%1c", append);
715 column++;
716 }
717 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000718#endif
719 }
720 }
721
Glenn L McGrath4d001292003-01-06 01:11:50 +0000722 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000723}
724
725/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000726
Glenn L McGrath792cae52004-01-18 05:15:16 +0000727/* "[-]Cadil1", POSIX mandated options, busybox always supports */
728/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
729/* "[-]Ak" GNU options, busybox always supports */
730/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
731/* "[-]p", POSIX non-mandated options, busybox optionally supports */
732/* "[-]SXvThw", GNU options, busybox optionally supports */
733/* "[-]K", SELinux mandated options, busybox optionally supports */
734/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000735
Denis Vlasenko5c759602006-10-28 12:37:16 +0000736static const char ls_options[] = "Cadil1gnsxAk"
737 USE_FEATURE_LS_TIMESTAMPS("cetu")
738 USE_FEATURE_LS_SORTFILES("SXrv")
739 USE_FEATURE_LS_FILETYPES("Fp")
740 USE_FEATURE_LS_FOLLOWLINKS("L")
741 USE_FEATURE_LS_RECURSIVE("R")
742 USE_FEATURE_HUMAN_READABLE("h")
743 USE_SELINUX("K")
744 USE_FEATURE_AUTOWIDTH("T:w:");
Glenn L McGrath792cae52004-01-18 05:15:16 +0000745
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000746#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000747#define STYLE_MASK_TRIGGER STYLE_MASK
748#define SORT_MASK_TRIGGER SORT_MASK
749#define DISP_MASK_TRIGGER DISP_ROWS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750
751static const unsigned opt_flags[] = {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000752 LIST_SHORT | STYLE_COLUMNS, /* C */
753 DISP_HIDDEN | DISP_DOT, /* a */
754 DISP_NOLIST, /* d */
755 LIST_INO, /* i */
756 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
757 LIST_SHORT | STYLE_SINGLE, /* 1 */
758 0, /* g - ingored */
759 LIST_ID_NUMERIC, /* n */
760 LIST_BLOCKS, /* s */
761 DISP_ROWS, /* x */
762 DISP_HIDDEN, /* A */
763#if ENABLE_SELINUX
764 LIST_CONTEXT, /* k */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000765#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000766 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000767#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000768#if ENABLE_FEATURE_LS_TIMESTAMPS
769 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
770 LIST_FULLTIME, /* e */
771 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
772 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000773#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000774#if ENABLE_FEATURE_LS_SORTFILES
775 SORT_SIZE, /* S */
776 SORT_EXT, /* X */
777 SORT_ORDER_REVERSE, /* r */
778 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000779#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000780#if ENABLE_FEATURE_LS_FILETYPES
781 LIST_FILETYPE | LIST_EXEC, /* F */
782 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000783#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000784#if ENABLE_FEATURE_LS_FOLLOWLINKS
785 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000786#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000787#if ENABLE_FEATURE_LS_RECURSIVE
788 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000789#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000790#if ENABLE_FEATURE_HUMAN_READABLE
791 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000792#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000793#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000794 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
795#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000796#if ENABLE_FEATURE_AUTOWIDTH
797 0, 0, /* T, w - ignored */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000798#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000799 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000800};
801
802
803/*----------------------------------------------------------------------*/
804
Rob Landleydfba7412006-03-06 20:47:33 +0000805int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000806{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000807 struct dnode **dnd;
808 struct dnode **dnf;
809 struct dnode **dnp;
810 struct dnode *dn;
811 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000812 unsigned opt;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000813 int nfiles = 0;
814 int dnfiles;
815 int dndirs;
816 int oi;
817 int ac;
818 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000819 char **av;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000820#if ENABLE_FEATURE_AUTOWIDTH
Glenn L McGrath792cae52004-01-18 05:15:16 +0000821 char *tabstops_str = NULL;
822 char *terminal_width_str = NULL;
823#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000824#if ENABLE_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000825 char *color_opt;
826#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000827
Rob Landley2b8a05a2006-06-20 17:43:01 +0000828 all_fmt = LIST_SHORT |
Rob Landleyea224be2006-06-18 20:20:07 +0000829 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000830
Denis Vlasenko5c759602006-10-28 12:37:16 +0000831#if ENABLE_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000832 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000833 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000834 /* Go one less... */
835 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000836#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000837
Denis Vlasenko5c759602006-10-28 12:37:16 +0000838#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000839 applet_long_options = ls_color_opt;
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000840#endif
841
Eric Andersen11c65522000-09-07 17:24:47 +0000842 /* process options */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000843#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000844 opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
Denis Vlasenko5c759602006-10-28 12:37:16 +0000845#if ENABLE_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000846 , &color_opt
847#endif
848 );
Denis Vlasenko5c759602006-10-28 12:37:16 +0000849 if (tabstops_str)
Denis Vlasenko13858992006-10-08 12:49:22 +0000850 tabstops = xatou(tabstops_str);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000851 if (terminal_width_str)
Denis Vlasenko13858992006-10-08 12:49:22 +0000852 terminal_width = xatou(terminal_width_str);
Glenn L McGrath792cae52004-01-18 05:15:16 +0000853#else
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000854 opt = getopt32(argc, argv, ls_options
Denis Vlasenko5c759602006-10-28 12:37:16 +0000855#if ENABLE_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000856 , &color_opt
857#endif
858 );
Manuel Novoa III cad53642003-03-19 09:13:01 +0000859#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000860 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000861 if (opt & (1 << i)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000862 unsigned flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000863
Denis Vlasenko5c759602006-10-28 12:37:16 +0000864 if (flags & LIST_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000865 all_fmt &= ~LIST_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000866 if (flags & STYLE_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000867 all_fmt &= ~STYLE_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000868 if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000869 all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000870 if (flags & DISP_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000871 all_fmt &= ~DISP_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000872 if (flags & TIME_MASK)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000873 all_fmt &= ~TIME_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000874 if (flags & LIST_CONTEXT)
Eric Andersen9e480452003-07-03 10:07:04 +0000875 all_fmt |= STYLE_SINGLE;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000876 if (ENABLE_FEATURE_HUMAN_READABLE && opt == 'l')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000877 all_fmt &= ~LS_DISP_HR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000878 all_fmt |= flags;
879 }
Eric Andersen11c65522000-09-07 17:24:47 +0000880 }
881
Denis Vlasenko5c759602006-10-28 12:37:16 +0000882#if ENABLE_FEATURE_LS_COLOR
883 /* find color bit value - last position for short getopt */
884 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
885 char *p = getenv("LS_COLORS");
886 /* LS_COLORS is unset, or (not empty && not "none") ? */
887 if (!p || (p[0] && strcmp(p, "none")))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000888 show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000889 }
890 if (opt & (1 << i)) { /* next flag after short options */
891 if (!color_opt || !strcmp("always", color_opt))
892 show_color = 1;
893 else if (color_opt && !strcmp("never", color_opt))
894 show_color = 0;
895 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
896 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000897 }
898#endif
899
Eric Andersen11c65522000-09-07 17:24:47 +0000900 /* sort out which command line options take precedence */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000901 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000902 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +0000903 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
904 if (all_fmt & TIME_CHANGE)
905 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
906 if (all_fmt & TIME_ACCESS)
907 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
908 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000909 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
910 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000911 if (ENABLE_FEATURE_LS_USERNAME)
912 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
913 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000914
Eric Andersen11c65522000-09-07 17:24:47 +0000915 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +0000916 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +0000917 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +0000918
919 /*
920 * when there are no cmd line args we have to supply a default "." arg.
921 * we will create a second argv array, "av" that will hold either
922 * our created "." arg, or the real cmd line args. The av array
923 * just holds the pointers- we don't move the date the pointers
924 * point to.
925 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000926 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +0000927 if (ac < 1) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000928 static const char *const dotdir[] = { "." };
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000929
930 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000931 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +0000932 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000933 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +0000934 }
935
936 /* now, everything is in the av array */
937 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000938 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +0000939
Denis Vlasenko5c759602006-10-28 12:37:16 +0000940 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000941 dn = NULL;
942 for (oi = 0; oi < ac; oi++) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000943 cur = my_stat(av[oi], av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000944 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000945 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000946 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000947 cur->next = dn;
948 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000949 nfiles++;
950 }
951
952 /* now that we know how many files there are
Denis Vlasenko5c759602006-10-28 12:37:16 +0000953 ** allocate memory for an array to hold dnode pointers
954 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000955 dnp = dnalloc(nfiles);
956 for (i = 0, cur = dn; i < nfiles; i++) {
957 dnp[i] = cur; /* save pointer to node in array */
958 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000959 }
960
Manuel Novoa III cad53642003-03-19 09:13:01 +0000961 if (all_fmt & DISP_NOLIST) {
Rob Landleyea224be2006-06-18 20:20:07 +0000962 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000963 if (nfiles > 0)
964 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000965 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000966 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
967 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
968 dndirs = countdirs(dnp, nfiles);
969 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000970 if (dnfiles > 0) {
Rob Landleyea224be2006-06-18 20:20:07 +0000971 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000972 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +0000973 if (ENABLE_FEATURE_CLEAN_UP)
974 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +0000975 }
976 if (dndirs > 0) {
Rob Landleyea224be2006-06-18 20:20:07 +0000977 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000978 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +0000979 if (ENABLE_FEATURE_CLEAN_UP)
980 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +0000981 }
982 }
Rob Landley26314862006-05-02 19:46:52 +0000983 if (ENABLE_FEATURE_CLEAN_UP)
984 dfree(dnp, nfiles);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000985 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000986}