Fix problems with spaces in config files

This commit is contained in:
Ron Pedde 2005-01-30 04:32:35 +00:00
parent 457bd13492
commit bef2d8714c
1 changed files with 46 additions and 38 deletions

View File

@ -232,8 +232,9 @@ int config_read(char *file) {
char path_buffer[PATH_MAX]; char path_buffer[PATH_MAX];
CONFIGELEMENT *pce; CONFIGELEMENT *pce;
int handled; int handled;
char *term;
buffer=(char*)malloc(MAX_LINE); buffer=(char*)malloc(MAX_LINE+1);
if(!buffer) if(!buffer)
return -1; return -1;
@ -278,49 +279,56 @@ int config_read(char *file) {
config.servername=strdup("mt-daapd " VERSION); config.servername=strdup("mt-daapd " VERSION);
while(fgets(buffer,MAX_LINE,fin)) { while(fgets(buffer,MAX_LINE,fin)) {
if(*buffer != '#') { buffer[MAX_LINE] = '\0';
value=buffer;
strsep(&value,"\t ");
if(value) {
while((*value==' ')||(*value=='\t'))
value++;
comment=value; comment=strchr(buffer,'#');
strsep(&comment,"#"); if(comment)
*comment = '\0';
if(value[strlen(value)-1] == '\n') while(strlen(buffer) && (strchr("\n\r ",buffer[strlen(buffer)-1])))
value[strlen(value)-1] = '\0'; buffer[strlen(buffer)-1] = '\0';
pce=config_elements; term=buffer;
handled=0;
while((!handled) && (pce->config_element != -1)) {
if((strcasecmp(buffer,pce->name)==0) && (pce->config_element)) {
/* valid config directive */
handled=1;
pce->changed=1;
DPRINTF(E_DBG,L_CONF,"Read %s: %s\n",pce->name,value);
switch(pce->type) { while((*term=='\t') || (*term==' '))
case CONFIG_TYPE_STRING: term++;
/* DWB: free space to prevent small leak */
if(*((char **)(pce->var))) value=term;
free(*((char **)(pce->var)));
*((char **)(pce->var)) = (void*)strdup(value); strsep(&value,"\t ");
break; if((value) && (term) && (strlen(term))) {
case CONFIG_TYPE_INT: while(strlen(value) && (strchr("\t ",*value)))
*((int*)(pce->var)) = atoi(value); value++;
break;
} pce=config_elements;
handled=0;
while((!handled) && (pce->config_element != -1)) {
if((strcasecmp(term,pce->name)==0) && (pce->config_element)) {
/* valid config directive */
handled=1;
pce->changed=1;
DPRINTF(E_DBG,L_CONF,"Read %s: %s\n",pce->name,value);
switch(pce->type) {
case CONFIG_TYPE_STRING:
/* DWB: free space to prevent small leak */
if(*((char **)(pce->var)))
free(*((char **)(pce->var)));
*((char **)(pce->var)) = (void*)strdup(value);
break;
case CONFIG_TYPE_INT:
*((int*)(pce->var)) = atoi(value);
break;
} }
pce++;
}
if(!handled) {
fprintf(stderr,"Invalid config directive: %s\n",buffer);
fclose(fin);
return -1;
} }
pce++;
}
if(!handled) {
fprintf(stderr,"Invalid config directive: %s\n",buffer);
fclose(fin);
return -1;
} }
} }
} }