statfs 查询文件系统相关的信息。
code:
#include <sys/statfs.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct statfs sfs; if (argc != 2 || strcmp(argv[1], "--help") == 0){ return -1; } if (statfs(argv[1], &sfs) == -1){ perror("statfs"); return -1; } printf("File system type: %#lx ", (unsigned long) sfs.f_type); printf("Optimal I/O block size: %lu ", (unsigned long) sfs.f_bsize); printf("Total data blocks: %lu ", (unsigned long) sfs.f_blocks); printf("Free data blocks: %lu ", (unsigned long) sfs.f_bfree); printf("Free blocks for nonsuperuser: %lu ", (unsigned long) sfs.f_bavail); printf("Total i-nodes: %lu ", (unsigned long) sfs.f_files); printf("File system ID: %#x, %#x ", (unsigned) sfs.f_fsid.__val[0], (unsigned) sfs.f_fsid.__val[1]); printf("Free i-nodes: %lu ", (unsigned long) sfs.f_ffree); printf("Maximum file name length: %lu ", (unsigned long) sfs.f_namelen); exit(0); }
result:
# ./a.out /dev/sdb1 File system type: 0x1021994 Optimal I/O block size: 4096 Total data blocks: 756675 Free data blocks: 756675 Free blocks for nonsuperuser: 756675 Total i-nodes: 756675 File system ID: 0, 0 Free i-nodes: 756172 Maximum file name length: 255