欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android系統(tǒng)在shell中的df命令實現(xiàn)

 更新時間:2018年12月20日 11:39:11   作者:Engineer-Bruce_Yang  
今天小編就為大家分享一篇關于Android系統(tǒng)在shell中的df命令實現(xiàn),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

當我們在shell中敲擊df這條命令的時候,會看到:

root@android:/ # df
Filesystem       Size  Used  Free  Blksize
/dev         446.8M 36.0K 446.8M  4096
/mnt/secure      446.8M 0.0 K 446.8M  4096
/mnt/asec       446.8M 0.0 K 446.8M  4096
/mnt/cart0      446.8M 0.0 K 446.8M  4096
/mnt/obb       446.8M 0.0 K 446.8M  4096
/system        1.5 G 376.6M 1.1 G  1024
/data         5.2 G 188.9M 5.0 G  4096
/cache        124.0M 16.1M 107.9M  4096
/mnt/.cci       503.9M 16.4M 487.6M  4096
/storage/sdcard0   5.2 G 188.9M 5.0 G  4096
/mnt/external_sd   7.5 G 475.0M 7.0 G  4096

那么,這是怎么實現(xiàn)的呢?

其實很簡單,就是利用statfs這個函數(shù)查詢文件系統(tǒng)相關的信息,然后依次列舉出來。

如果使用這個函數(shù)?

請看下文:

http://baike.baidu.com/link?url=EVV8n-l-DXfgNwYj5Lqzo0HFvYaXMYEzTBMVtuyrq0QCvpaD0Lr0RjX81L6jTE6RXplNC_cNec8tgdsDleX2pq

那么df是怎么實現(xiàn)的?請看源碼df.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/statfs.h>
static int ok = EXIT_SUCCESS;
//根據(jù)掛載的文件的大小來計算
static void printsize(long double n)
{
  char unit = 'K';
  n /= 1024;
  if (n > 1024) {
    n /= 1024;
    unit = 'M';
  }
  if (n > 1024) {
    n /= 1024;
    unit = 'G';
  }
  printf("%-4.1Lf%c", n, unit);
}
static void df(char *s, int always) {
 //
  struct statfs st;
 //statfs函數(shù)可用來查詢文件系統(tǒng)相關的信息。
  if (statfs(s, &st) < 0) {
    fprintf(stderr, "%s: %s\n", s, strerror(errno));
    ok = EXIT_FAILURE;
  } else {
    if (st.f_blocks == 0 && !always)
      return;    
    printf("%-20s ", s);
    printsize((long double)st.f_blocks * (long double)st.f_bsize);
    printf(" ");
    printsize((long double)(st.f_blocks - (long double)st.f_bfree) * st.f_bsize);
    printf(" ");
    printsize((long double)st.f_bfree * (long double)st.f_bsize);
    printf("  %d\n", (int) st.f_bsize);
  }
}
int df_main(int argc, char *argv[]) {
  printf("Filesystem       Size  Used  Free  Blksize\n");
  if (argc == 1) {
    char s[2000];
 //掛載的文件都在/proc/mounts下顯示
    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 = '\0';
          break;
        }
      }
      df(e, 0);
    }
    fclose(f);
  } else {
    int i;
    for (i = 1; i < argc; i++) {
      df(argv[i], 1);
    }
  }
  exit(ok);
}

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接

相關文章

最新評論