aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Ilijic <ilijic.andrew@gmail.com>2019-10-28 17:07:35 -0400
committerRob Landley <rob@landley.net>2019-10-31 22:24:11 -0500
commit342c088a70c6d7ccb02521b430b2fb0125611ed8 (patch)
treef7608bfa94d9cce8af4f5e68e1d62a511918b622
parent4885e8fea8f76d6ec4ba791bce56d70c4a5ded01 (diff)
downloadtoybox-342c088a70c6d7ccb02521b430b2fb0125611ed8.tar.gz
ls: Remove trailing whitespace so that tests pass
When in modes `-C` and `-x` we need to remove the trailing whitespace on each line. This is the behavior of other `ls` commands. Other `ls` commands will print the last filename and then print a newline. Prior to this patch we would print the last filename, followed by two spaces, and then print a newline. Previously, we would get to the end of the loop and print the padding. I couldn't figure out a way to determine when the program had reached the end of a line. So I piggybacked off of the newline code.
-rw-r--r--toys/posix/ls.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 809e2504..c7b09bf2 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -403,9 +403,10 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Loop through again to produce output.
width = 0;
+ unsigned curcol = 0;
for (ul = 0; ul<dtlen; ul++) {
int ii, zap;
- unsigned curcol, color = 0;
+ unsigned lastlen = *len, lastcol = curcol, color = 0;
unsigned long next = next_column(ul, dtlen, columns, &curcol);
struct stat *st = &(sort[next]->st);
mode_t mode = st->st_mode;
@@ -426,6 +427,10 @@ static void listfiles(int dirfd, struct dirtree *indir)
if (mm) xputc(',');
if (flags & (FLAG_C|FLAG_x)) {
if (!curcol) xputc('\n');
+ else { // Pad columns
+ lastcol = colsizes[lastcol]-lastlen-totpad;
+ printf("%*c", lastcol, ' ');
+ }
} else if ((flags & FLAG_1) || width+1+*len > TT.screen_width) {
xputc('\n');
width = 0;
@@ -521,12 +526,6 @@ static void listfiles(int dirfd, struct dirtree *indir)
}
if (et) xputc(et);
-
- // Pad columns
- if (flags & (FLAG_C|FLAG_x)) {
- curcol = colsizes[curcol]-(*len)-totpad;
- if (curcol < 255) printf("%*c", curcol, ' ');
- }
}
if (width) xputc('\n');