blob: 4e21454cef9af6d5e4dd91b38b2843c85d20a968 [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 *
Eric Andersencc8ed391999-10-05 16:24:54 +00006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * To achieve a small memory footprint, this version of 'ls' doesn't do any
23 * file sorting, and only has the most essential command line switches
Eric Andersen77d92682001-05-23 20:32:09 +000024 * (i.e., the ones I couldn't live without :-) All features which involve
Eric Andersencc8ed391999-10-05 16:24:54 +000025 * linking in substantial chunks of libc can be disabled.
26 *
27 * Although I don't really want to add new features to this program to
28 * keep it small, I *am* interested to receive bug fixes and ways to make
29 * it more portable.
30 *
31 * KNOWN BUGS:
Erik Andersen9ffdaa62000-02-11 21:55:04 +000032 * 1. ls -l of a directory doesn't give "total <blocks>" header
33 * 2. ls of a symlink to a directory doesn't list directory contents
34 * 3. hidden files can make column width too large
35 *
Eric Andersencc8ed391999-10-05 16:24:54 +000036 * NON-OPTIMAL BEHAVIOUR:
37 * 1. autowidth reads directories twice
38 * 2. if you do a short directory listing without filetype characters
39 * appended, there's no need to stat each one
40 * PORTABILITY:
41 * 1. requires lstat (BSD) - how do you do it without?
42 */
43
Eric Andersene57d54b2001-01-30 18:03:11 +000044enum {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +000045 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +000046 COLUMN_GAP = 2, /* includes the file type char */
Eric Andersene57d54b2001-01-30 18:03:11 +000047};
48
Eric Andersencc8ed391999-10-05 16:24:54 +000049/************************************************************************/
50
Eric Andersen11c65522000-09-07 17:24:47 +000051#include <sys/types.h>
52#include <sys/stat.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000053#include <stdio.h>
54#include <unistd.h>
55#include <dirent.h>
56#include <errno.h>
57#include <stdio.h>
Eric Andersen11c65522000-09-07 17:24:47 +000058#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000059#include <stdlib.h>
Eric Andersen5307eca2001-01-26 01:52:43 +000060#include <fcntl.h>
61#include <signal.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000062#include <termios.h>
Eric Andersen5307eca2001-01-26 01:52:43 +000063#include <sys/ioctl.h>
Eric Andersen4f807a82004-07-26 09:11:12 +000064#include <sys/sysmacros.h> /* major() and minor() */
Eric Andersencbe31da2001-02-20 06:14:08 +000065#include "busybox.h"
Eric Andersen9e480452003-07-03 10:07:04 +000066#ifdef CONFIG_SELINUX
67#include <fs_secure.h>
68#include <flask_util.h>
69#include <ss.h>
70#endif
Eric Andersen5307eca2001-01-26 01:52:43 +000071
Eric Andersenbdfd0d72001-10-24 05:00:29 +000072#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersenf1142c52001-02-20 06:16:29 +000073#include <time.h>
74#endif
75
Eric Andersen11c65522000-09-07 17:24:47 +000076/* what is the overall style of the listing */
Manuel Novoa III cad53642003-03-19 09:13:01 +000077#define STYLE_AUTO (0)
78#define STYLE_COLUMNS (1U<<21) /* fill columns */
79#define STYLE_LONG (2U<<21) /* one record per line, extended info */
80#define STYLE_SINGLE (3U<<21) /* one record per line */
81
82#define STYLE_MASK STYLE_SINGLE
83#define STYLE_ONE_RECORD_FLAG STYLE_LONG
Eric Andersencc8ed391999-10-05 16:24:54 +000084
Eric Andersen11c65522000-09-07 17:24:47 +000085/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
86/* what file information will be listed */
Manuel Novoa III cad53642003-03-19 09:13:01 +000087#define LIST_INO (1U<<0)
88#define LIST_BLOCKS (1U<<1)
89#define LIST_MODEBITS (1U<<2)
90#define LIST_NLINKS (1U<<3)
91#define LIST_ID_NAME (1U<<4)
92#define LIST_ID_NUMERIC (1U<<5)
Eric Andersen9e480452003-07-03 10:07:04 +000093#define LIST_CONTEXT (1U<<6)
94#define LIST_SIZE (1U<<7)
95#define LIST_DEV (1U<<8)
96#define LIST_DATE_TIME (1U<<9)
97#define LIST_FULLTIME (1U<<10)
98#define LIST_FILENAME (1U<<11)
99#define LIST_SYMLINK (1U<<12)
100#define LIST_FILETYPE (1U<<13)
101#define LIST_EXEC (1U<<14)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102
103#define LIST_MASK ((LIST_EXEC << 1) - 1)
Eric Andersen11c65522000-09-07 17:24:47 +0000104
105/* what files will be displayed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106/* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */
107#define DISP_NORMAL (1U<<14) /* show normal filenames */
108#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
109#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
110#define DISP_DOT (1U<<17) /* show . and .. */
111#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
112#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
113#define DISP_ROWS (1U<<20) /* print across rows */
114
115#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1))
Eric Andersen11c65522000-09-07 17:24:47 +0000116
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000117#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000118/* how will the files be sorted */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
120#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
121
122#define SORT_NAME 0 /* sort by file name */
123#define SORT_SIZE (1U<<28) /* sort by file size */
124#define SORT_ATIME (2U<<28) /* sort by last access time */
125#define SORT_CTIME (3U<<28) /* sort by last change time */
126#define SORT_MTIME (4U<<28) /* sort by last modification time */
127#define SORT_VERSION (5U<<28) /* sort by version */
128#define SORT_EXT (6U<<28) /* sort by file name extension */
129#define SORT_DIR (7U<<28) /* sort by file or directory */
130
131#define SORT_MASK (7U<<28)
Eric Andersencc8ed391999-10-05 16:24:54 +0000132#endif
133
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000134#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000135/* which of the three times will be used */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136#define TIME_MOD 0
137#define TIME_CHANGE (1U<<23)
138#define TIME_ACCESS (1U<<24)
139
140#define TIME_MASK (3U<<23)
141#endif
142
143#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
144#define FOLLOW_LINKS (1U<<25)
145#endif
146#ifdef CONFIG_FEATURE_HUMAN_READABLE
147#define LS_DISP_HR (1U<<26)
Eric Andersencc8ed391999-10-05 16:24:54 +0000148#endif
149
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000150#define LIST_SHORT (LIST_FILENAME)
151#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
152#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
153 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
154#define LIST_ILONG (LIST_INO | LIST_LONG)
Eric Andersencc8ed391999-10-05 16:24:54 +0000155
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156#define SPLIT_DIR 1
157#define SPLIT_FILE 0
158#define SPLIT_SUBDIR 2
Eric Andersencc8ed391999-10-05 16:24:54 +0000159
Eric Andersen11c65522000-09-07 17:24:47 +0000160#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
161#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000162
Eric Andersend598d412002-04-27 09:19:39 +0000163#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000164# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
Eric Andersen11c65522000-09-07 17:24:47 +0000165#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000166
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000167/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
168#ifdef CONFIG_FEATURE_LS_COLOR
169static int show_color = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000170
171#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
172 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
173#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
174 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000175#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176
Eric Andersen11c65522000-09-07 17:24:47 +0000177/*
178 * a directory entry and its stat info are stored here
179 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000180struct dnode { /* the basic node */
181 char *name; /* the dir entry name */
182 char *fullname; /* the dir entry name */
183 struct stat dstat; /* the file stat info */
Eric Andersen9e480452003-07-03 10:07:04 +0000184#ifdef CONFIG_SELINUX
185 security_id_t sid;
186#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000187 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000188};
189typedef struct dnode dnode_t;
190
Glenn L McGrath4d001292003-01-06 01:11:50 +0000191static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000192static struct dnode **dnalloc(int);
193static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000194
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195static unsigned int all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000196
Eric Andersen9e480452003-07-03 10:07:04 +0000197#ifdef CONFIG_SELINUX
198static int is_flask_enabled_flag;
199#endif
200
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000201#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000202static int terminal_width = TERMINAL_WIDTH;
Eric Andersene57d54b2001-01-30 18:03:11 +0000203static unsigned short tabstops = COLUMN_GAP;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000204#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000205#define tabstops COLUMN_GAP
206#define terminal_width TERMINAL_WIDTH
Eric Andersen11c65522000-09-07 17:24:47 +0000207#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000208
Matt Kraai33fdae52000-10-13 17:59:43 +0000209static int status = EXIT_SUCCESS;
210
Glenn L McGrath4d001292003-01-06 01:11:50 +0000211static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000212{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000213 struct stat dstat;
214 struct dnode *cur;
Eric Andersen9e480452003-07-03 10:07:04 +0000215#ifdef CONFIG_SELINUX
216 security_id_t sid;
217#endif
218 int rc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000219
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000220#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 if (all_fmt & FOLLOW_LINKS) {
Eric Andersen9e480452003-07-03 10:07:04 +0000222#ifdef CONFIG_SELINUX
223 if(is_flask_enabled_flag)
224 rc = stat_secure(fullname, &dstat, &sid);
225 else
226#endif
227 rc = stat(fullname, &dstat);
228 if(rc)
229 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000231 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000232 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000233 }
234 } else
235#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000236 {
237#ifdef CONFIG_SELINUX
238 if(is_flask_enabled_flag)
239 rc = lstat_secure(fullname, &dstat, &sid);
240 else
241#endif
242 rc = lstat(fullname, &dstat);
243 if(rc)
244 {
245 bb_perror_msg("%s", fullname);
246 status = EXIT_FAILURE;
247 return 0;
248 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000249 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000250
251 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
252 cur->fullname = fullname;
253 cur->name = name;
254 cur->dstat = dstat;
Eric Andersen9e480452003-07-03 10:07:04 +0000255#ifdef CONFIG_SELINUX
256 cur->sid = sid;
257#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000258 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000259}
260
Eric Andersen11c65522000-09-07 17:24:47 +0000261/*----------------------------------------------------------------------*/
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000262#ifdef CONFIG_FEATURE_LS_COLOR
263static char fgcolor(mode_t mode)
264{
265 /* Check wheter the file is existing (if so, color it red!) */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000266 if (errno == ENOENT) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000267 return '\037';
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000268 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000269 if (LIST_EXEC && S_ISREG(mode)
270 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000271 return COLOR(0xF000); /* File is executable ... */
272 return COLOR(mode);
273}
274
275/*----------------------------------------------------------------------*/
276static char bgcolor(mode_t mode)
277{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000278 if (LIST_EXEC && S_ISREG(mode)
279 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000280 return ATTR(0xF000); /* File is executable ... */
281 return ATTR(mode);
282}
283#endif
284
285/*----------------------------------------------------------------------*/
Eric Andersend598d412002-04-27 09:19:39 +0000286#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000287static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000288{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000289 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000290 return '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000291 if ((all_fmt & LIST_EXEC) && S_ISREG(mode)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000292 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
293 return '*';
294 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000295}
296#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000297
Eric Andersen11c65522000-09-07 17:24:47 +0000298/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000299
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300#define countdirs(A,B) count_dirs((A), (B), 1)
301#define countsubdirs(A,B) count_dirs((A), (B), 0)
302
303static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000304{
305 int i, dirs;
306
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000307 if (dn == NULL || nfiles < 1)
308 return (0);
309 dirs = 0;
310 for (i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000311 if (S_ISDIR(dn[i]->dstat.st_mode)
312 && (notsubdirs
313 || ((dn[i]->name[0] != '.')
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000314 || (dn[i]->name[1]
Manuel Novoa III cad53642003-03-19 09:13:01 +0000315 && ((dn[i]->name[1] != '.')
316 || dn[i]->name[2])))))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000317 dirs++;
Eric Andersen11c65522000-09-07 17:24:47 +0000318 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000319 return (dirs);
Eric Andersen11c65522000-09-07 17:24:47 +0000320}
321
Eric Andersen3e6ff902001-03-09 21:24:12 +0000322static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000323{
324 int nfiles;
325 struct dnode *cur;
326
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000327 if (dnp == NULL)
328 return (0);
329 nfiles = 0;
330 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
331 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000332 nfiles++;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000333 return (nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000334}
335
336/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000337static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000338{
339 struct dnode **p;
340
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000341 if (num < 1)
342 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000343
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000344 p = (struct dnode **) xcalloc((size_t) num,
345 (size_t) (sizeof(struct dnode *)));
346 return (p);
Eric Andersen11c65522000-09-07 17:24:47 +0000347}
348
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000349#ifdef CONFIG_FEATURE_LS_RECURSIVE
Eric Andersen3e6ff902001-03-09 21:24:12 +0000350static void dfree(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000351{
352 struct dnode *cur, *next;
353
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000354 if (dnp == NULL)
355 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000356
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000357 cur = dnp[0];
Eric Andersen11c65522000-09-07 17:24:47 +0000358 while (cur != NULL) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000359 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000360 next = cur->next;
361 free(cur); /* free the dnode */
362 cur = next;
Eric Andersen11c65522000-09-07 17:24:47 +0000363 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000364 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000365}
Eric Andersen20aab262001-07-19 22:28:02 +0000366#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000367
Eric Andersen3e6ff902001-03-09 21:24:12 +0000368static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000369{
370 int dncnt, i, d;
371 struct dnode **dnp;
372
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000373 if (dn == NULL || nfiles < 1)
374 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000375
376 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000377 if (which == SPLIT_SUBDIR)
378 dncnt = countsubdirs(dn, nfiles);
379 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000380 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000381 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000382 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000383 }
Eric Andersen11c65522000-09-07 17:24:47 +0000384
385 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000386 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000387
388 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000389 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000390 if (S_ISDIR(dn[i]->dstat.st_mode)) {
391 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
392 if ((which & SPLIT_DIR)
393 || ((dn[i]->name[0] != '.')
394 || (dn[i]->name[1]
395 && ((dn[i]->name[1] != '.')
396 || dn[i]->name[2])))) {
397 dnp[d++] = dn[i];
398 }
399 }
400 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
401 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000402 }
403 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000404 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000405}
406
407/*----------------------------------------------------------------------*/
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000408#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen3e6ff902001-03-09 21:24:12 +0000409static int sortcmp(struct dnode *d1, struct dnode *d2)
Eric Andersen11c65522000-09-07 17:24:47 +0000410{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000411 unsigned int sort_opts = all_fmt & SORT_MASK;
412 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000413
Manuel Novoa III cad53642003-03-19 09:13:01 +0000414 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000415 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000416 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000417 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000418 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000419 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000420 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000421 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000422 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000423 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000424 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000425 /* } else if (sort_opts == SORT_VERSION) { */
426 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000427 }
428
Eric Andersen11c65522000-09-07 17:24:47 +0000429 if (dif == 0) {
430 /* sort by name- may be a tie_breaker for time or size cmp */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000431#ifdef CONFIG_LOCALE_SUPPORT
432 dif = strcoll(d1->name, d2->name);
433#else
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000434 dif = strcmp(d1->name, d2->name);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000435#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000436 }
437
Manuel Novoa III cad53642003-03-19 09:13:01 +0000438 if (all_fmt & SORT_ORDER_REVERSE) {
439 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000440 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000441 return (dif);
Eric Andersen11c65522000-09-07 17:24:47 +0000442}
443
444/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000445static void shellsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000446{
447 struct dnode *temp;
448 int gap, i, j;
449
450 /* shell short the array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000451 if (dn == NULL || size < 2)
452 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000453
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000454 for (gap = size / 2; gap > 0; gap /= 2) {
455 for (i = gap; i < size; i++) {
456 for (j = i - gap; j >= 0; j -= gap) {
457 if (sortcmp(dn[j], dn[j + gap]) <= 0)
Eric Andersen11c65522000-09-07 17:24:47 +0000458 break;
459 /* they are out of order, swap them */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000460 temp = dn[j];
461 dn[j] = dn[j + gap];
462 dn[j + gap] = temp;
Eric Andersen11c65522000-09-07 17:24:47 +0000463 }
464 }
465 }
466}
467#endif
468
469/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000470static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000471{
472 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000473 int column = 0;
474 int nexttab = 0;
475 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000476
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000477 if (dn == NULL || nfiles < 1)
478 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000479
Manuel Novoa III cad53642003-03-19 09:13:01 +0000480 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000481 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000482 } else {
483 /* find the longest file name- use that as the column width */
484 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000485 int len = strlen(dn[i]->name) +
Eric Andersen9e480452003-07-03 10:07:04 +0000486#ifdef CONFIG_SELINUX
487 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
488#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000489 ((all_fmt & LIST_INO) ? 8 : 0) +
490 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
491 if (column_width < len)
492 column_width = len;
493 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000494 column_width += tabstops;
495 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000496 }
497
Eric Andersene57d54b2001-01-30 18:03:11 +0000498 if (ncols > 1) {
499 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000500 if ((nrows * ncols) < nfiles)
501 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000502 } else {
503 nrows = nfiles;
504 ncols = 1;
505 }
Eric Andersen11c65522000-09-07 17:24:47 +0000506
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000507 for (row = 0; row < nrows; row++) {
508 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000509 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000510 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000511 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000512 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000513 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000514 if (column > 0) {
515 nexttab -= column;
516 while (nexttab--) {
517 putchar(' ');
518 column++;
519 }
Eric Andersen79565b62000-08-11 18:10:21 +0000520 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000521 nexttab = column + column_width;
522 column += list_single(dn[i]);
Eric Andersen79565b62000-08-11 18:10:21 +0000523 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000524 }
525 putchar('\n');
526 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000527 }
Eric Andersen11c65522000-09-07 17:24:47 +0000528}
529
530/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000531static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000532{
533 int i, nfiles;
534 struct dnode **subdnp;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000535
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000536#ifdef CONFIG_FEATURE_LS_RECURSIVE
Eric Andersen11c65522000-09-07 17:24:47 +0000537 int dndirs;
538 struct dnode **dnd;
539#endif
540
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000541 if (dn == NULL || ndirs < 1)
542 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000543
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000544 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000545 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000546 if (!first)
547 printf("\n");
548 first = 0;
549 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000550 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000551 subdnp = list_dir(dn[i]->fullname);
552 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000553 if (nfiles > 0) {
554 /* list all files at this level */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000555#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000556 shellsort(subdnp, nfiles);
557#endif
558 showfiles(subdnp, nfiles);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000559#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000560 if (all_fmt & DISP_RECURSIVE) {
Eric Andersen11c65522000-09-07 17:24:47 +0000561 /* recursive- list the sub-dirs */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000562 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
563 dndirs = countsubdirs(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000564 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000565#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000566 shellsort(dnd, dndirs);
Matt Kraaia5bd2682000-10-28 06:40:09 +0000567#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000568 showdirs(dnd, dndirs, 0);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000569 free(dnd); /* free the array of dnode pointers to the dirs */
Eric Andersen11c65522000-09-07 17:24:47 +0000570 }
571 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000572 dfree(subdnp); /* free the dnodes and the fullname mem */
Eric Andersen11c65522000-09-07 17:24:47 +0000573#endif
574 }
575 }
576}
577
578/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000579static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000580{
581 struct dnode *dn, *cur, **dnp;
582 struct dirent *entry;
583 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000584 int i, nfiles;
585
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000586 if (path == NULL)
587 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000588
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000589 dn = NULL;
590 nfiles = 0;
Eric Andersene7e1e2d2000-10-12 22:40:14 +0000591 dir = opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000592 if (dir == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000593 bb_perror_msg("%s", path);
Matt Kraai33fdae52000-10-13 17:59:43 +0000594 status = EXIT_FAILURE;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000595 return (NULL); /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000596 }
597 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000598 char *fullname;
599
Eric Andersen11c65522000-09-07 17:24:47 +0000600 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000601 if (entry->d_name[0] == '.') {
602 if ((entry->d_name[1] == 0 || (
603 entry->d_name[1] == '.'
604 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000605 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000606 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000607 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000608 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000609 }
610 fullname = concat_path_file(path, entry->d_name);
611 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
612 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000613 continue;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000614 cur->next = dn;
615 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000616 nfiles++;
617 }
618 closedir(dir);
619
620 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000621 ** allocate memory for an array to hold dnode pointers
622 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000623 if (dn == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000624 return (NULL);
625 dnp = dnalloc(nfiles);
626 for (i = 0, cur = dn; i < nfiles; i++) {
627 dnp[i] = cur; /* save pointer to node in array */
628 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000629 }
630
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000631 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000632}
633
634/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000635static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000636{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000637 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000638
Matt Kraaia1bbde72002-03-08 16:25:33 +0000639#ifdef CONFIG_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000640 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000641#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000642#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000643 char *filetime;
644 time_t ttime, age;
645#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000646#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000647 struct stat info;
648 char append;
649#endif
650
Glenn L McGrath4d001292003-01-06 01:11:50 +0000651 if (dn->fullname == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000652 return (0);
Eric Andersen11c65522000-09-07 17:24:47 +0000653
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000654#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000655 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000656 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000657 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000658 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000659 ttime = dn->dstat.st_ctime;
660 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000661#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000662#ifdef CONFIG_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000663 append = append_char(dn->dstat.st_mode);
664#endif
665
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000666 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000667 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000668 case LIST_INO:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000669 column += printf("%7ld ", (long int) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000670 break;
671 case LIST_BLOCKS:
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000672#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000673 column += printf("%4lld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000674#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000675 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000676#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000677 break;
678 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000679 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000680 break;
681 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000682 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000683 break;
684 case LIST_ID_NAME:
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000685#ifdef CONFIG_FEATURE_LS_USERNAME
Eric Andersen52499cb2004-08-26 22:18:59 +0000686 my_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000687 printf("%-8.8s ", scratch);
Eric Andersen52499cb2004-08-26 22:18:59 +0000688 my_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000689 printf("%-8.8s", scratch);
690 column += 17;
691 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000692#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000693 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000694 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000695 break;
696 case LIST_SIZE:
697 case LIST_DEV:
698 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000699 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
700 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000701 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000702#ifdef CONFIG_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000703 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000704 column += printf("%9s ",
705 make_human_readable_str(dn->dstat.st_size, 1, 0));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000706 } else
707#endif
708 {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000709#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000710 column += printf("%9lld ", (long long) dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000711#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000712 column += printf("%9ld ", dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000713#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000714 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000715 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000716 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000717#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000718 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000719 printf("%24.24s ", filetime);
720 column += 25;
721 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000722 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000723 if ((all_fmt & LIST_FULLTIME) == 0) {
724 age = time(NULL) - ttime;
725 printf("%6.6s ", filetime + 4);
726 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
727 /* hh:mm if less than 6 months old */
728 printf("%5.5s ", filetime + 11);
729 } else {
730 printf(" %4.4s ", filetime + 20);
731 }
732 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000733 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000734 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000735#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000736#ifdef CONFIG_SELINUX
737 case LIST_CONTEXT:
738 {
739 char context[64];
740 int len = sizeof(context);
741 if(security_sid_to_context(dn->sid, context, &len))
742 {
743 strcpy(context, "unknown");
744 len = 7;
745 }
746 printf("%-32s ", context);
747 column += MAX(33, len);
748 }
749 break;
750#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000751 case LIST_FILENAME:
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000752#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000753 errno = 0;
754 if (show_color && !lstat(dn->fullname, &info)) {
755 printf("\033[%d;%dm", bgcolor(info.st_mode),
756 fgcolor(info.st_mode));
757 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000758#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000759 column += printf("%s", dn->name);
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000760#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000761 if (show_color) {
762 printf("\033[0m");
763 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000764#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000765 break;
766 case LIST_SYMLINK:
767 if (S_ISLNK(dn->dstat.st_mode)) {
768 char *lpath = xreadlink(dn->fullname);
769
770 if (lpath) {
771 printf(" -> ");
772#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
773 if (!stat(dn->fullname, &info)) {
774 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000775 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000776#endif
777#ifdef CONFIG_FEATURE_LS_COLOR
778 if (show_color) {
779 errno = 0;
780 printf("\033[%d;%dm", bgcolor(info.st_mode),
781 fgcolor(info.st_mode));
782 }
783#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000784 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000785#ifdef CONFIG_FEATURE_LS_COLOR
786 if (show_color) {
787 printf("\033[0m");
788 }
789#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000790 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000791 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000792 }
793 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000794#ifdef CONFIG_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000795 case LIST_FILETYPE:
796 if (append != '\0') {
797 printf("%1c", append);
798 column++;
799 }
800 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000801#endif
802 }
803 }
804
Glenn L McGrath4d001292003-01-06 01:11:50 +0000805 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000806}
807
808/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000809
Glenn L McGrath792cae52004-01-18 05:15:16 +0000810/* "[-]Cadil1", POSIX mandated options, busybox always supports */
811/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
812/* "[-]Ak" GNU options, busybox always supports */
813/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
814/* "[-]p", POSIX non-mandated options, busybox optionally supports */
815/* "[-]SXvThw", GNU options, busybox optionally supports */
816/* "[-]K", SELinux mandated options, busybox optionally supports */
817/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000818
Glenn L McGrath792cae52004-01-18 05:15:16 +0000819#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
820# define LS_STR_TIMESTAMPS "cetu"
821#else
822# define LS_STR_TIMESTAMPS ""
823#endif
824
825#ifdef CONFIG_FEATURE_LS_SORTFILES
826# define LS_STR_SORTFILES "SXrv"
827#else
828# define LS_STR_SORTFILES ""
829#endif
830
831#ifdef CONFIG_FEATURE_LS_FILETYPES
832# define LS_STR_FILETYPES "Fp"
833#else
834# define LS_STR_FILETYPES ""
835#endif
836
837#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
838# define LS_STR_FOLLOW_LINKS "L"
839#else
840# define LS_STR_FOLLOW_LINKS ""
841#endif
842
843#ifdef CONFIG_FEATURE_LS_RECURSIVE
844# define LS_STR_RECURSIVE "R"
845#else
846# define LS_STR_RECURSIVE ""
847#endif
848
849#ifdef CONFIG_FEATURE_HUMAN_READABLE
850# define LS_STR_HUMAN_READABLE "h"
851#else
852# define LS_STR_HUMAN_READABLE ""
853#endif
854
855#ifdef CONFIG_SELINUX
856# define LS_STR_SELINUX "K"
857#else
858# define LS_STR_SELINUX ""
859#endif
860
861#ifdef CONFIG_FEATURE_AUTOWIDTH
862# define LS_STR_AUTOWIDTH "T:w:"
863#else
864# define LS_STR_AUTOWIDTH ""
865#endif
866
867static const char ls_options[]="Cadil1gnsxAk" \
868 LS_STR_TIMESTAMPS \
869 LS_STR_SORTFILES \
870 LS_STR_FILETYPES \
871 LS_STR_FOLLOW_LINKS \
872 LS_STR_RECURSIVE \
873 LS_STR_HUMAN_READABLE \
874 LS_STR_SELINUX \
875 LS_STR_AUTOWIDTH;
876
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000877#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000878#define STYLE_MASK_TRIGGER STYLE_MASK
879#define SORT_MASK_TRIGGER SORT_MASK
880#define DISP_MASK_TRIGGER DISP_ROWS
881#define TIME_MASK_TRIGGER TIME_MASK
Manuel Novoa III cad53642003-03-19 09:13:01 +0000882
883static const unsigned opt_flags[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000884 LIST_SHORT | STYLE_COLUMNS, /* C */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000885 DISP_HIDDEN | DISP_DOT, /* a */
886 DISP_NOLIST, /* d */
887 LIST_INO, /* i */
888 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
889 LIST_SHORT | STYLE_SINGLE, /* 1 */
890 0, /* g - ingored */
891 LIST_ID_NUMERIC, /* n */
892 LIST_BLOCKS, /* s */
893 DISP_ROWS, /* x */
894 DISP_HIDDEN, /* A */
895#ifdef CONFIG_SELINUX
896 LIST_CONTEXT, /* k */
897#else
898 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000899#endif
900#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrath792cae52004-01-18 05:15:16 +0000901# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000902 TIME_CHANGE | SORT_CTIME, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000903# else
904 TIME_CHANGE, /* c */
905# endif
906 LIST_FULLTIME, /* e */
907# ifdef CONFIG_FEATURE_LS_SORTFILES
908 SORT_MTIME, /* t */
909# else
910 0, /* t - ignored -- is this correct? */
911# endif
912# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000913 TIME_ACCESS | SORT_ATIME, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000914# else
915 TIME_ACCESS, /* u */
916# endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000917#endif
918#ifdef CONFIG_FEATURE_LS_SORTFILES
Glenn L McGrath792cae52004-01-18 05:15:16 +0000919 SORT_SIZE, /* S */
Manuel Novoa III 1117c522004-03-08 10:54:29 +0000920 SORT_EXT, /* X */
921 SORT_ORDER_REVERSE, /* r */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000922 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000923#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000924#ifdef CONFIG_FEATURE_LS_FILETYPES
925 LIST_FILETYPE | LIST_EXEC, /* F */
926 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000927#endif
928#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Glenn L McGrath792cae52004-01-18 05:15:16 +0000929 FOLLOW_LINKS, /* L */
930#endif
931#ifdef CONFIG_FEATURE_LS_RECURSIVE
932 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000933#endif
934#ifdef CONFIG_FEATURE_HUMAN_READABLE
Glenn L McGrath792cae52004-01-18 05:15:16 +0000935 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000936#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000937#ifdef CONFIG_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000938 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
939#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000940 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000941};
942
943
944/*----------------------------------------------------------------------*/
945
Eric Andersen11c65522000-09-07 17:24:47 +0000946extern int ls_main(int argc, char **argv)
947{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000948 struct dnode **dnd;
949 struct dnode **dnf;
950 struct dnode **dnp;
951 struct dnode *dn;
952 struct dnode *cur;
953 long opt;
954 int nfiles = 0;
955 int dnfiles;
956 int dndirs;
957 int oi;
958 int ac;
959 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000960 char **av;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000961#ifdef CONFIG_FEATURE_AUTOWIDTH
962 char *tabstops_str = NULL;
963 char *terminal_width_str = NULL;
964#endif
965
Eric Andersen9e480452003-07-03 10:07:04 +0000966#ifdef CONFIG_SELINUX
967 is_flask_enabled_flag = is_flask_enabled();
968#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000969
Manuel Novoa III cad53642003-03-19 09:13:01 +0000970 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000971#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000972 | TIME_MOD
Eric Andersen11c65522000-09-07 17:24:47 +0000973#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000974#ifdef CONFIG_FEATURE_LS_SORTFILES
975 | SORT_NAME | SORT_ORDER_FORWARD
976#endif
977 ;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000978
Eric Andersen6d687812003-11-04 23:16:48 +0000979#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000980 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000981 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000982 /* Go one less... */
983 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000984#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000985
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000986#ifdef CONFIG_FEATURE_LS_COLOR
Eric Andersen70060d22004-03-27 10:02:48 +0000987 if (isatty(STDOUT_FILENO))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000988 show_color = 1;
989#endif
990
Eric Andersen11c65522000-09-07 17:24:47 +0000991 /* process options */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000992#ifdef CONFIG_FEATURE_AUTOWIDTH
Glenn L McGrath792cae52004-01-18 05:15:16 +0000993 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str);
994 if (tabstops_str) {
995 tabstops = atoi(tabstops_str);
996 }
997 if (terminal_width_str) {
998 terminal_width = atoi(terminal_width_str);
999 }
1000#else
1001 opt = bb_getopt_ulflags(argc, argv, ls_options);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001002#endif
Eric Andersend07cf592004-02-05 13:52:03 +00001003 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +00001004 if (opt & (1 << i)) {
1005 unsigned int flags = opt_flags[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +00001006 if (flags & LIST_MASK_TRIGGER) {
1007 all_fmt &= ~LIST_MASK;
1008 }
1009 if (flags & STYLE_MASK_TRIGGER) {
1010 all_fmt &= ~STYLE_MASK;
1011 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001012#ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +00001013 if (flags & SORT_MASK_TRIGGER) {
1014 all_fmt &= ~SORT_MASK;
1015 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001016#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001017 if (flags & DISP_MASK_TRIGGER) {
1018 all_fmt &= ~DISP_MASK;
1019 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001020#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +00001021 if (flags & TIME_MASK_TRIGGER) {
1022 all_fmt &= ~TIME_MASK;
1023 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001024#endif
Eric Andersen9e480452003-07-03 10:07:04 +00001025 if (flags & LIST_CONTEXT) {
1026 all_fmt |= STYLE_SINGLE;
1027 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001028#ifdef CONFIG_FEATURE_HUMAN_READABLE
1029 if (opt == 'l') {
1030 all_fmt &= ~LS_DISP_HR;
1031 }
1032#endif
1033 all_fmt |= flags;
1034 }
Eric Andersen11c65522000-09-07 17:24:47 +00001035 }
1036
1037 /* sort out which command line options take precedence */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001038#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +00001039 if (all_fmt & DISP_NOLIST)
1040 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Eric Andersen11c65522000-09-07 17:24:47 +00001041#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001042#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001043 if (all_fmt & TIME_CHANGE)
1044 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1045 if (all_fmt & TIME_ACCESS)
1046 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
Eric Andersen11c65522000-09-07 17:24:47 +00001047#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001048 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1049 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001050#ifdef CONFIG_FEATURE_LS_USERNAME
Manuel Novoa III cad53642003-03-19 09:13:01 +00001051 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1052 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersen11c65522000-09-07 17:24:47 +00001053#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001054
Eric Andersen11c65522000-09-07 17:24:47 +00001055 /* choose a display format */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001056 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
1057#if STYLE_AUTO != 0
1058 all_fmt = (all_fmt & ~STYLE_MASK)
Eric Andersen70060d22004-03-27 10:02:48 +00001059 | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001060#else
Eric Andersen70060d22004-03-27 10:02:48 +00001061 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001062#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001063
1064 /*
1065 * when there are no cmd line args we have to supply a default "." arg.
1066 * we will create a second argv array, "av" that will hold either
1067 * our created "." arg, or the real cmd line args. The av array
1068 * just holds the pointers- we don't move the date the pointers
1069 * point to.
1070 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001071 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +00001072 if (ac < 1) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001073 av = (char **) xcalloc((size_t) 1, (size_t) (sizeof(char *)));
Manuel Novoa III cad53642003-03-19 09:13:01 +00001074 av[0] = bb_xstrdup(".");
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001075 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +00001076 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001077 av = (char **) xcalloc((size_t) ac, (size_t) (sizeof(char *)));
1078 for (oi = 0; oi < ac; oi++) {
1079 av[oi] = argv[optind++]; /* copy pointer to real cmd line arg */
Eric Andersen11c65522000-09-07 17:24:47 +00001080 }
1081 }
1082
1083 /* now, everything is in the av array */
1084 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001085 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001086
1087 /* stuff the command line file names into an dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001088 dn = NULL;
1089 for (oi = 0; oi < ac; oi++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001090 char *fullname = bb_xstrdup(av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001091
1092 cur = my_stat(fullname, fullname);
1093 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001094 continue;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001095 cur->next = dn;
1096 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001097 nfiles++;
1098 }
1099
1100 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001101 ** allocate memory for an array to hold dnode pointers
1102 */
1103 dnp = dnalloc(nfiles);
1104 for (i = 0, cur = dn; i < nfiles; i++) {
1105 dnp[i] = cur; /* save pointer to node in array */
1106 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +00001107 }
1108
Manuel Novoa III cad53642003-03-19 09:13:01 +00001109 if (all_fmt & DISP_NOLIST) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001110#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +00001111 shellsort(dnp, nfiles);
1112#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001113 if (nfiles > 0)
1114 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001115 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001116 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1117 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1118 dndirs = countdirs(dnp, nfiles);
1119 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001120 if (dnfiles > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001121#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +00001122 shellsort(dnf, dnfiles);
1123#endif
1124 showfiles(dnf, dnfiles);
1125 }
1126 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001127#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +00001128 shellsort(dnd, dndirs);
1129#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +00001130 showdirs(dnd, dndirs, dnfiles == 0);
Eric Andersen11c65522000-09-07 17:24:47 +00001131 }
1132 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001133 return (status);
Eric Andersencc8ed391999-10-05 16:24:54 +00001134}