当我们在shell中敲击df这条命令的时候,会看到:
代码语言:javascript复制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
那么,这是怎么实现的呢?
其实很简单,就是利用statfs这个函数查询文件系统相关的信息,然后依次列举出来。
如果使用这个函数?
请看下文:
http://baike.baidu.com/link?url=EVV8n-l-DXfgNwYj5Lqzo0HFvYaXMYEzTBMVtuyrq0QCvpaD0Lr0RjX81L6jTE6RXplNC_cNec8tgdsDleX2pq
那么df是怎么实现的?请看源码df.c
代码语言:javascript复制#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 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函数可用来查询文件系统相关的信息。
if (statfs(s, &st) < 0) {
fprintf(stderr, "%s: %sn", 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(" %dn", (int) st.f_bsize);
}
}
int df_main(int argc, char *argv[]) {
printf("Filesystem Size Used Free Blksizen");
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 = '#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 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函数可用来查询文件系统相关的信息。
if (statfs(s, &st) < 0) {
fprintf(stderr, "%s: %sn", 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(" %dn", (int) st.f_bsize);
}
}
int df_main(int argc, char *argv[]) {
printf("Filesystem Size Used Free Blksizen");
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 = '