blob: 489c29ad1370489ad517d3272f87113d1b8167a5 [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 *
Eric Andersencc8ed391999-10-05 16:24:54 +00006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * To achieve a small memory footprint, this version of 'ls' doesn't do any
23 * file sorting, and only has the most essential command line switches
Eric Andersen77d92682001-05-23 20:32:09 +000024 * (i.e., the ones I couldn't live without :-) All features which involve
Eric Andersencc8ed391999-10-05 16:24:54 +000025 * linking in substantial chunks of libc can be disabled.
26 *
27 * Although I don't really want to add new features to this program to
28 * keep it small, I *am* interested to receive bug fixes and ways to make
29 * it more portable.
30 *
31 * KNOWN BUGS:
Erik Andersen9ffdaa62000-02-11 21:55:04 +000032 * 1. ls -l of a directory doesn't give "total <blocks>" header
33 * 2. ls of a symlink to a directory doesn't list directory contents
34 * 3. hidden files can make column width too large
35 *
Eric Andersencc8ed391999-10-05 16:24:54 +000036 * NON-OPTIMAL BEHAVIOUR:
37 * 1. autowidth reads directories twice
38 * 2. if you do a short directory listing without filetype characters
39 * appended, there's no need to stat each one
40 * PORTABILITY:
41 * 1. requires lstat (BSD) - how do you do it without?
42 */
43
Eric Andersene57d54b2001-01-30 18:03:11 +000044enum {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +000045 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +000046 COLUMN_GAP = 2, /* includes the file type char */
Eric Andersene57d54b2001-01-30 18:03:11 +000047};
48
Eric Andersencc8ed391999-10-05 16:24:54 +000049/************************************************************************/
50
Eric Andersen11c65522000-09-07 17:24:47 +000051#include <sys/types.h>
52#include <sys/stat.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000053#include <stdio.h>
54#include <unistd.h>
55#include <dirent.h>
56#include <errno.h>
57#include <stdio.h>
Eric Andersen11c65522000-09-07 17:24:47 +000058#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000059#include <stdlib.h>
Eric Andersen5307eca2001-01-26 01:52:43 +000060#include <fcntl.h>
61#include <signal.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000062#include <termios.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000063#include <getopt.h> /* struct option */
Eric Andersen5307eca2001-01-26 01:52:43 +000064#include <sys/ioctl.h>
Eric Andersen4f807a82004-07-26 09:11:12 +000065#include <sys/sysmacros.h> /* major() and minor() */
Eric Andersencbe31da2001-02-20 06:14:08 +000066#include "busybox.h"
Eric Andersen9e480452003-07-03 10:07:04 +000067#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000068#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000069#endif
Eric Andersen5307eca2001-01-26 01:52:43 +000070
Eric Andersenbdfd0d72001-10-24 05:00:29 +000071#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersenf1142c52001-02-20 06:16:29 +000072#include <time.h>
73#endif
74
Eric Andersen11c65522000-09-07 17:24:47 +000075/* what is the overall style of the listing */
Manuel Novoa III cad53642003-03-19 09:13:01 +000076#define STYLE_AUTO (0)
77#define STYLE_COLUMNS (1U<<21) /* fill columns */
78#define STYLE_LONG (2U<<21) /* one record per line, extended info */
79#define STYLE_SINGLE (3U<<21) /* one record per line */
80
81#define STYLE_MASK STYLE_SINGLE
82#define STYLE_ONE_RECORD_FLAG STYLE_LONG
Eric Andersencc8ed391999-10-05 16:24:54 +000083
Eric Andersen11c65522000-09-07 17:24:47 +000084/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
85/* what file information will be listed */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000086#define LIST_INO (1U<<0)
87#define LIST_BLOCKS (1U<<1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000088#define LIST_MODEBITS (1U<<2)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000089#define LIST_NLINKS (1U<<3)
Manuel Novoa III cad53642003-03-19 09:13:01 +000090#define LIST_ID_NAME (1U<<4)
91#define LIST_ID_NUMERIC (1U<<5)
Eric Andersen9e480452003-07-03 10:07:04 +000092#define LIST_CONTEXT (1U<<6)
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +000093#define LIST_SIZE (1U<<7)
94#define LIST_DEV (1U<<8)
Eric Andersen9e480452003-07-03 10:07:04 +000095#define LIST_DATE_TIME (1U<<9)
96#define LIST_FULLTIME (1U<<10)
97#define LIST_FILENAME (1U<<11)
98#define LIST_SYMLINK (1U<<12)
99#define LIST_FILETYPE (1U<<13)
100#define LIST_EXEC (1U<<14)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101
102#define LIST_MASK ((LIST_EXEC << 1) - 1)
Eric Andersen11c65522000-09-07 17:24:47 +0000103
104/* what files will be displayed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105/* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000106#define DISP_NORMAL (1U<<14) /* show normal filenames */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000108#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
109#define DISP_DOT (1U<<17) /* show . and .. */
110#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000112#define DISP_ROWS (1U<<20) /* print across rows */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113
114#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1))
Eric Andersen11c65522000-09-07 17:24:47 +0000115
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000116#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000117/* how will the files be sorted */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000118#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
120
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000121#define SORT_NAME 0 /* sort by file name */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122#define SORT_SIZE (1U<<28) /* sort by file size */
123#define SORT_ATIME (2U<<28) /* sort by last access time */
124#define SORT_CTIME (3U<<28) /* sort by last change time */
125#define SORT_MTIME (4U<<28) /* sort by last modification time */
126#define SORT_VERSION (5U<<28) /* sort by version */
127#define SORT_EXT (6U<<28) /* sort by file name extension */
128#define SORT_DIR (7U<<28) /* sort by file or directory */
129
130#define SORT_MASK (7U<<28)
Eric Andersencc8ed391999-10-05 16:24:54 +0000131#endif
132
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000133#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000134/* which of the three times will be used */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135#define TIME_MOD 0
136#define TIME_CHANGE (1U<<23)
137#define TIME_ACCESS (1U<<24)
138
139#define TIME_MASK (3U<<23)
140#endif
141
142#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
143#define FOLLOW_LINKS (1U<<25)
144#endif
145#ifdef CONFIG_FEATURE_HUMAN_READABLE
146#define LS_DISP_HR (1U<<26)
Eric Andersencc8ed391999-10-05 16:24:54 +0000147#endif
148
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000149#define LIST_SHORT (LIST_FILENAME)
150#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
151#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
152 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
153#define LIST_ILONG (LIST_INO | LIST_LONG)
Eric Andersencc8ed391999-10-05 16:24:54 +0000154
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155#define SPLIT_DIR 1
156#define SPLIT_FILE 0
157#define SPLIT_SUBDIR 2
Eric Andersencc8ed391999-10-05 16:24:54 +0000158
Eric Andersen11c65522000-09-07 17:24:47 +0000159#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
160#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000161
Eric Andersend598d412002-04-27 09:19:39 +0000162#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000163# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
Eric Andersen11c65522000-09-07 17:24:47 +0000164#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000165
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000166/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
167#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +0000168
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000169static int show_color = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000170
Paul Fox156dc412005-08-01 19:33:30 +0000171/* long option entry used only for --color, which has no short option
172 * equivalent. */
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000173static const struct option ls_color_opt[] =
Paul Fox156dc412005-08-01 19:33:30 +0000174{
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000175 {"color", optional_argument, NULL, 1},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000176 {NULL, 0, NULL, 0}
Paul Fox156dc412005-08-01 19:33:30 +0000177};
178
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000179#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000180 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000181#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000182 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000183#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184
Eric Andersen11c65522000-09-07 17:24:47 +0000185/*
186 * a directory entry and its stat info are stored here
187 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000188struct dnode { /* the basic node */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000189 char *name; /* the dir entry name */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000190 char *fullname; /* the dir entry name */
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000191 int allocated;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000192 struct stat dstat; /* the file stat info */
Eric Andersen9e480452003-07-03 10:07:04 +0000193#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000194 security_context_t sid;
Eric Andersen9e480452003-07-03 10:07:04 +0000195#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000196 struct dnode *next; /* point at the next node */
Eric Andersen11c65522000-09-07 17:24:47 +0000197};
198typedef struct dnode dnode_t;
199
Glenn L McGrath4d001292003-01-06 01:11:50 +0000200static struct dnode **list_dir(const char *);
Eric Andersen3e6ff902001-03-09 21:24:12 +0000201static struct dnode **dnalloc(int);
202static int list_single(struct dnode *);
Eric Andersen11c65522000-09-07 17:24:47 +0000203
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204static unsigned int all_fmt;
Eric Andersen11c65522000-09-07 17:24:47 +0000205
Eric Andersen9e480452003-07-03 10:07:04 +0000206#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000207static int selinux_enabled= 0;
Eric Andersen9e480452003-07-03 10:07:04 +0000208#endif
209
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000210#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000211static int terminal_width = TERMINAL_WIDTH;
Eric Andersene57d54b2001-01-30 18:03:11 +0000212static unsigned short tabstops = COLUMN_GAP;
Eric Andersenf5d5e772001-01-24 23:34:48 +0000213#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000214#define tabstops COLUMN_GAP
215#define terminal_width TERMINAL_WIDTH
Eric Andersen11c65522000-09-07 17:24:47 +0000216#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000217
Matt Kraai33fdae52000-10-13 17:59:43 +0000218static int status = EXIT_SUCCESS;
219
Glenn L McGrath4d001292003-01-06 01:11:50 +0000220static struct dnode *my_stat(char *fullname, char *name)
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000221{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000222 struct stat dstat;
223 struct dnode *cur;
Eric Andersen9e480452003-07-03 10:07:04 +0000224#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000225 security_context_t sid=NULL;
Eric Andersen9e480452003-07-03 10:07:04 +0000226#endif
227 int rc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000228
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000229#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230 if (all_fmt & FOLLOW_LINKS) {
Eric Andersen9e480452003-07-03 10:07:04 +0000231#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000232 if (is_selinux_enabled()) {
233 rc=0; /* Set the number which means success before hand. */
234 rc = getfilecon(fullname,&sid);
235 }
Eric Andersen9e480452003-07-03 10:07:04 +0000236#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000237 rc = stat(fullname, &dstat);
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000238 if(rc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000239 bb_perror_msg("%s", fullname);
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000240 status = EXIT_FAILURE;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000241 return 0;
Matt Kraai9a6e67c2000-10-13 18:03:21 +0000242 }
243 } else
244#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000245 {
246#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000247 if (is_selinux_enabled()) {
248 rc=0; /* Set the number which means success before hand. */
249 rc = lgetfilecon(fullname,&sid);
250 }
Eric Andersen9e480452003-07-03 10:07:04 +0000251#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000252 rc = lstat(fullname, &dstat);
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000253 if(rc) {
Eric Andersen9e480452003-07-03 10:07:04 +0000254 bb_perror_msg("%s", fullname);
255 status = EXIT_FAILURE;
256 return 0;
257 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000258 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000259
260 cur = (struct dnode *) xmalloc(sizeof(struct dnode));
261 cur->fullname = fullname;
262 cur->name = name;
263 cur->dstat = dstat;
Eric Andersen9e480452003-07-03 10:07:04 +0000264#ifdef CONFIG_SELINUX
265 cur->sid = sid;
266#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000267 return cur;
Eric Andersen79565b62000-08-11 18:10:21 +0000268}
269
Eric Andersen11c65522000-09-07 17:24:47 +0000270/*----------------------------------------------------------------------*/
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000271#ifdef CONFIG_FEATURE_LS_COLOR
272static char fgcolor(mode_t mode)
273{
274 /* Check wheter the file is existing (if so, color it red!) */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000275 if (errno == ENOENT) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000276 return '\037';
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000277 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000278 if (LIST_EXEC && S_ISREG(mode)
279 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000280 return COLOR(0xF000); /* File is executable ... */
281 return COLOR(mode);
282}
283
284/*----------------------------------------------------------------------*/
285static char bgcolor(mode_t mode)
286{
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000287 if (LIST_EXEC && S_ISREG(mode)
288 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000289 return ATTR(0xF000); /* File is executable ... */
290 return ATTR(mode);
291}
292#endif
293
294/*----------------------------------------------------------------------*/
Eric Andersend598d412002-04-27 09:19:39 +0000295#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000296static char append_char(mode_t mode)
Eric Andersen79565b62000-08-11 18:10:21 +0000297{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 if (!(all_fmt & LIST_FILETYPE))
Eric Andersen11c65522000-09-07 17:24:47 +0000299 return '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300 if ((all_fmt & LIST_EXEC) && S_ISREG(mode)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000301 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
302 return '*';
303 return APPCHAR(mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000304}
305#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000306
Eric Andersen11c65522000-09-07 17:24:47 +0000307/*----------------------------------------------------------------------*/
Eric Andersencf1189f2000-11-29 21:52:06 +0000308
Manuel Novoa III cad53642003-03-19 09:13:01 +0000309#define countdirs(A,B) count_dirs((A), (B), 1)
310#define countsubdirs(A,B) count_dirs((A), (B), 0)
311
312static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
Eric Andersen11c65522000-09-07 17:24:47 +0000313{
314 int i, dirs;
315
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000316 if (dn == NULL || nfiles < 1)
317 return (0);
318 dirs = 0;
319 for (i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320 if (S_ISDIR(dn[i]->dstat.st_mode)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000321 && (notsubdirs ||
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000322 ((dn[i]->name[0] != '.') || (dn[i]->name[1]
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 && ((dn[i]->name[1] != '.')
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000324 || dn[i]->name[2])))))
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000325 dirs++;
Eric Andersen11c65522000-09-07 17:24:47 +0000326 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000327 return (dirs);
Eric Andersen11c65522000-09-07 17:24:47 +0000328}
329
Eric Andersen3e6ff902001-03-09 21:24:12 +0000330static int countfiles(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000331{
332 int nfiles;
333 struct dnode *cur;
334
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000335 if (dnp == NULL)
336 return (0);
337 nfiles = 0;
338 for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
339 nfiles++;
Eric Andersen11c65522000-09-07 17:24:47 +0000340 nfiles++;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000341 return (nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000342}
343
344/* get memory to hold an array of pointers */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000345static struct dnode **dnalloc(int num)
Eric Andersen11c65522000-09-07 17:24:47 +0000346{
347 struct dnode **p;
348
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000349 if (num < 1)
350 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000351
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000352 p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *)));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000353 return (p);
Eric Andersen11c65522000-09-07 17:24:47 +0000354}
355
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000356#ifdef CONFIG_FEATURE_LS_RECURSIVE
Eric Andersen3e6ff902001-03-09 21:24:12 +0000357static void dfree(struct dnode **dnp)
Eric Andersen11c65522000-09-07 17:24:47 +0000358{
359 struct dnode *cur, *next;
360
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000361 if (dnp == NULL)
362 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000363
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000364 cur = dnp[0];
Eric Andersen11c65522000-09-07 17:24:47 +0000365 while (cur != NULL) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000366 if(cur->allocated)
367 free(cur->fullname); /* free the filename */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000368 next = cur->next;
369 free(cur); /* free the dnode */
370 cur = next;
Eric Andersen11c65522000-09-07 17:24:47 +0000371 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000372 free(dnp); /* free the array holding the dnode pointers */
Eric Andersen11c65522000-09-07 17:24:47 +0000373}
Eric Andersen20aab262001-07-19 22:28:02 +0000374#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000375
Eric Andersen3e6ff902001-03-09 21:24:12 +0000376static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
Eric Andersen11c65522000-09-07 17:24:47 +0000377{
378 int dncnt, i, d;
379 struct dnode **dnp;
380
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000381 if (dn == NULL || nfiles < 1)
382 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000383
384 /* count how many dirs and regular files there are */
Eric Andersencf1189f2000-11-29 21:52:06 +0000385 if (which == SPLIT_SUBDIR)
386 dncnt = countsubdirs(dn, nfiles);
387 else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000388 dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */
Eric Andersencf1189f2000-11-29 21:52:06 +0000389 if (which == SPLIT_FILE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000390 dncnt = nfiles - dncnt; /* looking for files */
Eric Andersencf1189f2000-11-29 21:52:06 +0000391 }
Eric Andersen11c65522000-09-07 17:24:47 +0000392
393 /* allocate a file array and a dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000394 dnp = dnalloc(dncnt);
Eric Andersen11c65522000-09-07 17:24:47 +0000395
396 /* copy the entrys into the file or dir array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000397 for (d = i = 0; i < nfiles; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000398 if (S_ISDIR(dn[i]->dstat.st_mode)) {
399 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
400 if ((which & SPLIT_DIR)
401 || ((dn[i]->name[0] != '.')
402 || (dn[i]->name[1]
403 && ((dn[i]->name[1] != '.')
404 || dn[i]->name[2])))) {
405 dnp[d++] = dn[i];
406 }
407 }
408 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
409 dnp[d++] = dn[i];
Eric Andersen11c65522000-09-07 17:24:47 +0000410 }
411 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000412 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000413}
414
415/*----------------------------------------------------------------------*/
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000416#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen3e6ff902001-03-09 21:24:12 +0000417static int sortcmp(struct dnode *d1, struct dnode *d2)
Eric Andersen11c65522000-09-07 17:24:47 +0000418{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000419 unsigned int sort_opts = all_fmt & SORT_MASK;
420 int dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000421
Manuel Novoa III cad53642003-03-19 09:13:01 +0000422 dif = 0; /* assume SORT_NAME */
Eric Andersen11c65522000-09-07 17:24:47 +0000423 if (sort_opts == SORT_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000424 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
Eric Andersen11c65522000-09-07 17:24:47 +0000425 } else if (sort_opts == SORT_ATIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000426 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
Eric Andersen11c65522000-09-07 17:24:47 +0000427 } else if (sort_opts == SORT_CTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000428 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
Eric Andersen11c65522000-09-07 17:24:47 +0000429 } else if (sort_opts == SORT_MTIME) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000430 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
Eric Andersen11c65522000-09-07 17:24:47 +0000431 } else if (sort_opts == SORT_DIR) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000432 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000433 /* } else if (sort_opts == SORT_VERSION) { */
434 /* } else if (sort_opts == SORT_EXT) { */
Eric Andersen11c65522000-09-07 17:24:47 +0000435 }
436
Eric Andersen11c65522000-09-07 17:24:47 +0000437 if (dif == 0) {
438 /* sort by name- may be a tie_breaker for time or size cmp */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000439#ifdef CONFIG_LOCALE_SUPPORT
440 dif = strcoll(d1->name, d2->name);
441#else
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000442 dif = strcmp(d1->name, d2->name);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000443#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000444 }
445
Manuel Novoa III cad53642003-03-19 09:13:01 +0000446 if (all_fmt & SORT_ORDER_REVERSE) {
447 dif = -dif;
Eric Andersen11c65522000-09-07 17:24:47 +0000448 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000449 return (dif);
Eric Andersen11c65522000-09-07 17:24:47 +0000450}
451
452/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000453static void shellsort(struct dnode **dn, int size)
Eric Andersen11c65522000-09-07 17:24:47 +0000454{
455 struct dnode *temp;
456 int gap, i, j;
457
458 /* shell short the array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000459 if (dn == NULL || size < 2)
460 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000461
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000462 for (gap = size / 2; gap > 0; gap /= 2) {
463 for (i = gap; i < size; i++) {
464 for (j = i - gap; j >= 0; j -= gap) {
465 if (sortcmp(dn[j], dn[j + gap]) <= 0)
Eric Andersen11c65522000-09-07 17:24:47 +0000466 break;
467 /* they are out of order, swap them */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000468 temp = dn[j];
469 dn[j] = dn[j + gap];
470 dn[j + gap] = temp;
Eric Andersen11c65522000-09-07 17:24:47 +0000471 }
472 }
473 }
474}
475#endif
476
477/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000478static void showfiles(struct dnode **dn, int nfiles)
Eric Andersen11c65522000-09-07 17:24:47 +0000479{
480 int i, ncols, nrows, row, nc;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000481 int column = 0;
482 int nexttab = 0;
483 int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */
Eric Andersen11c65522000-09-07 17:24:47 +0000484
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000485 if (dn == NULL || nfiles < 1)
486 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000487
Manuel Novoa III cad53642003-03-19 09:13:01 +0000488 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000489 ncols = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000490 } else {
491 /* find the longest file name- use that as the column width */
492 for (i = 0; i < nfiles; i++) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000493 int len = strlen(dn[i]->name) +
Eric Andersen9e480452003-07-03 10:07:04 +0000494#ifdef CONFIG_SELINUX
495 ((all_fmt & LIST_CONTEXT) ? 33 : 0) +
496#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000497 ((all_fmt & LIST_INO) ? 8 : 0) +
498 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
499 if (column_width < len)
500 column_width = len;
501 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000502 column_width += tabstops;
503 ncols = (int) (terminal_width / column_width);
Eric Andersen11c65522000-09-07 17:24:47 +0000504 }
505
Eric Andersene57d54b2001-01-30 18:03:11 +0000506 if (ncols > 1) {
507 nrows = nfiles / ncols;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000508 if ((nrows * ncols) < nfiles)
509 nrows++; /* round up fractionals */
Eric Andersene57d54b2001-01-30 18:03:11 +0000510 } else {
511 nrows = nfiles;
512 ncols = 1;
513 }
Eric Andersen11c65522000-09-07 17:24:47 +0000514
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000515 for (row = 0; row < nrows; row++) {
516 for (nc = 0; nc < ncols; nc++) {
Eric Andersen79565b62000-08-11 18:10:21 +0000517 /* reach into the array based on the column and row */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000518 i = (nc * nrows) + row; /* assume display by column */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000519 if (all_fmt & DISP_ROWS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000520 i = (row * ncols) + nc; /* display across row */
Eric Andersen11c65522000-09-07 17:24:47 +0000521 if (i < nfiles) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000522 if (column > 0) {
523 nexttab -= column;
524 while (nexttab--) {
525 putchar(' ');
526 column++;
527 }
Eric Andersen79565b62000-08-11 18:10:21 +0000528 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000529 nexttab = column + column_width;
530 column += list_single(dn[i]);
Eric Andersen79565b62000-08-11 18:10:21 +0000531 }
Glenn L McGrath4d001292003-01-06 01:11:50 +0000532 }
533 putchar('\n');
534 column = 0;
Eric Andersena42982e2000-06-07 17:28:53 +0000535 }
Eric Andersen11c65522000-09-07 17:24:47 +0000536}
537
538/*----------------------------------------------------------------------*/
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000539static void showdirs(struct dnode **dn, int ndirs, int first)
Eric Andersen11c65522000-09-07 17:24:47 +0000540{
541 int i, nfiles;
542 struct dnode **subdnp;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000543
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000544#ifdef CONFIG_FEATURE_LS_RECURSIVE
Eric Andersen11c65522000-09-07 17:24:47 +0000545 int dndirs;
546 struct dnode **dnd;
547#endif
548
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000549 if (dn == NULL || ndirs < 1)
550 return;
Eric Andersen11c65522000-09-07 17:24:47 +0000551
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000552 for (i = 0; i < ndirs; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000553 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000554 if (!first)
555 printf("\n");
556 first = 0;
557 printf("%s:\n", dn[i]->fullname);
Eric Andersen11c65522000-09-07 17:24:47 +0000558 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000559 subdnp = list_dir(dn[i]->fullname);
560 nfiles = countfiles(subdnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000561 if (nfiles > 0) {
562 /* list all files at this level */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000563#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000564 shellsort(subdnp, nfiles);
565#endif
566 showfiles(subdnp, nfiles);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000567#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000568 if (all_fmt & DISP_RECURSIVE) {
Eric Andersen11c65522000-09-07 17:24:47 +0000569 /* recursive- list the sub-dirs */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000570 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
571 dndirs = countsubdirs(subdnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +0000572 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000573#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +0000574 shellsort(dnd, dndirs);
Matt Kraaia5bd2682000-10-28 06:40:09 +0000575#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +0000576 showdirs(dnd, dndirs, 0);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000577 free(dnd); /* free the array of dnode pointers to the dirs */
Eric Andersen11c65522000-09-07 17:24:47 +0000578 }
579 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000580 dfree(subdnp); /* free the dnodes and the fullname mem */
Eric Andersen11c65522000-09-07 17:24:47 +0000581#endif
582 }
583 }
584}
585
586/*----------------------------------------------------------------------*/
Glenn L McGrath4d001292003-01-06 01:11:50 +0000587static struct dnode **list_dir(const char *path)
Eric Andersen11c65522000-09-07 17:24:47 +0000588{
589 struct dnode *dn, *cur, **dnp;
590 struct dirent *entry;
591 DIR *dir;
Eric Andersen11c65522000-09-07 17:24:47 +0000592 int i, nfiles;
593
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000594 if (path == NULL)
595 return (NULL);
Eric Andersen11c65522000-09-07 17:24:47 +0000596
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000597 dn = NULL;
598 nfiles = 0;
Eric Andersene7e1e2d2000-10-12 22:40:14 +0000599 dir = opendir(path);
Eric Andersen11c65522000-09-07 17:24:47 +0000600 if (dir == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000601 bb_perror_msg("%s", path);
Matt Kraai33fdae52000-10-13 17:59:43 +0000602 status = EXIT_FAILURE;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000603 return (NULL); /* could not open the dir */
Eric Andersen11c65522000-09-07 17:24:47 +0000604 }
605 while ((entry = readdir(dir)) != NULL) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000606 char *fullname;
607
Eric Andersen11c65522000-09-07 17:24:47 +0000608 /* are we going to list the file- it may be . or .. or a hidden file */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000609 if (entry->d_name[0] == '.') {
610 if ((entry->d_name[1] == 0 || (
611 entry->d_name[1] == '.'
612 && entry->d_name[2] == 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000613 && !(all_fmt & DISP_DOT))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000614 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000615 if (!(all_fmt & DISP_HIDDEN))
Matt Kraai782ab3c2001-04-23 01:07:00 +0000616 continue;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000617 }
618 fullname = concat_path_file(path, entry->d_name);
619 cur = my_stat(fullname, strrchr(fullname, '/') + 1);
620 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +0000621 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000622 cur->allocated = 1;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000623 cur->next = dn;
624 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +0000625 nfiles++;
626 }
627 closedir(dir);
628
629 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000630 ** allocate memory for an array to hold dnode pointers
631 */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000632 if (dn == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000633 return (NULL);
634 dnp = dnalloc(nfiles);
635 for (i = 0, cur = dn; i < nfiles; i++) {
636 dnp[i] = cur; /* save pointer to node in array */
637 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +0000638 }
639
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000640 return (dnp);
Eric Andersen11c65522000-09-07 17:24:47 +0000641}
642
643/*----------------------------------------------------------------------*/
Eric Andersen3e6ff902001-03-09 21:24:12 +0000644static int list_single(struct dnode *dn)
Eric Andersen11c65522000-09-07 17:24:47 +0000645{
Glenn L McGrath4d001292003-01-06 01:11:50 +0000646 int i, column = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000647
Matt Kraaia1bbde72002-03-08 16:25:33 +0000648#ifdef CONFIG_FEATURE_LS_USERNAME
Glenn L McGrath4d001292003-01-06 01:11:50 +0000649 char scratch[16];
Matt Kraaia1bbde72002-03-08 16:25:33 +0000650#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000651#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Eric Andersen11c65522000-09-07 17:24:47 +0000652 char *filetime;
653 time_t ttime, age;
654#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000655#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
Eric Andersen11c65522000-09-07 17:24:47 +0000656 struct stat info;
657 char append;
658#endif
659
Glenn L McGrath4d001292003-01-06 01:11:50 +0000660 if (dn->fullname == NULL)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000661 return (0);
Eric Andersen11c65522000-09-07 17:24:47 +0000662
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000663#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000664 ttime = dn->dstat.st_mtime; /* the default time */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000665 if (all_fmt & TIME_ACCESS)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000666 ttime = dn->dstat.st_atime;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000667 if (all_fmt & TIME_CHANGE)
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000668 ttime = dn->dstat.st_ctime;
669 filetime = ctime(&ttime);
Eric Andersen11c65522000-09-07 17:24:47 +0000670#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000671#ifdef CONFIG_FEATURE_LS_FILETYPES
Eric Andersen11c65522000-09-07 17:24:47 +0000672 append = append_char(dn->dstat.st_mode);
673#endif
674
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000675 for (i = 0; i <= 31; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000676 switch (all_fmt & (1 << i)) {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000677 case LIST_INO:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000678 column += printf("%7ld ", (long int) dn->dstat.st_ino);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000679 break;
680 case LIST_BLOCKS:
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000681#if _FILE_OFFSET_BITS == 64
Eric Andersen5e678872006-01-30 19:48:23 +0000682 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000683#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000684 column += printf("%4ld ", dn->dstat.st_blocks >> 1);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000685#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000686 break;
687 case LIST_MODEBITS:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000688 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000689 break;
690 case LIST_NLINKS:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000691 column += printf("%4ld ", (long) dn->dstat.st_nlink);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000692 break;
693 case LIST_ID_NAME:
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000694#ifdef CONFIG_FEATURE_LS_USERNAME
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000695 bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000696 printf("%-8.8s ", scratch);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000697 bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000698 printf("%-8.8s", scratch);
699 column += 17;
700 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000701#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000702 case LIST_ID_NUMERIC:
Glenn L McGrath4d001292003-01-06 01:11:50 +0000703 column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid);
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000704 break;
705 case LIST_SIZE:
706 case LIST_DEV:
707 if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
Eric Andersen4f807a82004-07-26 09:11:12 +0000708 column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
709 (int) minor(dn->dstat.st_rdev));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000710 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000711#ifdef CONFIG_FEATURE_HUMAN_READABLE
Manuel Novoa III cad53642003-03-19 09:13:01 +0000712 if (all_fmt & LS_DISP_HR) {
Glenn L McGrath4d001292003-01-06 01:11:50 +0000713 column += printf("%9s ",
714 make_human_readable_str(dn->dstat.st_size, 1, 0));
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000715 } else
716#endif
717 {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000718#if _FILE_OFFSET_BITS == 64
Glenn L McGrath4d001292003-01-06 01:11:50 +0000719 column += printf("%9lld ", (long long) dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000720#else
Glenn L McGrath4d001292003-01-06 01:11:50 +0000721 column += printf("%9ld ", dn->dstat.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000722#endif
Eric Andersen11c65522000-09-07 17:24:47 +0000723 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000724 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000725 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000726#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000727 case LIST_FULLTIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000728 printf("%24.24s ", filetime);
729 column += 25;
730 break;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000731 case LIST_DATE_TIME:
Glenn L McGrath65b6d8b2004-01-18 05:41:30 +0000732 if ((all_fmt & LIST_FULLTIME) == 0) {
733 age = time(NULL) - ttime;
734 printf("%6.6s ", filetime + 4);
735 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
736 /* hh:mm if less than 6 months old */
737 printf("%5.5s ", filetime + 11);
738 } else {
739 printf(" %4.4s ", filetime + 20);
740 }
741 column += 13;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000742 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000743 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000744#endif
Eric Andersen9e480452003-07-03 10:07:04 +0000745#ifdef CONFIG_SELINUX
746 case LIST_CONTEXT:
747 {
Rob Landley60158cb2005-05-03 06:25:50 +0000748 char context[80];
749 int len;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000750
Rob Landley60158cb2005-05-03 06:25:50 +0000751 if (dn->sid) {
752 /* I assume sid initilized with NULL */
753 len = strlen(dn->sid)+1;
754 safe_strncpy(context, dn->sid, len);
755 freecon(dn->sid);
756 }else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000757 safe_strncpy(context, "unknown", 8);
Eric Andersen9e480452003-07-03 10:07:04 +0000758 }
759 printf("%-32s ", context);
760 column += MAX(33, len);
761 }
762 break;
763#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000764 case LIST_FILENAME:
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000765#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000766 errno = 0;
767 if (show_color && !lstat(dn->fullname, &info)) {
768 printf("\033[%d;%dm", bgcolor(info.st_mode),
769 fgcolor(info.st_mode));
770 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000771#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000772 column += printf("%s", dn->name);
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000773#ifdef CONFIG_FEATURE_LS_COLOR
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000774 if (show_color) {
775 printf("\033[0m");
776 }
Eric Andersen3ad0bd92002-03-20 09:13:48 +0000777#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000778 break;
779 case LIST_SYMLINK:
780 if (S_ISLNK(dn->dstat.st_mode)) {
781 char *lpath = xreadlink(dn->fullname);
782
783 if (lpath) {
784 printf(" -> ");
785#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
786 if (!stat(dn->fullname, &info)) {
787 append = append_char(info.st_mode);
Eric Andersen11c65522000-09-07 17:24:47 +0000788 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000789#endif
790#ifdef CONFIG_FEATURE_LS_COLOR
791 if (show_color) {
792 errno = 0;
793 printf("\033[%d;%dm", bgcolor(info.st_mode),
794 fgcolor(info.st_mode));
795 }
796#endif
Glenn L McGrath4d001292003-01-06 01:11:50 +0000797 column += printf("%s", lpath) + 4;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000798#ifdef CONFIG_FEATURE_LS_COLOR
799 if (show_color) {
800 printf("\033[0m");
801 }
802#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000803 free(lpath);
Eric Andersen11c65522000-09-07 17:24:47 +0000804 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000805 }
806 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000807#ifdef CONFIG_FEATURE_LS_FILETYPES
Glenn L McGrathe3906fc2002-08-22 18:13:54 +0000808 case LIST_FILETYPE:
809 if (append != '\0') {
810 printf("%1c", append);
811 column++;
812 }
813 break;
Eric Andersen11c65522000-09-07 17:24:47 +0000814#endif
815 }
816 }
817
Glenn L McGrath4d001292003-01-06 01:11:50 +0000818 return column;
Eric Andersen11c65522000-09-07 17:24:47 +0000819}
820
821/*----------------------------------------------------------------------*/
Manuel Novoa III cad53642003-03-19 09:13:01 +0000822
Glenn L McGrath792cae52004-01-18 05:15:16 +0000823/* "[-]Cadil1", POSIX mandated options, busybox always supports */
824/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
825/* "[-]Ak" GNU options, busybox always supports */
826/* "[-]FLRctur", POSIX mandated options, busybox optionally supports */
827/* "[-]p", POSIX non-mandated options, busybox optionally supports */
828/* "[-]SXvThw", GNU options, busybox optionally supports */
829/* "[-]K", SELinux mandated options, busybox optionally supports */
830/* "[-]e", I think we made this one up */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000831
Glenn L McGrath792cae52004-01-18 05:15:16 +0000832#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
833# define LS_STR_TIMESTAMPS "cetu"
834#else
835# define LS_STR_TIMESTAMPS ""
836#endif
837
838#ifdef CONFIG_FEATURE_LS_SORTFILES
839# define LS_STR_SORTFILES "SXrv"
840#else
841# define LS_STR_SORTFILES ""
842#endif
843
844#ifdef CONFIG_FEATURE_LS_FILETYPES
845# define LS_STR_FILETYPES "Fp"
846#else
847# define LS_STR_FILETYPES ""
848#endif
849
850#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
851# define LS_STR_FOLLOW_LINKS "L"
852#else
853# define LS_STR_FOLLOW_LINKS ""
854#endif
855
856#ifdef CONFIG_FEATURE_LS_RECURSIVE
857# define LS_STR_RECURSIVE "R"
858#else
859# define LS_STR_RECURSIVE ""
860#endif
861
862#ifdef CONFIG_FEATURE_HUMAN_READABLE
863# define LS_STR_HUMAN_READABLE "h"
864#else
865# define LS_STR_HUMAN_READABLE ""
866#endif
867
868#ifdef CONFIG_SELINUX
869# define LS_STR_SELINUX "K"
870#else
871# define LS_STR_SELINUX ""
872#endif
873
874#ifdef CONFIG_FEATURE_AUTOWIDTH
875# define LS_STR_AUTOWIDTH "T:w:"
876#else
877# define LS_STR_AUTOWIDTH ""
878#endif
879
880static const char ls_options[]="Cadil1gnsxAk" \
881 LS_STR_TIMESTAMPS \
882 LS_STR_SORTFILES \
883 LS_STR_FILETYPES \
884 LS_STR_FOLLOW_LINKS \
885 LS_STR_RECURSIVE \
886 LS_STR_HUMAN_READABLE \
887 LS_STR_SELINUX \
888 LS_STR_AUTOWIDTH;
889
Glenn L McGrathafc9ab82004-09-24 02:04:13 +0000890#define LIST_MASK_TRIGGER 0
Glenn L McGrath792cae52004-01-18 05:15:16 +0000891#define STYLE_MASK_TRIGGER STYLE_MASK
892#define SORT_MASK_TRIGGER SORT_MASK
893#define DISP_MASK_TRIGGER DISP_ROWS
894#define TIME_MASK_TRIGGER TIME_MASK
Manuel Novoa III cad53642003-03-19 09:13:01 +0000895
896static const unsigned opt_flags[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000897 LIST_SHORT | STYLE_COLUMNS, /* C */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000898 DISP_HIDDEN | DISP_DOT, /* a */
899 DISP_NOLIST, /* d */
900 LIST_INO, /* i */
901 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
902 LIST_SHORT | STYLE_SINGLE, /* 1 */
903 0, /* g - ingored */
904 LIST_ID_NUMERIC, /* n */
905 LIST_BLOCKS, /* s */
906 DISP_ROWS, /* x */
907 DISP_HIDDEN, /* A */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000908#ifdef CONFIG_SELINUX
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000909 LIST_CONTEXT, /* k */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000910#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000911 0, /* k - ingored */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000912#endif
913#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Glenn L McGrath792cae52004-01-18 05:15:16 +0000914# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000915 TIME_CHANGE | SORT_CTIME, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000916# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000917 TIME_CHANGE, /* c */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000918# endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000919 LIST_FULLTIME, /* e */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000920# ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000921 SORT_MTIME, /* t */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000922# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000923 0, /* t - ignored -- is this correct? */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000924# endif
925# ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +0000926 TIME_ACCESS | SORT_ATIME, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000927# else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000928 TIME_ACCESS, /* u */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000929# endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000930#endif
931#ifdef CONFIG_FEATURE_LS_SORTFILES
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000932 SORT_SIZE, /* S */
933 SORT_EXT, /* X */
934 SORT_ORDER_REVERSE, /* r */
935 SORT_VERSION, /* v */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000936#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000937#ifdef CONFIG_FEATURE_LS_FILETYPES
938 LIST_FILETYPE | LIST_EXEC, /* F */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000939 LIST_FILETYPE, /* p */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000940#endif
941#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000942 FOLLOW_LINKS, /* L */
Glenn L McGrath792cae52004-01-18 05:15:16 +0000943#endif
944#ifdef CONFIG_FEATURE_LS_RECURSIVE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000945 DISP_RECURSIVE, /* R */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000946#endif
947#ifdef CONFIG_FEATURE_HUMAN_READABLE
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000948 LS_DISP_HR, /* h */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000949#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000950#ifdef CONFIG_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +0000951 LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
952#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000953#ifdef CONFIG_FEATURE_AUTOWIDTH
954 0, 0, /* T, w - ignored */
955#endif
Eric Andersend07cf592004-02-05 13:52:03 +0000956 (1U<<31)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000957};
958
959
960/*----------------------------------------------------------------------*/
961
Eric Andersen11c65522000-09-07 17:24:47 +0000962extern int ls_main(int argc, char **argv)
963{
Glenn L McGrath792cae52004-01-18 05:15:16 +0000964 struct dnode **dnd;
965 struct dnode **dnf;
966 struct dnode **dnp;
967 struct dnode *dn;
968 struct dnode *cur;
969 long opt;
970 int nfiles = 0;
971 int dnfiles;
972 int dndirs;
973 int oi;
974 int ac;
975 int i;
Eric Andersen11c65522000-09-07 17:24:47 +0000976 char **av;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000977#ifdef CONFIG_FEATURE_AUTOWIDTH
978 char *tabstops_str = NULL;
979 char *terminal_width_str = NULL;
980#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +0000981#ifdef CONFIG_FEATURE_LS_COLOR
982 char *color_opt;
983#endif
Glenn L McGrath792cae52004-01-18 05:15:16 +0000984
Manuel Novoa III cad53642003-03-19 09:13:01 +0000985 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000986#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +0000987 | TIME_MOD
Eric Andersen11c65522000-09-07 17:24:47 +0000988#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000989#ifdef CONFIG_FEATURE_LS_SORTFILES
990 | SORT_NAME | SORT_ORDER_FORWARD
991#endif
992 ;
Glenn L McGrath792cae52004-01-18 05:15:16 +0000993
Eric Andersen6d687812003-11-04 23:16:48 +0000994#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000995 /* Obtain the terminal width. */
Eric Andersen70060d22004-03-27 10:02:48 +0000996 get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
Eric Andersen8efe9672003-09-15 08:33:45 +0000997 /* Go one less... */
998 terminal_width--;
Eric Andersen6d687812003-11-04 23:16:48 +0000999#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001000
Eric Andersen3ad0bd92002-03-20 09:13:48 +00001001#ifdef CONFIG_FEATURE_LS_COLOR
Paul Fox156dc412005-08-01 19:33:30 +00001002 bb_applet_long_options = ls_color_opt;
Eric Andersen3ad0bd92002-03-20 09:13:48 +00001003#endif
1004
Eric Andersen11c65522000-09-07 17:24:47 +00001005 /* process options */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001006#ifdef CONFIG_FEATURE_AUTOWIDTH
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001007 opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str
1008#ifdef CONFIG_FEATURE_LS_COLOR
1009 , &color_opt
1010#endif
1011 );
Glenn L McGrath792cae52004-01-18 05:15:16 +00001012 if (tabstops_str) {
1013 tabstops = atoi(tabstops_str);
1014 }
1015 if (terminal_width_str) {
1016 terminal_width = atoi(terminal_width_str);
1017 }
1018#else
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001019 opt = bb_getopt_ulflags(argc, argv, ls_options
1020#ifdef CONFIG_FEATURE_LS_COLOR
1021 , &color_opt
1022#endif
1023 );
Manuel Novoa III cad53642003-03-19 09:13:01 +00001024#endif
Eric Andersend07cf592004-02-05 13:52:03 +00001025 for (i = 0; opt_flags[i] != (1U<<31); i++) {
Glenn L McGrath792cae52004-01-18 05:15:16 +00001026 if (opt & (1 << i)) {
1027 unsigned int flags = opt_flags[i];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001028
Manuel Novoa III cad53642003-03-19 09:13:01 +00001029 if (flags & LIST_MASK_TRIGGER) {
1030 all_fmt &= ~LIST_MASK;
1031 }
1032 if (flags & STYLE_MASK_TRIGGER) {
1033 all_fmt &= ~STYLE_MASK;
1034 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001035#ifdef CONFIG_FEATURE_LS_SORTFILES
Manuel Novoa III cad53642003-03-19 09:13:01 +00001036 if (flags & SORT_MASK_TRIGGER) {
1037 all_fmt &= ~SORT_MASK;
1038 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001039#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001040 if (flags & DISP_MASK_TRIGGER) {
1041 all_fmt &= ~DISP_MASK;
1042 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001043#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
Manuel Novoa III cad53642003-03-19 09:13:01 +00001044 if (flags & TIME_MASK_TRIGGER) {
1045 all_fmt &= ~TIME_MASK;
1046 }
Eric Andersenb7ebc612003-07-14 19:20:46 +00001047#endif
Eric Andersen9e480452003-07-03 10:07:04 +00001048 if (flags & LIST_CONTEXT) {
1049 all_fmt |= STYLE_SINGLE;
1050 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001051#ifdef CONFIG_FEATURE_HUMAN_READABLE
1052 if (opt == 'l') {
1053 all_fmt &= ~LS_DISP_HR;
1054 }
1055#endif
1056 all_fmt |= flags;
1057 }
Eric Andersen11c65522000-09-07 17:24:47 +00001058 }
1059
Paul Fox156dc412005-08-01 19:33:30 +00001060#ifdef CONFIG_FEATURE_LS_COLOR
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001061 {
1062 /* find color bit value - last position for short getopt */
Paul Fox156dc412005-08-01 19:33:30 +00001063
Paul Fox156dc412005-08-01 19:33:30 +00001064#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
Paul Fox156dc412005-08-01 19:33:30 +00001065 char *p;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001066
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001067 if ((p = getenv ("LS_COLORS")) != NULL &&
Paul Fox156dc412005-08-01 19:33:30 +00001068 (*p == '\0' || (strcmp(p, "none") == 0))) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001069 ;
Paul Fox156dc412005-08-01 19:33:30 +00001070 } else if (isatty(STDOUT_FILENO)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001071 show_color = 1;
Paul Fox156dc412005-08-01 19:33:30 +00001072 }
1073#endif
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001074
1075 if((opt & (1 << i))) { /* next flag after short options */
1076 if (color_opt == NULL || strcmp("always", color_opt) == 0)
1077 show_color = 1;
1078 else if (color_opt != NULL && strcmp("never", color_opt) == 0)
1079 show_color = 0;
1080 else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO))
1081 show_color = 1;
1082 }
Paul Fox156dc412005-08-01 19:33:30 +00001083 }
1084#endif
1085
Eric Andersen11c65522000-09-07 17:24:47 +00001086 /* sort out which command line options take precedence */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001087#ifdef CONFIG_FEATURE_LS_RECURSIVE
Manuel Novoa III cad53642003-03-19 09:13:01 +00001088 if (all_fmt & DISP_NOLIST)
1089 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
Eric Andersen11c65522000-09-07 17:24:47 +00001090#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001091#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001092 if (all_fmt & TIME_CHANGE)
1093 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
1094 if (all_fmt & TIME_ACCESS)
1095 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
Eric Andersen11c65522000-09-07 17:24:47 +00001096#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001097 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
1098 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001099#ifdef CONFIG_FEATURE_LS_USERNAME
Manuel Novoa III cad53642003-03-19 09:13:01 +00001100 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
1101 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
Eric Andersen11c65522000-09-07 17:24:47 +00001102#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001103
Eric Andersen11c65522000-09-07 17:24:47 +00001104 /* choose a display format */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001105 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
1106#if STYLE_AUTO != 0
1107 all_fmt = (all_fmt & ~STYLE_MASK)
Eric Andersen70060d22004-03-27 10:02:48 +00001108 | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001109#else
Eric Andersen70060d22004-03-27 10:02:48 +00001110 all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001111#endif
Eric Andersen11c65522000-09-07 17:24:47 +00001112
1113 /*
1114 * when there are no cmd line args we have to supply a default "." arg.
1115 * we will create a second argv array, "av" that will hold either
1116 * our created "." arg, or the real cmd line args. The av array
1117 * just holds the pointers- we don't move the date the pointers
1118 * point to.
1119 */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001120 ac = argc - optind; /* how many cmd line args are left */
Eric Andersen11c65522000-09-07 17:24:47 +00001121 if (ac < 1) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001122 static const char * const dotdir[] = { "." };
1123
1124 av = (char **) dotdir;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001125 ac = 1;
Eric Andersen11c65522000-09-07 17:24:47 +00001126 } else {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001127 av = argv + optind;
Eric Andersen11c65522000-09-07 17:24:47 +00001128 }
1129
1130 /* now, everything is in the av array */
1131 if (ac > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001132 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersen11c65522000-09-07 17:24:47 +00001133
1134 /* stuff the command line file names into an dnode array */
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001135 dn = NULL;
1136 for (oi = 0; oi < ac; oi++) {
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001137 cur = my_stat(av[oi], av[oi]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001138 if (!cur)
Eric Andersen11c65522000-09-07 17:24:47 +00001139 continue;
"Vladimir N. Oleynik"a8c23aa2005-09-05 15:06:57 +00001140 cur->allocated = 0;
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001141 cur->next = dn;
1142 dn = cur;
Eric Andersen11c65522000-09-07 17:24:47 +00001143 nfiles++;
1144 }
1145
1146 /* now that we know how many files there are
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001147 ** allocate memory for an array to hold dnode pointers
1148 */
1149 dnp = dnalloc(nfiles);
1150 for (i = 0, cur = dn; i < nfiles; i++) {
1151 dnp[i] = cur; /* save pointer to node in array */
1152 cur = cur->next;
Eric Andersen11c65522000-09-07 17:24:47 +00001153 }
1154
Manuel Novoa III cad53642003-03-19 09:13:01 +00001155 if (all_fmt & DISP_NOLIST) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001156#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +00001157 shellsort(dnp, nfiles);
1158#endif
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001159 if (nfiles > 0)
1160 showfiles(dnp, nfiles);
Eric Andersen11c65522000-09-07 17:24:47 +00001161 } else {
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001162 dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
1163 dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
1164 dndirs = countdirs(dnp, nfiles);
1165 dnfiles = nfiles - dndirs;
Eric Andersen11c65522000-09-07 17:24:47 +00001166 if (dnfiles > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001167#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +00001168 shellsort(dnf, dnfiles);
1169#endif
1170 showfiles(dnf, dnfiles);
1171 }
1172 if (dndirs > 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001173#ifdef CONFIG_FEATURE_LS_SORTFILES
Eric Andersen11c65522000-09-07 17:24:47 +00001174 shellsort(dnd, dndirs);
1175#endif
Glenn L McGrathc4db0832004-03-06 09:12:55 +00001176 showdirs(dnd, dndirs, dnfiles == 0);
Eric Andersen11c65522000-09-07 17:24:47 +00001177 }
1178 }
Glenn L McGrathe3906fc2002-08-22 18:13:54 +00001179 return (status);
Eric Andersencc8ed391999-10-05 16:24:54 +00001180}