mirror of
https://github.com/jarun/nnn.git
synced 2025-01-26 02:36:36 +00:00
Replace snprintf for printing file size
This commit is contained in:
parent
bf28a096ba
commit
d5408c4e9f
27
src/nnn.c
27
src/nnn.c
|
@ -2133,6 +2133,7 @@ static char *coolsize(off_t size)
|
||||||
const char * const U = "BKMGTPEZY";
|
const char * const U = "BKMGTPEZY";
|
||||||
static char size_buf[12]; /* Buffer to hold human readable size */
|
static char size_buf[12]; /* Buffer to hold human readable size */
|
||||||
off_t rem = 0;
|
off_t rem = 0;
|
||||||
|
size_t ret;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
while (size >= 1024) {
|
while (size >= 1024) {
|
||||||
|
@ -2177,10 +2178,28 @@ static char *coolsize(off_t size)
|
||||||
rem /= 10;
|
rem /= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i > 0 && i < 6)
|
if (i > 0 && i < 6) {
|
||||||
snprintf(size_buf, 12, "%u.%0*u%c", (uint)size, i & 0b11, (uint)rem, U[i]);
|
ret = xstrlcpy(size_buf, xitoa(size), 11);
|
||||||
else
|
size_buf[ret - 1] = '.';
|
||||||
snprintf(size_buf, 12, "%u%c", (uint)size, U[i]);
|
|
||||||
|
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;
|
return size_buf;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue