Skip to content

Instantly share code, notes, and snippets.

@aarmot
Created May 27, 2022 06:44
Show Gist options
  • Save aarmot/0e9e3cba9251650e950705d351341043 to your computer and use it in GitHub Desktop.
Save aarmot/0e9e3cba9251650e950705d351341043 to your computer and use it in GitHub Desktop.
Get filesystem usage in C
#include <sys/vfs.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv) {
if (argc < 2) exit(EXIT_FAILURE);
struct statfs info;
if (statfs(argv[1], &info) == 0) {
printf("%s - block size: %ld,"
" total data blocks: %ld,"
" free blocks for root: %ld,"
" free blocks for user: %ld\n ",
argv[1], info.f_bsize, info.f_blocks, info.f_bfree, info.f_bavail);
exit(EXIT_SUCCESS);
} else {
perror("statfs()");
exit(EXIT_FAILURE);
}
}
// gcc -Wall -Wextra -O2 -o statfs statfs.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment