From 1f0795842016f9ae1913a09ddf79638690ef37f4 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Wed, 23 Oct 2019 10:55:19 +0200 Subject: [PATCH] mx_util: Add mx_df() --- mx_util.c | 12 ++++++++++++ mx_util.h | 1 + test_mx_util.c | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/mx_util.c b/mx_util.c index 0dd65101..b686ba11 100644 --- a/mx_util.c +++ b/mx_util.c @@ -13,6 +13,7 @@ #include #include +#include #include #include "mx_log.h" @@ -1331,3 +1332,14 @@ void _mx_sort_linked_list (void **list, int (*cmp)(void *o1,void *o2), void ** } *list=sorted; } + +unsigned long mx_df(const char *path) { + int res; + struct statfs s; + + res=statfs(path, &s); + if (res<0) { + return 0; + } + return s.f_bavail*s.f_frsize; +} diff --git a/mx_util.h b/mx_util.h index 46107414..bf31f4ee 100644 --- a/mx_util.h +++ b/mx_util.h @@ -169,5 +169,6 @@ int mx_daemon(int nochdir, int noclose); void _mx_sort_linked_list(void **list, int (*cmp)(void *o1,void *o2), void ** (*getnextptr)(void *o)); #define mx_sort_linked_list(list,cmp,getnextptr) _mx_sort_linked_list((void **)(list),(int (*)(void *,void *))(cmp),(void ** (*)(void *))(getnextptr)) +unsigned long mx_df(const char *path); #endif diff --git a/test_mx_util.c b/test_mx_util.c index 4d4c4f38..54ba7e03 100644 --- a/test_mx_util.c +++ b/test_mx_util.c @@ -530,6 +530,10 @@ static void test_listsort(void) assert(o[9].next==NULL); } +static void test_mx_df() { + assert(mx_df("/") > 0); +} + int main(int argc, char *argv[]) { test_mx_strskipwhitespaces(); @@ -550,5 +554,6 @@ int main(int argc, char *argv[]) test_mx_strcat(); test_mx_cpuset(); test_listsort(); + test_mx_df(); return 0; }