C Program to print Statistics of a File System [System Programming]
This is the c program to print statistics of a file system ie:disk usage
The function statfs() returns information about a mounted file system. path is the pathname of any file within the mounted file system. buf is a pointer to a statfs structure defined approximately as follows:
#if __WORDSIZE == 32
/* System word size */
# define __SWORD_TYPE int
#else /* __WORDSIZE == 64 */
# define __SWORD_TYPE
long int
#endif
struct statfs {
__SWORD_TYPE f_type; /* type of file system (see below) */
__SWORD_TYPE f_bsize; /* optimal transfer block size */
fsblkcnt_t f_blocks; /* total data blocks in file system */
fsblkcnt_t f_bfree; /* free blocks in fs */
fsblkcnt_t f_bavail; /* free blocks available to
unprivileged user */
fsfilcnt_t f_files; /* total file nodes in file system */
fsfilcnt_t f_ffree; /* free file nodes in fs */
fsid_t f_fsid; /* file system id */
__SWORD_TYPE f_namelen; /* maximum length of filenames */
__SWORD_TYPE f_frsize; /* fragment size (since Linux 2.6) */
__SWORD_TYPE f_spare[5];
};
#include<sys/vfs.h>
#include<stdio.h>
main(int argc,char **argv)
{
struct statfs stfs;
statfs(argv[1],&stfs);
printf("Total Blocks in the Filesystem %dn",4*stfs.f_blocks);
printf("Block Size %dn",stfs.f_bsize/4);
printf("Total Free Blocks in FS %dn",stfs.f_bfree);
printf("Total Blocks Used %dn",4*(stfs.f_blocks-stfs.f_bfree));
printf("Total Blocks Available to non Super User %dn",4*stfs.f_bavail);
printf("Total File Nodes %dn",stfs.f_ffree);
printf("Total Blocks in the Filesystem %dn",stfs.f_blocks);
}
Recent Comments