systemd-gotify/config.c

123 lines
4.0 KiB
C

#include"config.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int loadConfig(char *str_file, char **str_entry,char **host,int *intervall,int size)
{
int i=0;
FILE *f_config;
char temp_str[256];
char *ret = NULL;
printf("\nopening config file\n");
f_config = fopen(str_file,"r");
if(f_config != NULL)
{
ret = fgets(temp_str,256,f_config);
while(ret!=NULL)
{
temp_str[strcspn(temp_str,"\n")]=0;
printf("first temp_str:%s\n",temp_str);
if(strcmp(temp_str,"[Host]")==0)
{
printf("temp_str host:%s\n",temp_str);
while(ret!=NULL)
{
printf("temp_str:%s\n",temp_str);
ret = fgets(temp_str,256,f_config);
if(strchr(temp_str,'[')==NULL)
{
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]==' ') //Potential segfault??
{
if(ptr_host < (temp_str + strlen(temp_str)))
{
ptr_host = ptr_host + 1;
}else{
break;
}
}
*host = strdup(ptr_host);
printf("my host is:%s\n",*host);
}
}else{
break;
}
}
}
else if(strcmp(temp_str,"[Services]")==0)
{
for(i=0;i<size;i++)
{
ret=fgets(temp_str,256,f_config);
printf("for loop:%d\n",i);
if(ret!=NULL)
{
if(strchr(temp_str,'[')==NULL)
{
temp_str[strcspn(temp_str,"\n")]=0;
str_entry[i]=strdup(temp_str);
printf("module path:%s",str_entry[i]);
}else
{
break;
}
}
else
{
break;
}
}
}
else if(strcmp(temp_str,"[APP]")==0)
{
while(ret!=NULL)
{
ret = fgets(temp_str,256,f_config);
printf("temp_str:%s\n",temp_str);
if(strchr(temp_str,'[')==NULL)
{
if(strstr(temp_str,"Intervall")!=NULL)
{
char *ptr_intervall = strrchr(temp_str,'=')+1;//Without =
ptr_intervall[strcspn(ptr_intervall,"\n")]=0;
while(ptr_intervall[0]==' ')
{
ptr_intervall = ptr_intervall + 1;
}
*intervall = atoi(ptr_intervall);
printf("intervall:%d\n",*intervall);
}
}else{
break;
}
}
}else{
ret = fgets(temp_str,256,f_config);
}
}
}
else
{
fprintf(stderr,"error opening file[%s]\n",str_file);
}
printf("Done reading config file\n");
return 0;
}
/*int parseConfig(File *file, char *str_entry)
{
char sub_str[256];
if(strcmp(str_entry,"[Host]")==0)//section
{
fgets(sub_str,256,file);
//do sub parsing
}
}*/