Skip to content

Instantly share code, notes, and snippets.

@maurice-schuppe
Forked from vgerak/statvfs-df.c
Created November 6, 2019 15:30
Show Gist options
  • Save maurice-schuppe/1c28db5f65d5859503cee9f69c0318c6 to your computer and use it in GitHub Desktop.
Save maurice-schuppe/1c28db5f65d5859503cee9f69c0318c6 to your computer and use it in GitHub Desktop.
Get disk usage with statvfs()
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, const char *argv[])
{
const unsigned int GB = (1024 * 1024) * 1024;
struct statvfs buffer;
int ret = statvfs(argv[1], &buffer);
if (!ret) {
const double total = (double)(buffer.f_blocks * buffer.f_frsize) / GB;
const double available = (double)(buffer.f_bfree * buffer.f_frsize) / GB;
const double used = total - available;
const double usedPercentage = (double)(used / total) * (double)100;
printf("Total: %f --> %.0f\n", total, total);
printf("Available: %f --> %.0f\n", available, available);
printf("Used: %f --> %.1f\n", used, used);
printf("Used Percentage: %f --> %.0f\n", usedPercentage, usedPercentage);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment