aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-05-13 00:04:50 -0500
committerRob Landley <rob@landley.net>2015-05-13 00:04:50 -0500
commitb18c7e8a59e61b8ec49e2d0f8cb2dda19b8f6a82 (patch)
tree31366cbef912d68bbb4381d0e85d98c36be0a53d
parentdec4669fa1285945910ffd47ff5830f250e1c1ed (diff)
downloadtoybox-b18c7e8a59e61b8ec49e2d0f8cb2dda19b8f6a82.tar.gz
Fix bug (len[7] wasn't zeroed if -Z off, thus -C overestimated entry lengths),
and some cleanups while I was there.
-rw-r--r--toys/posix/ls.c34
1 files changed, 9 insertions, 25 deletions
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 38db9977..35d1034a 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -95,19 +95,6 @@ int strwidth(char *s)
return total;
}
-void dlist_to_dirtree(struct dirtree *parent)
-{
- // Turn double_list into dirtree
- struct dirtree *dt = parent->child;
- if (dt) {
- dt->parent->next = NULL;
- while (dt) {
- dt->parent = parent;
- dt = dt->next;
- }
- }
-}
-
static char endtype(struct stat *st)
{
mode_t mode = st->st_mode;
@@ -205,8 +192,7 @@ static void entrylen(struct dirtree *dt, unsigned *len)
}
len[6] = (flags & FLAG_s) ? numlen(st->st_blocks) : 0;
-
- if (CFG_LS_Z && (flags & FLAG_Z)) len[7] = seclabel(dt, 0);
+ len[7] = (CFG_LS_Z && (flags & FLAG_Z)) ? seclabel(dt, 0) : 0;
}
static int compare(void *a, void *b)
@@ -324,6 +310,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
if (S_ISDIR(dt->st.st_mode) && !dt->next && !(flags & FLAG_d)) {
dt->extra = 1;
listfiles(open(dt->name, 0), dt);
+
return;
}
} else {
@@ -335,7 +322,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Copy linked list to array and sort it. Directories go in array because
// we visit them in sorted order too. (The nested loops let us measure and
// fill with the same inner loop.)
- for (sort = 0;;sort = xmalloc(dtlen * sizeof(void *))) {
+ for (sort = 0;;sort = xmalloc(dtlen*sizeof(void *))) {
for (dtlen = 0, dt = indir->child; dt; dt = dt->next, dtlen++)
if (sort) sort[dtlen] = dt;
if (sort) break;
@@ -545,17 +532,14 @@ void ls_main(void)
dt = dirtree_start(*s, !(toys.optflags&(FLAG_l|FLAG_d|FLAG_F)) ||
(toys.optflags&(FLAG_L|FLAG_H)));
- if (!dt) {
- toys.exitval = 1;
- continue;
- }
-
- // Typecast means double_list->prev temporarirly goes in dirtree->parent
- dlist_add_nomalloc((void *)&TT.files->child, (struct double_list *)dt);
+ // note: double_list->prev temporarirly goes in dirtree->parent
+ if (dt) dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);
+ else toys.exitval = 1;
}
- // Turn double_list into dirtree
- dlist_to_dirtree(TT.files);
+ // Convert double_list into dirtree.
+ dlist_terminate(TT.files->child);
+ for (dt = TT.files->child; dt; dt = dt->next) dt->parent = TT.files;
// Display the files we collected
listfiles(AT_FDCWD, TT.files);