From 342c088a70c6d7ccb02521b430b2fb0125611ed8 Mon Sep 17 00:00:00 2001 From: Andrew Ilijic Date: Mon, 28 Oct 2019 17:07:35 -0400 Subject: 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. --- toys/posix/ls.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'toys') 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; ulst); 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'); -- cgit v1.2.3