statfs is a very handy Linux library function. With its help, we can easily get hard drive information and the like.

In the past I used to shell out to command-line utilities for this — how lame. If you run man 2 statfs, you'll find plenty of usage details. This post simply writes a demo to show that it exists

C
#include <sys/vfs.h>
#include <stdio.h>

int main()
{
	struct statfs r;
	printf("%s\n",statfs("/boot",&r)==0?"success":"error");
	printf("Total:%ld ; Available:%ld ; Use:%2.2f\% \n",
			r.f_blocks,r.f_bavail,
			((double)(r.f_blocks-r.f_bavail)/r.f_bfree)*100);
	return 0;
}
It checks the block capacity of the partition mounted at /boot, along with the amount already used.

The compile output is as follows:

Bash
[yu@argcv-com statfs]$ ./a.out 
success
Total:495844 ; Available:362335 ; Use:34.42%