linux获取磁盘信息

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:19   1664   0
linux获取磁盘信息 (2009-02-19 13:44)
分类: C和C++


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <utmp.h>
#include <sys/vfs.h>
#include <mntent.h>
static const char *ignore_fs[] ={
"none", "proc", "sysfs", "devpts", "usbfs", "usbdevfs", NULL};

typedef struct devInfo_t devInfo_t;
struct devInfo_t
{
char source[256];
unsigned long totalSize;
unsigned long availSize;
};

int getDevMnt(char *target, devInfo_t *pu);

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

getDevMnt(argv[1], &pu);

return 0;
}

static unsigned long kScale(unsigned long b, unsigned long bs)
{
return (b * (unsigned long long) bs + 1024/2) / 1024;
}

int getDevMnt(char *target, devInfo_t *pu)
{
int flag = 0;
char **p;
char *table ="/proc/mounts"; //这个可以根据自己的需要修改
char buf[BUFSIZ];
memset (buf, 0, BUFSIZ);
struct mntent *mnt;
struct statvfs statbuf;
FILE*fp;

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))
{
flag =1;
break;
}
}
if(flag)
{
flag =0;
continue;
}
if(0 > statvfs(mnt->mnt_dir, &statbuf))
{
return -1;
}
else
{
if(!strcmp(mnt->mnt_dir, target))
{
sprintf(pu->source, "%s", mnt->mnt_fsname);
pu->availSize = kScale(statbuf.f_bavail, statbuf.f_bsize);
pu->totalSize = kScale(statbuf.f_blocks, statbuf.f_bsize);
printf("getDevMnt(). %s---%9lu---%9lu\n",
mnt->mnt_fsname, pu->totalSize, pu->availSize);
}
}
}
endmntent (fp);
return 0;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP