aboutsummaryrefslogtreecommitdiff
path: root/src/fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs.c')
-rw-r--r--src/fs.c25
1 files changed, 25 insertions, 0 deletions
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;