Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->网络应用 ->正文

高手进阶 学会做嵌入式Linux操作系统

来源:赛迪网技术社区  作者:Webmaster 时间:2007-06-03 点击: [收藏] [投稿]

#include

#include

#include

#include

#include

#include

#define NETCFGDIR "/etc/sysconfig/network-scripts/"

struct _ifcfg{

char device[8];

char bootproto[8];

char br[16];

char netmask[16];

char ip[16];

char network[16];

int onboot;

};

void ParseKey(struct _ifcfg *ifcfg,char *key,char *value)

{

if(!strcmp(key,"DEVICE"))

{

strcpy(ifcfg->device,value);

}

else if(!strcmp(key,"BOOTPROTO"))

{

strcpy(ifcfg->bootproto,value);

}

else if(!strcmp(key,"BROADCAST"))

{

strcpy(ifcfg->br,value);

}

else if(!strcmp(key,"IPADDR"))

{

strcpy(ifcfg->ip,value);

}

else if(!strcmp(key,"NETMASK"))

{

strcpy(ifcfg->netmask,value);

}

else if(!strcmp(key,"NETWORK"))

{

strcpy(ifcfg->network,value);

}

else if(!strcmp(key,"ONBOOT"))

{

ifcfg->onboot=(strcmp(value,"yes") ? 0 : 1);

}

}

int main(int argc,char **argv)

{

FILE *fp;

DIR *dir;

int i;

char filename[50],buf[80];

char *index,*key,*value,*p;

struct _ifcfg *ifcfg;

struct dirent *ptr;

ifcfg=(struct _ifcfg *)malloc(sizeof(struct _ifcfg));

memset(ifcfg,0,sizeof(struct _ifcfg));

dir=opendir(NETCFGDIR); /*打开脚本目录*/

while((ptr=readdir(dir))!=NULL) /*读取所有文件*/

{

if(strncmp(ptr->d_name,"ifcfg-eth",9)) /*这里,只启动了以太网卡^o^*/

{

continue;

}

memset(filename,0,sizeof(filename));

sprintf(filename,"%s%s",NETCFGDIR,ptr->d_name);

if((fp=fopen(filename,"r"))==NULL) /*打开配置文件*/

{

continue;

}

while(!feof(fp))

{

memset(buf,0,sizeof(buf));

if(fgets(buf,80,fp)!=NULL) /*逐行读取分析*/

{

p=strchr(buf,'n');

if(p)

{

*p='';

}

index=buf;

key=strtok(index,"="); /*读取配置变量*/

value=strtok(NULL,"="); /*读取变量的值*/

ParseKey(ifcfg,key,value); /*分析之,存入结构ifcfg中*/

}

}

/*构建相应的命令*/

memset(buf,0,80);

strcpy(buf,"/sbin/ifconfig");

if(ifcfg->onboot)

{

sprintf(buf,"%s %s %s netmask %s broadcast %s",

buf,

ifcfg->device,

ifcfg->ip,

ifcfg->netmask,

ifcfg->br);

/*直接调用system来实现,当然也可以自己通过ioctl来设置,相应源码,我以前在c/c++版发过*/

system(buf);

}

}

free(ife);

return 0;

}

platinum 2005-11-1 02:52

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:修正Linux下丑陋的FireFox表单控件问题   下一篇:实用技巧两则 主机名修改与Cat命令退出

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号