diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-28 01:48:04 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-28 01:48:04 -0500 |
commit | a267f8258162fdab350944676b2e71f858c3de51 (patch) | |
tree | 1352b3be09c9c6349b94aa2c82833c897fdb40e0 /src/fs.c | |
parent | 115076941358f58e13b9a8e79840fa741f70a84f (diff) | |
download | spmc-a267f8258162fdab350944676b2e71f858c3de51.tar.gz |
Started work on build helper functions
Diffstat (limited to 'src/fs.c')
-rw-r--r-- | src/fs.c | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -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; |