aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/ls.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-11-12 08:53:29 -0600
committerRob Landley <rob@landley.net>2019-11-12 08:53:29 -0600
commitdf6aaa39fbf7baa82e27e7eb83673629284966d0 (patch)
tree6e8da63c0277d60d545a30acb0564d0c6f4d7393 /toys/posix/ls.c
parent3def73006aa0d25a1b5a95fc19a4bd499858092c (diff)
downloadtoybox-df6aaa39fbf7baa82e27e7eb83673629284966d0.tar.gz
Replace a test I took out last commit because it seemed impossible to trigger,
but the problem is some vertical sort arrangements are impossible, and that's what it was testing for. For example, showing 29 entries in 9 columns with horizontal sort requires 4 rows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 29 29 x x x x x x x But with vertical sort that would be: 1 5 9 13 17 21 25 29 x 2 6 10 14 18 22 26 x x 3 7 11 15 19 23 27 x x 4 8 12 16 20 24 28 x x It still doesn't fit in 3 rows (3x9=27) but with 4 rows the 7 leftover spaces eats a whole column, so you _can't_ have 9 columns with vertical sort.
Diffstat (limited to 'toys/posix/ls.c')
-rw-r--r--toys/posix/ls.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 9d282acc..8bf9f693 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -356,13 +356,14 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Try to fit as many columns as we can, dropping down by one each time
for (;columns > 1; columns--) {
- unsigned c, totlen = columns;
+ unsigned c, cc, totlen = columns;
memset(colsizes, 0, columns*sizeof(unsigned));
for (ul = 0; ul<dtlen; ul++) {
- // measure each entry, plus two spaces between filenames
- entrylen(sort[next_column(ul, dtlen, columns, &c)], len);
- if (c<columns-1) *len += totpad+2;
+ cc = next_column(ul, dtlen, columns, &c);
+ if (cc>=dtlen) break; // tilt: remainder bigger than height
+ entrylen(sort[cc], len);
+ if (c<columns-1) *len += totpad+2; // 2 spaces between filenames
// Expand this column if necessary, break if that puts us over budget
if (*len > colsizes[c]) {
totlen += (*len)-colsizes[c];