X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_parse_option.c;fp=src%2Futils_parse_option.c;h=2168cd1af3d624801146905b853adb0c92db3d7b;hb=0c75c6cd9bd52480856a9ccb8289e9b17d708c63;hp=d94c1400919c5b4ce0ce8c2956cd91ce4c219c71;hpb=55e53e154eeaf317b051202f3310d611949e6ff8;p=collectd.git diff --git a/src/utils_parse_option.c b/src/utils_parse_option.c index d94c1400..2168cd1a 100644 --- a/src/utils_parse_option.c +++ b/src/utils_parse_option.c @@ -150,4 +150,55 @@ int parse_option (char **ret_buffer, char **ret_key, char **ret_value) return (0); } /* int parse_option */ +int escape_string (char *buffer, size_t buffer_size) +{ + char *temp; + size_t i; + size_t j; + + /* Check if we need to escape at all first */ + temp = strpbrk (buffer, " \t\"\\"); + if (temp == NULL) + return (0); + + temp = (char *) malloc (buffer_size); + if (temp == NULL) + return (-1); + memset (temp, 0, buffer_size); + + temp[0] = '"'; + j = 1; + + for (i = 0; i < buffer_size; i++) + { + if (buffer[i] == 0) + { + break; + } + else if ((buffer[i] == '"') || (buffer[i] == '\\')) + { + if (j > (buffer_size - 4)) + break; + temp[j] = '\\'; + temp[j + 1] = buffer[i]; + j += 2; + } + else + { + if (j > (buffer_size - 3)) + break; + temp[j] = buffer[i]; + j++; + } + } + + assert ((j + 1) < buffer_size); + temp[j] = '"'; + temp[j + 1] = 0; + + sstrncpy (buffer, temp, buffer_size); + sfree (temp); + return (0); +} /* int escape_string */ + /* vim: set sw=2 ts=8 tw=78 et : */