diff options
author | Arun Prakash Jana <engineerarun@gmail.com> | 2019-07-05 01:13:55 +0530 |
---|---|---|
committer | Arun Prakash Jana <engineerarun@gmail.com> | 2019-07-05 01:13:55 +0530 |
commit | d5408c4e9f3c0a558a30192322c3a6b8c6562b5d (patch) | |
tree | ca937f06a3bb283a4a4363c5728156e10bd0c625 /src/nnn.c | |
parent | bf28a096ba3a4180d7590c94516f792b8702c2d3 (diff) | |
download | nnn-d5408c4e9f3c0a558a30192322c3a6b8c6562b5d.tar.gz |
Replace snprintf for printing file size
Diffstat (limited to 'src/nnn.c')
-rw-r--r-- | src/nnn.c | 27 |
1 files changed, 23 insertions, 4 deletions
@@ -2133,6 +2133,7 @@ static char *coolsize(off_t size) const char * const U = "BKMGTPEZY"; static char size_buf[12]; /* Buffer to hold human readable size */ off_t rem = 0; + size_t ret; int i = 0; while (size >= 1024) { @@ -2177,10 +2178,28 @@ static char *coolsize(off_t size) rem /= 10; } - if (i > 0 && i < 6) - snprintf(size_buf, 12, "%u.%0*u%c", (uint)size, i & 0b11, (uint)rem, U[i]); - else - snprintf(size_buf, 12, "%u%c", (uint)size, U[i]); + if (i > 0 && i < 6) { + ret = xstrlcpy(size_buf, xitoa(size), 11); + size_buf[ret - 1] = '.'; + + char *frac = xitoa(rem); + size_t toprint = i > 3 ? 3 : i; + size_t len = strlen(frac); + + if (len < toprint) { + size_buf[ret] = size_buf[ret + 1] = size_buf[ret + 2] = '0'; + xstrlcpy(size_buf + ret + (toprint - len), frac, (toprint - len) + 1); + } else + xstrlcpy(size_buf + ret, frac, toprint + 1); + + ret += toprint; + } else { + ret = xstrlcpy(size_buf, size ? xitoa(size) : "0", 10); + --ret; + } + + size_buf[ret] = U[i]; + size_buf[ret + 1] = '\0'; return size_buf; } |