用C读取用户配置文件.conf的两个示例
最近研究如何在linux上写个简单易用的802.1x client ,所以研究了一些c代码
发现这两个代码蛮有用的,以后如果写要读文件的程序可以参考一下了
一
static void setConfig(void)
{
FILE *fp;
char buf[4096];
char *p;
int i,len;
static char name[32];
static char password[32];
static char nic[32];
static char fakeAddress[32];
int intelligentReconnect=-1;
int echoInterval=-1;
int authenticationMode=-1;
//the check and anylysis against Dot1xClient.conf *don't* work perfectly.
//this may be improved in the later version.
if( (fp=fopen("Dot1xClient.conf","r"))==NULL )
err_quit("cannot open file Dot1xClient.conf ! check it.\n");
while(fgets(buf,sizeof(buf),fp)!=NULL)//fgets遇到换行或EOF会结束
{
if( (buf[0]=='#') || (buf[0]=='\n') )
continue;//继续下一循环
len=strlen(buf);
if(buf[len-1]=='\n')
buf[len-1]='\0';//in order to form a string
if( ( (p=strchr(buf,'=')) == NULL) || (p==buf) )//if not find =
continue;
//the code above doesn't detect ALL the errors!! it should be improved in future.
*p='\0';//break the string into 2 parts.
p++;//p ponit to the value now
for(i=0; i buf[i]=tolower(buf[i]);
if(strcmp(buf,"name")==0)
{ strncpy(name,p,sizeof(name)-1); //char *strncpy(char *dest, const char *src, size_t n);
name[sizeof(name)-1]=0;
m_name=name;
}
else if(strcmp(buf,"password")==0)
{
strncpy(password,p,sizeof(password)-1); password[sizeof(password)-1]=0;
m_password=password;
}
else if(strcmp(buf,"authenticationmode")==0)
{ authenticationMode=atoi(p);
m_authenticationMode=authenticationMode; }
else if(strcmp(buf,"nic")==0)
{
for(i=0; i strncpy(nic,p,sizeof(nic)-1); nic[sizeof(nic)-1]=0; m_nic=nic;
}
else if(strcmp(buf,"echointerval")==0)
{ echoInterval=atoi(p); m_echoInterval=echoInterval; }
else if(strcmp(buf,"intelligentreconnect")==0)
{ intelligentReconnect=atoi(p); m_intelligentReconnect=intelligentReconnect; }
else if(strcmp(buf,"fakeaddress")==0)
{
strncpy(fakeAddress,p,sizeof(fakeAddress)-1);
fakeAddress[sizeof(fakeAddress)-1]=0;
if( inet_pton(AF_INET,fakeAddress,m_ip)<=0 )
err_msg("invalid fakeAddress found in Dot1xClient.conf, ignored...\n");
else m_fakeAddress=fakeAddress;
}
else continue;
}
if(ferror(fp)) err_quit("cannot read Dot1xClient.conf ! check it.\n");
fclose(fp);
if((m_name==NULL)||(m_name[0]==0)) err_quit("invalid name found in Dot1xClient.conf!\n");
if((m_password==NULL)||(m_password[0]==0)) err_quit("invalid password found in Dot1xClient.conf!\n");
if((m_authenticationMode<0)||(m_authenticationMode>1))
err_quit("invalid authenticationMode found in Dot1xClient.conf!\n");
if( (m_nic==NULL) || (strcmp(m_nic,"")==0) || (strcmp(m_nic,"any")==0) )
err_quit("invalid nic found in Dot1xClient.conf!\n");
if((m_echoInterval<0)||(m_echoInterval>100))
err_quit("invalid echo interval found in Dot1xClient.conf!\n" );
if((m_intelligentReconnect<0)||(m_intelligentReconnect>1))
err_quit("invalid intelligentReconnect found in Dot1xClient.conf!\n");
/*printf("m_name=%s\n",m_name);
printf("m_password=%s\n",m_password);
printf("m_nic=%s\n",m_nic);
printf("m_authenticationMode=%d\n",m_authenticationMode);
printf("m_echoInterval=%d\n",m_echoInterval);
printf("m_intelligentReconnect=%d\n",m_intelligentReconnect);//NOT supported now!!
printf("m_fakeAddress=%s\n",m_fakeAddress); */
//just set them to zero since they don't seem to be important.
memset(m_netgate,0,sizeof(m_netgate)); memset(m_dns1,0,sizeof(m_dns1));
}
二
void checkconfig()
{
char line[100];
printf("checking user config...\n");
while(fgets(line,100,file))
{
line[strlen(line)-1]='\0';//add a flag of string end
if(!memcmp("username",line,strlen("username")))//if find username
{
memcpy(usrData.username,(line+sizeof("username")),sizeof(usrData.username));
printf("%s\n",usrData.username);
continue;
}
if(!memcmp("password",line,strlen("password")))
{
memcpy(usrData.password,(line+sizeof("password")),sizeof(usrData.password));
printf("********\n");
continue;
}
if(!memcmp("Ip",line,strlen("Ip")))
{
memcpy(usrData.Ip,(unsigned char *)(line+sizeof("Ip")),sizeof(usrData.Ip));
sprintf(strIP,"%d.%d.%d.%d",usrData.Ip[0],usrData.Ip[1],usrData.Ip[2],usrData.Ip[3]);//先格式化一下字符窜
printf("%s\n",strIP);
continue;
}
if(!memcmp("Mac",line,strlen("Mac")))
{
memcpy(usrData.Mac,(unsigned char *)(line+sizeof("Mac")),sizeof(usrData.Mac));
printf("%02x-%02x-%02x-%02x-%02x-%02x\n",usrData.Mac[0],usrData.Mac[1],
usrData.Mac[2],usrData.Mac[3],usrData.Mac[4],usrData.Mac[5]);
continue;
}
if(!memcmp("nic",line,strlen("nic")))
{
memcpy(usrData.nic,(line+sizeof("nic")),sizeof(usrData.nic));
printf("%s\n",usrData.nic);
break;
}
}
fclose(file);
}
void writeconfig()
{
file=fopen("dot1xClient.conf","w");
fputs("username=",file);
fputs(usrData.username,file);
fputs("\n",file);
fputs("password=",file);
fputs(usrData.password,file);
fputs("\n",file);
fputs("Ip=",file);
fputc(usrData.Ip[0],file);
fputc(usrData.Ip[1],file);
fputc(usrData.Ip[2],file);
fputc(usrData.Ip[3],file);
fputs("\n",file);
fputs("Mac=",file);
fputc(usrData.Mac[0],file);
fputc(usrData.Mac[1],file);
fputc(usrData.Mac[2],file);
fputc(usrData.Mac[3],file);
fputc(usrData.Mac[4],file);
fputc(usrData.Mac[5],file);
fputs("\n",file);
fputs("nic=",file);
fputs(usrData.nic,file);
fputs("\n",file);
fclose(file);
}
阅读(2658) | 评论(0) | 转发(0) |