learning statfs func

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\n",
            (unsigned long) sfs.f_type);
    printf("Optimal I/O block size:        %lu\n",
            (unsigned long) sfs.f_bsize);
    printf("Total data blocks:             %lu\n",
            (unsigned long) sfs.f_blocks);
    printf("Free data blocks:              %lu\n",
            (unsigned long) sfs.f_bfree);
    printf("Free blocks for nonsuperuser:  %lu\n",
            (unsigned long) sfs.f_bavail);
    printf("Total i-nodes:                 %lu\n",
            (unsigned long) sfs.f_files);
    printf("File system ID:                %#x, %#x\n",
            (unsigned) sfs.f_fsid.__val[0], (unsigned) sfs.f_fsid.__val[1]);
    printf("Free i-nodes:                  %lu\n",
            (unsigned long) sfs.f_ffree);
    printf("Maximum file name length:      %lu\n",
            (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
posted @ 2020-01-09 17:10  嵌入式实操  阅读(157)  评论(0编辑  收藏  举报