From 10808df59581614c5d63eea19b7e786a21e49049 Mon Sep 17 00:00:00 2001 From: Jonathan Santis Date: Wed, 18 Sep 2024 13:18:29 +0200 Subject: [PATCH] modify config file, todo: fix over using fgets by storing temp_str in seperate strs --- config | 2 ++ config.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++---------- config.h | 2 +- main.c | 26 ++++++++++++-------------- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/config b/config index 99991a9..982271a 100644 --- a/config +++ b/config @@ -1,3 +1,5 @@ +[APP] +Intervall = 5 [Host] Hostname = https://5ccppi.org:4433 [Services] diff --git a/config.c b/config.c index 09daf37..921ddf0 100644 --- a/config.c +++ b/config.c @@ -3,7 +3,7 @@ #include #include -int loadConfig(char *str_file, char **str_entry,char **host,int size) +int loadConfig(char *str_file, char **str_entry,char **host,int *intervall,int size) { int i=0; FILE *f_config; @@ -16,19 +16,32 @@ int loadConfig(char *str_file, char **str_entry,char **host,int size) while(fgets(temp_str,256,f_config)!=NULL) { temp_str[strcspn(temp_str,"\n")]=0; + printf("first temp_str:%s\n",temp_str); if(strcmp(temp_str,"[Host]")==0) { - if(fgets(temp_str,256,f_config)!=NULL) + printf("temp_str host:%s\n",temp_str); + while(fgets(temp_str,256,f_config)!=NULL) { - char *ptr_host = strrchr(temp_str,'=')+1;//Without = - ptr_host[strcspn(ptr_host,"\n")]=0; - while(ptr_host[0]==' ') + printf("temp_str:%s\n",temp_str); + if(strchr(temp_str,'[')==NULL) { - ptr_host = ptr_host + 1; + printf("strchr\n"); + if(strstr(temp_str,"Hostname")!=NULL) + { + char *ptr_host = strrchr(temp_str,'=')+1;//Without = + ptr_host[strcspn(ptr_host,"\n")]=0; + while(ptr_host[0]==' ') + { + ptr_host = ptr_host + 1; + } + *host = strdup(ptr_host); + printf("my host is:%s\n",*host); + }else{ + fprintf(stderr,"keyword Hostname not found in config"); + } + }else{ + break; } - *host = strdup(ptr_host); - //strncpy(host,ptr_host,256); - printf("my host is:%s\n",*host); } } else if(strcmp(temp_str,"[Services]")==0) @@ -51,7 +64,30 @@ int loadConfig(char *str_file, char **str_entry,char **host,int size) else { break; - fclose(f_config); + } + } + } + else if(strcmp(temp_str,"[APP]")==0) + { + while(fgets(temp_str,256,f_config)!=NULL) + { + if(strchr(temp_str,'[')==NULL) + { + if(strstr(temp_str,"Intervall")!=NULL) + { + char *ptr_intervall = strrchr(temp_str,'=')+1;//Without = + ptr_intervall[strcspn(ptr_intervall,"p\n")]=0; + while(ptr_intervall[0]==' ') + { + ptr_intervall = ptr_intervall + 1; + } + *intervall = atoi(ptr_intervall); + printf("intervall:%d\n",*intervall); + }else{ + fprintf(stderr,"keyword Intervall not found in config"); + } + }else{ + break; } } } diff --git a/config.h b/config.h index f6db9d8..1fcb50a 100644 --- a/config.h +++ b/config.h @@ -1,2 +1,2 @@ -int loadConfig(char *file, char **str_entry,char **host,int size); +int loadConfig(char *file, char **str_entry,char **host,int *intervall,int size); diff --git a/main.c b/main.c index 7e6f32f..7dedbd4 100644 --- a/main.c +++ b/main.c @@ -36,13 +36,19 @@ int getPath(char *configPath,int size); //gotify_message("https://5ccppi.org:4433/message?token=","anothertitle","anothermessage"); * */ - int main(int argc,char *argv[]) { DBusConnection *connection=NULL; DBusError error; clock_t time; + char *str_entry[64]={'\0'}; + char *host=NULL; + int i=0; + int intervall=10; + Service services[64]={'\0'}; + char *ptr_name=NULL; + char path[256]; time = clock(); @@ -87,11 +93,8 @@ int main(int argc,char *argv[]) { if(argc==3 && strcmp(argv[2],"-d")!=0) { - char *str_entry[64]={'\0'}; - char *host=NULL; - int i=0; - - loadConfig(argv[2],str_entry,&host,64); + loadConfig(argv[2],str_entry,&host,&intervall,64); + printf("[main]intervall:%d\n",intervall); for(i=0;str_entry[i];i++) { printf("entry in config:%s\n",str_entry[i]); @@ -101,18 +104,13 @@ int main(int argc,char *argv[]) else if(argc==3 && strcmp(argv[2],"-d")==0) { printf("Running as a daemon\n"); - char *str_entry[64]={'\0'}; - int i=0; - Service services[64]={'\0'}; - char *ptr_name=NULL; - char path[256]; - char *host=NULL; getPath(path,256); printf("[main]configPath:%s\n",path); - loadConfig(path,str_entry,&host,64); + loadConfig(path,str_entry,&host,&intervall,64); printf("[main]host:%s\n",host); + printf("[main]intervall:%d\n",intervall); for(i=0;str_entry[i];i++) { services[i].targetPath = strdup(str_entry[i]); @@ -140,7 +138,7 @@ int main(int argc,char *argv[]) printf("entry in config:%s\n",services[i].name); ServiceGetSendStatus(connection,&services[i]); } - sleep(10); + sleep(intervall); } }else {