Confparse: Improve file_to_str.

This commit is contained in:
Martin Duquesnoy
2009-08-11 00:45:53 +02:00
parent 20d247438c
commit 5fb455840a
2 changed files with 11 additions and 4 deletions

View File

@@ -35,7 +35,7 @@
char* char*
file_to_str(char *path) file_to_str(char *path)
{ {
char *ret, *p; char *buf, *ret, *p;
int fd, i; int fd, i;
struct stat st; struct stat st;
@@ -46,7 +46,14 @@ file_to_str(char *path)
stat(path, &st); stat(path, &st);
/* Bufferize file */ /* Bufferize file */
ret = _strdup((char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, SEEK_SET)); buf = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, SEEK_SET);
/* Copy buffer in return value */
ret = _strdup(buf);
/* Unmap buffer, thanks linkdd. */
munmap(buf, st.st_size);
close(fd);
/* Erase comment line from return value */ /* Erase comment line from return value */
for(i = 0; (p = strchr(erase_delim_content(ret + i), COMMENT_CHAR));) for(i = 0; (p = strchr(erase_delim_content(ret + i), COMMENT_CHAR));)

View File

@@ -94,11 +94,11 @@ setwinstate(Window win, long state)
} }
/** My strdup. the strdup of string.h isn't ansi compatible.. /** My strdup. the strdup of string.h isn't ansi compatible..
* Thanks linkkd. * Thanks linkdd.
* \param str char pointer * \param str char pointer
*/ */
char* char*
_strdup(char const *str) _strdup(const char *str)
{ {
char *ret = emalloc(strlen(str) + 1, sizeof(char)); char *ret = emalloc(strlen(str) + 1, sizeof(char));