Skip to content

Commit

Permalink
mx_util: Add mx_df()
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Jan 29, 2020
1 parent 81340e7 commit 1f07958
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <fcntl.h>

#include "mx_log.h"
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -550,5 +554,6 @@ int main(int argc, char *argv[])
test_mx_strcat();
test_mx_cpuset();
test_listsort();
test_mx_df();
return 0;
}

0 comments on commit 1f07958

Please sign in to comment.