blob: 9c0e8f17dcddaf53f1043110361ab067fe8b45a5 [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/* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000094#define DISP_NORMAL (1U<<14) /* show normal filenames */
Manuel Novoa III cad53642003-03-19 09:13:01 +000095#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000096#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
97#define DISP_DOT (1U<<17) /* show . and .. */
98#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
Manuel Novoa III cad53642003-03-19 09:13:01 +000099#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000100#define DISP_ROWS (1U<<20) /* print across rows */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101
102#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1))
Eric Andersen11c65522000-09-07 17:24:47 +0000103
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000104#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000105/* how will the files be sorted */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000106#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
108
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000109#define SORT_NAME 0 /* sort by file name */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110#define SORT_SIZE (1U<<28) /* sort by file size */
111#define SORT_ATIME (2U<<28) /* sort by last access time */
112#define SORT_CTIME (3U<<28) /* sort by last change time */
113#define SORT_MTIME (4U<<28) /* sort by last modification time */
114#define SORT_VERSION (5U<<28) /* sort by version */
115#define SORT_EXT (6U<<28) /* sort by file name extension */
116#define SORT_DIR (7U<<28) /* sort by file or directory */
117
118#define SORT_MASK (7U<<28)
Eric Andersencc8ed391999-10-05 16:24:54 +0000119#endif
120
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000121#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000122/* which of the three times will be used */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123#define TIME_MOD 0
124#define TIME_CHANGE (1U<<23)
125#define TIME_ACCESS (1U<<24)
126
127#define TIME_MASK (3U<<23)
128#endif
129
130#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
131#define FOLLOW_LINKS (1U<<25)
132#endif
133#ifdef CONFIG_FEATURE_HUMAN_READABLE
134#define LS_DISP_HR (1U<<26)
Eric Andersencc8ed391999-10-05 16:24:54 +0000135#endif
136
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000137#define LIST_SHORT (LIST_FILENAME)
138#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
139#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
140 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
141#define LIST_ILONG (LIST_INO | LIST_LONG)
Eric Andersencc8ed391999-10-05 16:24:54 +0000142
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143#define SPLIT_DIR 1
144#define SPLIT_FILE 0
145#define SPLIT_SUBDIR 2
Eric Andersencc8ed391999-10-05 16:24:54 +0000146
Eric Andersen11c65522000-09-07 17:24:47 +0000147#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
148#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000149
Eric Andersend598d412002-04-27 09:19:39 +0000150#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000151# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
Eric Andersen11c65522000-09-07 17:24:47 +0000152#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000153
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000154/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
155#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000156
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000157static int show_color = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000158
Paul Fox156dc412005-08-01 19:33:30 +0000159/* long option entry used only for --color, which has no short option
160 * equivalent. */
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000161static const struct option ls_color_opt[] =
Paul Fox156dc412005-08-01 19:33:30 +0000162{
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000163 {"color", optional_argument, NULL, 1},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000164 {NULL, 0, NULL, 0}
Paul Fox156dc412005-08-01 19:33:30 +0000165};
166
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000167#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000168 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000169#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000170 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000171#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172
Eric Andersen11c65522000-09-07 17:24:47 +0000173/*
174 * a directory entry and its stat info are stored here
175 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000176struct dnode { /* the basic node */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000177 char *name; /* the dir entry name */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000178 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000179 int allocated;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000180 struct stat dstat; /* the file stat info */
Eric Andersen9e480452003-07-03 10:07:04 +0000181#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000182 security_context_t sid;
Eric Andersen9e480452003-07-03 10:07:04 +0000183#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000184 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000185};
186typedef struct dnode dnode_t;
187
Glenn L McGrath4d001292003-01-06 01:11:50 +0000188static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000189static struct dnode **dnalloc(int);
190static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000191
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192static unsigned int all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000193
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000194#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000195static int terminal_width = TERMINAL_WIDTH;
Eric Andersene57d54b2001-01-30 18:03:11 +0000196static unsigned short tabstops = COLUMN_GAP;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000197#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000198#define tabstops COLUMN_GAP
199#define terminal_width TERMINAL_WIDTH
Eric Andersen11c65522000-09-07 17:24:47 +0000200#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Matt Kraai33fdae52000-10-13 17:59:43 +0000202static int status = EXIT_SUCCESS;
203
Glenn L McGrath4d001292003-01-06 01:11:50 +0000204static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000205{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000206 struct stat dstat;
207 struct dnode *cur;
Eric Andersen9e480452003-07-03 10:07:04 +0000208#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000209 security_context_t sid=NULL;
Eric Andersen9e480452003-07-03 10:07:04 +0000210#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000211
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000212#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 if (all_fmt & FOLLOW_LINKS) {
Eric Andersen9e480452003-07-03 10:07:04 +0000214#ifdef CONFIG_SELINUX
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000215 if (is_selinux_enabled()) {
216 getfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000217 }
Eric Andersen9e480452003-07-03 10:07:04 +0000218#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000219 if (stat(fullname, &dstat)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000221 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000222 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000223 }
224 } else
225#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000226 {
227#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000228 if (is_selinux_enabled()) {
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000229 lgetfilecon(fullname,&sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000230 }
Eric Andersen9e480452003-07-03 10:07:04 +0000231#endif
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +0000232 if (lstat(fullname, &dstat)) {
Eric Andersen9e480452003-07-03 10:07:04 +0000233 bb_perror_msg("%s", fullname);
234 status = EXIT_FAILURE;
235 return 0;
236 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000237 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000238
239 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
240 cur->fullname = fullname;
241 cur->name = name;
242 cur->dstat = dstat;
Eric Andersen9e480452003-07-03 10:07:04 +0000243#ifdef CONFIG_SELINUX
244 cur->sid = sid;
245#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000246 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000247}
248
Eric Andersen11c65522000-09-07 17:24:47 +0000249/*----------------------------------------------------------------------*/
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000250#ifdef CONFIG_FEATURE_LS_COLOR
251static char fgcolor(mode_t mode)
252{
253 /* Check wheter the file is existing (if so, color it red!) */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000254 if (errno == ENOENT) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000255 return '\037';
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000256 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000257 if (LIST_EXEC && S_ISREG(mode)
258 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000259 return COLOR(0xF000); /* File is executable ... */
260 return COLOR(mode);
261}
262
263/*----------------------------------------------------------------------*/
264static char bgcolor(mode_t mode)
265{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000266 if (LIST_EXEC && S_ISREG(mode)
267 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000268 return ATTR(0xF000); /* File is executable ... */
269 return ATTR(mode);
270}
271#endif
272
273/*----------------------------------------------------------------------*/
Eric Andersend598d412002-04-27 09:19:39 +0000274#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000275static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000276{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000277 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000278 return '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 if ((all_fmt & LIST_EXEC) && S_ISREG(mode)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000280 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
281 return '*';
282 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000283}
284#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000285
Eric Andersen11c65522000-09-07 17:24:47 +0000286/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000287
Manuel Novoa III cad53642003-03-19 09:13:01 +0000288#define countdirs(A,B) count_dirs((A), (B), 1)
289#define countsubdirs(A,B) count_dirs((A), (B), 0)
290
291static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000292{
293 int i, dirs;
294
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000295 if (dn == NULL || nfiles < 1)
296 return (0);
297 dirs = 0;
298 for (i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000299 if (S_ISDIR(dn[i]->dstat.st_mode)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000300 && (notsubdirs ||
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000301 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302 && ((dn[i]->name[1] != '.')
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000303 || dn[i]->name[2])))))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000304 dirs++;
Eric Andersen11c65522000-09-07 17:24:47 +0000305 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000306 return (dirs);
Eric Andersen11c65522000-09-07 17:24:47 +0000307}
308
Eric Andersen3e6ff902001-03-09 21:24:12 +0000309static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000310{
311 int nfiles;
312 struct dnode *cur;
313
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000314 if (dnp == NULL)
315 return (0);
316 nfiles = 0;
317 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
318 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000319 nfiles++;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000320 return (nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000321}
322
323/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000324static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000325{
326 struct dnode **p;
327
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000328 if (num < 1)
329 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000330
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000331 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000332 return (p);
Eric Andersen11c65522000-09-07 17:24:47 +0000333}
334
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000335#ifdef CONFIG_FEATURE_LS_RECURSIVE
Rob Landley26314862006-05-02 19:46:52 +0000336static void dfree(struct dnode **dnp, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000337{
Rob Landley26314862006-05-02 19:46:52 +0000338 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000339
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000340 if (dnp == NULL)
341 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000342
Rob Landley26314862006-05-02 19:46:52 +0000343 for (i = 0; i < nfiles; i++) {
344 struct dnode *cur = dnp[i];
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000345 if(cur->allocated)
346 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000347 free(cur); /* free the dnode */
Eric Andersen11c65522000-09-07 17:24:47 +0000348 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000349 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000350}
Rob Landleyc44bc982006-05-28 01:19:06 +0000351#else
352#define dfree(...)
Eric Andersen20aab262001-07-19 22:28:02 +0000353#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000354
Eric Andersen3e6ff902001-03-09 21:24:12 +0000355static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000356{
357 int dncnt, i, d;
358 struct dnode **dnp;
359
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000360 if (dn == NULL || nfiles < 1)
361 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000362
363 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000364 if (which == SPLIT_SUBDIR)
365 dncnt = countsubdirs(dn, nfiles);
366 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000367 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000368 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000369 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000370 }
Eric Andersen11c65522000-09-07 17:24:47 +0000371
372 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000373 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000374
375 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000376 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000377 if (S_ISDIR(dn[i]->dstat.st_mode)) {
378 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
379 if ((which & SPLIT_DIR)
380 || ((dn[i]->name[0] != '.')
381 || (dn[i]->name[1]
382 && ((dn[i]->name[1] != '.')
383 || dn[i]->name[2])))) {
384 dnp[d++] = dn[i];
385 }
386 }
387 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
388 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000389 }
390 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000391 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000392}
393
394/*----------------------------------------------------------------------*/
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000395#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000396static int sortcmp(const void *a, const void *b)
Eric Andersen11c65522000-09-07 17:24:47 +0000397{
Rob Landley425e7582006-05-03 20:22:03 +0000398 struct dnode *d1 = *(struct dnode **)a;
399 struct dnode *d2 = *(struct dnode **)b;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000400 unsigned int sort_opts = all_fmt & SORT_MASK;
401 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000402
Manuel Novoa III cad53642003-03-19 09:13:01 +0000403 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000404 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000405 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000406 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000407 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000408 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000409 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000410 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000411 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000412 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000413 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000414 /* } else if (sort_opts == SORT_VERSION) { */
415 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000416 }
417
Eric Andersen11c65522000-09-07 17:24:47 +0000418 if (dif == 0) {
419 /* sort by name- may be a tie_breaker for time or size cmp */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000420#ifdef CONFIG_LOCALE_SUPPORT
421 dif = strcoll(d1->name, d2->name);
422#else
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000423 dif = strcmp(d1->name, d2->name);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000424#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000425 }
426
Manuel Novoa III cad53642003-03-19 09:13:01 +0000427 if (all_fmt & SORT_ORDER_REVERSE) {
428 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000429 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000430 return (dif);
Eric Andersen11c65522000-09-07 17:24:47 +0000431}
432
433/*----------------------------------------------------------------------*/
Rob Landley425e7582006-05-03 20:22:03 +0000434static void dnsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000435{
Rob Landley425e7582006-05-03 20:22:03 +0000436 qsort(dn, size, sizeof *dn, sortcmp);
Eric Andersen11c65522000-09-07 17:24:47 +0000437}
438#endif
439
440/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000441static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000442{
443 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000444 int column = 0;
445 int nexttab = 0;
446 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000447
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000448 if (dn == NULL || nfiles < 1)
449 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000450
Manuel Novoa III cad53642003-03-19 09:13:01 +0000451 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000452 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000453 } else {
454 /* find the longest file name- use that as the column width */
455 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000456 int len = strlen(dn[i]->name) +
Eric Andersen9e480452003-07-03 10:07:04 +0000457#ifdef CONFIG_SELINUX
458 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
459#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000460 ((all_fmt & LIST_INO) ? 8 : 0) +
461 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
462 if (column_width < len)
463 column_width = len;
464 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000465 column_width += tabstops;
466 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000467 }
468
Eric Andersene57d54b2001-01-30 18:03:11 +0000469 if (ncols > 1) {
470 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000471 if ((nrows * ncols) < nfiles)
472 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000473 } else {
474 nrows = nfiles;
475 ncols = 1;
476 }
Eric Andersen11c65522000-09-07 17:24:47 +0000477
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000478 for (row = 0; row < nrows; row++) {
479 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000480 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000481 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000482 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000483 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000484 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000485 if (column > 0) {
486 nexttab -= column;
487 while (nexttab--) {
488 putchar(' ');
489 column++;
490 }
Eric Andersen79565b62000-08-11 18:10:21 +0000491 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000492 nexttab = column + column_width;
493 column += list_single(dn[i]);
Eric Andersen79565b62000-08-11 18:10:21 +0000494 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000495 }
496 putchar('\n');
497 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000498 }
Eric Andersen11c65522000-09-07 17:24:47 +0000499}
500
501/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000502static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000503{
504 int i, nfiles;
505 struct dnode **subdnp;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000506
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000507#ifdef CONFIG_FEATURE_LS_RECURSIVE
Eric Andersen11c65522000-09-07 17:24:47 +0000508 int dndirs;
509 struct dnode **dnd;
510#endif
511
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000512 if (dn == NULL || ndirs < 1)
513 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000514
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000515 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000516 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000517 if (!first)
518 printf("\n");
519 first = 0;
520 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000521 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000522 subdnp = list_dir(dn[i]->fullname);
523 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000524 if (nfiles > 0) {
525 /* list all files at this level */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000526#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000527 dnsort(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000528#endif
529 showfiles(subdnp, nfiles);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000530#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000531 if (all_fmt & DISP_RECURSIVE) {
Eric Andersen11c65522000-09-07 17:24:47 +0000532 /* recursive- list the sub-dirs */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000533 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
534 dndirs = countsubdirs(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000535 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000536#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +0000537 dnsort(dnd, dndirs);
Matt Kraaia5bd2682000-10-28 06:40:09 +0000538#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000539 showdirs(dnd, dndirs, 0);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000540 free(dnd); /* free the array of dnode pointers to the dirs */
Eric Andersen11c65522000-09-07 17:24:47 +0000541 }
542 }
Rob Landley26314862006-05-02 19:46:52 +0000543 dfree(subdnp, nfiles); /* free the dnodes and the fullname mem */
Eric Andersen11c65522000-09-07 17:24:47 +0000544#endif
545 }
546 }
547}
548
549/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000550static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000551{
552 struct dnode *dn, *cur, **dnp;
553 struct dirent *entry;
554 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000555 int i, nfiles;
556
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000557 if (path == NULL)
558 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000559
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000560 dn = NULL;
561 nfiles = 0;
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +0000562 dir = bb_opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000563 if (dir == NULL) {
Matt Kraai33fdae52000-10-13 17:59:43 +0000564 status = EXIT_FAILURE;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000565 return (NULL); /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000566 }
567 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000568 char *fullname;
569
Eric Andersen11c65522000-09-07 17:24:47 +0000570 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000571 if (entry->d_name[0] == '.') {
572 if ((entry->d_name[1] == 0 || (
573 entry->d_name[1] == '.'
574 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000575 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000576 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000577 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000578 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000579 }
580 fullname = concat_path_file(path, entry->d_name);
581 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
582 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000583 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000584 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000585 cur->next = dn;
586 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000587 nfiles++;
588 }
589 closedir(dir);
590
591 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000592 ** allocate memory for an array to hold dnode pointers
593 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000594 if (dn == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000595 return (NULL);
596 dnp = dnalloc(nfiles);
597 for (i = 0, cur = dn; i < nfiles; i++) {
598 dnp[i] = cur; /* save pointer to node in array */
599 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000600 }
601
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000602 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000603}
604
605/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000606static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000607{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000608 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000609
Matt Kraaia1bbde72002-03-08 16:25:33 +0000610#ifdef CONFIG_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000611 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000612#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000613#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000614 char *filetime;
615 time_t ttime, age;
616#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000617#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000618 struct stat info;
619 char append;
620#endif
621
Glenn L McGrath4d001292003-01-06 01:11:50 +0000622 if (dn->fullname == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000623 return (0);
Eric Andersen11c65522000-09-07 17:24:47 +0000624
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000625#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000626 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000627 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000628 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000629 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000630 ttime = dn->dstat.st_ctime;
631 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000632#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000633#ifdef CONFIG_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000634 append = append_char(dn->dstat.st_mode);
635#endif
636
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000637 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000638 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000639 case LIST_INO:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000640 column += printf("%7ld ", (long int) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000641 break;
642 case LIST_BLOCKS:
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000643#if _FILE_OFFSET_BITS == 64
Eric Andersen5e678872006-01-30 19:48:23 +0000644 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000645#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000646 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000647#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000648 break;
649 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000650 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000651 break;
652 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000653 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000654 break;
655 case LIST_ID_NAME:
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000656#ifdef CONFIG_FEATURE_LS_USERNAME
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000657 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000658 printf("%-8.8s ", scratch);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000659 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000660 printf("%-8.8s", scratch);
661 column += 17;
662 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000663#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000664 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000665 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000666 break;
667 case LIST_SIZE:
668 case LIST_DEV:
669 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000670 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
671 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000672 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000673#ifdef CONFIG_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000674 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000675 column += printf("%9s ",
676 make_human_readable_str(dn->dstat.st_size, 1, 0));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000677 } else
678#endif
679 {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000680#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000681 column += printf("%9lld ", (long long) dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000682#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000683 column += printf("%9ld ", dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000684#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000685 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000686 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000687 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000688#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000689 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000690 printf("%24.24s ", filetime);
691 column += 25;
692 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000693 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000694 if ((all_fmt & LIST_FULLTIME) == 0) {
695 age = time(NULL) - ttime;
696 printf("%6.6s ", filetime + 4);
697 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
698 /* hh:mm if less than 6 months old */
699 printf("%5.5s ", filetime + 11);
700 } else {
701 printf(" %4.4s ", filetime + 20);
702 }
703 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000704 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000705 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000706#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000707#ifdef CONFIG_SELINUX
708 case LIST_CONTEXT:
709 {
Rob Landley60158cb2005-05-03 06:25:50 +0000710 char context[80];
Rob Landley08abe642006-03-01 20:48:44 +0000711 int len = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000712
Rob Landley60158cb2005-05-03 06:25:50 +0000713 if (dn->sid) {
714 /* I assume sid initilized with NULL */
715 len = strlen(dn->sid)+1;
716 safe_strncpy(context, dn->sid, len);
717 freecon(dn->sid);
718 }else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000719 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000720 }
721 printf("%-32s ", context);
722 column += MAX(33, len);
723 }
724 break;
725#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000726 case LIST_FILENAME:
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000727#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000728 errno = 0;
729 if (show_color && !lstat(dn->fullname, &info)) {
730 printf("\033[%d;%dm", bgcolor(info.st_mode),
731 fgcolor(info.st_mode));
732 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000733#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000734 column += printf("%s", dn->name);
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000735#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000736 if (show_color) {
737 printf("\033[0m");
738 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000739#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000740 break;
741 case LIST_SYMLINK:
742 if (S_ISLNK(dn->dstat.st_mode)) {
743 char *lpath = xreadlink(dn->fullname);
744
745 if (lpath) {
746 printf(" -> ");
747#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
748 if (!stat(dn->fullname, &info)) {
749 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000750 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000751#endif
752#ifdef CONFIG_FEATURE_LS_COLOR
753 if (show_color) {
754 errno = 0;
755 printf("\033[%d;%dm", bgcolor(info.st_mode),
756 fgcolor(info.st_mode));
757 }
758#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000759 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000760#ifdef CONFIG_FEATURE_LS_COLOR
761 if (show_color) {
762 printf("\033[0m");
763 }
764#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000765 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000766 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000767 }
768 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000769#ifdef CONFIG_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000770 case LIST_FILETYPE:
771 if (append != '\0') {
772 printf("%1c", append);
773 column++;
774 }
775 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000776#endif
777 }
778 }
779
Glenn L McGrath4d001292003-01-06 01:11:50 +0000780 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000781}
782
783/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000784
Glenn L McGrath792cae52004-01-18 05:15:16 +0000785/* "[-]Cadil1", POSIX mandated options, busybox always supports */
786/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
787/* "[-]Ak" GNU options, busybox always supports */
788/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
789/* "[-]p", POSIX non-mandated options, busybox optionally supports */
790/* "[-]SXvThw", GNU options, busybox optionally supports */
791/* "[-]K", SELinux mandated options, busybox optionally supports */
792/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000793
Glenn L McGrath792cae52004-01-18 05:15:16 +0000794#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
795# define LS_STR_TIMESTAMPS "cetu"
796#else
797# define LS_STR_TIMESTAMPS ""
798#endif
799
800#ifdef CONFIG_FEATURE_LS_SORTFILES
801# define LS_STR_SORTFILES "SXrv"
802#else
803# define LS_STR_SORTFILES ""
804#endif
805
806#ifdef CONFIG_FEATURE_LS_FILETYPES
807# define LS_STR_FILETYPES "Fp"
808#else
809# define LS_STR_FILETYPES ""
810#endif
811
812#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
813# define LS_STR_FOLLOW_LINKS "L"
814#else
815# define LS_STR_FOLLOW_LINKS ""
816#endif
817
818#ifdef CONFIG_FEATURE_LS_RECURSIVE
819# define LS_STR_RECURSIVE "R"
820#else
821# define LS_STR_RECURSIVE ""
822#endif
823
824#ifdef CONFIG_FEATURE_HUMAN_READABLE
825# define LS_STR_HUMAN_READABLE "h"
826#else
827# define LS_STR_HUMAN_READABLE ""
828#endif
829
830#ifdef CONFIG_SELINUX
831# define LS_STR_SELINUX "K"
832#else
833# define LS_STR_SELINUX ""
834#endif
835
836#ifdef CONFIG_FEATURE_AUTOWIDTH
837# define LS_STR_AUTOWIDTH "T:w:"
838#else
839# define LS_STR_AUTOWIDTH ""
840#endif
841
842static const char ls_options[]="Cadil1gnsxAk" \
843 LS_STR_TIMESTAMPS \
844 LS_STR_SORTFILES \
845 LS_STR_FILETYPES \
846 LS_STR_FOLLOW_LINKS \
847 LS_STR_RECURSIVE \
848 LS_STR_HUMAN_READABLE \
849 LS_STR_SELINUX \
850 LS_STR_AUTOWIDTH;
851
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000852#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000853#define STYLE_MASK_TRIGGER STYLE_MASK
854#define SORT_MASK_TRIGGER SORT_MASK
855#define DISP_MASK_TRIGGER DISP_ROWS
856#define TIME_MASK_TRIGGER TIME_MASK
Manuel Novoa III cad53642003-03-19 09:13:01 +0000857
858static const unsigned opt_flags[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000859 LIST_SHORT | STYLE_COLUMNS, /* C */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000860 DISP_HIDDEN | DISP_DOT, /* a */
861 DISP_NOLIST, /* d */
862 LIST_INO, /* i */
863 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
864 LIST_SHORT | STYLE_SINGLE, /* 1 */
865 0, /* g - ingored */
866 LIST_ID_NUMERIC, /* n */
867 LIST_BLOCKS, /* s */
868 DISP_ROWS, /* x */
869 DISP_HIDDEN, /* A */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000870#ifdef CONFIG_SELINUX
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000871 LIST_CONTEXT, /* k */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000872#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000873 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000874#endif
875#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrath792cae52004-01-18 05:15:16 +0000876# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000877 TIME_CHANGE | SORT_CTIME, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000878# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000879 TIME_CHANGE, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000880# endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000881 LIST_FULLTIME, /* e */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000882# ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000883 SORT_MTIME, /* t */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000884# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000885 0, /* t - ignored -- is this correct? */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000886# endif
887# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000888 TIME_ACCESS | SORT_ATIME, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000889# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000890 TIME_ACCESS, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000891# endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000892#endif
893#ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000894 SORT_SIZE, /* S */
895 SORT_EXT, /* X */
896 SORT_ORDER_REVERSE, /* r */
897 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000898#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000899#ifdef CONFIG_FEATURE_LS_FILETYPES
900 LIST_FILETYPE | LIST_EXEC, /* F */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000901 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000902#endif
903#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000904 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000905#endif
906#ifdef CONFIG_FEATURE_LS_RECURSIVE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000907 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000908#endif
909#ifdef CONFIG_FEATURE_HUMAN_READABLE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000910 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000911#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000912#ifdef CONFIG_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000913 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
914#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000915#ifdef CONFIG_FEATURE_AUTOWIDTH
916 0, 0, /* T, w - ignored */
917#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000918 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000919};
920
921
922/*----------------------------------------------------------------------*/
923
Rob Landleydfba7412006-03-06 20:47:33 +0000924int ls_main(int argc, char **argv)
Eric Andersen11c65522000-09-07 17:24:47 +0000925{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000926 struct dnode **dnd;
927 struct dnode **dnf;
928 struct dnode **dnp;
929 struct dnode *dn;
930 struct dnode *cur;
931 long opt;
932 int nfiles = 0;
933 int dnfiles;
934 int dndirs;
935 int oi;
936 int ac;
937 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000938 char **av;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000939#ifdef CONFIG_FEATURE_AUTOWIDTH
940 char *tabstops_str = NULL;
941 char *terminal_width_str = NULL;
942#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000943#ifdef CONFIG_FEATURE_LS_COLOR
944 char *color_opt;
945#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000946
Manuel Novoa III cad53642003-03-19 09:13:01 +0000947 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000948#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000949 | TIME_MOD
Eric Andersen11c65522000-09-07 17:24:47 +0000950#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000951#ifdef CONFIG_FEATURE_LS_SORTFILES
952 | SORT_NAME | SORT_ORDER_FORWARD
953#endif
954 ;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000955
Eric Andersen6d687812003-11-04 23:16:48 +0000956#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000957 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000958 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000959 /* Go one less... */
960 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000961#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000962
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000963#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000964 bb_applet_long_options = ls_color_opt;
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000965#endif
966
Eric Andersen11c65522000-09-07 17:24:47 +0000967 /* process options */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000968#ifdef CONFIG_FEATURE_AUTOWIDTH
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000969 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
970#ifdef CONFIG_FEATURE_LS_COLOR
971 , &color_opt
972#endif
973 );
Glenn L McGrath792cae52004-01-18 05:15:16 +0000974 if (tabstops_str) {
975 tabstops = atoi(tabstops_str);
976 }
977 if (terminal_width_str) {
978 terminal_width = atoi(terminal_width_str);
979 }
980#else
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000981 opt = bb_getopt_ulflags(argc, argv, ls_options
982#ifdef CONFIG_FEATURE_LS_COLOR
983 , &color_opt
984#endif
985 );
Manuel Novoa III cad53642003-03-19 09:13:01 +0000986#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000987 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +0000988 if (opt & (1 << i)) {
989 unsigned int flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000990
Manuel Novoa III cad53642003-03-19 09:13:01 +0000991 if (flags & LIST_MASK_TRIGGER) {
992 all_fmt &= ~LIST_MASK;
993 }
994 if (flags & STYLE_MASK_TRIGGER) {
995 all_fmt &= ~STYLE_MASK;
996 }
Eric Andersenb7ebc612003-07-14 19:20:46 +0000997#ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000998 if (flags & SORT_MASK_TRIGGER) {
999 all_fmt &= ~SORT_MASK;
1000 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001001#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001002 if (flags & DISP_MASK_TRIGGER) {
1003 all_fmt &= ~DISP_MASK;
1004 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001005#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +00001006 if (flags & TIME_MASK_TRIGGER) {
1007 all_fmt &= ~TIME_MASK;
1008 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001009#endif
Eric Andersen9e480452003-07-03 10:07:04 +00001010 if (flags & LIST_CONTEXT) {
1011 all_fmt |= STYLE_SINGLE;
1012 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001013#ifdef CONFIG_FEATURE_HUMAN_READABLE
1014 if (opt == 'l') {
1015 all_fmt &= ~LS_DISP_HR;
1016 }
1017#endif
1018 all_fmt |= flags;
1019 }
Eric Andersen11c65522000-09-07 17:24:47 +00001020 }
1021
Paul Fox156dc412005-08-01 19:33:30 +00001022#ifdef CONFIG_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001023 {
1024 /* find color bit value - last position for short getopt */
Paul Fox156dc412005-08-01 19:33:30 +00001025
Paul Fox156dc412005-08-01 19:33:30 +00001026#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
Paul Fox156dc412005-08-01 19:33:30 +00001027 char *p;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001028
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001029 if ((p = getenv ("LS_COLORS")) != NULL &&
Paul Fox156dc412005-08-01 19:33:30 +00001030 (*p == '\0' || (strcmp(p, "none") == 0))) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001031 ;
Paul Fox156dc412005-08-01 19:33:30 +00001032 } else if (isatty(STDOUT_FILENO)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001033 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +00001034 }
1035#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001036
1037 if((opt & (1 << i))) { /* next flag after short options */
1038 if (color_opt == NULL || strcmp("always", color_opt) == 0)
1039 show_color = 1;
1040 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
1041 show_color = 0;
1042 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
1043 show_color = 1;
1044 }
Paul Fox156dc412005-08-01 19:33:30 +00001045 }
1046#endif
1047
Eric Andersen11c65522000-09-07 17:24:47 +00001048 /* sort out which command line options take precedence */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001049#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +00001050 if (all_fmt & DISP_NOLIST)
1051 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Eric Andersen11c65522000-09-07 17:24:47 +00001052#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001053#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001054 if (all_fmt & TIME_CHANGE)
1055 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1056 if (all_fmt & TIME_ACCESS)
1057 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
Eric Andersen11c65522000-09-07 17:24:47 +00001058#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001059 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1060 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001061#ifdef CONFIG_FEATURE_LS_USERNAME
Manuel Novoa III cad53642003-03-19 09:13:01 +00001062 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1063 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersen11c65522000-09-07 17:24:47 +00001064#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001065
Eric Andersen11c65522000-09-07 17:24:47 +00001066 /* choose a display format */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001067 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
1068#if STYLE_AUTO != 0
1069 all_fmt = (all_fmt & ~STYLE_MASK)
Eric Andersen70060d22004-03-27 10:02:48 +00001070 | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001071#else
Eric Andersen70060d22004-03-27 10:02:48 +00001072 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001073#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001074
1075 /*
1076 * when there are no cmd line args we have to supply a default "." arg.
1077 * we will create a second argv array, "av" that will hold either
1078 * our created "." arg, or the real cmd line args. The av array
1079 * just holds the pointers- we don't move the date the pointers
1080 * point to.
1081 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001082 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +00001083 if (ac < 1) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001084 static const char * const dotdir[] = { "." };
1085
1086 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001087 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +00001088 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001089 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +00001090 }
1091
1092 /* now, everything is in the av array */
1093 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001094 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001095
1096 /* stuff the command line file names into an dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001097 dn = NULL;
1098 for (oi = 0; oi < ac; oi++) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001099 cur = my_stat(av[oi], av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001100 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001101 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001102 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001103 cur->next = dn;
1104 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001105 nfiles++;
1106 }
1107
1108 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001109 ** allocate memory for an array to hold dnode pointers
1110 */
1111 dnp = dnalloc(nfiles);
1112 for (i = 0, cur = dn; i < nfiles; i++) {
1113 dnp[i] = cur; /* save pointer to node in array */
1114 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +00001115 }
1116
Manuel Novoa III cad53642003-03-19 09:13:01 +00001117 if (all_fmt & DISP_NOLIST) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001118#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +00001119 dnsort(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001120#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001121 if (nfiles > 0)
1122 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001123 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001124 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1125 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1126 dndirs = countdirs(dnp, nfiles);
1127 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001128 if (dnfiles > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001129#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +00001130 dnsort(dnf, dnfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001131#endif
1132 showfiles(dnf, dnfiles);
Rob Landley26314862006-05-02 19:46:52 +00001133 if (ENABLE_FEATURE_CLEAN_UP)
1134 free(dnf);
Eric Andersen11c65522000-09-07 17:24:47 +00001135 }
1136 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001137#ifdef CONFIG_FEATURE_LS_SORTFILES
Rob Landley425e7582006-05-03 20:22:03 +00001138 dnsort(dnd, dndirs);
Eric Andersen11c65522000-09-07 17:24:47 +00001139#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +00001140 showdirs(dnd, dndirs, dnfiles == 0);
Rob Landley26314862006-05-02 19:46:52 +00001141 if (ENABLE_FEATURE_CLEAN_UP)
1142 free(dnd);
Eric Andersen11c65522000-09-07 17:24:47 +00001143 }
1144 }
Rob Landley26314862006-05-02 19:46:52 +00001145 if (ENABLE_FEATURE_CLEAN_UP)
1146 dfree(dnp, nfiles);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001147 return (status);
Eric Andersencc8ed391999-10-05 16:24:54 +00001148}