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 <sys/types.h>
#include <sys/stat.h>
+#include <sys/vfs.h>
#include <fcntl.h>
#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;
}