zoukankan      html  css  js  c++  java
  • Linux下使用fstatfs/statfs查询系统相关信息

    Linux下使用fstatfs/statfs查询系统相关信息

    1.   功能

    #include < sys/statfs.h >

    int statfs(const char *path, struct statfs *buf);

    int fstatfs(int fd, struct statfs *buf);

    查询文件系统相关的信息。

    2.   參数

    path: 须要查询信息的文件系统的文件路径名。

    fd: 须要查询信息的文件系统的文件描写叙述符。

    buf:下面结构体的指针变量,用于储存文件系统相关的信息

    struct statfs {

    long f_type; /* 文件系统类型 */

    long f_bsize; /* 经过优化的传输块大小 */

    long f_blocks; /* 文件系统数据块总数 */

    long f_bfree; /* 可用块数 */

    long f_bavail; /* 非超级用户可获取的块数*/

    long f_files; /* 文件结点总数 */

    long f_ffree; /* 可用文件结点数 */

    fsid_t f_fsid; /* 文件系统标识 */

    long f_namelen; /* 文件名称的最大长度 */

    };

    3.   返回值

    成功运行时,返回0。

    失败返回-1。errno被设为下面的某个值

    EACCES: (statfs())文件或路径名中包括的文件夹不可訪问

    EBADF : (fstatfs())文件描写叙述词无效

    EFAULT: 内存地址无效

    EINTR : 操作由信号中断

    EIO : 读写出错

    ELOOP : (statfs())解释路径名过程中存在太多的符号连接

    ENAMETOOLONG:(statfs()) 路径名太长

    ENOENT:(statfs()) 文件不存在

    ENOMEM: 核心内存不足

    ENOSYS: 文件系统不支持调用

    ENOTDIR:(statfs())路径名中当作文件夹的组件并不是文件夹

    EOVERFLOW:信息溢出

    4.   实例

    #include <sys/vfs.h>

    #include <stdio.h>

    int main()

    {

            struct statfs diskInfo;

            statfs("/",&diskInfo);

            unsigned long long blocksize =diskInfo.f_bsize;// 每一个block里面包括的字节数

            unsigned long long totalsize =blocksize * diskInfo.f_blocks;//总的字节数

            printf("TOTAL_SIZE == %luMB/n",totalsize>>20); // 1024*1024 =1MB  换算成MB单位

            unsigned long long freeDisk =diskInfo.f_bfree*blocksize; //再计算下剩余的空间大小

            printf("DISK_FREE == %ldMB/n",freeDisk>>20);

            return 0;

    }

    附:

    linux df命令实现:

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include <errno.h>

    #include <sys/statfs.h>

    static int ok = EXIT_SUCCESS;

    static void printsize(long long n)

    {

      char unit = 'K';

      n /= 1024;

      if (n > 1024) {

               n /= 1024;

               unit = 'M';

      }

      if (n > 1024) {

               n /= 1024;

      unit = 'G';

      }

      printf("%4lld%c", n, unit);

    }

    static void df(char *s, int always) {

      struct statfs st;

            if (statfs(s, &st) < 0) {

      fprintf(stderr,"%s: %s ", s, strerror(errno));

      ok = EXIT_FAILURE;

    } else {

      if(st.f_blocks == 0 && !always)

      return;

      printf("%-20s", s);

      printf("%-20s", s);

      printsize((longlong)st.f_blocks * (long long)st.f_bsize);

      printf("");

      printsize((longlong)(st.f_blocks - (long long)st.f_bfree) * st.f_bsize);

      printf("");

      printsize((longlong)st.f_bfree * (long long)st.f_bsize);

      printf("%d ", (int) st.f_bsize);

    }

    }

    int main(int argc, char *argv[]) {

      printf("Filesystem Size Used Free Blksize ");

      if (argc == 1) {

               char s[2000];

               FILE *f =fopen("/proc/mounts", "r");

                     while (fgets(s, 2000, f)) {

               char *c, *e = s;

                     for (c = s; *c; c++) {

                                  if(*c == ' ') {

                                  e =c + 1;

                                  break;

                         }

           }

                for (c = e; *c; c++) {

                    if (*c == '') {

                    *c = '';

                         break;

               }

    }

    df(e, 0);

    }

    fclose(f);

      } else {

      printf(" NO argv ");

      int i;

            for (i = 1; i< argc; i++) {

           df(argv[i],1);

      }

      }

    exit(ok);

    }

  • 相关阅读:
    C知识(#一些笔记)
    我整理的Python规则(2)
    我整理的Python代码规则
    教你如何通俗易懂的了解深度学习知识
    c#中委托和事件(续)(转)
    c#中的委托和事件(转)
    .Net neatupload上传控件实现文件上传的进度条
    C#常用日期格式处理转换[C#日期格式转换大全
    C#string常用函数总结
    数据库08版本如何移动到05的数据中
  • 原文地址:https://www.cnblogs.com/yfceshi/p/6994364.html
Copyright © 2011-2022 走看看