下面的代码可以得到硬盘使用情况:
#include
#include
#include
#include
#include
#include
#include
#include
static const char *ignore_fs[] = {
"none", "proc", "sysfs", "devpts", "usbfs", "usbdevfs", NULL };
int main (int argc, char **argv)
{
struct mntent *mnt;
struct statfs fsu;
FILE *fp;
char *table = MOUNTED;
long total, used, available, available_to_root;
double pct;
char **p;
int scale, flag = 0;
char buf[BUFSIZ];
memset (buf, 0, BUFSIZ);
fp = setmntent (table, "r");
if (fp == NULL)
{
return -1;
}
while ((mnt = getmntent (fp)))
{
for (p = (char **)ignore_fs; *p; p++)
{
if (strcmp (mnt->mnt_fsname, *p) == 0)
{
flag = 1;
break;;
}
}
if (flag)
{
flag = 0;
continue;
}
if (statfs (mnt->mnt_dir, &fsu) < 0)
{
return -1;
}
else
{
total = fsu.f_blocks;
available = fsu.f_bavail;
available_to_root = fsu.f_bfree;
used = total - available_to_root;
scale = fsu.f_bsize / 1024;
pct = 0;
if (total != 0 )
pct = (used * 100) / (used + available) + ((used *100) % (used
+ available) != 0);
fprintf (stdout, "%-15s %15s %10ld %10ld %10ld %9.0f%% %-s\n",
mnt->mnt_fsname, mnt->mnt_type, total*scale, used*scale,
available*scale, pct, mnt->mnt_dir);
}
}
endmntent (fp);
return 0;
}
[zhanghua@localhost ZA]$ gcc fsinfo.c -o fsinfo
[zhanghua@localhost ZA]$ ./fsinfo
/dev/sda2 ext3 5771500 5166820 311496 95% /
/dev/sda1 ext3 101089 9424 86446 10% /boot
阅读(1967) | 评论(0) | 转发(0) |