aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2020-04-08 10:36:03 -0700
committerRob Landley <rob@landley.net>2020-04-08 15:37:05 -0500
commit44c43973238ca1c8d7ac6e243932fb5b32731155 (patch)
treeb88dc431620cfd0ddf1cdc34581b1c742b85e219
parentb7bee86f384ef57a01686543ad2d108e8d6f4266 (diff)
downloadtoybox-44c43973238ca1c8d7ac6e243932fb5b32731155.tar.gz
ls: fix -h with block counts.
The filter() function modifies st_blocks so it's always 1KiB rather than 512B blocks, but the human-readable output was still assuming 512B. This meant that `ls -sh` was showing figures half the size of `ls -s`, and that the "total" line with -h was also off by a factor of 2. No new test, because I don't know how to write one that would work on all file systems. Bug: http://b/153383721
-rw-r--r--toys/posix/ls.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index dda82d40..01a64c2d 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -151,7 +151,7 @@ static void entrylen(struct dirtree *dt, unsigned *len)
} else len[5] = print_with_h(tmp, st->st_size, 1);
}
- len[6] = FLAG(s) ? print_with_h(tmp, st->st_blocks, 512) : 0;
+ len[6] = FLAG(s) ? print_with_h(tmp, st->st_blocks, 1024) : 0;
len[7] = FLAG(Z) ? strwidth((char *)dt->extra) : 0;
}
@@ -200,7 +200,7 @@ static int filter(struct dirtree *new)
if (FLAG(u)) new->st.st_mtime = new->st.st_atime;
if (FLAG(c)) new->st.st_mtime = new->st.st_ctime;
- new->st.st_blocks >>= 1;
+ new->st.st_blocks >>= 1; // Use 1KiB blocks rather than 512B blocks.
if (FLAG(a)||FLAG(f)) return DIRTREE_SAVE;
if (!FLAG(A) && new->name[0]=='.') return 0;
@@ -325,7 +325,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
totpad = totals[1]+!!totals[1]+totals[6]+!!totals[6]+totals[7]+!!totals[7];
if ((FLAG(h)||FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)||FLAG(s)) && indir->parent)
{
- print_with_h(tmp, blocks, 512);
+ print_with_h(tmp, blocks, 1024);
xprintf("total %s\n", tmp);
}
}
@@ -401,7 +401,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
if (FLAG(i)) zprint(zap, "lu ", totals[1], st->st_ino);
if (FLAG(s)) {
- print_with_h(tmp, st->st_blocks, 512);
+ print_with_h(tmp, st->st_blocks, 1024);
zprint(zap, "s ", totals[6], (unsigned long)tmp);
}