secgateway/Platform/user/configm/config-server/ipconfig/parsefile.c

486 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "parsefile.h"
#include "rpc.h"
#if 0
/*
*从配置文件中读取相应的值
*输入参数1配置文件路径 2匹配标记 3输出存储空间
*并且排除了空行,“=”前后无内容,无“=”的情况
*/
void read_config(char *conf_path,char *conf_name,char *config_buff)
{
char config_linebuf[256];
char line_name[40];
char exchange_buf[256];
char *config_sign = "=";
char *leave_line;
FILE *f;
f = fopen(conf_path,"r");
if(f == NULL)
{
printf("OPEN CONFIG FALID/n");
return 0;
}
fseek(f,0,SEEK_SET);
while(fgets(config_linebuf,256,f) != NULL)
{
if(strlen(config_linebuf) < 3) //判断是否是空行
{
continue;
}
if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是/n的情况
{
memset(exchange_buf,0,sizeof(exchange_buf));
strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1);
memset(config_linebuf,0,sizeof(config_linebuf));
strcpy(config_linebuf,exchange_buf);
}
memset(line_name,0,sizeof(line_name));
leave_line = strstr(config_linebuf,config_sign);
if(leave_line == NULL) //去除无"="的情况
{
continue;
}
int leave_num = leave_line - config_linebuf;
strncpy(line_name,config_linebuf,leave_num);
if(strcmp(line_name,conf_name) ==0)
{
strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-leave_num-1);
break;
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
fclose(f);
}
/*
*添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加)
*
*输入参数1配置文件路径 2匹配标记 3替换或添加的内容
*
*/
void add_set_config(char *conf_path,char *conf_name,char *config_buff)
{
char config_linebuf[256];
char line_name[40];
char *config_sign = "=";
char *leave_line;
int alter_sign = 0;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID/n");
return 0;
}
fseek(f,0,SEEK_END);
long congig_lenth = ftell(f);
int configbuf_lenth = strlen(config_buff);
configbuf_lenth = configbuf_lenth + 5;
char sum_buf[congig_lenth+configbuf_lenth];
memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET);
while(fgets(config_linebuf,256,f) != NULL)
{
if(strlen(config_linebuf) < 3) //判断是否是空行
{
strcat(sum_buf,config_linebuf);
continue;
}
leave_line = NULL;
leave_line = strstr(config_linebuf,config_sign);
if(leave_line == NULL) //去除无"="的情况
{
strcat(sum_buf,config_linebuf);
continue;
}
int leave_num = leave_line - config_linebuf;
memset(line_name,0,sizeof(line_name));
strncpy(line_name,config_linebuf,leave_num);
if(strcmp(line_name,conf_name) ==0)
{
strcat(sum_buf,config_buff);
strcat(sum_buf,"/n");
alter_sign = 1;
}
else
{
strcat(sum_buf,config_linebuf);
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
if(alter_sign == 0)
{
strcat(sum_buf,config_buff);
strcat(sum_buf,"/n");
}
printf("---sum_buf---->%s<----------/n",sum_buf);
remove(conf_path);
fclose(f);
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
printf("OPEN CONFIG FALID/n");
return 2;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
}
/*
*删除配置文件内容
*
*输入参数1匹配标记
*
*/
void del_if_config(char *conf_name)
{
char *conf_path = "/etc/network/interface";
char config_linebuf[256];
char line_name[40];
char *config_sign = "=";
char *leave_line;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID/n");
return 0;
}
fseek(f,0,SEEK_END);
long congig_lenth = ftell(f);
char sum_buf[congig_lenth+2];
memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET);
while(fgets(config_linebuf,256,f) != NULL)
{
if(strlen(config_linebuf) < 3) //判断是否是空行
{
strcat(sum_buf,config_linebuf);
continue;
}
leave_line = NULL;
leave_line = strstr(config_linebuf,config_sign);
if(leave_line == NULL) //去除无"="的情况
{
strcat(sum_buf,config_linebuf);
continue;
}
int leave_num = leave_line - config_linebuf;
memset(line_name,0,sizeof(line_name));
strncpy(line_name,config_linebuf,leave_num);
if(strcmp(line_name,conf_name) !=0)
{
strcat(sum_buf,config_linebuf);
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
printf("---sum_buf---->%s<----------/n",sum_buf);
remove(conf_path);
fclose(f);
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
printf("OPEN CONFIG FALID/n");
return 2;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
}
#endif
/*
*添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加)
*
*输入参数1接口名 2匹配标记 3替换或添加的内容
*
*/
void set_if_config(char *if_name, char *conf_name, char *conf_buff)
{
char *conf_path = IFCONFIG_PATH;
char config_linebuf[IF_BUFF_LEN];
char static_name[IF_BUFF_LEN] = {0};
char iface_str[IF_BUFF_LEN] = {0};
char auto_str[IF_BUFF_LEN] = {0};
char *auto_line = NULL;
char *iface_line = NULL;
char *config_line = NULL;
boolean next_flag = FALSE;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
rpc_log_error("OPEN CONFIG FALID\n");
return;
}
fseek(f,0,SEEK_END);
long config_lenth = ftell(f);
int configbuf_lenth = strlen(conf_buff);
configbuf_lenth = configbuf_lenth + 5;
char sum_buf[config_lenth + configbuf_lenth];
memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET);
sprintf(auto_str, "auto %s", if_name);
sprintf(iface_str, "iface %s inet", if_name);
sprintf(static_name, "iface %s inet static\n", if_name);
memset(config_linebuf,0,sizeof(config_linebuf));
while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf,config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf,config_linebuf);
goto next_while;
}
/* 没有找到接口配置块,则继续循环 */
if(auto_line == NULL)
{
auto_line = strstr(config_linebuf, auto_str);
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 已经是下一个接口了*/
if(strstr(config_linebuf, "auto"))
{
if(iface_line == NULL)
{
strcat(sum_buf, static_name);
}
if(config_line == NULL)
{
strcat(sum_buf, conf_buff);
}
strcat(sum_buf, config_linebuf);
next_flag = TRUE;
goto next_while;
}
/* 找到接口IP配置方式 */
if(iface_line == NULL)
{
iface_line = strstr(config_linebuf, iface_str);
if(iface_line)
{
strcat(sum_buf, static_name);
goto next_while;
}
}
/* 找到配置行 */
if(config_line == NULL)
{
config_line = strstr(config_linebuf, conf_name);
if(config_line)
{
next_flag = TRUE;
strcat(sum_buf, conf_buff);
goto next_while;
}
}
strcat(sum_buf, config_linebuf);
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
if( next_flag == FALSE )
{
if(auto_line == NULL)
{
strcat(sum_buf, auto_str);
strcat(sum_buf, "\n");
}
if(iface_line == NULL)
{
strcat(sum_buf, static_name);
}
if(config_line == NULL)
{
strcat(sum_buf, conf_buff);
}
}
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
remove(conf_path);
fclose(f);
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
rpc_log_error("OPEN CONFIG FALID\n");
return;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
return;
}
/*
*删除配置文件内容
*
*输入参数1匹配标记
*
*/
void del_if_config(char *if_name, char *conf_buff)
{
char *conf_path = IFCONFIG_PATH;
char config_linebuf[IF_BUFF_LEN];
char auto_str[IF_BUFF_LEN] = {0};
boolean next_flag = FALSE;
char *auto_line = NULL;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID\n");
return;
}
fseek(f,0,SEEK_END);
long congig_lenth = ftell(f);
int configbuf_lenth = strlen(conf_buff);
configbuf_lenth = configbuf_lenth + 5;
char sum_buf[congig_lenth+configbuf_lenth];
memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET);
sprintf(auto_str, "auto %s", if_name);
while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf,config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf,config_linebuf);
goto next_while;
}
/* 没有找到接口配置块,则继续循环 */
if(auto_line == NULL)
{
auto_line = strstr(config_linebuf, auto_str);
strcat(sum_buf,config_linebuf);
goto next_while;
}
/* 已经是下一个接口了, 则表示无法找到*/
if(strstr(config_linebuf, "auto"))
{
strcat(sum_buf,config_linebuf);
next_flag = TRUE;
goto next_while;
}
/* 找到配置行 */
if(strstr(config_linebuf,conf_buff))
{
next_flag = TRUE;
}
else
{
strcat(sum_buf, config_linebuf);
}
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
printf("---sum_buf---->%s<----------/n",sum_buf);
remove(conf_path);
fclose(f);
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
printf("OPEN CONFIG FALID/n");
return;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
}