df命令简单移植
来源:互联网 发布:仓廪实而知礼节的一辩 编辑:程序博客网 时间:2024/06/09 20:13
1、介绍
df命令用语查看挂在文件系统空间使用情况,存在于GNU coreutils 工具包里;busybox工具中的df命令也是从coreutils中移植而来的;
2、 Ubuntu Linux 相同df命令执行结果如下:
xmuhlj@xmuhlj-laptop:~$ df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda1 29039484 13277100 14287264 49% /
none 1024556 268 1024288 1% /dev
none 1029088 272 1028816 1% /dev/shm
none 1029088 104 1028984 1% /var/run
none 1029088 0 1029088 0% /var/lock
none 1029088 0 1029088 0% /lib/init/rw
/dev/sr0 56056 56056 0 100% /media/VBOXADDITIONS_4.2.6_82870
3、移植自busybox中的df命令,执行结果如下:
xmuhlj@xmuhlj-laptop:~$ ./df
Filesyste 1K-Block Used Available Used Mounted on
none 0 0 0 0% /sys
none 0 0 0 0% /proc
none 1024556 268 1024288 0% /dev
none 0 0 0 0% /dev/pts
/dev/disk/by-uuid/ae7d69fa-ec45-4dce-ad65-f8f82c196ea0 29039484 13277068 14287296 48% /
none 0 0 0 0% /sys/fs/fuse/connections
none 0 0 0 0% /sys/kernel/debug
none 0 0 0 0% /sys/kernel/security
none 1029088 272 1028816 0% /dev/shm
none 1029088 104 1028984 0% /var/run
none 1029088 0 1029088 0% /var/lock
none 1029088 0 1029088 0% /lib/init/rw
binfmt_misc 0 0 0 0% /proc/sys/fs/binfmt_misc
gvfs-fuse-daemon 0 0 0 0% /home/xmuhlj/.gvfs
/dev/sr0 56056 56056 0 100% /media/VBOXADDITIONS_4.2.6_82870
4、代码如下:
#include <mntent.h>#include <sys/vfs.h>
#include <stdio.h>
#define bb_path_mtab_file "/proc/mounts"
static unsigned long kscale(unsigned long b, unsigned long bs)
{
return (b * (unsigned long long) bs + 1024/2) / 1024;
}
int main()
{
unsigned long blocks_used;
unsigned blocks_percent_used;
FILE *mount_table;
struct mntent *mount_entry;
struct statfs s;
printf("Filesystem 1K-Block Used Available Used Mounted on\n");
mount_table = NULL;
mount_table = setmntent(bb_path_mtab_file, "r");
if (!mount_table)
return -1;
while (1) {
const char *device;
const char *mount_point;
if (mount_table) {
mount_entry = getmntent(mount_table);
if (!mount_entry) {
endmntent(mount_table);
break;
}
device = mount_entry->mnt_fsname;
mount_point = mount_entry->mnt_dir;
statfs(mount_point, &s);
blocks_used = s.f_blocks - s.f_bfree;
blocks_percent_used = 0;
if (blocks_used + s.f_bavail) {
blocks_percent_used = (blocks_used * 100ULL
+ (blocks_used + s.f_bavail)/2
) / (blocks_used + s.f_bavail);
}
/* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
if (!strcmp(device, "rootfs"))
continue;
printf("\n%-20s" + 1, device);
printf(" %9lu %9lu %9lu %3u%% %s\n",
kscale(s.f_blocks, s.f_bsize),
kscale(s.f_blocks - s.f_bfree, s.f_bsize),
kscale(s.f_bavail, s.f_bsize),
blocks_percent_used, mount_point);
}
}
return 0;
}
- df命令简单移植
- Linux中的df命令【简单使用】
- df 命令
- df命令
- df 命令
- df 命令
- df 命令
- df 命令
- df命令
- df命令
- 参考df代码写的一个简单的df命令的源代码
- apue编程之参考df代码写的一个简单的df命令的源代码
- Linux 命令 --- df命令
- linux命令之df
- du df 命令
- df 命令的用法
- df 命令的使用
- linux命令:df
- linux fen bianlv
- MyEclipse开发JSF之创建工程浅析
- 重定向与跳转的区别.
- ASP.net生成导出WORD文档
- curl 模拟登录163邮箱
- df命令简单移植
- 基于TCP的程序设计典例
- 每天一点oracle(3)oracle部分常用命令
- C语言通过宏定位内存问题
- 单例类与半单例类
- ZOJ 3699 Dakar Rally 解题报告
- 信息安全问题是互联网的首要问题.
- 会话管理COOKIE
- 畅谈一下对于个人资料隐私的安全问题