aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-02-07 13:22:28 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-02-07 13:22:28 -0800
commit7c4eacd1b86b8a5ed8b889066d01e35f2a0f42b4 (patch)
tree135e45f0bb3db429eb9dea7bf4353063b126df85
parentd145f13cca2e26be27aea134da47cdab5d0f91ec (diff)
Fixed bug in size rendering, added safety check.
-rw-r--r--draw.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/draw.c b/draw.c
index 6f04695..2e0dbe3 100644
--- a/draw.c
+++ b/draw.c
@@ -181,14 +181,19 @@ void col_random(entry_t *entry, const char *color, char *buf, int width)
void col_size(entry_t *entry, const char *color, char *buf, int width)
{
(void)color; (void)width;
- int j = 0;
+ int mag = 0;
const char* units = "BKMGTPEZY";
double bytes = (double)entry->info.st_size;
while (bytes > 1024) {
bytes /= 1024;
- j++;
+ mag++;
+ if (!units[mag]) {
+ sprintf(buf, " ???");
+ return;
+ }
}
- sprintf(buf, " %6.*f%c ", j > 0 ? 1 : 0, bytes, units[j]);
+ // Add 1 extra digit of precision if it would be nonzero:
+ sprintf(buf, "%5.*f%c ", ((int)(bytes*10.0 + 0.5)%10 >= 1) ? 1 : 0, bytes, units[mag]);
}
void col_name(entry_t *entry, const char *color, char *buf, int width)