1 #include <sys/statfs.h> 2 #include <stdio.h> 3 #include <fcntl.h> 4 5 int main(int argc,char **argv) 6 { 7 int fd = open("/bin/about.elf", O_RDONLY); 8 if (fd == -1) 9 return 0; 10 printf("fd = %d\n", fd); 11 struct statfs diskInfo; 12 13 14 fstatfs(fd, &diskInfo); 15 unsigned long long blocksize1 = diskInfo.f_bsize; //每个block里包含的字节数 16 unsigned long long totalsize = blocksize1 * diskInfo.f_blocks;//总的字节数,f_blocks为block的数目 17 printf("Total_size=%llu B =%llu KB =%llu MB = %llu GB\n", 18 totalsize,totalsize>>10,totalsize>>20, totalsize>>30); 19 20 /* 2.获取一下剩余空间和可用空间的大小 */ 21 unsigned long long freeDisk = diskInfo.f_bfree * blocksize1; //剩余空间的大小 22 unsigned long long availableDisk = diskInfo.f_bavail * blocksize1; //可用空间大小 23 printf("Disk_free=%llu MB =%llu GB Disk_available=%llu MB = %llu GB\n", 24 freeDisk>>20,freeDisk>>30,availableDisk>>20, availableDisk>>30); 25 26 27 printf("====================\n"); 28 printf("diskInfo address: %p\n", diskInfo); 29 printf("f_type= %lu\n", diskInfo.f_type); 30 printf("f_bsize = %lu\n", diskInfo.f_bsize); 31 printf("f_blocks = %d\n", diskInfo.f_blocks); 32 printf("f_bfree = %lu\n", diskInfo.f_bfree); 33 printf("b_avail = %d\n", diskInfo.f_bavail); 34 printf("f_files = %d\n", diskInfo.f_files); 35 printf("f_ffree = %lu\n", diskInfo.f_ffree); 36 printf("f_fsid = %ld\n", diskInfo.f_fsid); 37 printf("f_namelen = %ld\n", diskInfo.f_namelen); 38 printf("f_frsize = %ld\n", diskInfo.f_frsize); 39 printf("f_flags = %ld\n", diskInfo.f_flags); 40 return 0; 41 }