ls: cleanup part 2. ifdef forest is much less scary now :)
This commit is contained in:
parent
5c7596058d
commit
94cf69fe3e
@ -133,7 +133,7 @@ function create() {
|
|||||||
ln -s ../up dir/up
|
ln -s ../up dir/up
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
function test() {
|
function tst() {
|
||||||
(cd test1; $t1 $1)
|
(cd test1; $t1 $1)
|
||||||
(cd test2; $t2 $1)
|
(cd test2; $t2 $1)
|
||||||
(cd test1; ls -lR) >out1
|
(cd test1; ls -lR) >out1
|
||||||
@ -145,13 +145,13 @@ function test() {
|
|||||||
t1="/tmp/busybox chmod"
|
t1="/tmp/busybox chmod"
|
||||||
t2="/usr/bin/chmod"
|
t2="/usr/bin/chmod"
|
||||||
create test1; create test2
|
create test1; create test2
|
||||||
test "a+w file"
|
tst "a+w file"
|
||||||
test "a-w dir"
|
tst "a-w dir"
|
||||||
test "a+w linkfile"
|
tst "a+w linkfile"
|
||||||
test "a-w linkdir"
|
tst "a-w linkdir"
|
||||||
test "-R a+w file"
|
tst "-R a+w file"
|
||||||
test "-R a-w dir"
|
tst "-R a-w dir"
|
||||||
test "-R a+w linkfile"
|
tst "-R a+w linkfile"
|
||||||
test "-R a-w linkdir"
|
tst "-R a-w linkdir"
|
||||||
test "a-r,a+x linkfile"
|
tst "a-r,a+x linkfile"
|
||||||
*/
|
*/
|
||||||
|
324
coreutils/ls.c
324
coreutils/ls.c
@ -29,115 +29,98 @@
|
|||||||
* 1. requires lstat (BSD) - how do you do it without?
|
* 1. requires lstat (BSD) - how do you do it without?
|
||||||
*/
|
*/
|
||||||
|
|
||||||
enum {
|
|
||||||
TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
|
|
||||||
COLUMN_GAP = 2, /* includes the file type char */
|
|
||||||
};
|
|
||||||
|
|
||||||
/************************************************************************/
|
|
||||||
|
|
||||||
#include "busybox.h"
|
#include "busybox.h"
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
/* what is the overall style of the listing */
|
enum {
|
||||||
#define STYLE_COLUMNS (1U<<21) /* fill columns */
|
|
||||||
#define STYLE_LONG (2U<<21) /* one record per line, extended info */
|
|
||||||
#define STYLE_SINGLE (3U<<21) /* one record per line */
|
|
||||||
|
|
||||||
#define STYLE_MASK STYLE_SINGLE
|
TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
|
||||||
#define STYLE_ONE_RECORD_FLAG STYLE_LONG
|
COLUMN_GAP = 2, /* includes the file type char */
|
||||||
|
|
||||||
|
/* what is the overall style of the listing */
|
||||||
|
STYLE_COLUMNS = 1 << 21, /* fill columns */
|
||||||
|
STYLE_LONG = 2 << 21, /* one record per line, extended info */
|
||||||
|
STYLE_SINGLE = 3 << 21, /* one record per line */
|
||||||
|
STYLE_MASK = STYLE_SINGLE,
|
||||||
|
|
||||||
/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
|
/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
|
||||||
/* what file information will be listed */
|
/* what file information will be listed */
|
||||||
#define LIST_INO (1U<<0)
|
LIST_INO = 1 << 0,
|
||||||
#define LIST_BLOCKS (1U<<1)
|
LIST_BLOCKS = 1 << 1,
|
||||||
#define LIST_MODEBITS (1U<<2)
|
LIST_MODEBITS = 1 << 2,
|
||||||
#define LIST_NLINKS (1U<<3)
|
LIST_NLINKS = 1 << 3,
|
||||||
#define LIST_ID_NAME (1U<<4)
|
LIST_ID_NAME = 1 << 4,
|
||||||
#define LIST_ID_NUMERIC (1U<<5)
|
LIST_ID_NUMERIC = 1 << 5,
|
||||||
#define LIST_CONTEXT (1U<<6)
|
LIST_CONTEXT = 1 << 6,
|
||||||
#define LIST_SIZE (1U<<7)
|
LIST_SIZE = 1 << 7,
|
||||||
#define LIST_DEV (1U<<8)
|
LIST_DEV = 1 << 8,
|
||||||
#define LIST_DATE_TIME (1U<<9)
|
LIST_DATE_TIME = 1 << 9,
|
||||||
#define LIST_FULLTIME (1U<<10)
|
LIST_FULLTIME = 1 << 10,
|
||||||
#define LIST_FILENAME (1U<<11)
|
LIST_FILENAME = 1 << 11,
|
||||||
#define LIST_SYMLINK (1U<<12)
|
LIST_SYMLINK = 1 << 12,
|
||||||
#define LIST_FILETYPE (1U<<13)
|
LIST_FILETYPE = 1 << 13,
|
||||||
#define LIST_EXEC (1U<<14)
|
LIST_EXEC = 1 << 14,
|
||||||
|
LIST_MASK = (LIST_EXEC << 1) - 1,
|
||||||
#define LIST_MASK ((LIST_EXEC << 1) - 1)
|
|
||||||
|
|
||||||
/* what files will be displayed */
|
/* what files will be displayed */
|
||||||
#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
|
DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */
|
||||||
#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
|
DISP_HIDDEN = 1 << 16, /* show filenames starting with . */
|
||||||
#define DISP_DOT (1U<<17) /* show . and .. */
|
DISP_DOT = 1 << 17, /* show . and .. */
|
||||||
#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
|
DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */
|
||||||
#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
|
DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */
|
||||||
#define DISP_ROWS (1U<<20) /* print across rows */
|
DISP_ROWS = 1 << 20, /* print across rows */
|
||||||
|
DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
|
||||||
|
|
||||||
#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1))
|
/* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */
|
||||||
|
SORT_FORWARD = 0, /* sort in reverse order */
|
||||||
|
SORT_REVERSE = 1 << 27, /* sort in reverse order */
|
||||||
|
|
||||||
// CONFIG_FEATURE_LS_SORTFILES
|
SORT_NAME = 0, /* sort by file name */
|
||||||
/* how will the files be sorted */
|
SORT_SIZE = 1 << 28, /* sort by file size */
|
||||||
#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
|
SORT_ATIME = 2 << 28, /* sort by last access time */
|
||||||
#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
|
SORT_CTIME = 3 << 28, /* sort by last change time */
|
||||||
|
SORT_MTIME = 4 << 28, /* sort by last modification time */
|
||||||
#define SORT_NAME 0 /* sort by file name */
|
SORT_VERSION = 5 << 28, /* sort by version */
|
||||||
#define SORT_SIZE (1U<<28) /* sort by file size */
|
SORT_EXT = 6 << 28, /* sort by file name extension */
|
||||||
#define SORT_ATIME (2U<<28) /* sort by last access time */
|
SORT_DIR = 7 << 28, /* sort by file or directory */
|
||||||
#define SORT_CTIME (3U<<28) /* sort by last change time */
|
SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES,
|
||||||
#define SORT_MTIME (4U<<28) /* sort by last modification time */
|
|
||||||
#define SORT_VERSION (5U<<28) /* sort by version */
|
|
||||||
#define SORT_EXT (6U<<28) /* sort by file name extension */
|
|
||||||
#define SORT_DIR (7U<<28) /* sort by file or directory */
|
|
||||||
|
|
||||||
#define SORT_MASK (7U<<28)
|
|
||||||
|
|
||||||
/* which of the three times will be used */
|
/* which of the three times will be used */
|
||||||
#define TIME_CHANGE ((1U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
|
TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
|
||||||
#define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS)
|
TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS,
|
||||||
#define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS)
|
TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS,
|
||||||
|
|
||||||
#if ENABLE_FEATURE_LS_FOLLOWLINKS
|
FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS,
|
||||||
#define FOLLOW_LINKS (1U<<25)
|
|
||||||
#endif
|
|
||||||
#if ENABLE_FEATURE_HUMAN_READABLE
|
|
||||||
#define LS_DISP_HR (1U<<26)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define LIST_SHORT (LIST_FILENAME)
|
LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE,
|
||||||
//#define LIST_ISHORT (LIST_INO | LIST_FILENAME)
|
|
||||||
#define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
|
|
||||||
LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
|
|
||||||
//#define LIST_ILONG (LIST_INO | LIST_LONG)
|
|
||||||
|
|
||||||
#define SPLIT_DIR 1
|
LIST_SHORT = LIST_FILENAME,
|
||||||
#define SPLIT_FILE 0
|
LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
|
||||||
#define SPLIT_SUBDIR 2
|
LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK,
|
||||||
|
|
||||||
|
SPLIT_DIR = 1,
|
||||||
|
SPLIT_FILE = 0,
|
||||||
|
SPLIT_SUBDIR = 2,
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
|
#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
|
||||||
#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
|
#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
|
||||||
|
#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
|
||||||
#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
|
#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
|
||||||
# define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
|
"\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
|
||||||
#endif
|
#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
|
||||||
|
"\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
|
||||||
|
|
||||||
/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
|
/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
|
||||||
#if ENABLE_FEATURE_LS_COLOR
|
#if ENABLE_FEATURE_LS_COLOR
|
||||||
|
|
||||||
static int show_color;
|
static int show_color;
|
||||||
|
|
||||||
/* long option entry used only for --color, which has no short option
|
/* long option entry used only for --color, which has no short option
|
||||||
* equivalent. */
|
* equivalent. */
|
||||||
static const struct option ls_color_opt[] = {
|
static const struct option ls_color_opt[] = {
|
||||||
{ "color", optional_argument, NULL, 1 },
|
{ "color", optional_argument, NULL, 1 },
|
||||||
{ NULL, 0, NULL, 0 }
|
{ NULL, 0, NULL, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
|
|
||||||
"\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
|
|
||||||
#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
|
|
||||||
"\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
|
|
||||||
#else
|
#else
|
||||||
enum { show_color = 0 };
|
enum { show_color = 0 };
|
||||||
#endif
|
#endif
|
||||||
@ -145,13 +128,13 @@ enum { show_color = 0 };
|
|||||||
/*
|
/*
|
||||||
* a directory entry and its stat info are stored here
|
* a directory entry and its stat info are stored here
|
||||||
*/
|
*/
|
||||||
struct dnode { /* the basic node */
|
struct dnode { /* the basic node */
|
||||||
char *name; /* the dir entry name */
|
char *name; /* the dir entry name */
|
||||||
char *fullname; /* the dir entry name */
|
char *fullname; /* the dir entry name */
|
||||||
int allocated;
|
int allocated;
|
||||||
struct stat dstat; /* the file stat info */
|
struct stat dstat; /* the file stat info */
|
||||||
USE_SELINUX(security_context_t sid;)
|
USE_SELINUX(security_context_t sid;)
|
||||||
struct dnode *next; /* point at the next node */
|
struct dnode *next; /* point at the next node */
|
||||||
};
|
};
|
||||||
typedef struct dnode dnode_t;
|
typedef struct dnode dnode_t;
|
||||||
|
|
||||||
@ -179,7 +162,7 @@ static struct dnode *my_stat(char *fullname, char *name)
|
|||||||
struct dnode *cur;
|
struct dnode *cur;
|
||||||
USE_SELINUX(security_context_t sid = NULL;)
|
USE_SELINUX(security_context_t sid = NULL;)
|
||||||
|
|
||||||
if (ENABLE_FEATURE_LS_FOLLOWLINKS && (all_fmt & FOLLOW_LINKS)) {
|
if (all_fmt & FOLLOW_LINKS) {
|
||||||
#if ENABLE_SELINUX
|
#if ENABLE_SELINUX
|
||||||
if (is_selinux_enabled()) {
|
if (is_selinux_enabled()) {
|
||||||
getfilecon(fullname, &sid);
|
getfilecon(fullname, &sid);
|
||||||
@ -211,7 +194,6 @@ static struct dnode *my_stat(char *fullname, char *name)
|
|||||||
return cur;
|
return cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
#if ENABLE_FEATURE_LS_COLOR
|
#if ENABLE_FEATURE_LS_COLOR
|
||||||
static char fgcolor(mode_t mode)
|
static char fgcolor(mode_t mode)
|
||||||
{
|
{
|
||||||
@ -223,7 +205,6 @@ static char fgcolor(mode_t mode)
|
|||||||
return COLOR(mode);
|
return COLOR(mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
static char bgcolor(mode_t mode)
|
static char bgcolor(mode_t mode)
|
||||||
{
|
{
|
||||||
if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
|
if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
|
||||||
@ -232,7 +213,6 @@ static char bgcolor(mode_t mode)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
|
#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
|
||||||
static char append_char(mode_t mode)
|
static char append_char(mode_t mode)
|
||||||
{
|
{
|
||||||
@ -248,11 +228,8 @@ static char append_char(mode_t mode)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#define countdirs(A, B) count_dirs((A), (B), 1)
|
#define countdirs(A, B) count_dirs((A), (B), 1)
|
||||||
#define countsubdirs(A, B) count_dirs((A), (B), 0)
|
#define countsubdirs(A, B) count_dirs((A), (B), 0)
|
||||||
|
|
||||||
static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
|
static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
|
||||||
{
|
{
|
||||||
int i, dirs;
|
int i, dirs;
|
||||||
@ -282,7 +259,7 @@ static int countfiles(struct dnode **dnp)
|
|||||||
if (dnp == NULL)
|
if (dnp == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
nfiles = 0;
|
nfiles = 0;
|
||||||
for (cur = dnp[0]; cur->next != NULL; cur = cur->next)
|
for (cur = dnp[0]; cur->next; cur = cur->next)
|
||||||
nfiles++;
|
nfiles++;
|
||||||
nfiles++;
|
nfiles++;
|
||||||
return nfiles;
|
return nfiles;
|
||||||
@ -356,7 +333,6 @@ static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
|
|||||||
return dnp;
|
return dnp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
#if ENABLE_FEATURE_LS_SORTFILES
|
#if ENABLE_FEATURE_LS_SORTFILES
|
||||||
static int sortcmp(const void *a, const void *b)
|
static int sortcmp(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
@ -365,7 +341,9 @@ static int sortcmp(const void *a, const void *b)
|
|||||||
unsigned sort_opts = all_fmt & SORT_MASK;
|
unsigned sort_opts = all_fmt & SORT_MASK;
|
||||||
int dif;
|
int dif;
|
||||||
|
|
||||||
dif = 0; /* assume SORT_NAME */
|
dif = 0; /* assume SORT_NAME */
|
||||||
|
// TODO: use pre-initialized function pointer
|
||||||
|
// instead of branch forest
|
||||||
if (sort_opts == SORT_SIZE) {
|
if (sort_opts == SORT_SIZE) {
|
||||||
dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
|
dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
|
||||||
} else if (sort_opts == SORT_ATIME) {
|
} else if (sort_opts == SORT_ATIME) {
|
||||||
@ -386,24 +364,21 @@ static int sortcmp(const void *a, const void *b)
|
|||||||
else dif = strcmp(d1->name, d2->name);
|
else dif = strcmp(d1->name, d2->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (all_fmt & SORT_ORDER_REVERSE) {
|
if (all_fmt & SORT_REVERSE) {
|
||||||
dif = -dif;
|
dif = -dif;
|
||||||
}
|
}
|
||||||
return dif;
|
return dif;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
static void dnsort(struct dnode **dn, int size)
|
static void dnsort(struct dnode **dn, int size)
|
||||||
{
|
{
|
||||||
qsort(dn, size, sizeof *dn, sortcmp);
|
qsort(dn, size, sizeof(*dn), sortcmp);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#define sortcmp(a, b) 0
|
|
||||||
#define dnsort(dn, size) do {} while(0)
|
#define dnsort(dn, size) do {} while(0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
static void showfiles(struct dnode **dn, int nfiles)
|
static void showfiles(struct dnode **dn, int nfiles)
|
||||||
{
|
{
|
||||||
int i, ncols, nrows, row, nc;
|
int i, ncols, nrows, row, nc;
|
||||||
@ -414,27 +389,25 @@ static void showfiles(struct dnode **dn, int nfiles)
|
|||||||
if (dn == NULL || nfiles < 1)
|
if (dn == NULL || nfiles < 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (all_fmt & STYLE_ONE_RECORD_FLAG) {
|
if (all_fmt & STYLE_LONG) {
|
||||||
ncols = 1;
|
ncols = 1;
|
||||||
} else {
|
} else {
|
||||||
/* find the longest file name- use that as the column width */
|
/* find the longest file name, use that as the column width */
|
||||||
for (i = 0; i < nfiles; i++) {
|
for (i = 0; i < nfiles; i++) {
|
||||||
int len = strlen(dn[i]->name) +
|
int len = strlen(dn[i]->name);
|
||||||
#if ENABLE_SELINUX
|
|
||||||
((all_fmt & LIST_CONTEXT) ? 33 : 0) +
|
|
||||||
#endif
|
|
||||||
((all_fmt & LIST_INO) ? 8 : 0) +
|
|
||||||
((all_fmt & LIST_BLOCKS) ? 5 : 0);
|
|
||||||
if (column_width < len)
|
if (column_width < len)
|
||||||
column_width = len;
|
column_width = len;
|
||||||
}
|
}
|
||||||
column_width += tabstops;
|
column_width += tabstops +
|
||||||
|
USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + )
|
||||||
|
((all_fmt & LIST_INO) ? 8 : 0) +
|
||||||
|
((all_fmt & LIST_BLOCKS) ? 5 : 0);
|
||||||
ncols = (int) (terminal_width / column_width);
|
ncols = (int) (terminal_width / column_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ncols > 1) {
|
if (ncols > 1) {
|
||||||
nrows = nfiles / ncols;
|
nrows = nfiles / ncols;
|
||||||
if ((nrows * ncols) < nfiles)
|
if (nrows * ncols < nfiles)
|
||||||
nrows++; /* round up fractionals */
|
nrows++; /* round up fractionals */
|
||||||
} else {
|
} else {
|
||||||
nrows = nfiles;
|
nrows = nfiles;
|
||||||
@ -462,7 +435,7 @@ static void showfiles(struct dnode **dn, int nfiles)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
static void showdirs(struct dnode **dn, int ndirs, int first)
|
static void showdirs(struct dnode **dn, int ndirs, int first)
|
||||||
{
|
{
|
||||||
int i, nfiles;
|
int i, nfiles;
|
||||||
@ -484,7 +457,7 @@ static void showdirs(struct dnode **dn, int ndirs, int first)
|
|||||||
nfiles = countfiles(subdnp);
|
nfiles = countfiles(subdnp);
|
||||||
if (nfiles > 0) {
|
if (nfiles > 0) {
|
||||||
/* list all files at this level */
|
/* list all files at this level */
|
||||||
if (ENABLE_FEATURE_LS_SORTFILES) dnsort(subdnp, nfiles);
|
dnsort(subdnp, nfiles);
|
||||||
showfiles(subdnp, nfiles);
|
showfiles(subdnp, nfiles);
|
||||||
if (ENABLE_FEATURE_LS_RECURSIVE) {
|
if (ENABLE_FEATURE_LS_RECURSIVE) {
|
||||||
if (all_fmt & DISP_RECURSIVE) {
|
if (all_fmt & DISP_RECURSIVE) {
|
||||||
@ -492,7 +465,7 @@ static void showdirs(struct dnode **dn, int ndirs, int first)
|
|||||||
dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
|
dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
|
||||||
dndirs = countsubdirs(subdnp, nfiles);
|
dndirs = countsubdirs(subdnp, nfiles);
|
||||||
if (dndirs > 0) {
|
if (dndirs > 0) {
|
||||||
if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
|
dnsort(dnd, dndirs);
|
||||||
showdirs(dnd, dndirs, 0);
|
showdirs(dnd, dndirs, 0);
|
||||||
/* free the array of dnode pointers to the dirs */
|
/* free the array of dnode pointers to the dirs */
|
||||||
free(dnd);
|
free(dnd);
|
||||||
@ -505,7 +478,7 @@ static void showdirs(struct dnode **dn, int ndirs, int first)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
static struct dnode **list_dir(const char *path)
|
static struct dnode **list_dir(const char *path)
|
||||||
{
|
{
|
||||||
struct dnode *dn, *cur, **dnp;
|
struct dnode *dn, *cur, **dnp;
|
||||||
@ -532,14 +505,14 @@ static struct dnode **list_dir(const char *path)
|
|||||||
entry->d_name[1] == '.'
|
entry->d_name[1] == '.'
|
||||||
&& entry->d_name[2] == 0))
|
&& entry->d_name[2] == 0))
|
||||||
&& !(all_fmt & DISP_DOT))
|
&& !(all_fmt & DISP_DOT))
|
||||||
continue;
|
continue;
|
||||||
if (!(all_fmt & DISP_HIDDEN))
|
if (!(all_fmt & DISP_HIDDEN))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fullname = concat_path_file(path, entry->d_name);
|
fullname = concat_path_file(path, entry->d_name);
|
||||||
cur = my_stat(fullname, strrchr(fullname, '/') + 1);
|
cur = my_stat(fullname, strrchr(fullname, '/') + 1);
|
||||||
if (!cur) {
|
if (!cur) {
|
||||||
// FIXME: free(fullname); ?
|
free(fullname);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cur->allocated = 1;
|
cur->allocated = 1;
|
||||||
@ -563,7 +536,7 @@ static struct dnode **list_dir(const char *path)
|
|||||||
return dnp;
|
return dnp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
static int list_single(struct dnode *dn)
|
static int list_single(struct dnode *dn)
|
||||||
{
|
{
|
||||||
int i, column = 0;
|
int i, column = 0;
|
||||||
@ -627,7 +600,7 @@ static int list_single(struct dnode *dn)
|
|||||||
column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
|
column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
|
||||||
(int) minor(dn->dstat.st_rdev));
|
(int) minor(dn->dstat.st_rdev));
|
||||||
} else {
|
} else {
|
||||||
if (ENABLE_FEATURE_HUMAN_READABLE && (all_fmt & LS_DISP_HR)) {
|
if (all_fmt & LS_DISP_HR) {
|
||||||
column += printf("%9s ",
|
column += printf("%9s ",
|
||||||
make_human_readable_str(dn->dstat.st_size, 1, 0));
|
make_human_readable_str(dn->dstat.st_size, 1, 0));
|
||||||
} else {
|
} else {
|
||||||
@ -661,12 +634,12 @@ static int list_single(struct dnode *dn)
|
|||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
if (dn->sid) {
|
if (dn->sid) {
|
||||||
/* I assume sid initilized with NULL */
|
/* I assume sid initilized with NULL */
|
||||||
len = strlen(dn->sid)+1;
|
len = strlen(dn->sid)+1;
|
||||||
safe_strncpy(context, dn->sid, len);
|
safe_strncpy(context, dn->sid, len);
|
||||||
freecon(dn->sid);
|
freecon(dn->sid);
|
||||||
}else {
|
} else {
|
||||||
safe_strncpy(context, "unknown", 8);
|
safe_strncpy(context, "unknown", 8);
|
||||||
}
|
}
|
||||||
printf("%-32s ", context);
|
printf("%-32s ", context);
|
||||||
column += MAX(33, len);
|
column += MAX(33, len);
|
||||||
@ -677,7 +650,7 @@ static int list_single(struct dnode *dn)
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
if (show_color && !lstat(dn->fullname, &info)) {
|
if (show_color && !lstat(dn->fullname, &info)) {
|
||||||
printf("\033[%d;%dm", bgcolor(info.st_mode),
|
printf("\033[%d;%dm", bgcolor(info.st_mode),
|
||||||
fgcolor(info.st_mode));
|
fgcolor(info.st_mode));
|
||||||
}
|
}
|
||||||
column += printf("%s", dn->name);
|
column += printf("%s", dn->name);
|
||||||
if (show_color) {
|
if (show_color) {
|
||||||
@ -687,31 +660,29 @@ static int list_single(struct dnode *dn)
|
|||||||
case LIST_SYMLINK:
|
case LIST_SYMLINK:
|
||||||
if (S_ISLNK(dn->dstat.st_mode)) {
|
if (S_ISLNK(dn->dstat.st_mode)) {
|
||||||
char *lpath = xreadlink(dn->fullname);
|
char *lpath = xreadlink(dn->fullname);
|
||||||
|
if (!lpath) break;
|
||||||
if (lpath) {
|
printf(" -> ");
|
||||||
printf(" -> ");
|
#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
|
||||||
#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR)
|
if (!stat(dn->fullname, &info)) {
|
||||||
if (!stat(dn->fullname, &info)) {
|
append = append_char(info.st_mode);
|
||||||
append = append_char(info.st_mode);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (show_color) {
|
|
||||||
errno = 0;
|
|
||||||
printf("\033[%d;%dm", bgcolor(info.st_mode),
|
|
||||||
fgcolor(info.st_mode));
|
|
||||||
}
|
|
||||||
column += printf("%s", lpath) + 4;
|
|
||||||
if (show_color) {
|
|
||||||
printf("\033[0m");
|
|
||||||
}
|
|
||||||
free(lpath);
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
if (show_color) {
|
||||||
|
errno = 0;
|
||||||
|
printf("\033[%d;%dm", bgcolor(info.st_mode),
|
||||||
|
fgcolor(info.st_mode));
|
||||||
|
}
|
||||||
|
column += printf("%s", lpath) + 4;
|
||||||
|
if (show_color) {
|
||||||
|
printf("\033[0m");
|
||||||
|
}
|
||||||
|
free(lpath);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#if ENABLE_FEATURE_LS_FILETYPES
|
#if ENABLE_FEATURE_LS_FILETYPES
|
||||||
case LIST_FILETYPE:
|
case LIST_FILETYPE:
|
||||||
if (append != '\0') {
|
if (append) {
|
||||||
printf("%1c", append);
|
putchar(append);
|
||||||
column++;
|
column++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -722,8 +693,6 @@ static int list_single(struct dnode *dn)
|
|||||||
return column;
|
return column;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
/* "[-]Cadil1", POSIX mandated options, busybox always supports */
|
/* "[-]Cadil1", POSIX mandated options, busybox always supports */
|
||||||
/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
|
/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
|
||||||
/* "[-]Ak" GNU options, busybox always supports */
|
/* "[-]Ak" GNU options, busybox always supports */
|
||||||
@ -732,7 +701,6 @@ static int list_single(struct dnode *dn)
|
|||||||
/* "[-]SXvThw", GNU options, busybox optionally supports */
|
/* "[-]SXvThw", GNU options, busybox optionally supports */
|
||||||
/* "[-]K", SELinux mandated options, busybox optionally supports */
|
/* "[-]K", SELinux mandated options, busybox optionally supports */
|
||||||
/* "[-]e", I think we made this one up */
|
/* "[-]e", I think we made this one up */
|
||||||
|
|
||||||
static const char ls_options[] = "Cadil1gnsxAk"
|
static const char ls_options[] = "Cadil1gnsxAk"
|
||||||
USE_FEATURE_LS_TIMESTAMPS("cetu")
|
USE_FEATURE_LS_TIMESTAMPS("cetu")
|
||||||
USE_FEATURE_LS_SORTFILES("SXrv")
|
USE_FEATURE_LS_SORTFILES("SXrv")
|
||||||
@ -743,10 +711,12 @@ static const char ls_options[] = "Cadil1gnsxAk"
|
|||||||
USE_SELINUX("K")
|
USE_SELINUX("K")
|
||||||
USE_FEATURE_AUTOWIDTH("T:w:");
|
USE_FEATURE_AUTOWIDTH("T:w:");
|
||||||
|
|
||||||
#define LIST_MASK_TRIGGER 0
|
enum {
|
||||||
#define STYLE_MASK_TRIGGER STYLE_MASK
|
LIST_MASK_TRIGGER = 0,
|
||||||
#define SORT_MASK_TRIGGER SORT_MASK
|
STYLE_MASK_TRIGGER = STYLE_MASK,
|
||||||
#define DISP_MASK_TRIGGER DISP_ROWS
|
DISP_MASK_TRIGGER = DISP_ROWS,
|
||||||
|
SORT_MASK_TRIGGER = SORT_MASK,
|
||||||
|
};
|
||||||
|
|
||||||
static const unsigned opt_flags[] = {
|
static const unsigned opt_flags[] = {
|
||||||
LIST_SHORT | STYLE_COLUMNS, /* C */
|
LIST_SHORT | STYLE_COLUMNS, /* C */
|
||||||
@ -760,11 +730,7 @@ static const unsigned opt_flags[] = {
|
|||||||
LIST_BLOCKS, /* s */
|
LIST_BLOCKS, /* s */
|
||||||
DISP_ROWS, /* x */
|
DISP_ROWS, /* x */
|
||||||
DISP_HIDDEN, /* A */
|
DISP_HIDDEN, /* A */
|
||||||
#if ENABLE_SELINUX
|
ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */
|
||||||
LIST_CONTEXT, /* k */
|
|
||||||
#else
|
|
||||||
0, /* k - ingored */
|
|
||||||
#endif
|
|
||||||
#if ENABLE_FEATURE_LS_TIMESTAMPS
|
#if ENABLE_FEATURE_LS_TIMESTAMPS
|
||||||
TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
|
TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */
|
||||||
LIST_FULLTIME, /* e */
|
LIST_FULLTIME, /* e */
|
||||||
@ -774,7 +740,7 @@ static const unsigned opt_flags[] = {
|
|||||||
#if ENABLE_FEATURE_LS_SORTFILES
|
#if ENABLE_FEATURE_LS_SORTFILES
|
||||||
SORT_SIZE, /* S */
|
SORT_SIZE, /* S */
|
||||||
SORT_EXT, /* X */
|
SORT_EXT, /* X */
|
||||||
SORT_ORDER_REVERSE, /* r */
|
SORT_REVERSE, /* r */
|
||||||
SORT_VERSION, /* v */
|
SORT_VERSION, /* v */
|
||||||
#endif
|
#endif
|
||||||
#if ENABLE_FEATURE_LS_FILETYPES
|
#if ENABLE_FEATURE_LS_FILETYPES
|
||||||
@ -800,8 +766,6 @@ static const unsigned opt_flags[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
int ls_main(int argc, char **argv)
|
int ls_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct dnode **dnd;
|
struct dnode **dnd;
|
||||||
@ -817,16 +781,12 @@ int ls_main(int argc, char **argv)
|
|||||||
int ac;
|
int ac;
|
||||||
int i;
|
int i;
|
||||||
char **av;
|
char **av;
|
||||||
#if ENABLE_FEATURE_AUTOWIDTH
|
USE_FEATURE_AUTOWIDTH(char *tabstops_str = NULL;)
|
||||||
char *tabstops_str = NULL;
|
USE_FEATURE_AUTOWIDTH(char *terminal_width_str = NULL;)
|
||||||
char *terminal_width_str = NULL;
|
USE_FEATURE_LS_COLOR(char *color_opt;)
|
||||||
#endif
|
|
||||||
#if ENABLE_FEATURE_LS_COLOR
|
|
||||||
char *color_opt;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
all_fmt = LIST_SHORT |
|
all_fmt = LIST_SHORT |
|
||||||
(ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD));
|
(ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD));
|
||||||
|
|
||||||
#if ENABLE_FEATURE_AUTOWIDTH
|
#if ENABLE_FEATURE_AUTOWIDTH
|
||||||
/* Obtain the terminal width. */
|
/* Obtain the terminal width. */
|
||||||
@ -835,27 +795,17 @@ int ls_main(int argc, char **argv)
|
|||||||
terminal_width--;
|
terminal_width--;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ENABLE_FEATURE_LS_COLOR
|
|
||||||
applet_long_options = ls_color_opt;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* process options */
|
/* process options */
|
||||||
|
USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;)
|
||||||
#if ENABLE_FEATURE_AUTOWIDTH
|
#if ENABLE_FEATURE_AUTOWIDTH
|
||||||
opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
|
opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str
|
||||||
#if ENABLE_FEATURE_LS_COLOR
|
USE_FEATURE_LS_COLOR(, &color_opt));
|
||||||
, &color_opt
|
|
||||||
#endif
|
|
||||||
);
|
|
||||||
if (tabstops_str)
|
if (tabstops_str)
|
||||||
tabstops = xatou(tabstops_str);
|
tabstops = xatou(tabstops_str);
|
||||||
if (terminal_width_str)
|
if (terminal_width_str)
|
||||||
terminal_width = xatou(terminal_width_str);
|
terminal_width = xatou(terminal_width_str);
|
||||||
#else
|
#else
|
||||||
opt = getopt32(argc, argv, ls_options
|
opt = getopt32(argc, argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt));
|
||||||
#if ENABLE_FEATURE_LS_COLOR
|
|
||||||
, &color_opt
|
|
||||||
#endif
|
|
||||||
);
|
|
||||||
#endif
|
#endif
|
||||||
for (i = 0; opt_flags[i] != (1U<<31); i++) {
|
for (i = 0; opt_flags[i] != (1U<<31); i++) {
|
||||||
if (opt & (1 << i)) {
|
if (opt & (1 << i)) {
|
||||||
@ -865,7 +815,7 @@ int ls_main(int argc, char **argv)
|
|||||||
all_fmt &= ~LIST_MASK;
|
all_fmt &= ~LIST_MASK;
|
||||||
if (flags & STYLE_MASK_TRIGGER)
|
if (flags & STYLE_MASK_TRIGGER)
|
||||||
all_fmt &= ~STYLE_MASK;
|
all_fmt &= ~STYLE_MASK;
|
||||||
if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER))
|
if (flags & SORT_MASK_TRIGGER)
|
||||||
all_fmt &= ~SORT_MASK;
|
all_fmt &= ~SORT_MASK;
|
||||||
if (flags & DISP_MASK_TRIGGER)
|
if (flags & DISP_MASK_TRIGGER)
|
||||||
all_fmt &= ~DISP_MASK;
|
all_fmt &= ~DISP_MASK;
|
||||||
@ -873,7 +823,7 @@ int ls_main(int argc, char **argv)
|
|||||||
all_fmt &= ~TIME_MASK;
|
all_fmt &= ~TIME_MASK;
|
||||||
if (flags & LIST_CONTEXT)
|
if (flags & LIST_CONTEXT)
|
||||||
all_fmt |= STYLE_SINGLE;
|
all_fmt |= STYLE_SINGLE;
|
||||||
if (ENABLE_FEATURE_HUMAN_READABLE && opt == 'l')
|
if (LS_DISP_HR && opt == 'l')
|
||||||
all_fmt &= ~LS_DISP_HR;
|
all_fmt &= ~LS_DISP_HR;
|
||||||
all_fmt |= flags;
|
all_fmt |= flags;
|
||||||
}
|
}
|
||||||
@ -959,7 +909,7 @@ int ls_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (all_fmt & DISP_NOLIST) {
|
if (all_fmt & DISP_NOLIST) {
|
||||||
if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles);
|
dnsort(dnp, nfiles);
|
||||||
if (nfiles > 0)
|
if (nfiles > 0)
|
||||||
showfiles(dnp, nfiles);
|
showfiles(dnp, nfiles);
|
||||||
} else {
|
} else {
|
||||||
@ -968,13 +918,13 @@ int ls_main(int argc, char **argv)
|
|||||||
dndirs = countdirs(dnp, nfiles);
|
dndirs = countdirs(dnp, nfiles);
|
||||||
dnfiles = nfiles - dndirs;
|
dnfiles = nfiles - dndirs;
|
||||||
if (dnfiles > 0) {
|
if (dnfiles > 0) {
|
||||||
if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles);
|
dnsort(dnf, dnfiles);
|
||||||
showfiles(dnf, dnfiles);
|
showfiles(dnf, dnfiles);
|
||||||
if (ENABLE_FEATURE_CLEAN_UP)
|
if (ENABLE_FEATURE_CLEAN_UP)
|
||||||
free(dnf);
|
free(dnf);
|
||||||
}
|
}
|
||||||
if (dndirs > 0) {
|
if (dndirs > 0) {
|
||||||
if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs);
|
dnsort(dnd, dndirs);
|
||||||
showdirs(dnd, dndirs, dnfiles == 0);
|
showdirs(dnd, dndirs, dnfiles == 0);
|
||||||
if (ENABLE_FEATURE_CLEAN_UP)
|
if (ENABLE_FEATURE_CLEAN_UP)
|
||||||
free(dnd);
|
free(dnd);
|
||||||
|
Loading…
Reference in New Issue
Block a user