对wifi的配置文件wpa.conf的ssid和password进行修改

#include 
#include 
#include 
#include 
#include 
 
#define STR_NETWORKCFG_FILE "/etc/wpa.conf"
 
struct info_t{
        char name[20];
        char data[60];
};

struct WiFi_INFO_S
{
    char wifissid[20];
    char wifipsk[60];
};
struct WiFi_INFO_S wifi_info;

int wifijiexi(char *data)
{ 	char *lastname="\"";
	// const char *data="WIFI:T:WPA;S:stredsdsam;P:123456sssds789;;";
	
   	 static char g_qrcode_result[512];
   	 strncpy(g_qrcode_result,data,sizeof(g_qrcode_result));
    	char delims[]=":;";
    	char *result=NULL;
	char *result1=NULL;
	char *result2=NULL;
//WIFI
    	result=strtok(g_qrcode_result,delims);
	if(result)
	printf("r1:%s\n",result);
//T
	result = strtok( NULL, delims );
	if(result)
	printf("r2:%s\n",result); 
//WPA
	result = strtok( NULL, delims ); 
	if(result)
	printf("r3:%s\n",result);
//S
	result = strtok( NULL, delims ); 
	if(result)
	printf("r4:%s\n",result);
//stream
	result1 = strtok( NULL, delims ); 
	if(result1)
	printf("r5:%s\n",result1);
char *name1 = (char *) malloc(strlen(lastname) + strlen(result1));
	sprintf(name1, "%s%s%s", lastname,result1,lastname);
	printf("name1:%s\n",name1);
	strcpy(wifi_info.wifissid,name1);
//P
	result = strtok( NULL, delims ); 
	if(result)
	printf("r6:%s\n",result);
//123456
	result2 = strtok( NULL, delims ); 
	if(result2)
	printf("r7:%s\n",result2);
	char *name = (char *) malloc(strlen(lastname) + strlen(result2));
	sprintf(name, "%s%s%s", lastname,result2,lastname);
	printf("name:%s\n",name);
	
	//strcpy(wifi_info.wifissid,name1);
    	strcpy(wifi_info.wifipsk,name);
	printf("%s\n",wifi_info.wifissid);
	printf("%s\n",wifi_info.wifipsk);
	return 0;
}



int GetFile_Info(char *name,char *result)
{
        char linebuffer[100] = {0};
        char buffer1[100] = {0};
        char buffer2[100] = {0};
        uint8_t flag = 0;
        int buffer_size=0;
        FILE *fp = fopen(STR_NETWORKCFG_FILE, "r");
        if(fp == NULL)
        {
                printf("open error");
                return 1;
        }
        while(1)
        {
                char *ret = fgets(linebuffer, 100, fp);
                if(ret == NULL)
                {
                        break;
                }
                sscanf(linebuffer, "%[^=]=%[^=]", buffer1,buffer2);
                printf("%s",linebuffer);
		printf("buffer1:%s\n",buffer1);
		printf("buffer2:%s\n",buffer2);

                printf("\n**\n");
                if(!strcmp(name, buffer1))
                {
                        buffer_size=strlen(buffer2);
                        memcpy(result,buffer2,buffer_size);
                        result[buffer_size-1]='\0';//remove the \n
                        flag = 1;
                        break;
                }
                memset(buffer1,0,sizeof(buffer1));
		 memset(buffer2,0,sizeof(buffer2));
                memset(linebuffer,0,sizeof(linebuffer));
        }
        fclose(fp);
        if(flag != 1)
                return 1;
	
 
        return 0;
}


 
/*获取所有文件信息,存入结构体中,遇到需修改的字段,修改后存入结构体,后再全部重新写入保存*/
int ChangeFile_Info(char *name,char *writedata)
{
        int i;
        uint8_t flag = 0,cnt = 0;
        struct info_t info[9];
        char linebuffer[256] = {0};
        char buffer[80];
        FILE *fp = fopen(STR_NETWORKCFG_FILE, "r");
        if(fp == NULL)
        {
                printf("open error");
                return 1;
        }
        for(i = 0 ; i < 9 ; i++)
        {
                char *ret = fgets(linebuffer, 256, fp);
                if(ret == NULL)
                {
                        break;
                }
                sscanf(linebuffer, "%[^=]=%[^=]", info[i].name,info[i].data);
		//printf("%s",linebuffer);
		printf("%d %s %s\n",i,info[i].name,info[i].data);
 	if(!strcmp(info[i].name, name))
                {
                        memcpy(info[i].data,writedata,strlen(writedata));
                        info[i].data[strlen(writedata)] = '\n';
                        info[i].data[strlen(writedata)+1] = '\0';
                        flag = 1;
                }
                memset(linebuffer,0,sizeof(linebuffer));
                cnt++;
		
        }
        fclose(fp);
        if(flag)
        {
                fp = fopen(STR_NETWORKCFG_FILE,"w");
                if(fp == NULL)
                {
                        return 1;
                }
                for(i = 0 ; i < cnt ; i++)
                {
                        memset(buffer,0,sizeof(buffer));
                        sprintf(buffer,"%s=%s",info[i].name,info[i].data);
                        fprintf(fp,"%s",buffer);
                }
                fclose(fp);
        }
        else
                return 1;
	system("echo '}'>> /etc/wpa.conf");
        return 0;
}
 
int main()
{
	 char *zbar_data="WIFI:T:WPA;S:ssssss;P:dd1adddsdj;;";
	wifijiexi(zbar_data);


        char result[100] = {0};
        char *writedata = wifi_info.wifissid;
		char *writedata1 = wifi_info.wifipsk;
		// char *writedata = "\"llllllllm\"";
	//char *writedata1 = "\"68989896\"";
       // GetFile_Info("abc",result);
        //printf("result:%s\n",result);
        ChangeFile_Info("ssid",writedata);
        //GetFile_Info("ssid",result);
        //printf("result:%s\n",result);

	//GetFile_Info("psk",result);
       // printf("result:%s\n",result);
        ChangeFile_Info("psk",writedata1);
        //GetFile_Info("psk",result);
       // printf("result:%s\n",result);
        return 0;
}


你可能感兴趣的:(对wifi的配置文件wpa.conf的ssid和password进行修改)