Skip to content

Instantly share code, notes, and snippets.

@nirbhayc
Last active January 22, 2016 22:26
Show Gist options
  • Save nirbhayc/07bba0ed221f130467eb to your computer and use it in GitHub Desktop.
Save nirbhayc/07bba0ed221f130467eb to your computer and use it in GitHub Desktop.
// Free disk space using statvfs() and df -h.
#include <stdio.h>
#include <stdlib.h>
#include <sys/statvfs.h>
float _statvfs()
{
int err;
struct statvfs stat;
if ((statvfs("/", &stat)) < 0 )
{
printf("failed to stat!\n");
exit(1);
}
return (stat.f_bavail * stat.f_bsize) / 1024 / 1024 / 1024;
}
char * _system(const char *cmd, char *buf)
{
FILE *f;
f = popen(cmd, "r");
if (!f)
{
printf("failed to open pipe!\n");
exit(1);
}
fgets(buf, 10, f);
pclose(f);
return buf;
}
void main()
{
char buf[10];
printf("statvfs() : %.2fG\n", _statvfs());
printf("df -f : %s",
_system("df -h / | tail -1 | awk '{print $4}'", buf));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment