diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-16 19:15:01 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-16 19:15:01 -0400 |
commit | 67173fb97117a41cebb82a1364ae686a9574927b (patch) | |
tree | e631e7515bcb7420b6348c0e3159336298d4e3df /lib/string.c | |
parent | update ls (diff) | |
download | lazysphere-67173fb97117a41cebb82a1364ae686a9574927b.tar.gz lazysphere-67173fb97117a41cebb82a1364ae686a9574927b.tar.bz2 lazysphere-67173fb97117a41cebb82a1364ae686a9574927b.zip |
slight refactor and start documenting
Diffstat (limited to 'lib/string.c')
-rw-r--r-- | lib/string.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c new file mode 100644 index 0000000..13e414e --- /dev/null +++ b/lib/string.c @@ -0,0 +1,79 @@ +#include "lslib.h" + +#include <ctype.h> +#include <stddef.h> +#include <time.h> + +static char fs_types[5] = {'K','M','G','T','P'}; +void print_file_size(size_t bytes, char buf[5]) { + int index, n; + float next; + + index = 0; + next = bytes; + + while (true) { + if (next < 1000) { + break; + } + + if (index == 5) { + printf("999P"); + return; + } + + next /= 1024; + index++; + } + + n = snprintf(buf, 4, "%u", (int)(next+.5)); + + if (index > 0) { + buf[n] = (fs_types[index - 1]); + n++; + } + + 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, char buf[13]) { + struct tm* info; + int n; + + info = localtime(&mills); + 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); +} + +void print_file_path(char* path) { + if (streql("-", path)) { + printf("(standard input)"); + } else { + printf("%s", path); + } +} + +void nuke_str(char* type) { + *type = 0; + while(*(++type)) { + *type = 0; + } +} + +bool printable_char(char c) { + switch (c) { + case '\n': return true; + case '\r': return true; + case '\b': return true; + case '\t': return true; + default: return isprint(c) == 0; + } +} |