blob: 0099f18feb02f57933bcfda57ff10c90cbdb7529 [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
101#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
102#define FOLLOW_LINKS (1U<<25)
103#endif
104#ifdef CONFIG_FEATURE_HUMAN_READABLE
105#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)
109#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
110#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
111 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
112#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 */
126#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000127
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000128static int show_color = 0;
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. */
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000132static const struct option ls_color_opt[] =
Paul Fox156dc412005-08-01 19:33:30 +0000133{
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000134 {"color", optional_argument, NULL, 1},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000135 {NULL, 0, NULL, 0}
Paul Fox156dc412005-08-01 19:33:30 +0000136};
137
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000138#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000139 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000140#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000141 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000142#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143
Eric Andersen11c65522000-09-07 17:24:47 +0000144/*
145 * a directory entry and its stat info are stored here
146 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000147struct dnode { /* the basic node */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000148 char *name; /* the dir entry name */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000149 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000150 int allocated;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000151 struct stat dstat; /* the file stat info */
Eric Andersen9e480452003-07-03 10:07:04 +0000152#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000153 security_context_t sid;
Eric Andersen9e480452003-07-03 10:07:04 +0000154#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000155 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000156};
157typedef struct dnode dnode_t;
158
Glenn L McGrath4d001292003-01-06 01:11:50 +0000159static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000160static struct dnode **dnalloc(int);
161static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000162
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163static unsigned int all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000164
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000165#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000166static int terminal_width = TERMINAL_WIDTH;
Eric Andersene57d54b2001-01-30 18:03:11 +0000167static unsigned short tabstops = COLUMN_GAP;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000168#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000169#define tabstops COLUMN_GAP
170#define terminal_width TERMINAL_WIDTH
Eric Andersen11c65522000-09-07 17:24:47 +0000171#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Matt Kraai33fdae52000-10-13 17:59:43 +0000173static int status = EXIT_SUCCESS;
174
Glenn L McGrath4d001292003-01-06 01:11:50 +0000175static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000176{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000177 struct stat dstat;
178 struct dnode *cur;
Eric Andersen9e480452003-07-03 10:07:04 +0000179#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000180 security_context_t sid=NULL;
Eric Andersen9e480452003-07-03 10:07:04 +0000181#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000182
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000183#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000184 if (all_fmt & FOLLOW_LINKS) {
Eric Andersen9e480452003-07-03 10:07:04 +0000185#ifdef CONFIG_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000186 if (is_selinux_enabled()) {
187 getfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000188 }
Eric Andersen9e480452003-07-03 10:07:04 +0000189#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000190 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000192 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000193 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000194 }
195 } else
196#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000197 {
198#ifdef CONFIG_SELINUX
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000199 if (is_selinux_enabled()) {
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000200 lgetfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000201 }
Eric Andersen9e480452003-07-03 10:07:04 +0000202#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000203 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000204 bb_perror_msg("%s", fullname);
205 status = EXIT_FAILURE;
206 return 0;
207 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000208 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000209
210 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
211 cur->fullname = fullname;
212 cur->name = name;
213 cur->dstat = dstat;
Eric Andersen9e480452003-07-03 10:07:04 +0000214#ifdef CONFIG_SELINUX
215 cur->sid = sid;
216#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000217 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000218}
219
Eric Andersen11c65522000-09-07 17:24:47 +0000220/*----------------------------------------------------------------------*/
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000221#ifdef CONFIG_FEATURE_LS_COLOR
222static char fgcolor(mode_t mode)
223{
224 /* Check wheter the file is existing (if so, color it red!) */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000225 if (errno == ENOENT) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000226 return '\037';
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000227 }
Rob Landley9947a242006-06-15 22:11:10 +0000228 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000229 return COLOR(0xF000); /* File is executable ... */
230 return COLOR(mode);
231}
232
233/*----------------------------------------------------------------------*/
234static char bgcolor(mode_t mode)
235{
Rob Landley9947a242006-06-15 22:11:10 +0000236 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000237 return ATTR(0xF000); /* File is executable ... */
238 return ATTR(mode);
239}
240#endif
241
242/*----------------------------------------------------------------------*/
Eric Andersend598d412002-04-27 09:19:39 +0000243#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000244static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000245{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000246 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000247 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000248 if (S_ISDIR(mode))
249 return '/';
250 if (!(all_fmt & LIST_EXEC))
251 return '\0';
252 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000253 return '*';
254 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000255}
256#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000257
Eric Andersen11c65522000-09-07 17:24:47 +0000258/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000259
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260#define countdirs(A,B) count_dirs((A), (B), 1)
261#define countsubdirs(A,B) count_dirs((A), (B), 0)
262
263static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000264{
265 int i, dirs;
266
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000267 if (dn == NULL || nfiles < 1)
268 return (0);
269 dirs = 0;
270 for (i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000271 if (S_ISDIR(dn[i]->dstat.st_mode)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000272 && (notsubdirs ||
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000273 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
Manuel Novoa III cad53642003-03-19 09:13:01 +0000274 && ((dn[i]->name[1] != '.')
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000275 || dn[i]->name[2])))))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000276 dirs++;
Eric Andersen11c65522000-09-07 17:24:47 +0000277 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000278 return (dirs);
Eric Andersen11c65522000-09-07 17:24:47 +0000279}
280
Eric Andersen3e6ff902001-03-09 21:24:12 +0000281static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000282{
283 int nfiles;
284 struct dnode *cur;
285
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000286 if (dnp == NULL)
287 return (0);
288 nfiles = 0;
289 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
290 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000291 nfiles++;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000292 return (nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000293}
294
295/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000296static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000297{
298 struct dnode **p;
299
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000300 if (num < 1)
301 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000302
Rob Landley081e3842006-08-03 20:07:35 +0000303 p = (struct dnode **) xzalloc(num * sizeof(struct dnode *));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000304 return (p);
Eric Andersen11c65522000-09-07 17:24:47 +0000305}
306
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000307#ifdef CONFIG_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000308static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000309{
Rob Landley26314862006-05-02 19:46:52 +0000310 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000311
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000312 if (dnp == NULL)
313 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000314
Rob Landley26314862006-05-02 19:46:52 +0000315 for (i = 0; i < nfiles; i++) {
316 struct dnode *cur = dnp[i];
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000317 if(cur->allocated)
318 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000319 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000320 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000321 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000322}
Rob Landleyc44bc982006-05-28 01:19:06 +0000323#else
324#define dfree(...)
Eric Andersen20aab262001-07-19 22:28:02 +0000325#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000326
Eric Andersen3e6ff902001-03-09 21:24:12 +0000327static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000328{
329 int dncnt, i, d;
330 struct dnode **dnp;
331
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000332 if (dn == NULL || nfiles < 1)
333 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000334
335 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000336 if (which == SPLIT_SUBDIR)
337 dncnt = countsubdirs(dn, nfiles);
338 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000339 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000340 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000341 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000342 }
Eric Andersen11c65522000-09-07 17:24:47 +0000343
344 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000345 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000346
347 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000348 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000349 if (S_ISDIR(dn[i]->dstat.st_mode)) {
350 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
351 if ((which & SPLIT_DIR)
352 || ((dn[i]->name[0] != '.')
353 || (dn[i]->name[1]
354 && ((dn[i]->name[1] != '.')
355 || dn[i]->name[2])))) {
356 dnp[d++] = dn[i];
357 }
358 }
359 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
360 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000361 }
362 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000363 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000364}
365
366/*----------------------------------------------------------------------*/
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000367#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000368static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000369{
Rob Landley425e7582006-05-03 20:22:03 +0000370 struct dnode *d1 = *(struct dnode **)a;
371 struct dnode *d2 = *(struct dnode **)b;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000372 unsigned int sort_opts = all_fmt & SORT_MASK;
373 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000374
Manuel Novoa III cad53642003-03-19 09:13:01 +0000375 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000376 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000377 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000378 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000379 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000380 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000381 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000382 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000383 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000384 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000385 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000386 /* } else if (sort_opts == SORT_VERSION) { */
387 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000388 }
389
Eric Andersen11c65522000-09-07 17:24:47 +0000390 if (dif == 0) {
391 /* sort by name- may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000392 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
393 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000394 }
395
Manuel Novoa III cad53642003-03-19 09:13:01 +0000396 if (all_fmt & SORT_ORDER_REVERSE) {
397 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000398 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000399 return (dif);
Eric Andersen11c65522000-09-07 17:24:47 +0000400}
401
402/*----------------------------------------------------------------------*/
Rob Landley425e7582006-05-03 20:22:03 +0000403static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000404{
Rob Landley425e7582006-05-03 20:22:03 +0000405 qsort(dn, size, sizeof *dn, sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000406}
Rob Landleyea224be2006-06-18 20:20:07 +0000407#else
408#define sortcmp(a, b) 0
409#define dnsort(dn, size)
Eric Andersen11c65522000-09-07 17:24:47 +0000410#endif
411
Rob Landleyea224be2006-06-18 20:20:07 +0000412
Eric Andersen11c65522000-09-07 17:24:47 +0000413/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000414static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000415{
416 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000417 int column = 0;
418 int nexttab = 0;
419 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000420
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000421 if (dn == NULL || nfiles < 1)
422 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000423
Manuel Novoa III cad53642003-03-19 09:13:01 +0000424 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000425 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000426 } else {
427 /* find the longest file name- use that as the column width */
428 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000429 int len = strlen(dn[i]->name) +
Eric Andersen9e480452003-07-03 10:07:04 +0000430#ifdef CONFIG_SELINUX
431 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
432#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000433 ((all_fmt & LIST_INO) ? 8 : 0) +
434 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
435 if (column_width < len)
436 column_width = len;
437 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000438 column_width += tabstops;
439 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000440 }
441
Eric Andersene57d54b2001-01-30 18:03:11 +0000442 if (ncols > 1) {
443 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000444 if ((nrows * ncols) < nfiles)
445 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000446 } else {
447 nrows = nfiles;
448 ncols = 1;
449 }
Eric Andersen11c65522000-09-07 17:24:47 +0000450
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000451 for (row = 0; row < nrows; row++) {
452 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000453 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000454 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000455 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000456 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000457 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000458 if (column > 0) {
459 nexttab -= column;
460 while (nexttab--) {
461 putchar(' ');
462 column++;
463 }
Eric Andersen79565b62000-08-11 18:10:21 +0000464 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000465 nexttab = column + column_width;
466 column += list_single(dn[i]);
Eric Andersen79565b62000-08-11 18:10:21 +0000467 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000468 }
469 putchar('\n');
470 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000471 }
Eric Andersen11c65522000-09-07 17:24:47 +0000472}
473
474/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000475static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000476{
477 int i, nfiles;
478 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000479 int dndirs;
480 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000481
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000482 if (dn == NULL || ndirs < 1)
483 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000484
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000485 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000486 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000487 if (!first)
488 printf("\n");
489 first = 0;
490 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000491 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000492 subdnp = list_dir(dn[i]->fullname);
493 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000494 if (nfiles > 0) {
495 /* list all files at this level */
Rob Landleyea224be2006-06-18 20:20:07 +0000496 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000497 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000498 if (ENABLE_FEATURE_LS_RECURSIVE) {
499 if (all_fmt & DISP_RECURSIVE) {
500 /* recursive- list the sub-dirs */
501 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
502 dndirs = countsubdirs(subdnp, nfiles);
503 if (dndirs > 0) {
504 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
505 showdirs(dnd, dndirs, 0);
506 /* free the array of dnode pointers to the dirs */
507 free(dnd);
508 }
Eric Andersen11c65522000-09-07 17:24:47 +0000509 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000510 /* free the dnodes and the fullname mem */
511 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000512 }
Eric Andersen11c65522000-09-07 17:24:47 +0000513 }
514 }
515}
516
517/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000518static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000519{
520 struct dnode *dn, *cur, **dnp;
521 struct dirent *entry;
522 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000523 int i, nfiles;
524
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000525 if (path == NULL)
526 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000527
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000528 dn = NULL;
529 nfiles = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000530 dir = warn_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000531 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000532 status = EXIT_FAILURE;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000533 return (NULL); /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000534 }
535 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000536 char *fullname;
537
Eric Andersen11c65522000-09-07 17:24:47 +0000538 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000539 if (entry->d_name[0] == '.') {
540 if ((entry->d_name[1] == 0 || (
541 entry->d_name[1] == '.'
542 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000543 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000544 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000545 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000546 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000547 }
548 fullname = concat_path_file(path, entry->d_name);
549 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
550 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000551 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000552 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000553 cur->next = dn;
554 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000555 nfiles++;
556 }
557 closedir(dir);
558
559 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000560 ** allocate memory for an array to hold dnode pointers
561 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000562 if (dn == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000563 return (NULL);
564 dnp = dnalloc(nfiles);
565 for (i = 0, cur = dn; i < nfiles; i++) {
566 dnp[i] = cur; /* save pointer to node in array */
567 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000568 }
569
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000570 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000571}
572
573/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000574static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000575{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000576 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000577
Matt Kraaia1bbde72002-03-08 16:25:33 +0000578#ifdef CONFIG_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000579 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000580#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000581#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000582 char *filetime;
583 time_t ttime, age;
584#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000585#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000586 struct stat info;
587 char append;
588#endif
589
Glenn L McGrath4d001292003-01-06 01:11:50 +0000590 if (dn->fullname == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000591 return (0);
Eric Andersen11c65522000-09-07 17:24:47 +0000592
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000593#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000594 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000595 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000596 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000597 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000598 ttime = dn->dstat.st_ctime;
599 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000600#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000601#ifdef CONFIG_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000602 append = append_char(dn->dstat.st_mode);
603#endif
604
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000605 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000606 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000607 case LIST_INO:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000608 column += printf("%7ld ", (long int) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000609 break;
610 case LIST_BLOCKS:
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000611#if _FILE_OFFSET_BITS == 64
Eric Andersen5e678872006-01-30 19:48:23 +0000612 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000613#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000614 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000615#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000616 break;
617 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000618 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000619 break;
620 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000621 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000622 break;
623 case LIST_ID_NAME:
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000624#ifdef CONFIG_FEATURE_LS_USERNAME
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000625 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000626 printf("%-8.8s ", scratch);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000627 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000628 printf("%-8.8s", scratch);
629 column += 17;
630 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000631#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000632 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000633 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000634 break;
635 case LIST_SIZE:
636 case LIST_DEV:
637 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000638 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
639 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000640 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000641#ifdef CONFIG_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000642 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000643 column += printf("%9s ",
644 make_human_readable_str(dn->dstat.st_size, 1, 0));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000645 } else
646#endif
647 {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000648#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000649 column += printf("%9lld ", (long long) dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000650#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000651 column += printf("%9ld ", dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000652#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000653 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000654 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000655 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000656#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000657 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000658 printf("%24.24s ", filetime);
659 column += 25;
660 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000661 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000662 if ((all_fmt & LIST_FULLTIME) == 0) {
663 age = time(NULL) - ttime;
664 printf("%6.6s ", filetime + 4);
665 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
666 /* hh:mm if less than 6 months old */
667 printf("%5.5s ", filetime + 11);
668 } else {
669 printf(" %4.4s ", filetime + 20);
670 }
671 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000672 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000673 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000674#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000675#ifdef CONFIG_SELINUX
676 case LIST_CONTEXT:
677 {
Rob Landley60158cb2005-05-03 06:25:50 +0000678 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000679 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000680
Rob Landley60158cb2005-05-03 06:25:50 +0000681 if (dn->sid) {
682 /* I assume sid initilized with NULL */
683 len = strlen(dn->sid)+1;
684 safe_strncpy(context, dn->sid, len);
685 freecon(dn->sid);
686 }else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000687 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000688 }
689 printf("%-32s ", context);
690 column += MAX(33, len);
691 }
692 break;
693#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000694 case LIST_FILENAME:
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000695#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000696 errno = 0;
697 if (show_color && !lstat(dn->fullname, &info)) {
698 printf("\033[%d;%dm", bgcolor(info.st_mode),
699 fgcolor(info.st_mode));
700 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000701#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000702 column += printf("%s", dn->name);
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000703#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000704 if (show_color) {
705 printf("\033[0m");
706 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000707#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000708 break;
709 case LIST_SYMLINK:
710 if (S_ISLNK(dn->dstat.st_mode)) {
711 char *lpath = xreadlink(dn->fullname);
712
713 if (lpath) {
714 printf(" -> ");
715#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
716 if (!stat(dn->fullname, &info)) {
717 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000718 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000719#endif
720#ifdef CONFIG_FEATURE_LS_COLOR
721 if (show_color) {
722 errno = 0;
723 printf("\033[%d;%dm", bgcolor(info.st_mode),
724 fgcolor(info.st_mode));
725 }
726#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000727 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000728#ifdef CONFIG_FEATURE_LS_COLOR
729 if (show_color) {
730 printf("\033[0m");
731 }
732#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000733 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000734 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000735 }
736 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000737#ifdef CONFIG_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000738 case LIST_FILETYPE:
739 if (append != '\0') {
740 printf("%1c", append);
741 column++;
742 }
743 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000744#endif
745 }
746 }
747
Glenn L McGrath4d001292003-01-06 01:11:50 +0000748 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000749}
750
751/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000752
Glenn L McGrath792cae52004-01-18 05:15:16 +0000753/* "[-]Cadil1", POSIX mandated options, busybox always supports */
754/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
755/* "[-]Ak" GNU options, busybox always supports */
756/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
757/* "[-]p", POSIX non-mandated options, busybox optionally supports */
758/* "[-]SXvThw", GNU options, busybox optionally supports */
759/* "[-]K", SELinux mandated options, busybox optionally supports */
760/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000761
Glenn L McGrath792cae52004-01-18 05:15:16 +0000762#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
763# define LS_STR_TIMESTAMPS "cetu"
764#else
765# define LS_STR_TIMESTAMPS ""
766#endif
767
Glenn L McGrath792cae52004-01-18 05:15:16 +0000768#ifdef CONFIG_FEATURE_LS_FILETYPES
769# define LS_STR_FILETYPES "Fp"
770#else
771# define LS_STR_FILETYPES ""
772#endif
773
774#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
775# define LS_STR_FOLLOW_LINKS "L"
776#else
777# define LS_STR_FOLLOW_LINKS ""
778#endif
779
780#ifdef CONFIG_FEATURE_LS_RECURSIVE
781# define LS_STR_RECURSIVE "R"
782#else
783# define LS_STR_RECURSIVE ""
784#endif
785
786#ifdef CONFIG_FEATURE_HUMAN_READABLE
787# define LS_STR_HUMAN_READABLE "h"
788#else
789# define LS_STR_HUMAN_READABLE ""
790#endif
791
792#ifdef CONFIG_SELINUX
793# define LS_STR_SELINUX "K"
794#else
795# define LS_STR_SELINUX ""
796#endif
797
798#ifdef CONFIG_FEATURE_AUTOWIDTH
799# define LS_STR_AUTOWIDTH "T:w:"
800#else
801# define LS_STR_AUTOWIDTH ""
802#endif
803
804static const char ls_options[]="Cadil1gnsxAk" \
805 LS_STR_TIMESTAMPS \
Rob Landleyea224be2006-06-18 20:20:07 +0000806 USE_FEATURE_LS_SORTFILES("SXrv") \
Glenn L McGrath792cae52004-01-18 05:15:16 +0000807 LS_STR_FILETYPES \
808 LS_STR_FOLLOW_LINKS \
809 LS_STR_RECURSIVE \
810 LS_STR_HUMAN_READABLE \
811 LS_STR_SELINUX \
812 LS_STR_AUTOWIDTH;
813
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000814#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000815#define STYLE_MASK_TRIGGER STYLE_MASK
816#define SORT_MASK_TRIGGER SORT_MASK
817#define DISP_MASK_TRIGGER DISP_ROWS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000818
819static const unsigned opt_flags[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000820 LIST_SHORT | STYLE_COLUMNS, /* C */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000821 DISP_HIDDEN | DISP_DOT, /* a */
822 DISP_NOLIST, /* d */
823 LIST_INO, /* i */
824 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
825 LIST_SHORT | STYLE_SINGLE, /* 1 */
826 0, /* g - ingored */
827 LIST_ID_NUMERIC, /* n */
828 LIST_BLOCKS, /* s */
829 DISP_ROWS, /* x */
830 DISP_HIDDEN, /* A */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000831#ifdef CONFIG_SELINUX
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000832 LIST_CONTEXT, /* k */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000833#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000834 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000835#endif
836#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Rob Landleyea224be2006-06-18 20:20:07 +0000837 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000838 LIST_FULLTIME, /* e */
Rob Landleyea224be2006-06-18 20:20:07 +0000839 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
840 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000841#endif
842#ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000843 SORT_SIZE, /* S */
844 SORT_EXT, /* X */
845 SORT_ORDER_REVERSE, /* r */
846 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000847#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000848#ifdef CONFIG_FEATURE_LS_FILETYPES
849 LIST_FILETYPE | LIST_EXEC, /* F */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000850 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000851#endif
852#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000853 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000854#endif
855#ifdef CONFIG_FEATURE_LS_RECURSIVE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000856 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000857#endif
858#ifdef CONFIG_FEATURE_HUMAN_READABLE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000859 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000860#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000861#ifdef CONFIG_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000862 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
863#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000864#ifdef CONFIG_FEATURE_AUTOWIDTH
865 0, 0, /* T, w - ignored */
866#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000867 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000868};
869
870
871/*----------------------------------------------------------------------*/
872
Rob Landleydfba7412006-03-06 20:47:33 +0000873int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000874{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000875 struct dnode **dnd;
876 struct dnode **dnf;
877 struct dnode **dnp;
878 struct dnode *dn;
879 struct dnode *cur;
880 long opt;
881 int nfiles = 0;
882 int dnfiles;
883 int dndirs;
884 int oi;
885 int ac;
886 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000887 char **av;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000888#ifdef CONFIG_FEATURE_AUTOWIDTH
889 char *tabstops_str = NULL;
890 char *terminal_width_str = NULL;
891#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000892#ifdef CONFIG_FEATURE_LS_COLOR
893 char *color_opt;
894#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000895
Rob Landley2b8a05a2006-06-20 17:43:01 +0000896 all_fmt = LIST_SHORT |
Rob Landleyea224be2006-06-18 20:20:07 +0000897 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000898
Eric Andersen6d687812003-11-04 23:16:48 +0000899#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000900 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000901 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000902 /* Go one less... */
903 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000904#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000905
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000906#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000907 bb_applet_long_options = ls_color_opt;
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000908#endif
909
Eric Andersen11c65522000-09-07 17:24:47 +0000910 /* process options */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000911#ifdef CONFIG_FEATURE_AUTOWIDTH
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000912 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
913#ifdef CONFIG_FEATURE_LS_COLOR
914 , &color_opt
915#endif
916 );
Glenn L McGrath792cae52004-01-18 05:15:16 +0000917 if (tabstops_str) {
918 tabstops = atoi(tabstops_str);
919 }
920 if (terminal_width_str) {
921 terminal_width = atoi(terminal_width_str);
922 }
923#else
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000924 opt = bb_getopt_ulflags(argc, argv, ls_options
925#ifdef CONFIG_FEATURE_LS_COLOR
926 , &color_opt
927#endif
928 );
Manuel Novoa III cad53642003-03-19 09:13:01 +0000929#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000930 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000931 if (opt & (1 << i)) {
932 unsigned int flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000933
Manuel Novoa III cad53642003-03-19 09:13:01 +0000934 if (flags & LIST_MASK_TRIGGER) {
935 all_fmt &= ~LIST_MASK;
936 }
937 if (flags & STYLE_MASK_TRIGGER) {
938 all_fmt &= ~STYLE_MASK;
939 }
Rob Landleyea224be2006-06-18 20:20:07 +0000940 if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000941 all_fmt &= ~SORT_MASK;
942 }
943 if (flags & DISP_MASK_TRIGGER) {
944 all_fmt &= ~DISP_MASK;
945 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000946 if (flags & TIME_MASK) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000947 all_fmt &= ~TIME_MASK;
948 }
Eric Andersen9e480452003-07-03 10:07:04 +0000949 if (flags & LIST_CONTEXT) {
950 all_fmt |= STYLE_SINGLE;
951 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000952#ifdef CONFIG_FEATURE_HUMAN_READABLE
953 if (opt == 'l') {
954 all_fmt &= ~LS_DISP_HR;
955 }
956#endif
957 all_fmt |= flags;
958 }
Eric Andersen11c65522000-09-07 17:24:47 +0000959 }
960
Paul Fox156dc412005-08-01 19:33:30 +0000961#ifdef CONFIG_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000962 {
963 /* find color bit value - last position for short getopt */
Paul Fox156dc412005-08-01 19:33:30 +0000964
Paul Fox156dc412005-08-01 19:33:30 +0000965#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
Paul Fox156dc412005-08-01 19:33:30 +0000966 char *p;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000967
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000968 if ((p = getenv ("LS_COLORS")) != NULL &&
Paul Fox156dc412005-08-01 19:33:30 +0000969 (*p == '\0' || (strcmp(p, "none") == 0))) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000970 ;
Paul Fox156dc412005-08-01 19:33:30 +0000971 } else if (isatty(STDOUT_FILENO)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000972 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000973 }
974#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000975
976 if((opt & (1 << i))) { /* next flag after short options */
977 if (color_opt == NULL || strcmp("always", color_opt) == 0)
978 show_color = 1;
979 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
980 show_color = 0;
981 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
982 show_color = 1;
983 }
Paul Fox156dc412005-08-01 19:33:30 +0000984 }
985#endif
986
Eric Andersen11c65522000-09-07 17:24:47 +0000987 /* sort out which command line options take precedence */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000988#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000989 if (all_fmt & DISP_NOLIST)
990 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000991#endif
Rob Landleyea224be2006-06-18 20:20:07 +0000992 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
993 if (all_fmt & TIME_CHANGE)
994 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
995 if (all_fmt & TIME_ACCESS)
996 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
997 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000998 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
999 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001000#ifdef CONFIG_FEATURE_LS_USERNAME
Manuel Novoa III cad53642003-03-19 09:13:01 +00001001 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1002 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersen11c65522000-09-07 17:24:47 +00001003#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001004
Eric Andersen11c65522000-09-07 17:24:47 +00001005 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +00001006 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +00001007 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +00001008
1009 /*
1010 * when there are no cmd line args we have to supply a default "." arg.
1011 * we will create a second argv array, "av" that will hold either
1012 * our created "." arg, or the real cmd line args. The av array
1013 * just holds the pointers- we don't move the date the pointers
1014 * point to.
1015 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001016 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +00001017 if (ac < 1) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001018 static const char * const dotdir[] = { "." };
1019
1020 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001021 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +00001022 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001023 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +00001024 }
1025
1026 /* now, everything is in the av array */
1027 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001028 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001029
1030 /* stuff the command line file names into an dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001031 dn = NULL;
1032 for (oi = 0; oi < ac; oi++) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001033 cur = my_stat(av[oi], av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001034 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001035 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001036 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001037 cur->next = dn;
1038 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001039 nfiles++;
1040 }
1041
1042 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001043 ** allocate memory for an array to hold dnode pointers
1044 */
1045 dnp = dnalloc(nfiles);
1046 for (i = 0, cur = dn; i < nfiles; i++) {
1047 dnp[i] = cur; /* save pointer to node in array */
1048 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +00001049 }
1050
Manuel Novoa III cad53642003-03-19 09:13:01 +00001051 if (all_fmt & DISP_NOLIST) {
Rob Landleyea224be2006-06-18 20:20:07 +00001052 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001053 if (nfiles > 0)
1054 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001055 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001056 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1057 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1058 dndirs = countdirs(dnp, nfiles);
1059 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001060 if (dnfiles > 0) {
Rob Landleyea224be2006-06-18 20:20:07 +00001061 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001062 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +00001063 if (ENABLE_FEATURE_CLEAN_UP)
1064 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +00001065 }
1066 if (dndirs > 0) {
Rob Landleyea224be2006-06-18 20:20:07 +00001067 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +00001068 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +00001069 if (ENABLE_FEATURE_CLEAN_UP)
1070 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +00001071 }
1072 }
Rob Landley26314862006-05-02 19:46:52 +00001073 if (ENABLE_FEATURE_CLEAN_UP)
1074 dfree(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001075 return (status);
Eric Andersencc8ed391999-10-05 16:24:54 +00001076}