`
Code
`#include <stdio.h>
#include <sys/statvfs.h>
void print_filesystem_stats(const char *path) {
struct statvfs stat;
if (statvfs(path, &stat) != 0) {
perror("statvfs");
return;
}
// Calculate the total size, used size, and available size
unsigned long long total_size = (stat.f_blocks * stat.f_frsize)/(1024*1024);
unsigned long long free_size = (stat.f_bfree * stat.f_frsize)/(1024*1024);
unsigned long long available_size = (stat.f_bavail * stat.f_frsize)/(1024*1024);
unsigned long long used_size = (total_size - free_size)/(1024*1024);
printf("Filesystem statistics for %s:n", path);
printf(" Total size: %llu MBn", total_size);
printf(" Used size: %llu MBn", used_size);
printf(" Available size: %llu MBn", available_size);
}
int main() {
const char *path = "/mnt/sd";
print_filesystem_stats(path);
return 0;
}`
fstab file
stock fstab – you probably want to override this with a machine specific one
/dev/root / auto defaults 1 1
proc /proc proc defaults 0 0
devpts /dev/pts devpts mode=0620,gid=5 0 0
none /dev/shm tmpfs defaults 0 0
tmpfs /run tmpfs mode=0755,nodev,nosuid,strictatime 0 0
tmpfs /var/volatile tmpfs defaults 0 0
uncomment this if your device has a SD/MMC/Transflash slot
#/dev/mmcblk0p1 /media/card auto defaults,sync,noauto 0 0
Output of my code
Filesystem statistics for /mnt/sd:
Total size: 1249 MB
Used size: 17592186044415 MB
Available size: 2533 MB
I want to calculate the total and available size of /mnt/sd. But output of my code is different from df command.
What can be the issue?
df -h
Filesystem Size Used Avail Use% Mounted on
ubi0:fs 342M 261M 82M 77% /
devtmpfs 2.0G 8.0K 2.0G 1% /dev
tmpfs 2.0G 140K 2.0G 1% /run
tmpfs 2.0G 56K 2.0G 1% /var/volatile
/dev/mmcblk0 3.5G 7.2M 3.3G 1% /mnt/sd1
/dev/sda1 30G 690M 28G 3% /mnt/sata
/dev/sda2 30G 1.3G 27G 5% /mnt/sd`
Rinkal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.