高手进阶 学会做嵌入式Linux操作系统
#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
更多相关文章
|
推荐文章
精彩文章
|