LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cal free disk space (https://www.linuxquestions.org/questions/programming-9/cal-free-disk-space-500286/)

ninjanesto 11-10-2006 04:37 AM

cal free disk space
 
I all,
i'm trying to calculate the disk usage and free space using statvfs.
I' using this code but when I execute th "df ." the values dont match
Can anyone help me??

Best regards

/* Any file on the filesystem in question */
char *filename = "/home/nesto/test/test.cpp";

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");
}
return 0;

Mara 11-10-2006 03:52 PM

df . prints values in 1K size, so you need to multiply by 4, not by blksize.


All times are GMT -5. The time now is 07:25 AM.