sfm

simple file manager
git clone git://git.afify.dev/sfm
Log | Files | Refs | README | LICENSE

commit a16d3b2c44b2c280a1a7548684df4bff95473da1
parent 205dde4cf3acaf708c93f5ad25f59144877e940b
Author: afify <hassan@afify.dev>
Date:   Wed, 31 Mar 2021 23:38:21 +0300

[ref] get_fsize()

- check snprintf return value.
- rename unit variable to unit.
- remove lsize unused variable.

Diffstat:
Msfm.c | 23+++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/sfm.c b/sfm.c @@ -547,42 +547,41 @@ static char * get_fsize(off_t size) { char *result; /* need to be freed */ - char hsize; + char unit; int result_len; - int lsize; int counter; counter = 0; result_len = 6; /* 9999X/0 */ result = ecalloc(result_len, sizeof(char)); - lsize = (int)size; - while (lsize >= 1000) { - lsize /= 1024; + while (size >= 1000) { + size /= 1024; ++counter; } switch (counter) { case 0: - hsize = 'B'; + unit = 'B'; break; case 1: - hsize = 'K'; + unit = 'K'; break; case 2: - hsize = 'M'; + unit = 'M'; break; case 3: - hsize = 'G'; + unit = 'G'; break; case 4: - hsize = 'T'; + unit = 'T'; break; default: - hsize = '?'; + unit = '?'; } - (void)snprintf(result, result_len, "%d%c", lsize, hsize); + if (snprintf(result, result_len, "%ld%c", size, unit) < 0) + strncat(result, "???", result_len); return result; }