blob: 2e68eb8481061ab1faa2ce9a1e5b6d0e90980792 [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
Rob Landleyea224be2006-06-18 20:20:07 +000032#include "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000033#include <getopt.h>
Eric Andersenf1142c52001-02-20 06:16:29 +000034
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000035enum {
Manuel Novoa III cad53642003-03-19 09:13:01 +000036
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000037TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
38COLUMN_GAP = 2, /* includes the file type char */
39
40/* what is the overall style of the listing */
41STYLE_COLUMNS = 1 << 21, /* fill columns */
42STYLE_LONG = 2 << 21, /* one record per line, extended info */
43STYLE_SINGLE = 3 << 21, /* one record per line */
44STYLE_MASK = STYLE_SINGLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersen11c65522000-09-07 17:24:47 +000046/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
47/* what file information will be listed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000048LIST_INO = 1 << 0,
49LIST_BLOCKS = 1 << 1,
50LIST_MODEBITS = 1 << 2,
51LIST_NLINKS = 1 << 3,
52LIST_ID_NAME = 1 << 4,
53LIST_ID_NUMERIC = 1 << 5,
54LIST_CONTEXT = 1 << 6,
55LIST_SIZE = 1 << 7,
56LIST_DEV = 1 << 8,
57LIST_DATE_TIME = 1 << 9,
58LIST_FULLTIME = 1 << 10,
59LIST_FILENAME = 1 << 11,
60LIST_SYMLINK = 1 << 12,
61LIST_FILETYPE = 1 << 13,
62LIST_EXEC = 1 << 14,
63LIST_MASK = (LIST_EXEC << 1) - 1,
Eric Andersen11c65522000-09-07 17:24:47 +000064
65/* what files will be displayed */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000066DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
Denis Vlasenko656f7462006-10-28 13:02:55 +000067DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000068DISP_DOT = 1 << 17, /* show . and .. */
69DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
70DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
71DISP_ROWS = 1 << 20, /* print across rows */
72DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
Manuel Novoa III cad53642003-03-19 09:13:01 +000073
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000074/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
75SORT_FORWARD = 0, /* sort in reverse order */
76SORT_REVERSE = 1 << 27, /* sort in reverse order */
Eric Andersen11c65522000-09-07 17:24:47 +000077
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000078SORT_NAME = 0, /* sort by file name */
79SORT_SIZE = 1 << 28, /* sort by file size */
80SORT_ATIME = 2 << 28, /* sort by last access time */
81SORT_CTIME = 3 << 28, /* sort by last change time */
82SORT_MTIME = 4 << 28, /* sort by last modification time */
83SORT_VERSION = 5 << 28, /* sort by version */
84SORT_EXT = 6 << 28, /* sort by file name extension */
85SORT_DIR = 7 << 28, /* sort by file or directory */
86SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Eric Andersen11c65522000-09-07 17:24:47 +000088/* which of the three times will be used */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000089TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
90TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
91TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
Manuel Novoa III cad53642003-03-19 09:13:01 +000092
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000093FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
Eric Andersencc8ed391999-10-05 16:24:54 +000094
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000095LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
Eric Andersencc8ed391999-10-05 16:24:54 +000096
Denis Vlasenko94cf69f2006-10-28 12:37:51 +000097LIST_SHORT = LIST_FILENAME,
98LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
99 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
100
101SPLIT_DIR = 1,
102SPLIT_FILE = 0,
103SPLIT_SUBDIR = 2,
104
105};
Eric Andersencc8ed391999-10-05 16:24:54 +0000106
Eric Andersen11c65522000-09-07 17:24:47 +0000107#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
108#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000109#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
110#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
111 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
112#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
113 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000114
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000115/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000116#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenko5c759602006-10-28 12:37:16 +0000117static int show_color;
Paul Fox156dc412005-08-01 19:33:30 +0000118/* long option entry used only for --color, which has no short option
Denis Vlasenko656f7462006-10-28 13:02:55 +0000119 * equivalent */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000120static const struct option ls_color_opt[] = {
121 { "color", optional_argument, NULL, 1 },
122 { NULL, 0, NULL, 0 }
Paul Fox156dc412005-08-01 19:33:30 +0000123};
Denis Vlasenko5c759602006-10-28 12:37:16 +0000124#else
125enum { show_color = 0 };
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000126#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127
Eric Andersen11c65522000-09-07 17:24:47 +0000128/*
129 * a directory entry and its stat info are stored here
130 */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000131struct dnode { /* the basic node */
132 char *name; /* the dir entry name */
133 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000134 int allocated;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000135 struct stat dstat; /* the file stat info */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000136 USE_SELINUX(security_context_t sid;)
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000137 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000138};
139typedef struct dnode dnode_t;
140
Glenn L McGrath4d001292003-01-06 01:11:50 +0000141static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000142static struct dnode **dnalloc(int);
143static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000144
Denis Vlasenko5c759602006-10-28 12:37:16 +0000145static unsigned all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000146
Denis Vlasenko5c759602006-10-28 12:37:16 +0000147#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko13858992006-10-08 12:49:22 +0000148static unsigned tabstops = COLUMN_GAP;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000149static unsigned terminal_width = TERMINAL_WIDTH;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000150#else
Denis Vlasenko5c759602006-10-28 12:37:16 +0000151enum {
152 tabstops = COLUMN_GAP,
153 terminal_width = TERMINAL_WIDTH,
154};
Eric Andersen11c65522000-09-07 17:24:47 +0000155#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000156
Matt Kraai33fdae52000-10-13 17:59:43 +0000157static int status = EXIT_SUCCESS;
158
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000159static struct dnode *my_stat(char *fullname, char *name, int force_follow)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000160{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000161 struct stat dstat;
162 struct dnode *cur;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000163 USE_SELINUX(security_context_t sid = NULL;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000164
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000165 if ((all_fmt & FOLLOW_LINKS) || force_follow) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000166#if ENABLE_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000167 if (is_selinux_enabled()) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000168 getfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000169 }
Eric Andersen9e480452003-07-03 10:07:04 +0000170#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000171 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000173 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000174 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000175 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000176 } else {
177#if ENABLE_SELINUX
178 if (is_selinux_enabled()) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000179 lgetfilecon(fullname, &sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000180 }
Eric Andersen9e480452003-07-03 10:07:04 +0000181#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000182 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000183 bb_perror_msg("%s", fullname);
184 status = EXIT_FAILURE;
185 return 0;
186 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000187 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000188
Denis Vlasenko5c759602006-10-28 12:37:16 +0000189 cur = xmalloc(sizeof(struct dnode));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000190 cur->fullname = fullname;
191 cur->name = name;
192 cur->dstat = dstat;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000193 USE_SELINUX(cur->sid = sid;)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000194 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000195}
196
Denis Vlasenko5c759602006-10-28 12:37:16 +0000197#if ENABLE_FEATURE_LS_COLOR
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000198static char fgcolor(mode_t mode)
199{
200 /* Check wheter the file is existing (if so, color it red!) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000201 if (errno == ENOENT)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000202 return '\037';
Rob Landley9947a242006-06-15 22:11:10 +0000203 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000204 return COLOR(0xF000); /* File is executable ... */
205 return COLOR(mode);
206}
207
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000208static char bgcolor(mode_t mode)
209{
Rob Landley9947a242006-06-15 22:11:10 +0000210 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000211 return ATTR(0xF000); /* File is executable ... */
212 return ATTR(mode);
213}
214#endif
215
Denis Vlasenko5c759602006-10-28 12:37:16 +0000216#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000217static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000218{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000220 return '\0';
Rob Landley9947a242006-06-15 22:11:10 +0000221 if (S_ISDIR(mode))
222 return '/';
223 if (!(all_fmt & LIST_EXEC))
224 return '\0';
225 if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000226 return '*';
227 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000228}
229#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000230
Denis Vlasenko5c759602006-10-28 12:37:16 +0000231#define countdirs(A, B) count_dirs((A), (B), 1)
232#define countsubdirs(A, B) count_dirs((A), (B), 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000234{
235 int i, dirs;
236
Denis Vlasenko5c759602006-10-28 12:37:16 +0000237 if (!dn)
238 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000239 dirs = 0;
240 for (i = 0; i < nfiles; i++) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000241 char *name;
242 if (!S_ISDIR(dn[i]->dstat.st_mode))
243 continue;
244 name = dn[i]->name;
245 if (notsubdirs
246 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
247 ) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000248 dirs++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000249 }
Eric Andersen11c65522000-09-07 17:24:47 +0000250 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000251 return dirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000252}
253
Eric Andersen3e6ff902001-03-09 21:24:12 +0000254static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000255{
256 int nfiles;
257 struct dnode *cur;
258
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000259 if (dnp == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000260 return 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000261 nfiles = 0;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000262 for (cur = dnp[0]; cur->next; cur = cur->next)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000263 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000264 nfiles++;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000265 return nfiles;
Eric Andersen11c65522000-09-07 17:24:47 +0000266}
267
268/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000269static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000270{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000271 if (num < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000272 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000273
Denis Vlasenko5c759602006-10-28 12:37:16 +0000274 return xzalloc(num * sizeof(struct dnode *));
Eric Andersen11c65522000-09-07 17:24:47 +0000275}
276
Denis Vlasenko5c759602006-10-28 12:37:16 +0000277#if ENABLE_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000278static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000279{
Rob Landley26314862006-05-02 19:46:52 +0000280 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000281
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000282 if (dnp == NULL)
283 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000284
Rob Landley26314862006-05-02 19:46:52 +0000285 for (i = 0; i < nfiles; i++) {
286 struct dnode *cur = dnp[i];
Denis Vlasenko5c759602006-10-28 12:37:16 +0000287 if (cur->allocated)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000288 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000289 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000290 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000291 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000292}
Rob Landleyc44bc982006-05-28 01:19:06 +0000293#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000294#define dfree(...) ((void)0)
Eric Andersen20aab262001-07-19 22:28:02 +0000295#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000296
Eric Andersen3e6ff902001-03-09 21:24:12 +0000297static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000298{
299 int dncnt, i, d;
300 struct dnode **dnp;
301
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000302 if (dn == NULL || nfiles < 1)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000303 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000304
305 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000306 if (which == SPLIT_SUBDIR)
307 dncnt = countsubdirs(dn, nfiles);
308 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000309 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000310 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000311 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000312 }
Eric Andersen11c65522000-09-07 17:24:47 +0000313
314 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000315 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000316
317 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000318 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000319 if (S_ISDIR(dn[i]->dstat.st_mode)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000320 char *name;
321 if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
322 continue;
323 name = dn[i]->name;
324 if ((which & SPLIT_DIR)
325 || name[0]!='.' || (name[1] && (name[1]!='.' || name[2]))
326 ) {
327 dnp[d++] = dn[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000328 }
329 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
330 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000331 }
332 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000333 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000334}
335
Denis Vlasenko5c759602006-10-28 12:37:16 +0000336#if ENABLE_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000337static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000338{
Rob Landley425e7582006-05-03 20:22:03 +0000339 struct dnode *d1 = *(struct dnode **)a;
340 struct dnode *d2 = *(struct dnode **)b;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000341 unsigned sort_opts = all_fmt & SORT_MASK;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000342 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000343
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000344 dif = 0; /* assume SORT_NAME */
345 // TODO: use pre-initialized function pointer
346 // instead of branch forest
Eric Andersen11c65522000-09-07 17:24:47 +0000347 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000348 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000349 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000350 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000351 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000352 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000353 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000354 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000355 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000356 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000357 /* } else if (sort_opts == SORT_VERSION) { */
358 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000359 }
360
Eric Andersen11c65522000-09-07 17:24:47 +0000361 if (dif == 0) {
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000362 /* sort by name - may be a tie_breaker for time or size cmp */
Rob Landleyea224be2006-06-18 20:20:07 +0000363 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name);
364 else dif = strcmp(d1->name, d2->name);
Eric Andersen11c65522000-09-07 17:24:47 +0000365 }
366
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000367 if (all_fmt & SORT_REVERSE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000368 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000369 }
Denis Vlasenko5c759602006-10-28 12:37:16 +0000370 return dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000371}
372
Rob Landley425e7582006-05-03 20:22:03 +0000373static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000374{
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000375 qsort(dn, size, sizeof(*dn), sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000376}
Rob Landleyea224be2006-06-18 20:20:07 +0000377#else
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000378#define dnsort(dn, size) ((void)0)
Eric Andersen11c65522000-09-07 17:24:47 +0000379#endif
380
Rob Landleyea224be2006-06-18 20:20:07 +0000381
Eric Andersen3e6ff902001-03-09 21:24:12 +0000382static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000383{
384 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000385 int column = 0;
386 int nexttab = 0;
387 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000388
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000389 if (dn == NULL || nfiles < 1)
390 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000391
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000392 if (all_fmt & STYLE_LONG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000393 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000394 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000395 /* find the longest file name, use that as the column width */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000396 for (i = 0; i < nfiles; i++) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000397 int len = strlen(dn[i]->name);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000398 if (column_width < len)
399 column_width = len;
400 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000401 column_width += tabstops +
402 USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
403 ((all_fmt & LIST_INO) ? 8 : 0) +
404 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000405 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000406 }
407
Eric Andersene57d54b2001-01-30 18:03:11 +0000408 if (ncols > 1) {
409 nrows = nfiles / ncols;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000410 if (nrows * ncols < nfiles)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000411 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000412 } else {
413 nrows = nfiles;
414 ncols = 1;
415 }
Eric Andersen11c65522000-09-07 17:24:47 +0000416
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000417 for (row = 0; row < nrows; row++) {
418 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000419 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000420 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000421 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000422 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000423 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000424 if (column > 0) {
425 nexttab -= column;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000426 printf("%*s", nexttab, "");
427 column += nexttab;
428 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000429 nexttab = column + column_width;
430 column += list_single(dn[i]);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000431 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000432 }
433 putchar('\n');
434 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000435 }
Eric Andersen11c65522000-09-07 17:24:47 +0000436}
437
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000438
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000439static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000440{
441 int i, nfiles;
442 struct dnode **subdnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000443 int dndirs;
444 struct dnode **dnd;
Eric Andersen11c65522000-09-07 17:24:47 +0000445
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000446 if (dn == NULL || ndirs < 1)
447 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000448
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000449 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000450 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000451 if (!first)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000452 puts("");
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000453 first = 0;
454 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000455 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000456 subdnp = list_dir(dn[i]->fullname);
457 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000458 if (nfiles > 0) {
459 /* list all files at this level */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000460 dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000461 showfiles(subdnp, nfiles);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000462 if (ENABLE_FEATURE_LS_RECURSIVE) {
463 if (all_fmt & DISP_RECURSIVE) {
464 /* recursive- list the sub-dirs */
465 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
466 dndirs = countsubdirs(subdnp, nfiles);
467 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000468 dnsort(dnd, dndirs);
Rob Landley2b8a05a2006-06-20 17:43:01 +0000469 showdirs(dnd, dndirs, 0);
470 /* free the array of dnode pointers to the dirs */
471 free(dnd);
472 }
Eric Andersen11c65522000-09-07 17:24:47 +0000473 }
Rob Landley2b8a05a2006-06-20 17:43:01 +0000474 /* free the dnodes and the fullname mem */
475 dfree(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000476 }
Eric Andersen11c65522000-09-07 17:24:47 +0000477 }
478 }
479}
480
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000481
Glenn L McGrath4d001292003-01-06 01:11:50 +0000482static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000483{
484 struct dnode *dn, *cur, **dnp;
485 struct dirent *entry;
486 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000487 int i, nfiles;
488
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000489 if (path == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000490 return NULL;
Eric Andersen11c65522000-09-07 17:24:47 +0000491
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000492 dn = NULL;
493 nfiles = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000494 dir = warn_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000495 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000496 status = EXIT_FAILURE;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000497 return NULL; /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000498 }
499 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000500 char *fullname;
501
Eric Andersen11c65522000-09-07 17:24:47 +0000502 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000503 if (entry->d_name[0] == '.') {
504 if ((entry->d_name[1] == 0 || (
505 entry->d_name[1] == '.'
506 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000507 && !(all_fmt & DISP_DOT))
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000508 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000509 if (!(all_fmt & DISP_HIDDEN))
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000510 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000511 }
512 fullname = concat_path_file(path, entry->d_name);
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000513 cur = my_stat(fullname, strrchr(fullname, '/') + 1, 0);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000514 if (!cur) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000515 free(fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000516 continue;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000517 }
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000518 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000519 cur->next = dn;
520 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000521 nfiles++;
522 }
523 closedir(dir);
524
525 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000526 * allocate memory for an array to hold dnode pointers
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000527 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000528 if (dn == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000529 return NULL;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000530 dnp = dnalloc(nfiles);
531 for (i = 0, cur = dn; i < nfiles; i++) {
532 dnp[i] = cur; /* save pointer to node in array */
533 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000534 }
535
Denis Vlasenko5c759602006-10-28 12:37:16 +0000536 return dnp;
Eric Andersen11c65522000-09-07 17:24:47 +0000537}
538
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000539
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000540#if ENABLE_FEATURE_LS_TIMESTAMPS
541/* Do time() just once. Saves one syscall per file for "ls -l" */
542/* Initialized in main() */
543static time_t current_time_t;
544#endif
545
Eric Andersen3e6ff902001-03-09 21:24:12 +0000546static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000547{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000548 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000549
Denis Vlasenko5c759602006-10-28 12:37:16 +0000550#if ENABLE_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000551 char *filetime;
552 time_t ttime, age;
553#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000554#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
Eric Andersen11c65522000-09-07 17:24:47 +0000555 struct stat info;
556 char append;
557#endif
558
Glenn L McGrath4d001292003-01-06 01:11:50 +0000559 if (dn->fullname == NULL)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000560 return 0;
Eric Andersen11c65522000-09-07 17:24:47 +0000561
Denis Vlasenko5c759602006-10-28 12:37:16 +0000562#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000563 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000564 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000565 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000566 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000567 ttime = dn->dstat.st_ctime;
568 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000569#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000570#if ENABLE_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000571 append = append_char(dn->dstat.st_mode);
572#endif
573
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000574 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000575 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000576 case LIST_INO:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000577 column += printf("%7ld ", (long) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000578 break;
579 case LIST_BLOCKS:
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000580 column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000581 break;
582 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000583 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000584 break;
585 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000586 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000587 break;
588 case LIST_ID_NAME:
Denis Vlasenko5c759602006-10-28 12:37:16 +0000589#if ENABLE_FEATURE_LS_USERNAME
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000590 printf("%-8.8s %-8.8s",
591 get_cached_username(dn->dstat.st_uid),
592 get_cached_groupname(dn->dstat.st_gid));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000593 column += 17;
594 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000595#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000596 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000597 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000598 break;
599 case LIST_SIZE:
600 case LIST_DEV:
601 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000602 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
603 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000604 } else {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000605 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000606 column += printf("%9s ",
Denis Vlasenko5c759602006-10-28 12:37:16 +0000607 make_human_readable_str(dn->dstat.st_size, 1, 0));
608 } else {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000609 column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000610 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000611 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000612 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000613#if ENABLE_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000614 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000615 printf("%24.24s ", filetime);
616 column += 25;
617 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000618 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000619 if ((all_fmt & LIST_FULLTIME) == 0) {
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000620 /* current_time_t ~== time(NULL) */
621 age = current_time_t - ttime;
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000622 printf("%6.6s ", filetime + 4);
623 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
624 /* hh:mm if less than 6 months old */
625 printf("%5.5s ", filetime + 11);
626 } else {
627 printf(" %4.4s ", filetime + 20);
628 }
629 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000630 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000631 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000632#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000633#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000634 case LIST_CONTEXT:
635 {
Rob Landley60158cb2005-05-03 06:25:50 +0000636 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000637 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000638
Rob Landley60158cb2005-05-03 06:25:50 +0000639 if (dn->sid) {
Denis Vlasenko656f7462006-10-28 13:02:55 +0000640 /* I assume sid initilized with NULL */
641 len = strlen(dn->sid) + 1;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000642 safe_strncpy(context, dn->sid, len);
643 freecon(dn->sid);
644 } else {
645 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000646 }
647 printf("%-32s ", context);
648 column += MAX(33, len);
649 }
650 break;
651#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000652 case LIST_FILENAME:
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000653 errno = 0;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000654#if ENABLE_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000655 if (show_color && !lstat(dn->fullname, &info)) {
656 printf("\033[%d;%dm", bgcolor(info.st_mode),
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000657 fgcolor(info.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000658 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000659#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000660 column += printf("%s", dn->name);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000661 if (show_color) {
662 printf("\033[0m");
663 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000664 break;
665 case LIST_SYMLINK:
666 if (S_ISLNK(dn->dstat.st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000667 char *lpath = xmalloc_readlink_or_warn(dn->fullname);
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000668 if (!lpath) break;
669 printf(" -> ");
670#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
671 if (!stat(dn->fullname, &info)) {
672 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000673 }
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000674#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000675#if ENABLE_FEATURE_LS_COLOR
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000676 if (show_color) {
677 errno = 0;
678 printf("\033[%d;%dm", bgcolor(info.st_mode),
679 fgcolor(info.st_mode));
680 }
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000681#endif
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000682 column += printf("%s", lpath) + 4;
683 if (show_color) {
684 printf("\033[0m");
685 }
686 free(lpath);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000687 }
688 break;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000689#if ENABLE_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000690 case LIST_FILETYPE:
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000691 if (append) {
692 putchar(append);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000693 column++;
694 }
695 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000696#endif
697 }
698 }
699
Glenn L McGrath4d001292003-01-06 01:11:50 +0000700 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000701}
702
Glenn L McGrath792cae52004-01-18 05:15:16 +0000703/* "[-]Cadil1", POSIX mandated options, busybox always supports */
704/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
705/* "[-]Ak" GNU options, busybox always supports */
706/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
707/* "[-]p", POSIX non-mandated options, busybox optionally supports */
708/* "[-]SXvThw", GNU options, busybox optionally supports */
709/* "[-]K", SELinux mandated options, busybox optionally supports */
710/* "[-]e", I think we made this one up */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000711static const char ls_options[] = "Cadil1gnsxAk"
712 USE_FEATURE_LS_TIMESTAMPS("cetu")
713 USE_FEATURE_LS_SORTFILES("SXrv")
714 USE_FEATURE_LS_FILETYPES("Fp")
715 USE_FEATURE_LS_FOLLOWLINKS("L")
716 USE_FEATURE_LS_RECURSIVE("R")
717 USE_FEATURE_HUMAN_READABLE("h")
718 USE_SELINUX("K")
Denis Vlasenko49622d72007-03-10 16:58:49 +0000719 USE_FEATURE_AUTOWIDTH("T:w:")
720 USE_SELINUX("Z");
Glenn L McGrath792cae52004-01-18 05:15:16 +0000721
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000722enum {
723 LIST_MASK_TRIGGER = 0,
724 STYLE_MASK_TRIGGER = STYLE_MASK,
725 DISP_MASK_TRIGGER = DISP_ROWS,
726 SORT_MASK_TRIGGER = SORT_MASK,
727};
Manuel Novoa III cad53642003-03-19 09:13:01 +0000728
729static const unsigned opt_flags[] = {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000730 LIST_SHORT | STYLE_COLUMNS, /* C */
731 DISP_HIDDEN | DISP_DOT, /* a */
732 DISP_NOLIST, /* d */
733 LIST_INO, /* i */
734 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
735 LIST_SHORT | STYLE_SINGLE, /* 1 */
736 0, /* g - ingored */
737 LIST_ID_NUMERIC, /* n */
738 LIST_BLOCKS, /* s */
739 DISP_ROWS, /* x */
740 DISP_HIDDEN, /* A */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000741 ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000742#if ENABLE_FEATURE_LS_TIMESTAMPS
743 TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
744 LIST_FULLTIME, /* e */
745 ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */
746 TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000747#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000748#if ENABLE_FEATURE_LS_SORTFILES
749 SORT_SIZE, /* S */
750 SORT_EXT, /* X */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000751 SORT_REVERSE, /* r */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000752 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000753#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000754#if ENABLE_FEATURE_LS_FILETYPES
755 LIST_FILETYPE | LIST_EXEC, /* F */
756 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000757#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000758#if ENABLE_FEATURE_LS_FOLLOWLINKS
759 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000760#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000761#if ENABLE_FEATURE_LS_RECURSIVE
762 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000763#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000764#if ENABLE_FEATURE_HUMAN_READABLE
765 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000766#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000767#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000768 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
769#endif
Denis Vlasenko5c759602006-10-28 12:37:16 +0000770#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000771 0, 0, /* T, w - ignored */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000772#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000773#if ENABLE_SELINUX
774 LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT, /* Z */
775#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000776 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000777};
778
779
Denis Vlasenko97fd6d82007-03-19 20:59:20 +0000780/* THIS IS A "SAFE" APPLET, main() MAY BE CALLED INTERNALLY FROM SHELL */
781/* BE CAREFUL! */
782
Denis Vlasenko06af2162007-02-03 17:28:39 +0000783int ls_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000784int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000785{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000786 struct dnode **dnd;
787 struct dnode **dnf;
788 struct dnode **dnp;
789 struct dnode *dn;
790 struct dnode *cur;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000791 unsigned opt;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000792 int nfiles = 0;
793 int dnfiles;
794 int dndirs;
795 int oi;
796 int ac;
797 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000798 char **av;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000799 USE_FEATURE_AUTOWIDTH(char *tabstops_str = NULL;)
800 USE_FEATURE_AUTOWIDTH(char *terminal_width_str = NULL;)
801 USE_FEATURE_LS_COLOR(char *color_opt;)
Glenn L McGrath792cae52004-01-18 05:15:16 +0000802
Denis Vlasenkoe0554432007-01-19 22:03:06 +0000803#if ENABLE_FEATURE_LS_TIMESTAMPS
804 time(&current_time_t);
805#endif
806
Rob Landley2b8a05a2006-06-20 17:43:01 +0000807 all_fmt = LIST_SHORT |
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000808 (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
Glenn L McGrath792cae52004-01-18 05:15:16 +0000809
Denis Vlasenko5c759602006-10-28 12:37:16 +0000810#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko656f7462006-10-28 13:02:55 +0000811 /* Obtain the terminal width */
Eric Andersen70060d22004-03-27 10:02:48 +0000812 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000813 /* Go one less... */
814 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000815#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000816
Eric Andersen11c65522000-09-07 17:24:47 +0000817 /* process options */
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000818 USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
Denis Vlasenko5c759602006-10-28 12:37:16 +0000819#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000820 opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000821 USE_FEATURE_LS_COLOR(, &color_opt));
Denis Vlasenko5c759602006-10-28 12:37:16 +0000822 if (tabstops_str)
Denis Vlasenko13858992006-10-08 12:49:22 +0000823 tabstops = xatou(tabstops_str);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000824 if (terminal_width_str)
Denis Vlasenko13858992006-10-08 12:49:22 +0000825 terminal_width = xatou(terminal_width_str);
Glenn L McGrath792cae52004-01-18 05:15:16 +0000826#else
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000827 opt = getopt32(argc, argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000828#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000829 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000830 if (opt & (1 << i)) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000831 unsigned flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000832
Denis Vlasenko5c759602006-10-28 12:37:16 +0000833 if (flags & LIST_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000834 all_fmt &= ~LIST_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000835 if (flags & STYLE_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000836 all_fmt &= ~STYLE_MASK;
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000837 if (flags & SORT_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000838 all_fmt &= ~SORT_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000839 if (flags & DISP_MASK_TRIGGER)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000840 all_fmt &= ~DISP_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000841 if (flags & TIME_MASK)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000842 all_fmt &= ~TIME_MASK;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000843 if (flags & LIST_CONTEXT)
Eric Andersen9e480452003-07-03 10:07:04 +0000844 all_fmt |= STYLE_SINGLE;
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000845 /* huh?? opt cannot be 'l' */
846 //if (LS_DISP_HR && opt == 'l')
847 // all_fmt &= ~LS_DISP_HR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000848 all_fmt |= flags;
849 }
Eric Andersen11c65522000-09-07 17:24:47 +0000850 }
851
Denis Vlasenko5c759602006-10-28 12:37:16 +0000852#if ENABLE_FEATURE_LS_COLOR
853 /* find color bit value - last position for short getopt */
854 if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
855 char *p = getenv("LS_COLORS");
856 /* LS_COLORS is unset, or (not empty && not "none") ? */
857 if (!p || (p[0] && strcmp(p, "none")))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000858 show_color = 1;
Denis Vlasenko5c759602006-10-28 12:37:16 +0000859 }
860 if (opt & (1 << i)) { /* next flag after short options */
861 if (!color_opt || !strcmp("always", color_opt))
862 show_color = 1;
863 else if (color_opt && !strcmp("never", color_opt))
864 show_color = 0;
865 else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO))
866 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +0000867 }
868#endif
869
Eric Andersen11c65522000-09-07 17:24:47 +0000870 /* sort out which command line options take precedence */
Denis Vlasenko5c759602006-10-28 12:37:16 +0000871 if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000872 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Rob Landleyea224be2006-06-18 20:20:07 +0000873 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
874 if (all_fmt & TIME_CHANGE)
875 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
876 if (all_fmt & TIME_ACCESS)
877 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
878 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000879 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
880 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000881 if (ENABLE_FEATURE_LS_USERNAME)
882 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
883 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000884
Eric Andersen11c65522000-09-07 17:24:47 +0000885 /* choose a display format */
Rob Landleyea224be2006-06-18 20:20:07 +0000886 if (!(all_fmt & STYLE_MASK))
Eric Andersen70060d22004-03-27 10:02:48 +0000887 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Eric Andersen11c65522000-09-07 17:24:47 +0000888
889 /*
890 * when there are no cmd line args we have to supply a default "." arg.
891 * we will create a second argv array, "av" that will hold either
892 * our created "." arg, or the real cmd line args. The av array
893 * just holds the pointers- we don't move the date the pointers
894 * point to.
895 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000896 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +0000897 if (ac < 1) {
Denis Vlasenko5c759602006-10-28 12:37:16 +0000898 static const char *const dotdir[] = { "." };
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000899
900 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000901 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +0000902 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000903 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +0000904 }
905
906 /* now, everything is in the av array */
907 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000908 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +0000909
Denis Vlasenko5c759602006-10-28 12:37:16 +0000910 /* stuff the command line file names into a dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000911 dn = NULL;
912 for (oi = 0; oi < ac; oi++) {
Denis Vlasenko2110aa92007-02-28 23:14:06 +0000913 /* ls w/o -l follows links on command line */
914 cur = my_stat(av[oi], av[oi], !(all_fmt & STYLE_LONG));
Glenn L McGrath4d001292003-01-06 01:11:50 +0000915 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000916 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000917 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000918 cur->next = dn;
919 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000920 nfiles++;
921 }
922
923 /* now that we know how many files there are
Denis Vlasenko656f7462006-10-28 13:02:55 +0000924 * allocate memory for an array to hold dnode pointers
925 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000926 dnp = dnalloc(nfiles);
927 for (i = 0, cur = dn; i < nfiles; i++) {
928 dnp[i] = cur; /* save pointer to node in array */
929 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000930 }
931
Manuel Novoa III cad53642003-03-19 09:13:01 +0000932 if (all_fmt & DISP_NOLIST) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000933 dnsort(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000934 if (nfiles > 0)
935 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000936 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000937 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
938 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
939 dndirs = countdirs(dnp, nfiles);
940 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +0000941 if (dnfiles > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000942 dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000943 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +0000944 if (ENABLE_FEATURE_CLEAN_UP)
945 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +0000946 }
947 if (dndirs > 0) {
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000948 dnsort(dnd, dndirs);
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000949 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +0000950 if (ENABLE_FEATURE_CLEAN_UP)
951 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +0000952 }
953 }
Rob Landley26314862006-05-02 19:46:52 +0000954 if (ENABLE_FEATURE_CLEAN_UP)
955 dfree(dnp, nfiles);
Denis Vlasenko5c759602006-10-28 12:37:16 +0000956 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000957}