Confparse: prevent multiple _strdup call in file_to_str()

This commit is contained in:
Philippe Pepiot 2009-08-26 03:03:07 +02:00
parent a0838ba8fc
commit 1cfe0ce94e
3 changed files with 23 additions and 9 deletions

View File

@ -38,6 +38,7 @@ file_to_str(char *path)
char *buf, *ret, *p;
int fd, i;
struct stat st;
Bool is_char = False;
if(!path || !(fd = open(path, O_RDONLY)))
return NULL;
@ -49,17 +50,29 @@ file_to_str(char *path)
if((buf = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, SEEK_SET)) == (char*) -1)
return NULL;
/* Copy buffer in return value */
ret = _strdup(buf);
/* Copy buffer without comments in return value */
ret = emalloc(strlen(buf) + 1, sizeof(char));
for(p = buf, i = 0; *p != '\0'; p++)
{
if(strchr("\"'", *p))
is_char = (is_char == False) ? True : False;
if(*p == COMMENT_CHAR && is_char == False)
{
if(!(p = strchr(p, '\n')))
break;
ret[i++] = '\n';
}
else
ret[i++] = *p;
}
ret[i++] = '\0';
/* Unmap buffer, thanks linkdd. */
munmap(buf, st.st_size);
close(fd);
/* Erase comment line from return value */
for(i = 0; (p = strchr(erase_delim_content(ret + i), COMMENT_CHAR));)
for(i = st.st_size - strlen(p); ret[i] && ret[i] != '\n'; ret[i++] = ' ');
fprintf(stderr, "WMFS Configuration info: '%s' read.\n", path);
return ret;

View File

@ -42,12 +42,13 @@ erase_delim_content(char *buf)
return NULL;
for(i = 0; i < strlen(str); ++i)
if((c = str[i]) == '"' || (c = str[i]) == '\'')
if(strchr("\"'", (c = str[i])))
{
for(*(str + (j = i)) = ' '; str[j] && str[j] != c; str[j++] = ' ');
str[j] = ' ';
}
return str;
}

View File

@ -55,14 +55,14 @@
#include "config.h"
#include "structs.h"
/* Optional dependences */
/* Optional dependencies */
#ifdef HAVE_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* HAVE_XINERAMA */
#ifdef HAVE_XRANDR
#include <X11/extensions/Xrandr.h>
#endif /* HAVE_XINERAMA */
#endif /* HAVE_XRANDR */
/* MACRO */
#define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)