linux获取磁盘信息

 
 
 
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值