From a267f8258162fdab350944676b2e71f858c3de51 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sat, 28 Dec 2019 01:48:04 -0500 Subject: Started work on build helper functions --- src/fs.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/fs.c') diff --git a/src/fs.c b/src/fs.c index f561c84..e20b6a7 100644 --- a/src/fs.c +++ b/src/fs.c @@ -299,6 +299,11 @@ int rsync(const char *_args, const char *_source, const char *_destination) { return returncode; } +/** + * Return the size of a file + * @param filename + * @return + */ long int get_file_size(const char *filename) { long int result = 0; FILE *fp = fopen(filename, "rb"); @@ -337,6 +342,26 @@ int mkdirs(const char *_path, mode_t mode) { return result; } +/** + * Convert size in bytes to the closest human-readable unit + * + * NOTE: Caller is responsible for freeing memory + * + * Example: + * ~~~{.c} + * char *output; + * output = human_readable_size(1); // "1B" + * free(output); + * output = human_readable_size(1024) // "1.0K" + * free(output); + * output = human_readable_size(1024000) // "1.0MB" + * free(output); + * // and so on + * ~~~ + * + * @param n size to convert + * @return string + */ char *human_readable_size(uint64_t n) { int i; double result = (double)n; -- cgit