summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/librcscripts/misc.c')
-rw-r--r--src/core/librcscripts/misc.c86
1 files changed, 39 insertions, 47 deletions
diff --git a/src/core/librcscripts/misc.c b/src/core/librcscripts/misc.c
index b08a3a6..08546ee 100644
--- a/src/core/librcscripts/misc.c
+++ b/src/core/librcscripts/misc.c
@@ -86,7 +86,9 @@ strndup (const char *str, size_t size)
char *new_str = NULL;
size_t len;
- if (!check_arg_str (str))
+ /* We cannot check if its a valid string here, as it might
+ * not be '\0' terminated ... */
+ if (!check_arg_ptr (str))
return NULL;
/* Check lenght of str without breaching the size limit */
@@ -406,20 +408,20 @@ ls_dir (const char *pathname, int hidden)
&& (hidden ? 1 : dir_entry->d_name[0] != '.'))
{
char *d_name = dir_entry->d_name;
- char *tmp_p;
+ char *str_ptr;
/* Do not list current or parent entries */
if ((0 == strcmp (d_name, ".")) || (0 == strcmp (d_name, "..")))
continue;
- tmp_p = strcatpaths (pathname, d_name);
- if (NULL == tmp_p)
+ str_ptr = strcatpaths (pathname, d_name);
+ if (NULL == str_ptr)
{
DBG_MSG ("Failed to allocate buffer!\n");
goto error;
}
- str_list_add_item (dirlist, tmp_p, error);
+ str_list_add_item (dirlist, str_ptr, error);
}
}
while (NULL != dir_entry);
@@ -457,14 +459,11 @@ error:
char *
get_cnf_entry (const char *pathname, const char *entry)
{
+ dyn_buf_t *dynbuf = NULL;
char *buf = NULL;
- char *tmp_buf = NULL;
- char *tmp_p;
+ char *str_ptr;
char *value = NULL;
char *token;
- size_t lenght;
- int count;
- int current = 0;
if ((!check_arg_str (pathname)) || (!check_arg_str (entry)))
@@ -478,28 +477,23 @@ get_cnf_entry (const char *pathname, const char *entry)
return NULL;
}
- if (-1 == file_map (pathname, &buf, &lenght))
+ dynbuf = new_dyn_buf_mmap_file (pathname);
+ if (NULL == dynbuf)
{
DBG_MSG ("Could not open config file for reading!\n");
return NULL;
}
- while (current < lenght)
+ while (NULL != (buf = read_line_dyn_buf (dynbuf)))
{
- count = buf_get_line (buf, lenght, current);
-
- tmp_buf = xstrndup (&buf[current], count);
- if (NULL == tmp_buf)
- goto error;
-
- tmp_p = tmp_buf;
+ str_ptr = buf;
/* Strip leading spaces/tabs */
- while ((tmp_p[0] == ' ') || (tmp_p[0] == '\t'))
- tmp_p++;
+ while ((str_ptr[0] == ' ') || (str_ptr[0] == '\t'))
+ str_ptr++;
/* Get entry and value */
- token = strsep (&tmp_p, "=");
+ token = strsep (&str_ptr, "=");
/* Bogus entry or value */
if (NULL == token)
goto _continue;
@@ -511,7 +505,7 @@ get_cnf_entry (const char *pathname, const char *entry)
do
{
/* Bash variables are usually quoted */
- token = strsep (&tmp_p, "\"\'");
+ token = strsep (&str_ptr, "\"\'");
/* If quoted, the first match will be "" */
}
while ((NULL != token) && (0 == strlen (token)));
@@ -537,7 +531,12 @@ get_cnf_entry (const char *pathname, const char *entry)
value = xstrndup (token, strlen (token));
if (NULL == value)
- goto error;
+ {
+ free_dyn_buf (dynbuf);
+ free (buf);
+
+ return NULL;
+ }
/* We do not break, as there might be more than one entry
* defined, and as bash uses the last, so should we */
@@ -545,34 +544,27 @@ get_cnf_entry (const char *pathname, const char *entry)
}
_continue:
- current += count + 1;
- free (tmp_buf);
- /* Set to NULL in case we error out above and have
- * to free below */
- tmp_buf = NULL;
+ free (buf);
+ }
+
+ /* read_line_dyn_buf() returned NULL with errno set */
+ if ((NULL == buf) && (0 != errno))
+ {
+ DBG_MSG ("Failed to read line from dynamic buffer!\n");
+ free_dyn_buf (dynbuf);
+ if (NULL != value)
+ free (value);
+
+ return NULL;
}
if (NULL == value)
DBG_MSG ("Failed to get value for config entry '%s'!\n", entry);
- file_unmap (buf, lenght);
+ free_dyn_buf (dynbuf);
return value;
-
-error:
- free (tmp_buf);
- free (value);
-
- if (NULL != buf)
- {
- save_errno ();
- file_unmap (buf, lenght);
- /* unmmap() might have changed it */
- restore_errno ();
- }
-
- return NULL;
}
@@ -614,7 +606,7 @@ file_map (const char *filename, char **buf, size_t * bufsize)
if (fstat (fd, &stats) < 0)
{
DBG_MSG ("Failed to stat file!\n");
-
+
save_errno ();
close (fd);
restore_errno ();
@@ -626,7 +618,7 @@ file_map (const char *filename, char **buf, size_t * bufsize)
{
errno = EINVAL;
DBG_MSG ("Failed to mmap file with 0 size!\n");
-
+
save_errno ();
close (fd);
restore_errno ();
@@ -638,7 +630,7 @@ file_map (const char *filename, char **buf, size_t * bufsize)
if (*buf == MAP_FAILED)
{
DBG_MSG ("Failed to mmap file!\n");
-
+
save_errno ();
close (fd);
restore_errno ();