diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-28 12:31:49 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-28 12:31:49 -0400 |
commit | 3b53b4c96d05cfadcf275d24f021159471343048 (patch) | |
tree | 275af254cd3402fccfa214d8f0f7c08353e7869f /src/shared.c | |
parent | remove __ on uid and gid for alpine (diff) | |
download | lazysphere-3b53b4c96d05cfadcf275d24f021159471343048.tar.gz lazysphere-3b53b4c96d05cfadcf275d24f021159471343048.tar.bz2 lazysphere-3b53b4c96d05cfadcf275d24f021159471343048.zip |
improve ls command
Diffstat (limited to '')
-rw-r--r-- | src/shared.c | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/src/shared.c b/src/shared.c index a8837c4..b167e6f 100644 --- a/src/shared.c +++ b/src/shared.c @@ -47,7 +47,7 @@ bool prefix(const char* pre, const char* str) { } static char fs_types[5] = {'K','M','G','T','P'}; -void print_file_size(size_t bytes) { +void print_file_size(size_t bytes, char buf[5]) { int index = 0; float next = bytes; while (true) { @@ -61,25 +61,26 @@ void print_file_size(size_t bytes) { next /= 1024; index++; } - - if (next/100 < 1) putchar(' '); - if (next/10 < 1) putchar(' '); - if (index == 0) putchar(' '); - - printf("%u", (int)(next+.5)); + + int n = snprintf(buf, 4, "%u", (int)(next+.5)); if (index > 0) { - putchar(fs_types[index - 1]); + buf[n] = (fs_types[index - 1]); + n++; } - putchar(' '); + buf[n] = '\0'; } static char* months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; -void print_date_time(time_t mills) { +void print_date_time(time_t mills, char buf[13]) { struct tm* info; info = localtime(&mills); - printf("%s ", months[info->tm_mon]); - if (info->tm_mday < 10) - printf(" "); - printf("%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec); + int n = snprintf(buf, 5, "%s ", months[info->tm_mon]); + + if (info->tm_mday < 10) { + buf[n] = ' '; + n++; + } + + snprintf(buf + n, 13 - n, "%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec); } |