blob: de8405dab3154bb42c5858384f6693f79652d570 [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"
Eric Andersencc8ed391999-10-05 16:24:54 +000040#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000041#include <errno.h>
Eric Andersen11c65522000-09-07 17:24:47 +000042#include <string.h>
Eric Andersen5307eca2001-01-26 01:52:43 +000043#include <fcntl.h>
44#include <signal.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000045#include <getopt.h> /* struct option */
Eric Andersen5307eca2001-01-26 01:52:43 +000046#include <sys/ioctl.h>
Eric Andersen4f807a82004-07-26 09:11:12 +000047#include <sys/sysmacros.h> /* major() and minor() */
Eric Andersenf1142c52001-02-20 06:16:29 +000048#include <time.h>
Eric Andersenf1142c52001-02-20 06:16:29 +000049
Eric Andersen11c65522000-09-07 17:24:47 +000050/* what is the overall style of the listing */
Manuel Novoa III cad53642003-03-19 09:13:01 +000051#define STYLE_COLUMNS (1U<<21) /* fill columns */
52#define STYLE_LONG (2U<<21) /* one record per line, extended info */
53#define STYLE_SINGLE (3U<<21) /* one record per line */
54
55#define STYLE_MASK STYLE_SINGLE
56#define STYLE_ONE_RECORD_FLAG STYLE_LONG
Eric Andersencc8ed391999-10-05 16:24:54 +000057
Eric Andersen11c65522000-09-07 17:24:47 +000058/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
59/* what file information will be listed */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000060#define LIST_INO (1U<<0)
61#define LIST_BLOCKS (1U<<1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000062#define LIST_MODEBITS (1U<<2)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000063#define LIST_NLINKS (1U<<3)
Manuel Novoa III cad53642003-03-19 09:13:01 +000064#define LIST_ID_NAME (1U<<4)
65#define LIST_ID_NUMERIC (1U<<5)
Eric Andersen9e480452003-07-03 10:07:04 +000066#define LIST_CONTEXT (1U<<6)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000067#define LIST_SIZE (1U<<7)
68#define LIST_DEV (1U<<8)
Eric Andersen9e480452003-07-03 10:07:04 +000069#define LIST_DATE_TIME (1U<<9)
70#define LIST_FULLTIME (1U<<10)
71#define LIST_FILENAME (1U<<11)
72#define LIST_SYMLINK (1U<<12)
73#define LIST_FILETYPE (1U<<13)
74#define LIST_EXEC (1U<<14)
Manuel Novoa III cad53642003-03-19 09:13:01 +000075
76#define LIST_MASK ((LIST_EXEC << 1) - 1)
Eric Andersen11c65522000-09-07 17:24:47 +000077
78/* what files will be displayed */
Manuel Novoa III cad53642003-03-19 09:13:01 +000079#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000080#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
81#define DISP_DOT (1U<<17) /* show . and .. */
82#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
Manuel Novoa III cad53642003-03-19 09:13:01 +000083#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000084#define DISP_ROWS (1U<<20) /* print across rows */
Manuel Novoa III cad53642003-03-19 09:13:01 +000085
Rob Landley9947a242006-06-15 22:11:10 +000086#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1))
Eric Andersen11c65522000-09-07 17:24:47 +000087
Rob Landleyea224be2006-06-18 20:20:07 +000088// CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +000089/* how will the files be sorted */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000090#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
Manuel Novoa III cad53642003-03-19 09:13:01 +000091#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
92
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000093#define SORT_NAME 0 /* sort by file name */
Manuel Novoa III cad53642003-03-19 09:13:01 +000094#define SORT_SIZE (1U<<28) /* sort by file size */
95#define SORT_ATIME (2U<<28) /* sort by last access time */
96#define SORT_CTIME (3U<<28) /* sort by last change time */
97#define SORT_MTIME (4U<<28) /* sort by last modification time */
98#define SORT_VERSION (5U<<28) /* sort by version */
99#define SORT_EXT (6U<<28) /* sort by file name extension */
100#define SORT_DIR (7U<<28) /* sort by file or directory */
101
102#define SORT_MASK (7U<<28)
Eric Andersencc8ed391999-10-05 16:24:54 +0000103
Eric Andersen11c65522000-09-07 17:24:47 +0000104/* which of the three times will be used */
Rob Landley2b8a05a2006-06-20 17:43:01 +0000105#define TIME_CHANGE ((1U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
106#define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS)
107#define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108
109#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
110#define FOLLOW_LINKS (1U<<25)
111#endif
112#ifdef CONFIG_FEATURE_HUMAN_READABLE
113#define LS_DISP_HR (1U<<26)
Eric Andersencc8ed391999-10-05 16:24:54 +0000114#endif
115
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000116#define LIST_SHORT (LIST_FILENAME)
117#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
118#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
119 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
120#define LIST_ILONG (LIST_INO | LIST_LONG)
Eric Andersencc8ed391999-10-05 16:24:54 +0000121
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122#define SPLIT_DIR 1
123#define SPLIT_FILE 0
124#define SPLIT_SUBDIR 2
Eric Andersencc8ed391999-10-05 16:24:54 +0000125
Eric Andersen11c65522000-09-07 17:24:47 +0000126#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
127#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000128
Eric Andersend598d412002-04-27 09:19:39 +0000129#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000130# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
Eric Andersen11c65522000-09-07 17:24:47 +0000131#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000132
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000133/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
134#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000135
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000136static int show_color = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000137
Paul Fox156dc412005-08-01 19:33:30 +0000138/* long option entry used only for --color, which has no short option
139 * equivalent. */
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000140static const struct option ls_color_opt[] =
Paul Fox156dc412005-08-01 19:33:30 +0000141{
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000142 {"color", optional_argument, NULL, 1},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000143 {NULL, 0, NULL, 0}
Paul Fox156dc412005-08-01 19:33:30 +0000144};
145
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000146#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000147 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000148#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000149 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000150#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151
Eric Andersen11c65522000-09-07 17:24:47 +0000152/*
153 * a directory entry and its stat info are stored here
154 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000155struct dnode { /* the basic node */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000156 char *name; /* the dir entry name */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000157 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000158 int allocated;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000159 struct stat dstat; /* the file stat info */
Eric Andersen9e480452003-07-03 10:07:04 +0000160#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000161 security_context_t sid;
Eric Andersen9e480452003-07-03 10:07:04 +0000162#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000163 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000164};
165typedef struct dnode dnode_t;
166
Glenn L McGrath4d001292003-01-06 01:11:50 +0000167static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000168static struct dnode **dnalloc(int);
169static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000170
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171static unsigned int all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000172
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000173#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000174static int terminal_width = TERMINAL_WIDTH;
Eric Andersene57d54b2001-01-30 18:03:11 +0000175static unsigned short tabstops = COLUMN_GAP;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000176#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000177#define tabstops COLUMN_GAP
178#define terminal_width TERMINAL_WIDTH
Eric Andersen11c65522000-09-07 17:24:47 +0000179#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Matt Kraai33fdae52000-10-13 17:59:43 +0000181static int status = EXIT_SUCCESS;
182
Glenn L McGrath4d001292003-01-06 01:11:50 +0000183static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000184{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000185 struct stat dstat;
186 struct dnode *cur;
Eric Andersen9e480452003-07-03 10:07:04 +0000187#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000188 security_context_t sid=NULL;
Eric Andersen9e480452003-07-03 10:07:04 +0000189#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000190
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000191#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192 if (all_fmt & FOLLOW_LINKS) {
Eric Andersen9e480452003-07-03 10:07:04 +0000193#ifdef CONFIG_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000194 if (is_selinux_enabled()) {
195 getfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000196 }
Eric Andersen9e480452003-07-03 10:07:04 +0000197#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000198 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000200 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000201 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000202 }
203 } else
204#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000205 {
206#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000207 if (is_selinux_enabled()) {
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000208 lgetfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000209 }
Eric Andersen9e480452003-07-03 10:07:04 +0000210#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000211 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000212 bb_perror_msg("%s", fullname);
213 status = EXIT_FAILURE;
214 return 0;
215 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000216 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000217
218 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
219 cur->fullname = fullname;
220 cur->name = name;
221 cur->dstat = dstat;
Eric Andersen9e480452003-07-03 10:07:04 +0000222#ifdef CONFIG_SELINUX
223 cur->sid = sid;
224#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000225 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000226}
227
Eric Andersen11c65522000-09-07 17:24:47 +0000228/*----------------------------------------------------------------------*/
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000229#ifdef CONFIG_FEATURE_LS_COLOR
230static char fgcolor(mode_t mode)
231{
232 /* Check wheter the file is existing (if so, color it red!) */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000233 if (errno == ENOENT) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000234 return '\037';
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000235 }
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 COLOR(0xF000); /* File is executable ... */
238 return COLOR(mode);
239}
240
241/*----------------------------------------------------------------------*/
242static char bgcolor(mode_t mode)
243{
Rob Landley9947a242006-06-15 22:11:10 +0000244 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000245 return ATTR(0xF000); /* File is executable ... */
246 return ATTR(mode);
247}
248#endif
249
250/*----------------------------------------------------------------------*/
Eric Andersend598d412002-04-27 09:19:39 +0000251#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000252static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000253{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000254 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000255 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000256 if (S_ISDIR(mode))
257 return '/';
258 if (!(all_fmt & LIST_EXEC))
259 return '\0';
260 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000261 return '*';
262 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000263}
264#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000265
Eric Andersen11c65522000-09-07 17:24:47 +0000266/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000267
Manuel Novoa III cad53642003-03-19 09:13:01 +0000268#define countdirs(A,B) count_dirs((A), (B), 1)
269#define countsubdirs(A,B) count_dirs((A), (B), 0)
270
271static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000272{
273 int i, dirs;
274
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000275 if (dn == NULL || nfiles < 1)
276 return (0);
277 dirs = 0;
278 for (i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 if (S_ISDIR(dn[i]->dstat.st_mode)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000280 && (notsubdirs ||
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000281 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
Manuel Novoa III cad53642003-03-19 09:13:01 +0000282 && ((dn[i]->name[1] != '.')
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000283 || dn[i]->name[2])))))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000284 dirs++;
Eric Andersen11c65522000-09-07 17:24:47 +0000285 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000286 return (dirs);
Eric Andersen11c65522000-09-07 17:24:47 +0000287}
288
Eric Andersen3e6ff902001-03-09 21:24:12 +0000289static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000290{
291 int nfiles;
292 struct dnode *cur;
293
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000294 if (dnp == NULL)
295 return (0);
296 nfiles = 0;
297 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
298 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000299 nfiles++;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000300 return (nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000301}
302
303/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000304static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000305{
306 struct dnode **p;
307
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000308 if (num < 1)
309 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000310
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000311 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000312 return (p);
Eric Andersen11c65522000-09-07 17:24:47 +0000313}
314
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000315#ifdef CONFIG_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000316static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000317{
Rob Landley26314862006-05-02 19:46:52 +0000318 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000319
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000320 if (dnp == NULL)
321 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000322
Rob Landley26314862006-05-02 19:46:52 +0000323 for (i = 0; i < nfiles; i++) {
324 struct dnode *cur = dnp[i];
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000325 if(cur->allocated)
326 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000327 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000328 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000329 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000330}
Rob Landleyc44bc982006-05-28 01:19:06 +0000331#else
332#define dfree(...)
Eric Andersen20aab262001-07-19 22:28:02 +0000333#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000334
Eric Andersen3e6ff902001-03-09 21:24:12 +0000335static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000336{
337 int dncnt, i, d;
338 struct dnode **dnp;
339
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000340 if (dn == NULL || nfiles < 1)
341 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000342
343 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000344 if (which == SPLIT_SUBDIR)
345 dncnt = countsubdirs(dn, nfiles);
346 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000347 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000348 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000349 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000350 }
Eric Andersen11c65522000-09-07 17:24:47 +0000351
352 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000353 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000354
355 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000356 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000357 if (S_ISDIR(dn[i]->dstat.st_mode)) {
358 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
359 if ((which & SPLIT_DIR)
360 || ((dn[i]->name[0] != '.')
361 || (dn[i]->name[1]
362 && ((dn[i]->name[1] != '.')
363 || dn[i]->name[2])))) {
364 dnp[d++] = dn[i];
365 }
366 }
367 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
368 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000369 }
370 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000371 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000372}
373
374/*----------------------------------------------------------------------*/
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000375#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000376static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000377{
Rob Landley425e7582006-05-03 20:22:03 +0000378 struct dnode *d1 = *(struct dnode **)a;
379 struct dnode *d2 = *(struct dnode **)b;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000380 unsigned int sort_opts = all_fmt & SORT_MASK;
381 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000382
Manuel Novoa III cad53642003-03-19 09:13:01 +0000383 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000384 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000385 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000386 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000387 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000388 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000389 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000390 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000391 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000392 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000393 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000394 /* } else if (sort_opts == SORT_VERSION) { */
395 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000396 }
397
Eric Andersen11c65522000-09-07 17:24:47 +0000398 if (dif == 0) {
399 /* sort by name- may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000400 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
401 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000402 }
403
Manuel Novoa III cad53642003-03-19 09:13:01 +0000404 if (all_fmt & SORT_ORDER_REVERSE) {
405 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000406 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000407 return (dif);
Eric Andersen11c65522000-09-07 17:24:47 +0000408}
409
410/*----------------------------------------------------------------------*/
Rob Landley425e7582006-05-03 20:22:03 +0000411static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000412{
Rob Landley425e7582006-05-03 20:22:03 +0000413 qsort(dn, size, sizeof *dn, sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000414}
Rob Landleyea224be2006-06-18 20:20:07 +0000415#else
416#define sortcmp(a, b) 0
417#define dnsort(dn, size)
Eric Andersen11c65522000-09-07 17:24:47 +0000418#endif
419
Rob Landleyea224be2006-06-18 20:20:07 +0000420
Eric Andersen11c65522000-09-07 17:24:47 +0000421/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000422static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000423{
424 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000425 int column = 0;
426 int nexttab = 0;
427 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000428
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000429 if (dn == NULL || nfiles < 1)
430 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000431
Manuel Novoa III cad53642003-03-19 09:13:01 +0000432 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000433 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000434 } else {
435 /* find the longest file name- use that as the column width */
436 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000437 int len = strlen(dn[i]->name) +
Eric Andersen9e480452003-07-03 10:07:04 +0000438#ifdef CONFIG_SELINUX
439 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
440#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000441 ((all_fmt & LIST_INO) ? 8 : 0) +
442 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
443 if (column_width < len)
444 column_width = len;
445 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000446 column_width += tabstops;
447 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000448 }
449
Eric Andersene57d54b2001-01-30 18:03:11 +0000450 if (ncols > 1) {
451 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000452 if ((nrows * ncols) < nfiles)
453 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000454 } else {
455 nrows = nfiles;
456 ncols = 1;
457 }
Eric Andersen11c65522000-09-07 17:24:47 +0000458
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000459 for (row = 0; row < nrows; row++) {
460 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000461 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000462 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000463 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000464 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000465 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000466 if (column > 0) {
467 nexttab -= column;
468 while (nexttab--) {
469 putchar(' ');
470 column++;
471 }
Eric Andersen79565b62000-08-11 18:10:21 +0000472 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000473 nexttab = column + column_width;
474 column += list_single(dn[i]);
Eric Andersen79565b62000-08-11 18:10:21 +0000475 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000476 }
477 putchar('\n');
478 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000479 }
Eric Andersen11c65522000-09-07 17:24:47 +0000480}
481
482/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000483static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000484{
485 int i, nfiles;
486 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000487 int dndirs;
488 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000489
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000490 if (dn == NULL || ndirs < 1)
491 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000492
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000493 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000494 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000495 if (!first)
496 printf("\n");
497 first = 0;
498 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000499 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000500 subdnp = list_dir(dn[i]->fullname);
501 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000502 if (nfiles > 0) {
503 /* list all files at this level */
Rob Landleyea224be2006-06-18 20:20:07 +0000504 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000505 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000506 if (ENABLE_FEATURE_LS_RECURSIVE) {
507 if (all_fmt & DISP_RECURSIVE) {
508 /* recursive- list the sub-dirs */
509 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
510 dndirs = countsubdirs(subdnp, nfiles);
511 if (dndirs > 0) {
512 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
513 showdirs(dnd, dndirs, 0);
514 /* free the array of dnode pointers to the dirs */
515 free(dnd);
516 }
Eric Andersen11c65522000-09-07 17:24:47 +0000517 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000518 /* free the dnodes and the fullname mem */
519 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000520 }
Eric Andersen11c65522000-09-07 17:24:47 +0000521 }
522 }
523}
524
525/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000526static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000527{
528 struct dnode *dn, *cur, **dnp;
529 struct dirent *entry;
530 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000531 int i, nfiles;
532
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000533 if (path == NULL)
534 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000535
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000536 dn = NULL;
537 nfiles = 0;
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +0000538 dir = bb_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000539 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000540 status = EXIT_FAILURE;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000541 return (NULL); /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000542 }
543 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000544 char *fullname;
545
Eric Andersen11c65522000-09-07 17:24:47 +0000546 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000547 if (entry->d_name[0] == '.') {
548 if ((entry->d_name[1] == 0 || (
549 entry->d_name[1] == '.'
550 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000551 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000552 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000553 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000554 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000555 }
556 fullname = concat_path_file(path, entry->d_name);
557 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
558 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000559 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000560 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000561 cur->next = dn;
562 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000563 nfiles++;
564 }
565 closedir(dir);
566
567 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000568 ** allocate memory for an array to hold dnode pointers
569 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000570 if (dn == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000571 return (NULL);
572 dnp = dnalloc(nfiles);
573 for (i = 0, cur = dn; i < nfiles; i++) {
574 dnp[i] = cur; /* save pointer to node in array */
575 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000576 }
577
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000578 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000579}
580
581/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000582static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000583{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000584 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000585
Matt Kraaia1bbde72002-03-08 16:25:33 +0000586#ifdef CONFIG_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000587 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000588#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000589#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000590 char *filetime;
591 time_t ttime, age;
592#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000593#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000594 struct stat info;
595 char append;
596#endif
597
Glenn L McGrath4d001292003-01-06 01:11:50 +0000598 if (dn->fullname == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000599 return (0);
Eric Andersen11c65522000-09-07 17:24:47 +0000600
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000601#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000602 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000603 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000604 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000605 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000606 ttime = dn->dstat.st_ctime;
607 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000608#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000609#ifdef CONFIG_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000610 append = append_char(dn->dstat.st_mode);
611#endif
612
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000613 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000614 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000615 case LIST_INO:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000616 column += printf("%7ld ", (long int) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000617 break;
618 case LIST_BLOCKS:
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000619#if _FILE_OFFSET_BITS == 64
Eric Andersen5e678872006-01-30 19:48:23 +0000620 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000621#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000622 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000623#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000624 break;
625 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000626 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000627 break;
628 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000629 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000630 break;
631 case LIST_ID_NAME:
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000632#ifdef CONFIG_FEATURE_LS_USERNAME
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000633 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000634 printf("%-8.8s ", scratch);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000635 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000636 printf("%-8.8s", scratch);
637 column += 17;
638 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000639#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000640 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000641 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000642 break;
643 case LIST_SIZE:
644 case LIST_DEV:
645 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000646 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
647 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000648 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000649#ifdef CONFIG_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000650 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000651 column += printf("%9s ",
652 make_human_readable_str(dn->dstat.st_size, 1, 0));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000653 } else
654#endif
655 {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000656#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000657 column += printf("%9lld ", (long long) dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000658#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000659 column += printf("%9ld ", dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000660#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000661 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000662 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000663 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000664#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000665 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000666 printf("%24.24s ", filetime);
667 column += 25;
668 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000669 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000670 if ((all_fmt & LIST_FULLTIME) == 0) {
671 age = time(NULL) - ttime;
672 printf("%6.6s ", filetime + 4);
673 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
674 /* hh:mm if less than 6 months old */
675 printf("%5.5s ", filetime + 11);
676 } else {
677 printf(" %4.4s ", filetime + 20);
678 }
679 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000680 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000681 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000682#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000683#ifdef CONFIG_SELINUX
684 case LIST_CONTEXT:
685 {
Rob Landley60158cb2005-05-03 06:25:50 +0000686 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000687 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000688
Rob Landley60158cb2005-05-03 06:25:50 +0000689 if (dn->sid) {
690 /* I assume sid initilized with NULL */
691 len = strlen(dn->sid)+1;
692 safe_strncpy(context, dn->sid, len);
693 freecon(dn->sid);
694 }else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000695 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000696 }
697 printf("%-32s ", context);
698 column += MAX(33, len);
699 }
700 break;
701#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000702 case LIST_FILENAME:
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000703#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000704 errno = 0;
705 if (show_color && !lstat(dn->fullname, &info)) {
706 printf("\033[%d;%dm", bgcolor(info.st_mode),
707 fgcolor(info.st_mode));
708 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000709#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000710 column += printf("%s", dn->name);
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000711#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000712 if (show_color) {
713 printf("\033[0m");
714 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000715#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000716 break;
717 case LIST_SYMLINK:
718 if (S_ISLNK(dn->dstat.st_mode)) {
719 char *lpath = xreadlink(dn->fullname);
720
721 if (lpath) {
722 printf(" -> ");
723#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
724 if (!stat(dn->fullname, &info)) {
725 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000726 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000727#endif
728#ifdef CONFIG_FEATURE_LS_COLOR
729 if (show_color) {
730 errno = 0;
731 printf("\033[%d;%dm", bgcolor(info.st_mode),
732 fgcolor(info.st_mode));
733 }
734#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000735 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000736#ifdef CONFIG_FEATURE_LS_COLOR
737 if (show_color) {
738 printf("\033[0m");
739 }
740#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000741 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000742 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000743 }
744 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000745#ifdef CONFIG_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000746 case LIST_FILETYPE:
747 if (append != '\0') {
748 printf("%1c", append);
749 column++;
750 }
751 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000752#endif
753 }
754 }
755
Glenn L McGrath4d001292003-01-06 01:11:50 +0000756 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000757}
758
759/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000760
Glenn L McGrath792cae52004-01-18 05:15:16 +0000761/* "[-]Cadil1", POSIX mandated options, busybox always supports */
762/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
763/* "[-]Ak" GNU options, busybox always supports */
764/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
765/* "[-]p", POSIX non-mandated options, busybox optionally supports */
766/* "[-]SXvThw", GNU options, busybox optionally supports */
767/* "[-]K", SELinux mandated options, busybox optionally supports */
768/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000769
Glenn L McGrath792cae52004-01-18 05:15:16 +0000770#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
771# define LS_STR_TIMESTAMPS "cetu"
772#else
773# define LS_STR_TIMESTAMPS ""
774#endif
775
Glenn L McGrath792cae52004-01-18 05:15:16 +0000776#ifdef CONFIG_FEATURE_LS_FILETYPES
777# define LS_STR_FILETYPES "Fp"
778#else
779# define LS_STR_FILETYPES ""
780#endif
781
782#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
783# define LS_STR_FOLLOW_LINKS "L"
784#else
785# define LS_STR_FOLLOW_LINKS ""
786#endif
787
788#ifdef CONFIG_FEATURE_LS_RECURSIVE
789# define LS_STR_RECURSIVE "R"
790#else
791# define LS_STR_RECURSIVE ""
792#endif
793
794#ifdef CONFIG_FEATURE_HUMAN_READABLE
795# define LS_STR_HUMAN_READABLE "h"
796#else
797# define LS_STR_HUMAN_READABLE ""
798#endif
799
800#ifdef CONFIG_SELINUX
801# define LS_STR_SELINUX "K"
802#else
803# define LS_STR_SELINUX ""
804#endif
805
806#ifdef CONFIG_FEATURE_AUTOWIDTH
807# define LS_STR_AUTOWIDTH "T:w:"
808#else
809# define LS_STR_AUTOWIDTH ""
810#endif
811
812static const char ls_options[]="Cadil1gnsxAk" \
813 LS_STR_TIMESTAMPS \
Rob Landleyea224be2006-06-18 20:20:07 +0000814 USE_FEATURE_LS_SORTFILES("SXrv") \
Glenn L McGrath792cae52004-01-18 05:15:16 +0000815 LS_STR_FILETYPES \
816 LS_STR_FOLLOW_LINKS \
817 LS_STR_RECURSIVE \
818 LS_STR_HUMAN_READABLE \
819 LS_STR_SELINUX \
820 LS_STR_AUTOWIDTH;
821
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000822#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000823#define STYLE_MASK_TRIGGER STYLE_MASK
824#define SORT_MASK_TRIGGER SORT_MASK
825#define DISP_MASK_TRIGGER DISP_ROWS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000826
827static const unsigned opt_flags[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000828 LIST_SHORT | STYLE_COLUMNS, /* C */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000829 DISP_HIDDEN | DISP_DOT, /* a */
830 DISP_NOLIST, /* d */
831 LIST_INO, /* i */
832 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
833 LIST_SHORT | STYLE_SINGLE, /* 1 */
834 0, /* g - ingored */
835 LIST_ID_NUMERIC, /* n */
836 LIST_BLOCKS, /* s */
837 DISP_ROWS, /* x */
838 DISP_HIDDEN, /* A */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000839#ifdef CONFIG_SELINUX
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000840 LIST_CONTEXT, /* k */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000841#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000842 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000843#endif
844#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Rob Landleyea224be2006-06-18 20:20:07 +0000845 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000846 LIST_FULLTIME, /* e */
Rob Landleyea224be2006-06-18 20:20:07 +0000847 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
848 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000849#endif
850#ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000851 SORT_SIZE, /* S */
852 SORT_EXT, /* X */
853 SORT_ORDER_REVERSE, /* r */
854 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000855#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000856#ifdef CONFIG_FEATURE_LS_FILETYPES
857 LIST_FILETYPE | LIST_EXEC, /* F */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000858 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000859#endif
860#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000861 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000862#endif
863#ifdef CONFIG_FEATURE_LS_RECURSIVE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000864 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000865#endif
866#ifdef CONFIG_FEATURE_HUMAN_READABLE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000867 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000868#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000869#ifdef CONFIG_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000870 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
871#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000872#ifdef CONFIG_FEATURE_AUTOWIDTH
873 0, 0, /* T, w - ignored */
874#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000875 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000876};
877
878
879/*----------------------------------------------------------------------*/
880
Rob Landleydfba7412006-03-06 20:47:33 +0000881int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000882{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000883 struct dnode **dnd;
884 struct dnode **dnf;
885 struct dnode **dnp;
886 struct dnode *dn;
887 struct dnode *cur;
888 long opt;
889 int nfiles = 0;
890 int dnfiles;
891 int dndirs;
892 int oi;
893 int ac;
894 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000895 char **av;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000896#ifdef CONFIG_FEATURE_AUTOWIDTH
897 char *tabstops_str = NULL;
898 char *terminal_width_str = NULL;
899#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000900#ifdef CONFIG_FEATURE_LS_COLOR
901 char *color_opt;
902#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000903
Rob Landley2b8a05a2006-06-20 17:43:01 +0000904 all_fmt = LIST_SHORT |
Rob Landleyea224be2006-06-18 20:20:07 +0000905 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000906
Eric Andersen6d687812003-11-04 23:16:48 +0000907#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000908 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000909 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000910 /* Go one less... */
911 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000912#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000913
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000914#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000915 bb_applet_long_options = ls_color_opt;
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000916#endif
917
Eric Andersen11c65522000-09-07 17:24:47 +0000918 /* process options */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000919#ifdef CONFIG_FEATURE_AUTOWIDTH
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000920 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
921#ifdef CONFIG_FEATURE_LS_COLOR
922 , &color_opt
923#endif
924 );
Glenn L McGrath792cae52004-01-18 05:15:16 +0000925 if (tabstops_str) {
926 tabstops = atoi(tabstops_str);
927 }
928 if (terminal_width_str) {
929 terminal_width = atoi(terminal_width_str);
930 }
931#else
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000932 opt = bb_getopt_ulflags(argc, argv, ls_options
933#ifdef CONFIG_FEATURE_LS_COLOR
934 , &color_opt
935#endif
936 );
Manuel Novoa III cad53642003-03-19 09:13:01 +0000937#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000938 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000939 if (opt & (1 << i)) {
940 unsigned int flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000941
Manuel Novoa III cad53642003-03-19 09:13:01 +0000942 if (flags & LIST_MASK_TRIGGER) {
943 all_fmt &= ~LIST_MASK;
944 }
945 if (flags & STYLE_MASK_TRIGGER) {
946 all_fmt &= ~STYLE_MASK;
947 }
Rob Landleyea224be2006-06-18 20:20:07 +0000948 if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000949 all_fmt &= ~SORT_MASK;
950 }
951 if (flags & DISP_MASK_TRIGGER) {
952 all_fmt &= ~DISP_MASK;
953 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000954 if (flags & TIME_MASK) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000955 all_fmt &= ~TIME_MASK;
956 }
Eric Andersen9e480452003-07-03 10:07:04 +0000957 if (flags & LIST_CONTEXT) {
958 all_fmt |= STYLE_SINGLE;
959 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000960#ifdef CONFIG_FEATURE_HUMAN_READABLE
961 if (opt == 'l') {
962 all_fmt &= ~LS_DISP_HR;
963 }
964#endif
965 all_fmt |= flags;
966 }
Eric Andersen11c65522000-09-07 17:24:47 +0000967 }
968
Paul Fox156dc412005-08-01 19:33:30 +0000969#ifdef CONFIG_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000970 {
971 /* find color bit value - last position for short getopt */
Paul Fox156dc412005-08-01 19:33:30 +0000972
Paul Fox156dc412005-08-01 19:33:30 +0000973#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
Paul Fox156dc412005-08-01 19:33:30 +0000974 char *p;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000975
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000976 if ((p = getenv ("LS_COLORS")) != NULL &&
Paul Fox156dc412005-08-01 19:33:30 +0000977 (*p == '\0' || (strcmp(p, "none") == 0))) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000978 ;
Paul Fox156dc412005-08-01 19:33:30 +0000979 } else if (isatty(STDOUT_FILENO)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000980 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000981 }
982#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000983
984 if((opt & (1 << i))) { /* next flag after short options */
985 if (color_opt == NULL || strcmp("always", color_opt) == 0)
986 show_color = 1;
987 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
988 show_color = 0;
989 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
990 show_color = 1;
991 }
Paul Fox156dc412005-08-01 19:33:30 +0000992 }
993#endif
994
Eric Andersen11c65522000-09-07 17:24:47 +0000995 /* sort out which command line options take precedence */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000996#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000997 if (all_fmt & DISP_NOLIST)
998 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000999#endif
Rob Landleyea224be2006-06-18 20:20:07 +00001000 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
1001 if (all_fmt & TIME_CHANGE)
1002 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1003 if (all_fmt & TIME_ACCESS)
1004 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
1005 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001006 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1007 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001008#ifdef CONFIG_FEATURE_LS_USERNAME
Manuel Novoa III cad53642003-03-19 09:13:01 +00001009 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1010 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersen11c65522000-09-07 17:24:47 +00001011#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001012
Eric Andersen11c65522000-09-07 17:24:47 +00001013 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +00001014 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +00001015 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +00001016
1017 /*
1018 * when there are no cmd line args we have to supply a default "." arg.
1019 * we will create a second argv array, "av" that will hold either
1020 * our created "." arg, or the real cmd line args. The av array
1021 * just holds the pointers- we don't move the date the pointers
1022 * point to.
1023 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001024 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +00001025 if (ac < 1) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001026 static const char * const dotdir[] = { "." };
1027
1028 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001029 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +00001030 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001031 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +00001032 }
1033
1034 /* now, everything is in the av array */
1035 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001036 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001037
1038 /* stuff the command line file names into an dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001039 dn = NULL;
1040 for (oi = 0; oi < ac; oi++) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001041 cur = my_stat(av[oi], av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001042 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001043 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001044 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001045 cur->next = dn;
1046 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001047 nfiles++;
1048 }
1049
1050 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001051 ** allocate memory for an array to hold dnode pointers
1052 */
1053 dnp = dnalloc(nfiles);
1054 for (i = 0, cur = dn; i < nfiles; i++) {
1055 dnp[i] = cur; /* save pointer to node in array */
1056 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +00001057 }
1058
Manuel Novoa III cad53642003-03-19 09:13:01 +00001059 if (all_fmt & DISP_NOLIST) {
Rob Landleyea224be2006-06-18 20:20:07 +00001060 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001061 if (nfiles > 0)
1062 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001063 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001064 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1065 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1066 dndirs = countdirs(dnp, nfiles);
1067 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001068 if (dnfiles > 0) {
Rob Landleyea224be2006-06-18 20:20:07 +00001069 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001070 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +00001071 if (ENABLE_FEATURE_CLEAN_UP)
1072 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +00001073 }
1074 if (dndirs > 0) {
Rob Landleyea224be2006-06-18 20:20:07 +00001075 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +00001076 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +00001077 if (ENABLE_FEATURE_CLEAN_UP)
1078 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +00001079 }
1080 }
Rob Landley26314862006-05-02 19:46:52 +00001081 if (ENABLE_FEATURE_CLEAN_UP)
1082 dfree(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001083 return (status);
Eric Andersencc8ed391999-10-05 16:24:54 +00001084}