Confparse: prevent multiple _strdup call in file_to_str()
This commit is contained in:
parent
a0838ba8fc
commit
1cfe0ce94e
@ -38,6 +38,7 @@ file_to_str(char *path)
|
|||||||
char *buf, *ret, *p;
|
char *buf, *ret, *p;
|
||||||
int fd, i;
|
int fd, i;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
Bool is_char = False;
|
||||||
|
|
||||||
if(!path || !(fd = open(path, O_RDONLY)))
|
if(!path || !(fd = open(path, O_RDONLY)))
|
||||||
return NULL;
|
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)
|
if((buf = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, SEEK_SET)) == (char*) -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Copy buffer in return value */
|
/* Copy buffer without comments in return value */
|
||||||
ret = _strdup(buf);
|
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. */
|
/* Unmap buffer, thanks linkdd. */
|
||||||
munmap(buf, st.st_size);
|
munmap(buf, st.st_size);
|
||||||
close(fd);
|
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);
|
fprintf(stderr, "WMFS Configuration info: '%s' read.\n", path);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@ -42,12 +42,13 @@ erase_delim_content(char *buf)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for(i = 0; i < strlen(str); ++i)
|
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++] = ' ');
|
for(*(str + (j = i)) = ' '; str[j] && str[j] != c; str[j++] = ' ');
|
||||||
str[j] = ' ';
|
str[j] = ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -55,14 +55,14 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "structs.h"
|
#include "structs.h"
|
||||||
|
|
||||||
/* Optional dependences */
|
/* Optional dependencies */
|
||||||
#ifdef HAVE_XINERAMA
|
#ifdef HAVE_XINERAMA
|
||||||
#include <X11/extensions/Xinerama.h>
|
#include <X11/extensions/Xinerama.h>
|
||||||
#endif /* HAVE_XINERAMA */
|
#endif /* HAVE_XINERAMA */
|
||||||
|
|
||||||
#ifdef HAVE_XRANDR
|
#ifdef HAVE_XRANDR
|
||||||
#include <X11/extensions/Xrandr.h>
|
#include <X11/extensions/Xrandr.h>
|
||||||
#endif /* HAVE_XINERAMA */
|
#endif /* HAVE_XRANDR */
|
||||||
|
|
||||||
/* MACRO */
|
/* MACRO */
|
||||||
#define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
|
#define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user