被操作文件内容
root$ cat /etc/kingcan/lan.cfg
LAN1_IPADDR='192.168.1.1'
LAN1_NETMASK='255.255.255.0'
LAN1_NAT_STATUS='ENABLE'
LAN2_IPADDR='192.168.2.1'
LAN2_NETMASK='255.255.255.0'
LAN2_NAT_STATUS='ENABLE'
LAN3_IPADDR='192.168.3.1'
LAN3_NETMASK='255.255.255.0'
LAN3_NAT_STATUS='ENABLE'
LAN4_IPADDR='192.168.4.1'
LAN4_NETMASK='255.255.255.0'
LAN4_NAT_STATUS='ENABLE'
要求取出ip和子网掩码
代码
void get_net_from_mf(char *config_file, //config file
struct aconf_net_info *netinfo, //subnet info
int *num) //subnet number
{
FILE * pf;
int f_len;
char buf[256];
char *str,*find_str,*name,*value;
if(!(pf=fopen(config_file,"r")))
{
printf("config file %s open failed!\n",config_file);
return;
}
fseek(pf,0,SEEK_END);
f_len=ftell(pf);
fseek(pf,0,SEEK_SET);
while(1)
{
str=fgets(buf,255,pf);
printf("%s\n",str);
if(!str)
break;
if(!(find_str=strchr(str,'=')))
continue;
printf("find_str=%s\n",find_str);
printf("find_str=%x\n",find_str);
*find_str=0;
printf("find_str=%s\n",find_str);
printf("find_str=%x\n",find_str);
printf("%s\n",str);
name=str;
printf("name=%s\n",name);
str=find_str+1;
printf("%s\n",str);
printf("str=%x\n",str);
if(!(find_str=strchr(str,'\'')))
continue;
printf("find_str=%s\n",find_str);
*find_str=0;
printf("find_str=%s\n",find_str);
printf("find_str=%x\n",find_str);
str=value=find_str+1;
printf("value=%s\n",value);
if(!(find_str=strchr(str,'\'')))
continue;
printf("find_str=%s\n",find_str);
printf("find_str=%x\n",find_str);
*find_str=0;
printf("find_str=%s\n",find_str);
printf("find_str=%x\n",find_str);
if(!strncmp(name,"LAN1_IPADDR",strlen("LAN1_IPADDR")))
netinfo[*num].ip=(int)inet_addr(value);
else if(!strncmp(name,"LAN1_NETMASK",strlen("LAN1_NETMASK")))
netinfo[(*num)++].mask=inet_addr(value);
if(!strncmp(name,"LAN2_IPADDR",strlen("LAN2_IPADDR")))
netinfo[*num].ip=(int)inet_addr(value);
else if(!strncmp(name,"LAN2_NETMASK",strlen("LAN2_NETMASK")))
netinfo[(*num)++].mask=inet_addr(value);
if(!strncmp(name,"LAN3_IPADDR",strlen("LAN3_IPADDR")))
netinfo[*num].ip=(int)inet_addr(value);
else if(!strncmp(name,"LAN3_NETMASK",strlen("LAN3_NETMASK")))
netinfo[(*num)++].mask=inet_addr(value);
if(!strncmp(name,"LAN4_IPADDR",strlen("LAN4_IPADDR")))
netinfo[*num].ip=(int)inet_addr(value);
else if(!strncmp(name,"LAN4_NETMASK",strlen("LAN4_NETMASK")))
netinfo[(*num)++].mask=inet_addr(value);
};
fclose(pf);
return;
}
运行结果
LAN1_IPADDR='192.168.1.1'
find_str=='192.168.1.1'
find_str=bffffabf
find_str=
find_str=bffffabf
LAN1_IPADDR
name=LAN1_IPADDR
'192.168.1.1'
str=bffffac0
find_str='192.168.1.1'
find_str=
find_str=bffffac0
value=192.168.1.1'
find_str='
find_str=bffffacc
find_str=
find_str=bffffacc
LAN1_NETMASK='255.255.255.0'
find_str=='255.255.255.0'
find_str=bffffac0
find_str=
find_str=bffffac0
LAN1_NETMASK
name=LAN1_NETMASK
'255.255.255.0'
str=bffffac1
find_str='255.255.255.0'
find_str=
find_str=bffffac1
value=255.255.255.0'
find_str='
find_str=bffffacf
find_str=
find_str=bffffacf
LAN1_NAT_STATUS='ENABLE'
find_str=='ENABLE'
find_str=bffffac3
find_str=
find_str=bffffac3
LAN1_NAT_STATUS
name=LAN1_NAT_STATUS
'ENABLE'
str=bffffac4
find_str='ENABLE'
find_str=
find_str=bffffac4
value=ENABLE'
find_str='
find_str=bffffacb
find_str=
find_str=bffffacb
LAN2_IPADDR='192.168.2.1'
find_str=='192.168.2.1'
find_str=bffffabf
find_str=
find_str=bffffabf
LAN2_IPADDR
name=LAN2_IPADDR
'192.168.2.1'
str=bffffac0
find_str='192.168.2.1'
find_str=
find_str=bffffac0
value=192.168.2.1'
find_str='
find_str=bffffacc
find_str=
find_str=bffffacc
LAN2_NETMASK='255.255.255.0'
find_str=='255.255.255.0'
find_str=bffffac0
find_str=
find_str=bffffac0
LAN2_NETMASK
name=LAN2_NETMASK
'255.255.255.0'
str=bffffac1
find_str='255.255.255.0'
find_str=
find_str=bffffac1
value=255.255.255.0'
find_str='
find_str=bffffacf
find_str=
find_str=bffffacf
LAN2_NAT_STATUS='ENABLE'
find_str=='ENABLE'
find_str=bffffac3
find_str=
find_str=bffffac3
LAN2_NAT_STATUS
name=LAN2_NAT_STATUS
'ENABLE'
str=bffffac4
find_str='ENABLE'
find_str=
find_str=bffffac4
value=ENABLE'
find_str='
find_str=bffffacb
find_str=
find_str=bffffacb
LAN3_IPADDR='192.168.3.1'
find_str=='192.168.3.1'
find_str=bffffabf
find_str=
find_str=bffffabf
LAN3_IPADDR
name=LAN3_IPADDR
'192.168.3.1'
str=bffffac0
find_str='192.168.3.1'
find_str=
find_str=bffffac0
value=192.168.3.1'
find_str='
find_str=bffffacc
find_str=
find_str=bffffacc
LAN3_NETMASK='255.255.255.0'
find_str=='255.255.255.0'
find_str=bffffac0
find_str=
find_str=bffffac0
LAN3_NETMASK
name=LAN3_NETMASK
'255.255.255.0'
str=bffffac1
find_str='255.255.255.0'
find_str=
find_str=bffffac1
value=255.255.255.0'
find_str='
find_str=bffffacf
find_str=
find_str=bffffacf
LAN3_NAT_STATUS='ENABLE'
find_str=='ENABLE'
find_str=bffffac3
find_str=
find_str=bffffac3
LAN3_NAT_STATUS
name=LAN3_NAT_STATUS
'ENABLE'
str=bffffac4
find_str='ENABLE'
find_str=
find_str=bffffac4
value=ENABLE'
find_str='
find_str=bffffacb
find_str=
find_str=bffffacb
LAN4_IPADDR='192.168.4.1'
find_str=='192.168.4.1'
find_str=bffffabf
find_str=
find_str=bffffabf
LAN4_IPADDR
name=LAN4_IPADDR
'192.168.4.1'
str=bffffac0
find_str='192.168.4.1'
find_str=
find_str=bffffac0
value=192.168.4.1'
find_str='
find_str=bffffacc
find_str=
find_str=bffffacc
LAN4_NETMASK='255.255.255.0'
find_str=='255.255.255.0'
find_str=bffffac0
find_str=
find_str=bffffac0
LAN4_NETMASK
name=LAN4_NETMASK
'255.255.255.0'
str=bffffac1
find_str='255.255.255.0'
find_str=
find_str=bffffac1
value=255.255.255.0'
find_str='
find_str=bffffacf
find_str=
find_str=bffffacf
LAN4_NAT_STATUS='ENABLE'
find_str=='ENABLE'
find_str=bffffac3
find_str=
find_str=bffffac3
LAN4_NAT_STATUS
name=LAN4_NAT_STATUS
'ENABLE'
str=bffffac4
find_str='ENABLE'
find_str=
find_str=bffffac4
value=ENABLE'
find_str='
find_str=bffffacb
find_str=
find_str=bffffacb
(null)