分享
 
 
 

一段可以在UNIX系统下读硬盘空间信息的代码(原创)

王朝system·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

#include<stdio.h>

#include<mntent.h>

#include<sys/vfs.h>

#include<error.h>

#include<sys/types.h>

#include<stdlib.h>

#include<string.h>

#define Gsize (1024.00*1024.00*1024.00)

#define Msize (1024.00*1024.00)

#ifndef EXT2_SUPER_MAGIC

#define EXT2_SUPER_MAGIC 0xef53

#endif

#define MOUNTED "/etc/mtab"

#ifndef ME_DUMMY

#define ME_DUMMY(Fs_name, Fs_type) (!strcmp (Fs_type, "autofs") /* for Irix 6.5 */ || !strcmp (Fs_type, "ignore"))

#endif

#undef STREQ

#define STREQ(a, b) (strcmp ((a), (b)) == 0)

#ifndef ME_REMOTE

/* A file system is `remote' if its Fs_name contains a `:'

or if (it is of type smbfs and its Fs_name starts with `//'). */

# define ME_REMOTE(Fs_name, Fs_type) (strchr ((Fs_name), ':') != 0 || ((Fs_name)[0] == '/' && (Fs_name)[1] == '/' && STREQ (Fs_type, "smbfs")))

#endif

struct mount_entry

{

char *me_devname; /* Device node pathname, including "/dev/". */

char *me_mountdir; /* Mount point directory pathname */

char *me_type; /* "nfs", "4.2", etc. */

dev_t me_dev; /* Device number of me_mountdir. */

unsigned int me_dummy : 1; /* Nonzero for dummy filesystems. */

unsigned int me_remote : 1; /* Nonzero for remote fileystems. */

unsigned int me_type_malloced :1; /* Nonzero if me_type was malloced. */

struct mount_entry *me_next;

};

void free_mount_entry_node(struct mount_entry *node)

{

if( node == NULL )

return ;

free(node->me_devname);

free(node->me_mountdir);

free(node->me_type);

free(node);

}

void free_mount_entry_list(struct mount_entry *head)

{

struct mount_entry * tmp = NULL;

while( head != NULL )

{

tmp = head;

head = head->me_next;

free_mount_entry_node(tmp);

}

}

char* xstrdup(char *str)

{

char *dupstr;

dupstr = (char*)malloc(sizeof(char)*(strlen(str) + 1 ));

strcpy(dupstr, str);

return dupstr;

}

static int xatoi (char *cp)

{

int val;

val = 0;

while (*cp)

{

if (*cp >= 'a' && *cp <= 'f')

val = val * 16 + *cp - 'a' + 10;

else if (*cp >= 'A' && *cp <= 'F')

val = val * 16 + *cp - 'A' + 10;

else if (*cp >= '0' && *cp <= '9')

val = val * 16 + *cp - '0';

else

break;

cp++;

}

return val;

}

struct mount_entry * get_mount_entry_list(void)

{

struct mntent *mnt;

char *table = MOUNTED;

FILE *fp;

char *devopt;

struct mount_entry *mount_list;

struct mount_entry *me;

struct mount_entry **mtail = &mount_list;

fp = setmntent (table, "r");

if (fp == NULL)

return NULL;

while ((mnt = getmntent (fp)))

{

me = (struct mount_entry*)malloc (sizeof(struct mount_entry));

me->me_devname = xstrdup (mnt->mnt_fsname);

me->me_mountdir = xstrdup (mnt->mnt_dir);

me->me_type = xstrdup (mnt->mnt_type);

me->me_type_malloced = 1;

me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);

me->me_remote = ME_REMOTE (me->me_devname, me->me_type);

devopt = (char*)strstr (mnt->mnt_opts, "dev=");

if (devopt)

{

if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))

me->me_dev = xatoi (devopt + 6);

else

me->me_dev = xatoi (devopt + 4);

}

else

me->me_dev = (dev_t) -1; /* Magic; means not known yet. */

/* Add to the linked list. */

*mtail = me;

mtail = &me->me_next;

}

if (endmntent (fp) == 0)

{

free_mount_entry_list(mount_list);

mount_list = NULL;

return NULL;

}

mtail = NULL;

return mount_list;

}

char* HDInfo()

{

long long blocks,bfree;

struct statfs fs;

struct mount_entry *head,*thead;

char *ret,*tret;

int count=0;

tret=ret=(char*)malloc(sizeof(char)*1024);

thead=head = get_mount_entry_list();

if( head == NULL )

{

fprintf(stderr, "get mount list error!\n");

return NULL;

}

while( head != NULL )

{

//we want real and local filesystem

if( head->me_devname != NULL

&& strcmp(head->me_devname, "none") != 0

&& head->me_dummy == 0

&& head->me_remote == 0

&& head->me_devname[0] == '/')

{

count=sprintf(tret,"%s\t%s\t%s\t%d\t%d\t%d",

head->me_devname,

head->me_mountdir,

head->me_type,

head->me_dummy,

head->me_remote,

head->me_type_malloced);

if(count==-1)return NULL;

tret+=count/sizeof(char);

if(statfs(head->me_mountdir,&fs)<0)

{

perror("statfs");

return NULL;

}

blocks=fs.f_blocks;

bfree=fs.f_bfree;

if(fs.f_type==EXT2_SUPER_MAGIC)

{

count=sprintf(tret,"\tTotal size is %fM",blocks*fs.f_bsize/Msize);

if(count==-1)return NULL;

tret+=count/sizeof(char);

count=sprintf(tret,"\tFree size is %fM",bfree*fs.f_bsize/Msize);

if(count==-1)return NULL;

tret+=count/sizeof(char);

}

count=sprintf(tret,"\r\n");

if(count==-1)return NULL;

tret+=count/sizeof(char);

}

head=head->me_next;

}

free_mount_entry_list(thead);

printf("%s",ret);

return ret;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有