📄 read_cfgd.c
字号:
/*************************************************************************** read_cfg.c - description ------------------- begin : Tue May 15 10:30:03 EET 2001 copyright : (C) 2001-2002 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#include "linux_mond.h"//Private functionschar *parseword(char *ptr2,cfgKeywordValue word_type);//Global variablesint process_stat = 0;extern unsigned char CRYPTIV[];extern char CRYPTKEY[];extern char PIDDIR[]; //defined in linux_mon.cextern struct conf_t server_config; //defined in linux_mond.cint server_read=0;int read_confd(char *filename){FILE *fp;char *ptr="";char *word="";char SECTION[53]="\0";char PARAMETER[53]="\0";char buf[128];int len=0;int process_total=0;//int tmp=0;fp=fopen(filename,"r");if(fp==NULL){slog(2,"FATAL Error: while opening config file: %s",filename);return -1;}while(fgets(buf,128,fp)!=NULL){word = rm_first_spaces(buf);if(is_comment(word)){if(*word=='<'){if((ptr = parseword(word,CFG_SECTION)) == NULL){ slog(2,"Error: NULL section value.\n"); free(ptr); fclose(fp); return -1;}strncpy(SECTION,ptr,50);if(ptr!=NULL) free(ptr);}//We have found some section//Lets see if we can use itif(process_stat==2 && *SECTION!='<') //are we realy in section{ if(strcmp(SECTION,"GENERAL")==0)//lets parse GENERAL section { word = rm_first_spaces(word); if((ptr = parseword(word,CFG_PARAMETER)) == NULL) { slog(2,"Error: NULL parameter value.\n"); slog(2,"Error: Check config file.\n"); free(ptr); fclose(fp); return(-1); } strncpy(PARAMETER,ptr,51); free(ptr); word = rm_first_spaces(word); if((ptr = parseword(word,CFG_VALUE)) == NULL) { slog(2,"Error: NULL parameter value.\n"); slog(2,"Error: Check config file.\n"); fclose(fp); return(-1); } //printf("VALUE: %s\n",ptr); //DEBUG if(strcmp(PARAMETER,"DBUSER")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>20) len=20; strncpy(server_config.dbuser,ptr,len); } if(strcmp(PARAMETER,"DBPASSWD")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>20) len=20; strncpy(server_config.dbpasswd,ptr,len); } if(strcmp(PARAMETER,"DBHOST")==0) { len=strlen(ptr); *(ptr+len)='\0'; if(len>20) len=20; strncpy(server_config.dbhost,ptr,len); } if(strcmp(PARAMETER,"PIDDIR")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>20) len=20; strncpy(PIDDIR,ptr,len); } else { strcpy(PIDDIR,"/var/run"); } } if(strcmp(PARAMETER,"CRYPTKEY")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>16) len=16; strncpy(CRYPTKEY,ptr,len); } else { slog(1,"Error no CRYPTKEY defined!"); strcpy(CRYPTKEY,"NO KEY PROVIDED"); } } if(strcmp(PARAMETER,"CRYPTIV")==0) { if(ptr!=NULL) { len=strlen(ptr); if(len>8) len=8; strncpy(CRYPTIV,ptr,len); } else { slog(1,"Error no CRYPTIV defined!"); strcpy(CRYPTIV,"NO IV"); } } }//Ends GENERAL section reading}//Section processing ends}//is_comment endsif(process_stat==1) //go to section processing process_stat++;}//while endsserver_read=0;fclose(fp);return process_total;}//main endschar *parseword(char *ptr2,cfgKeywordValue word_type){int len=0;char *word2; if(*(ptr2+len)=='<') { ptr2++; if (*(ptr2+len)=='/') { process_stat = 0; //section close } else { process_stat = 1; //section open } } for(;;) { if (*(ptr2+len)==' ' || *(ptr2+len)=='\t' || *(ptr2+len)=='\0' || *(ptr2+len)=='#' || (*(ptr2+len)=='=' && word_type==CFG_PARAMETER) || (*(ptr2+len)=='>' && word_type==CFG_SECTION) || (*(ptr2+len)=='=' && word_type==CFG_VALUE)) { break; } len++; }//for ends ptr2=rm_first_spaces(ptr2); //printf("PTR2: %s\n", ptr2); //DEBUG //printf("cpycpy LEN: %d\n", len); //DEBUG if((word2=malloc(len+1))==NULL){ slog(2,"Error: Out of memory.\n"); return NULL; } strncpy(word2,ptr2,len); *(word2+len)='\0'; //printf("WORD: %s\n", word2); //DEBUG //printf("LEN: %d\n", len); //DEBUG ptr2+=len; ptr2=rm_first_spaces(ptr2); switch(word_type){ case CFG_PARAMETER: ptr2=rm_first_spaces(ptr2); //printf("PARA: %s\n", ptr2); //DEBUG if(*ptr2=='='){ return word2; } break; case CFG_VALUE: ptr2=rm_first_spaces(ptr2); if(*ptr2!='\0' && *ptr2!='#'){ if(*ptr2=='='){ ptr2++; ptr2=rm_first_spaces(ptr2); len = strlen(ptr2); *(ptr2+len-1)='\0'; ptr2=rm_first_spaces(ptr2); free(word2); return ptr2; } } break; case CFG_SECTION: //ptr2+=len; ptr2=rm_first_spaces(ptr2); if(*ptr2=='>') { if (word2 != NULL) return word2; } break; default: free(word2); return NULL; } //return ptr2; free(word2); return NULL;}//parseword ends
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -