在内核中读写配置文件

在内核中读写配置文件,读文件我测试过,写文件还没。这段代码摘自网络,源出处已无可考。

#define FILE_MAX_SIZE 2048 /*maximum size of configure file*/
char filebuff[FILE_MAX_SIZE];
int filesize= 0;
struct file *filcp = NULL;
#define LEFT_BRACE '['
#define RIGHT_BRACE ']'
#define FILEC ("./sys.conf")


int newline(char c)
{
return ('\n' == c || '\r' == c )? 1 : 0;
}

int end_of_string(char c)
{
return '\0'==c? 1 : 0;
}

int left_barce(char c)
{
return LEFT_BRACE == c? 1 : 0;
}

int isspace(char c)
{
return ' ' == c ?1:0;
}

int isright_brace(char c )
{
return RIGHT_BRACE == c ? 1 : 0;
}


int load_ini_file(void)
{
int i=0;
ssize_t ret;
mm_segment_t old_fs;

ipline = 0;
filesize= 0;

memset(filebuff,0,sizeof(filebuff));

/*open readonly configure file */
filcp = filp_open(FILEC, O_RDONLY , 0644);
if(IS_ERR(filcp))
printk(KERN_EMERG"open error...\n");

/*store user data space */
old_fs = get_fs();

/*set kernel space in order to operate file in kernel*/
set_fs(get_ds());

filcp->f_op->llseek(filcp,0,0);

do
{
ret = filcp->f_op->read(filcp, &filebuff[i], 1, &filcp->f_pos);
if(ret > 0)
{
//printk(KERN_EMERG"%c",buff[i]);
}
else if(ret == 0)
{
//printk(KERN_EMERG"read nothing.............\n");
}
else
{
printk(KERN_EMERG"read error\n");
set_fs(old_fs);
filp_close(filcp,NULL);

return -1;
}

if(i >= FILE_MAX_SIZE -1)
{
printk(KERN_EMERG"read overcoving FILE_MAX_SIZE error\n");
set_fs(old_fs);
filp_close(filcp,NULL);
return -1;
}
}while(filebuff[i++] > 0);

set_fs(old_fs);

filebuff[i]='\0';
filesize = i;

filp_close(filcp,NULL);
return 1;
}
//

int parse_file(const char *section, const char *key, const char *buf,int *sec_s,int *sec_e,
int *key_s,int *key_e, int *value_s, int *value_e)
{
const char *p = buf;
int i=0;

if(buf == NULL || section == NULL || key == NULL)
return -1;

*sec_e = *sec_s = *key_e = *key_s = *value_s = *value_e = -1;

while( !end_of_string(p[i]) ) {
/*find the section*/
if( ( 0==i || newline(p[i-1]) ) && left_barce(p[i]) )
{
int section_start=i+1;

/*find the ']'*/
do {
i++;
} while( !isright_brace(p[i]) && !end_of_string(p[i]));

if( 0 == strncmp(p+section_start,section, i-section_start)) {
int newline_start=0;

i++;

/*Skip over space char after ']'*/
while(isspace(p[i])) {
i++;
}

/*find the section*/
*sec_s = section_start;
*sec_e = i;

while( ! (newline(p[i-1]) && left_barce(p[i]))
&& !end_of_string(p[i]) ) {
int j=0;
/*get a new line*/
newline_start = i;

while( !newline(p[i]) && !end_of_string(p[i]) ) {
i++;
}

/*now i is equal to end of the line*/
j = newline_start;

if(';' != p[j]) /*skip over comment*/
{
while(j < i && p[j]!='=') {
j++;
if('=' == p[j]) {
if(strncmp(key,p+newline_start,j-newline_start)==0)
{
/*find the key ok*/
*key_s = newline_start;
*key_e = j-1;

*value_s = j+1;
*value_e = i;

return 0;
}
}
}
}

i++;
}
}
}
else
{
i++;
}
}
return -1;
}

/**
*@brief read string in initialization file\n
* retrieves a string from the specified section in an initialization file
*@param section [in] name of the section containing the key name
*@param key [in] name of the key pairs to value
*@param value [in] pointer to the buffer that receives the retrieved string
*@param size [in] size of result's buffer
*@return0 : read success; \n -1 : read fail
*/
int read_profile_string( const char *section, const char *key,char *value,int size)
{
int sec_s,sec_e,key_s,key_e, value_s, value_e;

/*check parameters*/
if(section == NULL || key == NULL || value == NULL || size < 0 )
{
return -1;
}

if(parse_file(section,key,filebuff,&sec_s,&sec_e,&key_s,&key_e,&value_s,&value_e)==-1)
{
printk("parse_file func error\n");
return -1; /*not find the key*/
}
else
{
int cpcount = value_e -value_s;

if( size-1 < cpcount)
{
cpcount = size-1;
}

memset(value, 0, size);
memcpy(value,filebuff+value_s, cpcount );
value[cpcount] = '\0';

return 0;
}

return 0;
}


以下是测试用的代码

int MTU;
int ETH1;
char mac[6]
char tmp[50];

if(load_ini_file()!=1)
return -1;

memset(tmp,0,sizeof(tmp));
if(read_profile_string("GLOBA", "MTU", tmp, sizeof(tmp)) == -1)
{
printk( KERN_EMERG "Read MTU error.\n");
return -1;
}
MTU=atoi(name);

memset(tmp,0,sizeof(tmp));
if(read_profile_string("GLOBA", "Eth1", tmp, sizeof(tmp)) == -1)
{
printk( KERN_EMERG "Read Eth1 error.\n");
return -1;
}
ETH1=iptoint(tmp);

memset(tmp,0,sizeof(tmp));
if(read_profile_string(section, "mac", tmp, sizeof(tmp)) == -1)
{
printk( KERN_EMERG "Read mac error.\n");
break;
}
str2hex(tmp,mac,12);



调用这些测试代码需要以下几个转换方法


int atoi(const char *s)
{
int i, n;

n = 0;
for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10*n + s[i] - '0';

return (n);
}

char* strrev(char* szT)
{
int i,j,k,t;
char ch;
if ( !szT ) // 处理传入的空串.
return "";
i = strlen(szT);
t = !(i%2)? 1 : 0; // 检查串长度.
for(j = i-1 , k = 0 ; j > (i/2 -t) ; j-- )
{
ch = szT[j];
szT[j] = szT[k];
szT[k++] = ch;
}
return szT;
}

char* itoa(int value, char* str, int radix)
{
int rem = 0;
int pos = 0;
char ch = '!' ;
do
{
rem = value % radix ;
value /= radix;
if ( 16 == radix )
{
if( rem >= 10 && rem <= 15 )
{
switch( rem )
{
case 10:
ch = 'a' ;
break;
case 11:
ch ='b' ;
break;
case 12:
ch = 'c' ;
break;
case 13:
ch ='d' ;
break;
case 14:
ch = 'e' ;
break;
case 15:
ch ='f' ;
break;
}
}
}
if( '!' == ch )
{
str[pos++] = (char) ( rem + 0x30 );
}
else
{
str[pos++] = ch ;
}
}while( value != 0 );
str[pos] = '\0' ;
return strrev(str);
}

// 将16进制的字符串转换成数组
int str2hex(const char* pstrin,unsigned char* pstrout,int len)
{
int i = 0;
if(len & 1)
return -1;

for(; i < len; i++)
{
if(pstrin[i] >= '0' && pstrin[i] <= '9')
{
if(i & 1)
pstrout[i>>1] |= ((pstrin[i] - '0') & 0x0f);
else
pstrout[i>>1] = (pstrin[i] - '0') << 4;
}
else if(pstrin[i] >= 'A' && pstrin[i] <= 'F')
{
if(i & 1)
pstrout[i>>1] |= ((pstrin[i] - 0x37) & 0x0f);
else
pstrout[i>>1] = (pstrin[i] - 0x37) << 4;
}
else if(pstrin[i] >= 'a' && pstrin[i] <= 'f')
{
if(i & 1)
pstrout[i>>1] |= ((pstrin[i] - 0x57) & 0x0f);
else
pstrout[i>>1] = (pstrin[i] - 0x57) << 4;
}
else
return -1;

}
return 0;
}

// 将字符串格式类似于“192.168.0.1”的ip地址转换成整形数
int iptoint(const char *ip)
{
const char *ptmp1 = ip;
unsigned char actmp[4] ={0};
int iVal = 0,j=0,iIP =0,c = 0;
int m= 0,k =0;

memset(actmp,0,sizeof(actmp));
while( *ptmp1 != '\0')
{
if(j >= 4)
return -1;

if((*ptmp1 < '0' || *ptmp1 > '9') && (*ptmp1!=0x2e))
return -1;

actmp[j++] = *ptmp1-0x30;

if(*ptmp1++ == '.' || (*ptmp1 == '\0' && c == 3))
{
if(*ptmp1 != '\0')
{
for( m= 1;m < j ; m++)
{
iVal = iVal*10 + actmp[m-1];
}
}
else
{
for( k = 0;k <j ; k++)
{
iVal = iVal*10 + actmp[k];
}
}

if(iVal > 255)
return -1;

((unsigned char*)&iIP)[c] = iVal;

memset(actmp,0,sizeof(actmp));
j = 0;
iVal =0;
c++;
}

}

if(c== 4 && j == 0 && (actmp[0] ^ 0x80))
return iIP;
else
return -1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值