blob: 47acec60314ccbb1bfed749d6a6841dd5d466dd7 [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
Eric Andersen11c65522000-09-07 17:24:47 +000039#include <sys/types.h>
40#include <sys/stat.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000041#include <stdio.h>
42#include <unistd.h>
43#include <dirent.h>
44#include <errno.h>
45#include <stdio.h>
Eric Andersen11c65522000-09-07 17:24:47 +000046#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000047#include <stdlib.h>
Eric Andersen5307eca2001-01-26 01:52:43 +000048#include <fcntl.h>
49#include <signal.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000050#include <termios.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000051#include <getopt.h> /* struct option */
Eric Andersen5307eca2001-01-26 01:52:43 +000052#include <sys/ioctl.h>
Eric Andersen4f807a82004-07-26 09:11:12 +000053#include <sys/sysmacros.h> /* major() and minor() */
Eric Andersencbe31da2001-02-20 06:14:08 +000054#include "busybox.h"
Eric Andersen9e480452003-07-03 10:07:04 +000055#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000056#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000057#endif
Eric Andersen5307eca2001-01-26 01:52:43 +000058
Eric Andersenbdfd0d72001-10-24 05:00:29 +000059#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersenf1142c52001-02-20 06:16:29 +000060#include <time.h>
61#endif
62
Eric Andersen11c65522000-09-07 17:24:47 +000063/* what is the overall style of the listing */
Manuel Novoa III cad53642003-03-19 09:13:01 +000064#define STYLE_AUTO (0)
65#define STYLE_COLUMNS (1U<<21) /* fill columns */
66#define STYLE_LONG (2U<<21) /* one record per line, extended info */
67#define STYLE_SINGLE (3U<<21) /* one record per line */
68
69#define STYLE_MASK STYLE_SINGLE
70#define STYLE_ONE_RECORD_FLAG STYLE_LONG
Eric Andersencc8ed391999-10-05 16:24:54 +000071
Eric Andersen11c65522000-09-07 17:24:47 +000072/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
73/* what file information will be listed */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000074#define LIST_INO (1U<<0)
75#define LIST_BLOCKS (1U<<1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000076#define LIST_MODEBITS (1U<<2)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000077#define LIST_NLINKS (1U<<3)
Manuel Novoa III cad53642003-03-19 09:13:01 +000078#define LIST_ID_NAME (1U<<4)
79#define LIST_ID_NUMERIC (1U<<5)
Eric Andersen9e480452003-07-03 10:07:04 +000080#define LIST_CONTEXT (1U<<6)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000081#define LIST_SIZE (1U<<7)
82#define LIST_DEV (1U<<8)
Eric Andersen9e480452003-07-03 10:07:04 +000083#define LIST_DATE_TIME (1U<<9)
84#define LIST_FULLTIME (1U<<10)
85#define LIST_FILENAME (1U<<11)
86#define LIST_SYMLINK (1U<<12)
87#define LIST_FILETYPE (1U<<13)
88#define LIST_EXEC (1U<<14)
Manuel Novoa III cad53642003-03-19 09:13:01 +000089
90#define LIST_MASK ((LIST_EXEC << 1) - 1)
Eric Andersen11c65522000-09-07 17:24:47 +000091
92/* what files will be displayed */
Manuel Novoa III cad53642003-03-19 09:13:01 +000093#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000094#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
95#define DISP_DOT (1U<<17) /* show . and .. */
96#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
Manuel Novoa III cad53642003-03-19 09:13:01 +000097#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000098#define DISP_ROWS (1U<<20) /* print across rows */
Manuel Novoa III cad53642003-03-19 09:13:01 +000099
Rob Landley9947a242006-06-15 22:11:10 +0000100#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1))
Eric Andersen11c65522000-09-07 17:24:47 +0000101
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000102#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000103/* how will the files be sorted */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000104#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
106
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000107#define SORT_NAME 0 /* sort by file name */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108#define SORT_SIZE (1U<<28) /* sort by file size */
109#define SORT_ATIME (2U<<28) /* sort by last access time */
110#define SORT_CTIME (3U<<28) /* sort by last change time */
111#define SORT_MTIME (4U<<28) /* sort by last modification time */
112#define SORT_VERSION (5U<<28) /* sort by version */
113#define SORT_EXT (6U<<28) /* sort by file name extension */
114#define SORT_DIR (7U<<28) /* sort by file or directory */
115
116#define SORT_MASK (7U<<28)
Eric Andersencc8ed391999-10-05 16:24:54 +0000117#endif
118
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000119#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000120/* which of the three times will be used */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121#define TIME_MOD 0
122#define TIME_CHANGE (1U<<23)
123#define TIME_ACCESS (1U<<24)
124
125#define TIME_MASK (3U<<23)
126#endif
127
128#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
129#define FOLLOW_LINKS (1U<<25)
130#endif
131#ifdef CONFIG_FEATURE_HUMAN_READABLE
132#define LS_DISP_HR (1U<<26)
Eric Andersencc8ed391999-10-05 16:24:54 +0000133#endif
134
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000135#define LIST_SHORT (LIST_FILENAME)
136#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
137#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
138 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
139#define LIST_ILONG (LIST_INO | LIST_LONG)
Eric Andersencc8ed391999-10-05 16:24:54 +0000140
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141#define SPLIT_DIR 1
142#define SPLIT_FILE 0
143#define SPLIT_SUBDIR 2
Eric Andersencc8ed391999-10-05 16:24:54 +0000144
Eric Andersen11c65522000-09-07 17:24:47 +0000145#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
146#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000147
Eric Andersend598d412002-04-27 09:19:39 +0000148#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000149# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
Eric Andersen11c65522000-09-07 17:24:47 +0000150#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000151
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000152/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
153#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000154
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000155static int show_color = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000156
Paul Fox156dc412005-08-01 19:33:30 +0000157/* long option entry used only for --color, which has no short option
158 * equivalent. */
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000159static const struct option ls_color_opt[] =
Paul Fox156dc412005-08-01 19:33:30 +0000160{
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000161 {"color", optional_argument, NULL, 1},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000162 {NULL, 0, NULL, 0}
Paul Fox156dc412005-08-01 19:33:30 +0000163};
164
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000165#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000166 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000167#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000168 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000169#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170
Eric Andersen11c65522000-09-07 17:24:47 +0000171/*
172 * a directory entry and its stat info are stored here
173 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000174struct dnode { /* the basic node */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000175 char *name; /* the dir entry name */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000176 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000177 int allocated;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000178 struct stat dstat; /* the file stat info */
Eric Andersen9e480452003-07-03 10:07:04 +0000179#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000180 security_context_t sid;
Eric Andersen9e480452003-07-03 10:07:04 +0000181#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000182 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000183};
184typedef struct dnode dnode_t;
185
Glenn L McGrath4d001292003-01-06 01:11:50 +0000186static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000187static struct dnode **dnalloc(int);
188static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000189
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190static unsigned int all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000191
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000192#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000193static int terminal_width = TERMINAL_WIDTH;
Eric Andersene57d54b2001-01-30 18:03:11 +0000194static unsigned short tabstops = COLUMN_GAP;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000195#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000196#define tabstops COLUMN_GAP
197#define terminal_width TERMINAL_WIDTH
Eric Andersen11c65522000-09-07 17:24:47 +0000198#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000199
Matt Kraai33fdae52000-10-13 17:59:43 +0000200static int status = EXIT_SUCCESS;
201
Glenn L McGrath4d001292003-01-06 01:11:50 +0000202static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000203{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000204 struct stat dstat;
205 struct dnode *cur;
Eric Andersen9e480452003-07-03 10:07:04 +0000206#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000207 security_context_t sid=NULL;
Eric Andersen9e480452003-07-03 10:07:04 +0000208#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000209
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000210#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 if (all_fmt & FOLLOW_LINKS) {
Eric Andersen9e480452003-07-03 10:07:04 +0000212#ifdef CONFIG_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000213 if (is_selinux_enabled()) {
214 getfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000215 }
Eric Andersen9e480452003-07-03 10:07:04 +0000216#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000217 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000219 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000220 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000221 }
222 } else
223#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000224 {
225#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000226 if (is_selinux_enabled()) {
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000227 lgetfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000228 }
Eric Andersen9e480452003-07-03 10:07:04 +0000229#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000230 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000231 bb_perror_msg("%s", fullname);
232 status = EXIT_FAILURE;
233 return 0;
234 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000235 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000236
237 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
238 cur->fullname = fullname;
239 cur->name = name;
240 cur->dstat = dstat;
Eric Andersen9e480452003-07-03 10:07:04 +0000241#ifdef CONFIG_SELINUX
242 cur->sid = sid;
243#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000244 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000245}
246
Eric Andersen11c65522000-09-07 17:24:47 +0000247/*----------------------------------------------------------------------*/
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000248#ifdef CONFIG_FEATURE_LS_COLOR
249static char fgcolor(mode_t mode)
250{
251 /* Check wheter the file is existing (if so, color it red!) */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000252 if (errno == ENOENT) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000253 return '\037';
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000254 }
Rob Landley9947a242006-06-15 22:11:10 +0000255 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000256 return COLOR(0xF000); /* File is executable ... */
257 return COLOR(mode);
258}
259
260/*----------------------------------------------------------------------*/
261static char bgcolor(mode_t mode)
262{
Rob Landley9947a242006-06-15 22:11:10 +0000263 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000264 return ATTR(0xF000); /* File is executable ... */
265 return ATTR(mode);
266}
267#endif
268
269/*----------------------------------------------------------------------*/
Eric Andersend598d412002-04-27 09:19:39 +0000270#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000271static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000272{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000273 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000274 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000275 if (S_ISDIR(mode))
276 return '/';
277 if (!(all_fmt & LIST_EXEC))
278 return '\0';
279 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000280 return '*';
281 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000282}
283#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000284
Eric Andersen11c65522000-09-07 17:24:47 +0000285/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000286
Manuel Novoa III cad53642003-03-19 09:13:01 +0000287#define countdirs(A,B) count_dirs((A), (B), 1)
288#define countsubdirs(A,B) count_dirs((A), (B), 0)
289
290static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000291{
292 int i, dirs;
293
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000294 if (dn == NULL || nfiles < 1)
295 return (0);
296 dirs = 0;
297 for (i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 if (S_ISDIR(dn[i]->dstat.st_mode)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000299 && (notsubdirs ||
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000300 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
Manuel Novoa III cad53642003-03-19 09:13:01 +0000301 && ((dn[i]->name[1] != '.')
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000302 || dn[i]->name[2])))))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000303 dirs++;
Eric Andersen11c65522000-09-07 17:24:47 +0000304 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000305 return (dirs);
Eric Andersen11c65522000-09-07 17:24:47 +0000306}
307
Eric Andersen3e6ff902001-03-09 21:24:12 +0000308static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000309{
310 int nfiles;
311 struct dnode *cur;
312
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000313 if (dnp == NULL)
314 return (0);
315 nfiles = 0;
316 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
317 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000318 nfiles++;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000319 return (nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000320}
321
322/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000323static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000324{
325 struct dnode **p;
326
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000327 if (num < 1)
328 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000329
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000330 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000331 return (p);
Eric Andersen11c65522000-09-07 17:24:47 +0000332}
333
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000334#ifdef CONFIG_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000335static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000336{
Rob Landley26314862006-05-02 19:46:52 +0000337 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000338
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000339 if (dnp == NULL)
340 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000341
Rob Landley26314862006-05-02 19:46:52 +0000342 for (i = 0; i < nfiles; i++) {
343 struct dnode *cur = dnp[i];
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000344 if(cur->allocated)
345 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000346 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000347 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000348 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000349}
Rob Landleyc44bc982006-05-28 01:19:06 +0000350#else
351#define dfree(...)
Eric Andersen20aab262001-07-19 22:28:02 +0000352#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000353
Eric Andersen3e6ff902001-03-09 21:24:12 +0000354static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000355{
356 int dncnt, i, d;
357 struct dnode **dnp;
358
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000359 if (dn == NULL || nfiles < 1)
360 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000361
362 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000363 if (which == SPLIT_SUBDIR)
364 dncnt = countsubdirs(dn, nfiles);
365 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000366 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000367 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000368 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000369 }
Eric Andersen11c65522000-09-07 17:24:47 +0000370
371 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000372 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000373
374 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000375 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000376 if (S_ISDIR(dn[i]->dstat.st_mode)) {
377 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
378 if ((which & SPLIT_DIR)
379 || ((dn[i]->name[0] != '.')
380 || (dn[i]->name[1]
381 && ((dn[i]->name[1] != '.')
382 || dn[i]->name[2])))) {
383 dnp[d++] = dn[i];
384 }
385 }
386 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
387 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000388 }
389 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000390 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000391}
392
393/*----------------------------------------------------------------------*/
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000394#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000395static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000396{
Rob Landley425e7582006-05-03 20:22:03 +0000397 struct dnode *d1 = *(struct dnode **)a;
398 struct dnode *d2 = *(struct dnode **)b;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000399 unsigned int sort_opts = all_fmt & SORT_MASK;
400 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000401
Manuel Novoa III cad53642003-03-19 09:13:01 +0000402 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000403 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000404 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000405 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000406 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000407 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000408 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000409 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000410 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000411 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000412 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000413 /* } else if (sort_opts == SORT_VERSION) { */
414 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000415 }
416
Eric Andersen11c65522000-09-07 17:24:47 +0000417 if (dif == 0) {
418 /* sort by name- may be a tie_breaker for time or size cmp */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000419#ifdef CONFIG_LOCALE_SUPPORT
420 dif = strcoll(d1->name, d2->name);
421#else
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000422 dif = strcmp(d1->name, d2->name);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000423#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000424 }
425
Manuel Novoa III cad53642003-03-19 09:13:01 +0000426 if (all_fmt & SORT_ORDER_REVERSE) {
427 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000428 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000429 return (dif);
Eric Andersen11c65522000-09-07 17:24:47 +0000430}
431
432/*----------------------------------------------------------------------*/
Rob Landley425e7582006-05-03 20:22:03 +0000433static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000434{
Rob Landley425e7582006-05-03 20:22:03 +0000435 qsort(dn, size, sizeof *dn, sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000436}
437#endif
438
439/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000440static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000441{
442 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000443 int column = 0;
444 int nexttab = 0;
445 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000446
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000447 if (dn == NULL || nfiles < 1)
448 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000449
Manuel Novoa III cad53642003-03-19 09:13:01 +0000450 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000451 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000452 } else {
453 /* find the longest file name- use that as the column width */
454 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000455 int len = strlen(dn[i]->name) +
Eric Andersen9e480452003-07-03 10:07:04 +0000456#ifdef CONFIG_SELINUX
457 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
458#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000459 ((all_fmt & LIST_INO) ? 8 : 0) +
460 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
461 if (column_width < len)
462 column_width = len;
463 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000464 column_width += tabstops;
465 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000466 }
467
Eric Andersene57d54b2001-01-30 18:03:11 +0000468 if (ncols > 1) {
469 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000470 if ((nrows * ncols) < nfiles)
471 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000472 } else {
473 nrows = nfiles;
474 ncols = 1;
475 }
Eric Andersen11c65522000-09-07 17:24:47 +0000476
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000477 for (row = 0; row < nrows; row++) {
478 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000479 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000480 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000481 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000482 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000483 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000484 if (column > 0) {
485 nexttab -= column;
486 while (nexttab--) {
487 putchar(' ');
488 column++;
489 }
Eric Andersen79565b62000-08-11 18:10:21 +0000490 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000491 nexttab = column + column_width;
492 column += list_single(dn[i]);
Eric Andersen79565b62000-08-11 18:10:21 +0000493 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000494 }
495 putchar('\n');
496 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000497 }
Eric Andersen11c65522000-09-07 17:24:47 +0000498}
499
500/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000501static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000502{
503 int i, nfiles;
504 struct dnode **subdnp;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000505
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000506#ifdef CONFIG_FEATURE_LS_RECURSIVE
Eric Andersen11c65522000-09-07 17:24:47 +0000507 int dndirs;
508 struct dnode **dnd;
509#endif
510
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000511 if (dn == NULL || ndirs < 1)
512 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000513
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000514 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000515 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000516 if (!first)
517 printf("\n");
518 first = 0;
519 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000520 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000521 subdnp = list_dir(dn[i]->fullname);
522 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000523 if (nfiles > 0) {
524 /* list all files at this level */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000525#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000526 dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000527#endif
528 showfiles(subdnp, nfiles);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000529#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000530 if (all_fmt & DISP_RECURSIVE) {
Eric Andersen11c65522000-09-07 17:24:47 +0000531 /* recursive- list the sub-dirs */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000532 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
533 dndirs = countsubdirs(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000534 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000535#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000536 dnsort(dnd, dndirs);
Matt Kraaia5bd2682000-10-28 06:40:09 +0000537#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000538 showdirs(dnd, dndirs, 0);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000539 free(dnd); /* free the array of dnode pointers to the dirs */
Eric Andersen11c65522000-09-07 17:24:47 +0000540 }
541 }
Rob Landley26314862006-05-02 19:46:52 +0000542 dfree(subdnp, nfiles); /* free the dnodes and the fullname mem */
Eric Andersen11c65522000-09-07 17:24:47 +0000543#endif
544 }
545 }
546}
547
548/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000549static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000550{
551 struct dnode *dn, *cur, **dnp;
552 struct dirent *entry;
553 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000554 int i, nfiles;
555
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000556 if (path == NULL)
557 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000558
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000559 dn = NULL;
560 nfiles = 0;
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +0000561 dir = bb_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000562 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000563 status = EXIT_FAILURE;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000564 return (NULL); /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000565 }
566 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000567 char *fullname;
568
Eric Andersen11c65522000-09-07 17:24:47 +0000569 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000570 if (entry->d_name[0] == '.') {
571 if ((entry->d_name[1] == 0 || (
572 entry->d_name[1] == '.'
573 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000574 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000575 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000576 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000577 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000578 }
579 fullname = concat_path_file(path, entry->d_name);
580 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
581 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000582 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000583 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000584 cur->next = dn;
585 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000586 nfiles++;
587 }
588 closedir(dir);
589
590 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000591 ** allocate memory for an array to hold dnode pointers
592 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000593 if (dn == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000594 return (NULL);
595 dnp = dnalloc(nfiles);
596 for (i = 0, cur = dn; i < nfiles; i++) {
597 dnp[i] = cur; /* save pointer to node in array */
598 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000599 }
600
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000601 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000602}
603
604/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000605static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000606{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000607 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000608
Matt Kraaia1bbde72002-03-08 16:25:33 +0000609#ifdef CONFIG_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000610 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000611#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000612#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000613 char *filetime;
614 time_t ttime, age;
615#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000616#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000617 struct stat info;
618 char append;
619#endif
620
Glenn L McGrath4d001292003-01-06 01:11:50 +0000621 if (dn->fullname == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000622 return (0);
Eric Andersen11c65522000-09-07 17:24:47 +0000623
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000624#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000625 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000626 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000627 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000628 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000629 ttime = dn->dstat.st_ctime;
630 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000631#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000632#ifdef CONFIG_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000633 append = append_char(dn->dstat.st_mode);
634#endif
635
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000636 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000637 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000638 case LIST_INO:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000639 column += printf("%7ld ", (long int) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000640 break;
641 case LIST_BLOCKS:
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000642#if _FILE_OFFSET_BITS == 64
Eric Andersen5e678872006-01-30 19:48:23 +0000643 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000644#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000645 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000646#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000647 break;
648 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000649 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000650 break;
651 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000652 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000653 break;
654 case LIST_ID_NAME:
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000655#ifdef CONFIG_FEATURE_LS_USERNAME
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000656 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000657 printf("%-8.8s ", scratch);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000658 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000659 printf("%-8.8s", scratch);
660 column += 17;
661 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000662#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000663 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000664 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000665 break;
666 case LIST_SIZE:
667 case LIST_DEV:
668 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000669 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
670 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000671 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000672#ifdef CONFIG_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000673 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000674 column += printf("%9s ",
675 make_human_readable_str(dn->dstat.st_size, 1, 0));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000676 } else
677#endif
678 {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000679#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000680 column += printf("%9lld ", (long long) dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000681#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000682 column += printf("%9ld ", dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000683#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000684 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000685 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000686 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000687#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000688 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000689 printf("%24.24s ", filetime);
690 column += 25;
691 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000692 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000693 if ((all_fmt & LIST_FULLTIME) == 0) {
694 age = time(NULL) - ttime;
695 printf("%6.6s ", filetime + 4);
696 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
697 /* hh:mm if less than 6 months old */
698 printf("%5.5s ", filetime + 11);
699 } else {
700 printf(" %4.4s ", filetime + 20);
701 }
702 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000703 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000704 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000705#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000706#ifdef CONFIG_SELINUX
707 case LIST_CONTEXT:
708 {
Rob Landley60158cb2005-05-03 06:25:50 +0000709 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000710 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000711
Rob Landley60158cb2005-05-03 06:25:50 +0000712 if (dn->sid) {
713 /* I assume sid initilized with NULL */
714 len = strlen(dn->sid)+1;
715 safe_strncpy(context, dn->sid, len);
716 freecon(dn->sid);
717 }else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000718 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000719 }
720 printf("%-32s ", context);
721 column += MAX(33, len);
722 }
723 break;
724#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000725 case LIST_FILENAME:
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000726#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000727 errno = 0;
728 if (show_color && !lstat(dn->fullname, &info)) {
729 printf("\033[%d;%dm", bgcolor(info.st_mode),
730 fgcolor(info.st_mode));
731 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000732#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000733 column += printf("%s", dn->name);
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000734#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000735 if (show_color) {
736 printf("\033[0m");
737 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000738#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000739 break;
740 case LIST_SYMLINK:
741 if (S_ISLNK(dn->dstat.st_mode)) {
742 char *lpath = xreadlink(dn->fullname);
743
744 if (lpath) {
745 printf(" -> ");
746#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
747 if (!stat(dn->fullname, &info)) {
748 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000749 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000750#endif
751#ifdef CONFIG_FEATURE_LS_COLOR
752 if (show_color) {
753 errno = 0;
754 printf("\033[%d;%dm", bgcolor(info.st_mode),
755 fgcolor(info.st_mode));
756 }
757#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000758 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000759#ifdef CONFIG_FEATURE_LS_COLOR
760 if (show_color) {
761 printf("\033[0m");
762 }
763#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000764 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000765 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000766 }
767 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000768#ifdef CONFIG_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000769 case LIST_FILETYPE:
770 if (append != '\0') {
771 printf("%1c", append);
772 column++;
773 }
774 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000775#endif
776 }
777 }
778
Glenn L McGrath4d001292003-01-06 01:11:50 +0000779 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000780}
781
782/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000783
Glenn L McGrath792cae52004-01-18 05:15:16 +0000784/* "[-]Cadil1", POSIX mandated options, busybox always supports */
785/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
786/* "[-]Ak" GNU options, busybox always supports */
787/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
788/* "[-]p", POSIX non-mandated options, busybox optionally supports */
789/* "[-]SXvThw", GNU options, busybox optionally supports */
790/* "[-]K", SELinux mandated options, busybox optionally supports */
791/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000792
Glenn L McGrath792cae52004-01-18 05:15:16 +0000793#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
794# define LS_STR_TIMESTAMPS "cetu"
795#else
796# define LS_STR_TIMESTAMPS ""
797#endif
798
799#ifdef CONFIG_FEATURE_LS_SORTFILES
800# define LS_STR_SORTFILES "SXrv"
801#else
802# define LS_STR_SORTFILES ""
803#endif
804
805#ifdef CONFIG_FEATURE_LS_FILETYPES
806# define LS_STR_FILETYPES "Fp"
807#else
808# define LS_STR_FILETYPES ""
809#endif
810
811#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
812# define LS_STR_FOLLOW_LINKS "L"
813#else
814# define LS_STR_FOLLOW_LINKS ""
815#endif
816
817#ifdef CONFIG_FEATURE_LS_RECURSIVE
818# define LS_STR_RECURSIVE "R"
819#else
820# define LS_STR_RECURSIVE ""
821#endif
822
823#ifdef CONFIG_FEATURE_HUMAN_READABLE
824# define LS_STR_HUMAN_READABLE "h"
825#else
826# define LS_STR_HUMAN_READABLE ""
827#endif
828
829#ifdef CONFIG_SELINUX
830# define LS_STR_SELINUX "K"
831#else
832# define LS_STR_SELINUX ""
833#endif
834
835#ifdef CONFIG_FEATURE_AUTOWIDTH
836# define LS_STR_AUTOWIDTH "T:w:"
837#else
838# define LS_STR_AUTOWIDTH ""
839#endif
840
841static const char ls_options[]="Cadil1gnsxAk" \
842 LS_STR_TIMESTAMPS \
843 LS_STR_SORTFILES \
844 LS_STR_FILETYPES \
845 LS_STR_FOLLOW_LINKS \
846 LS_STR_RECURSIVE \
847 LS_STR_HUMAN_READABLE \
848 LS_STR_SELINUX \
849 LS_STR_AUTOWIDTH;
850
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000851#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000852#define STYLE_MASK_TRIGGER STYLE_MASK
853#define SORT_MASK_TRIGGER SORT_MASK
854#define DISP_MASK_TRIGGER DISP_ROWS
855#define TIME_MASK_TRIGGER TIME_MASK
Manuel Novoa III cad53642003-03-19 09:13:01 +0000856
857static const unsigned opt_flags[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000858 LIST_SHORT | STYLE_COLUMNS, /* C */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000859 DISP_HIDDEN | DISP_DOT, /* a */
860 DISP_NOLIST, /* d */
861 LIST_INO, /* i */
862 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
863 LIST_SHORT | STYLE_SINGLE, /* 1 */
864 0, /* g - ingored */
865 LIST_ID_NUMERIC, /* n */
866 LIST_BLOCKS, /* s */
867 DISP_ROWS, /* x */
868 DISP_HIDDEN, /* A */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000869#ifdef CONFIG_SELINUX
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000870 LIST_CONTEXT, /* k */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000871#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000872 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000873#endif
874#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrath792cae52004-01-18 05:15:16 +0000875# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000876 TIME_CHANGE | SORT_CTIME, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000877# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000878 TIME_CHANGE, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000879# endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000880 LIST_FULLTIME, /* e */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000881# ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000882 SORT_MTIME, /* t */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000883# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000884 0, /* t - ignored -- is this correct? */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000885# endif
886# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000887 TIME_ACCESS | SORT_ATIME, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000888# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000889 TIME_ACCESS, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000890# endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000891#endif
892#ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000893 SORT_SIZE, /* S */
894 SORT_EXT, /* X */
895 SORT_ORDER_REVERSE, /* r */
896 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000897#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000898#ifdef CONFIG_FEATURE_LS_FILETYPES
899 LIST_FILETYPE | LIST_EXEC, /* F */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000900 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000901#endif
902#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000903 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000904#endif
905#ifdef CONFIG_FEATURE_LS_RECURSIVE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000906 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000907#endif
908#ifdef CONFIG_FEATURE_HUMAN_READABLE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000909 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000910#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000911#ifdef CONFIG_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000912 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
913#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000914#ifdef CONFIG_FEATURE_AUTOWIDTH
915 0, 0, /* T, w - ignored */
916#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000917 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000918};
919
920
921/*----------------------------------------------------------------------*/
922
Rob Landleydfba7412006-03-06 20:47:33 +0000923int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000924{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000925 struct dnode **dnd;
926 struct dnode **dnf;
927 struct dnode **dnp;
928 struct dnode *dn;
929 struct dnode *cur;
930 long opt;
931 int nfiles = 0;
932 int dnfiles;
933 int dndirs;
934 int oi;
935 int ac;
936 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000937 char **av;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000938#ifdef CONFIG_FEATURE_AUTOWIDTH
939 char *tabstops_str = NULL;
940 char *terminal_width_str = NULL;
941#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000942#ifdef CONFIG_FEATURE_LS_COLOR
943 char *color_opt;
944#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000945
Rob Landley9947a242006-06-15 22:11:10 +0000946 all_fmt = LIST_SHORT | STYLE_AUTO
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000947#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000948 | TIME_MOD
Eric Andersen11c65522000-09-07 17:24:47 +0000949#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000950#ifdef CONFIG_FEATURE_LS_SORTFILES
951 | SORT_NAME | SORT_ORDER_FORWARD
952#endif
953 ;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000954
Eric Andersen6d687812003-11-04 23:16:48 +0000955#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000956 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000957 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000958 /* Go one less... */
959 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000960#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000961
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000962#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000963 bb_applet_long_options = ls_color_opt;
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000964#endif
965
Eric Andersen11c65522000-09-07 17:24:47 +0000966 /* process options */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000967#ifdef CONFIG_FEATURE_AUTOWIDTH
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000968 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
969#ifdef CONFIG_FEATURE_LS_COLOR
970 , &color_opt
971#endif
972 );
Glenn L McGrath792cae52004-01-18 05:15:16 +0000973 if (tabstops_str) {
974 tabstops = atoi(tabstops_str);
975 }
976 if (terminal_width_str) {
977 terminal_width = atoi(terminal_width_str);
978 }
979#else
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000980 opt = bb_getopt_ulflags(argc, argv, ls_options
981#ifdef CONFIG_FEATURE_LS_COLOR
982 , &color_opt
983#endif
984 );
Manuel Novoa III cad53642003-03-19 09:13:01 +0000985#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000986 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000987 if (opt & (1 << i)) {
988 unsigned int flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000989
Manuel Novoa III cad53642003-03-19 09:13:01 +0000990 if (flags & LIST_MASK_TRIGGER) {
991 all_fmt &= ~LIST_MASK;
992 }
993 if (flags & STYLE_MASK_TRIGGER) {
994 all_fmt &= ~STYLE_MASK;
995 }
Eric Andersenb7ebc612003-07-14 19:20:46 +0000996#ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000997 if (flags & SORT_MASK_TRIGGER) {
998 all_fmt &= ~SORT_MASK;
999 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001000#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001001 if (flags & DISP_MASK_TRIGGER) {
1002 all_fmt &= ~DISP_MASK;
1003 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001004#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +00001005 if (flags & TIME_MASK_TRIGGER) {
1006 all_fmt &= ~TIME_MASK;
1007 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001008#endif
Eric Andersen9e480452003-07-03 10:07:04 +00001009 if (flags & LIST_CONTEXT) {
1010 all_fmt |= STYLE_SINGLE;
1011 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001012#ifdef CONFIG_FEATURE_HUMAN_READABLE
1013 if (opt == 'l') {
1014 all_fmt &= ~LS_DISP_HR;
1015 }
1016#endif
1017 all_fmt |= flags;
1018 }
Eric Andersen11c65522000-09-07 17:24:47 +00001019 }
1020
Paul Fox156dc412005-08-01 19:33:30 +00001021#ifdef CONFIG_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001022 {
1023 /* find color bit value - last position for short getopt */
Paul Fox156dc412005-08-01 19:33:30 +00001024
Paul Fox156dc412005-08-01 19:33:30 +00001025#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
Paul Fox156dc412005-08-01 19:33:30 +00001026 char *p;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001027
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001028 if ((p = getenv ("LS_COLORS")) != NULL &&
Paul Fox156dc412005-08-01 19:33:30 +00001029 (*p == '\0' || (strcmp(p, "none") == 0))) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001030 ;
Paul Fox156dc412005-08-01 19:33:30 +00001031 } else if (isatty(STDOUT_FILENO)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001032 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +00001033 }
1034#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001035
1036 if((opt & (1 << i))) { /* next flag after short options */
1037 if (color_opt == NULL || strcmp("always", color_opt) == 0)
1038 show_color = 1;
1039 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
1040 show_color = 0;
1041 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
1042 show_color = 1;
1043 }
Paul Fox156dc412005-08-01 19:33:30 +00001044 }
1045#endif
1046
Eric Andersen11c65522000-09-07 17:24:47 +00001047 /* sort out which command line options take precedence */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001048#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +00001049 if (all_fmt & DISP_NOLIST)
1050 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Eric Andersen11c65522000-09-07 17:24:47 +00001051#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001052#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001053 if (all_fmt & TIME_CHANGE)
1054 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1055 if (all_fmt & TIME_ACCESS)
1056 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
Eric Andersen11c65522000-09-07 17:24:47 +00001057#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001058 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1059 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001060#ifdef CONFIG_FEATURE_LS_USERNAME
Manuel Novoa III cad53642003-03-19 09:13:01 +00001061 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1062 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersen11c65522000-09-07 17:24:47 +00001063#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001064
Eric Andersen11c65522000-09-07 17:24:47 +00001065 /* choose a display format */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001066 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
1067#if STYLE_AUTO != 0
1068 all_fmt = (all_fmt & ~STYLE_MASK)
Eric Andersen70060d22004-03-27 10:02:48 +00001069 | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001070#else
Eric Andersen70060d22004-03-27 10:02:48 +00001071 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001072#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001073
1074 /*
1075 * when there are no cmd line args we have to supply a default "." arg.
1076 * we will create a second argv array, "av" that will hold either
1077 * our created "." arg, or the real cmd line args. The av array
1078 * just holds the pointers- we don't move the date the pointers
1079 * point to.
1080 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001081 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +00001082 if (ac < 1) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001083 static const char * const dotdir[] = { "." };
1084
1085 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001086 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +00001087 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001088 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +00001089 }
1090
1091 /* now, everything is in the av array */
1092 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001093 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001094
1095 /* stuff the command line file names into an dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001096 dn = NULL;
1097 for (oi = 0; oi < ac; oi++) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001098 cur = my_stat(av[oi], av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001099 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001100 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001101 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001102 cur->next = dn;
1103 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001104 nfiles++;
1105 }
1106
1107 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001108 ** allocate memory for an array to hold dnode pointers
1109 */
1110 dnp = dnalloc(nfiles);
1111 for (i = 0, cur = dn; i < nfiles; i++) {
1112 dnp[i] = cur; /* save pointer to node in array */
1113 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +00001114 }
1115
Manuel Novoa III cad53642003-03-19 09:13:01 +00001116 if (all_fmt & DISP_NOLIST) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001117#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +00001118 dnsort(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001119#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001120 if (nfiles > 0)
1121 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001122 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001123 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1124 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1125 dndirs = countdirs(dnp, nfiles);
1126 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001127 if (dnfiles > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001128#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +00001129 dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001130#endif
1131 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +00001132 if (ENABLE_FEATURE_CLEAN_UP)
1133 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +00001134 }
1135 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001136#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +00001137 dnsort(dnd, dndirs);
Eric Andersen11c65522000-09-07 17:24:47 +00001138#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +00001139 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +00001140 if (ENABLE_FEATURE_CLEAN_UP)
1141 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +00001142 }
1143 }
Rob Landley26314862006-05-02 19:46:52 +00001144 if (ENABLE_FEATURE_CLEAN_UP)
1145 dfree(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001146 return (status);
Eric Andersencc8ed391999-10-05 16:24:54 +00001147}