X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fconfigfile.c;h=fe2bce3c365124fd700becaf29599b79f103b3d5;hb=76489692cb9eef2590d8280a3cefad1c9f9244a2;hp=79ad9f6d4f6e2ae6cb60af07fab551f645f3cb1f;hpb=a1a8bdce8ff1d7c70f3fbf94e8ffe4c3aac8715c;p=collectd.git diff --git a/src/configfile.c b/src/configfile.c index 79ad9f6d..fe2bce3c 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -575,14 +575,14 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth) ERROR ("configfile: malloc failed."); return (NULL); } - memset (root, '\0', sizeof (oconfig_item_t)); + memset (root, 0, sizeof (oconfig_item_t)); while ((de = readdir (dh)) != NULL) { char name[1024]; char **tmp; - if ((de->d_name[0] == '.') || (de->d_name[0] == '\0')) + if ((de->d_name[0] == '.') || (de->d_name[0] == 0)) continue; status = ssnprintf (name, sizeof (name), "%s/%s", @@ -624,13 +624,11 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth) char *name = filenames[i]; temp = cf_read_generic (name, depth); - if (temp == NULL) { - int j; - for (j = i; j < filenames_num; ++j) - free (filenames[j]); - free (filenames); - oconfig_free (root); - return (NULL); + if (temp == NULL) + { + /* An error should already have been reported. */ + sfree (name); + continue; } cf_ci_append_children (root, temp); @@ -943,7 +941,7 @@ int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR ("cf_util_get_string: The %s plugin requires " + ERROR ("cf_util_get_string: The %s option requires " "exactly one string argument.", ci->key); return (-1); } @@ -959,6 +957,62 @@ int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */ return (0); } /* }}} int cf_util_get_string */ +/* Assures the config option is a string and copies it to the provided buffer. + * Assures null-termination. */ +int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */ + size_t buffer_size) +{ + if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1)) + return (EINVAL); + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + { + ERROR ("cf_util_get_string_buffer: The %s option requires " + "exactly one string argument.", ci->key); + return (-1); + } + + strncpy (buffer, ci->values[0].value.string, buffer_size); + buffer[buffer_size - 1] = 0; + + return (0); +} /* }}} int cf_util_get_string_buffer */ + +/* Assures the config option is a number and returns it as an int. */ +int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */ +{ + if ((ci == NULL) || (ret_value == NULL)) + return (EINVAL); + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) + { + ERROR ("cf_util_get_int: The %s option requires " + "exactly one numeric argument.", ci->key); + return (-1); + } + + *ret_value = (int) ci->values[0].value.number; + + return (0); +} /* }}} int cf_util_get_int */ + +int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */ +{ + if ((ci == NULL) || (ret_bool == NULL)) + return (EINVAL); + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) + { + ERROR ("cf_util_get_boolean: The %s option requires " + "exactly one boolean argument.", ci->key); + return (-1); + } + + *ret_bool = ci->values[0].value.boolean ? true : false; + + return (0); +} /* }}} int cf_util_get_boolean */ + /* Assures that the config option is a string. The string is then converted to * a port number using `service_name_to_port_number' and returned. Returns the * port number in the range [1-65535] or less than zero upon failure. */ @@ -966,7 +1020,7 @@ int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */ { if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR ("cf_util_get_port_number: The %s plugin requires " + ERROR ("cf_util_get_port_number: The %s option requires " "exactly one string argument.", ci->key); return (-1); }