On a POSIX system you would use the file system statistics data type and function calls. These are defined in <sys/statvfs.h>.
Here is what the data structure and funtion prototypes should look like in <sys/statvfs.h>
-
struct statvfs {
unsigned long f_bsize; /* file system block size */
-
unsigned long f_frsize; /* fragment size */
-
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
-
fsblkcnt_t f_bfree; /* # free blocks */
-
fsblkcnt_t f_bavail; /* # free blocks for non-root */
-
fsfilcnt_t f_files; /* # inodes */
-
fsfilcnt_t f_ffree; /* # free inodes */
-
fsfilcnt_t f_favail; /* # free inodes for non-root */
-
unsigned long f_fsid; /* file system ID */
-
unsigned long f_flag; /* mount flags */
-
unsigned long f_namemax; /* maximum filename length */};
-
-
/* These functions return 0 on success and -1 on failure */
-
-
int statvfs(const char *filename, struct statvfs buf);
-
int fstatvfs(int fd, struct statvfs buf);
-
This sample code outputs the disk usage and free space in bytes.
-
include <sys/statvfs.h>
-
-
...
-
-
/* Any file on the filesystem in question */
-
char *filename = "/home/tyreld/somefile.txt";
-
-
struct statvfs buf;
-
if (!statvfs(filename, &buf)) {
unsigned long blksize, blocks, freeblks, disk_size, used, free;
-
-
blksize = buf.f_bsize;
-
blocks = buf.f_blocks;
-
freeblks = buf.f_bfree;
-
-
disk_size = blocks * blksize;
-
free = freeblks * blksize;
-
used = disk_size - free;
-
-
printf("Disk usage : %lu \t Free space %lu\n", used, free);} else {
printf("Couldn't get file system statistics\n");
} -
-