X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcstrutil.c;h=3251c2b0a4d47c43255ca71746e17cf899ffedb4;hb=e7b6c157b80152bf9fb9266e6bdd93f9fb0db776;hp=e7344ad5b9d76fa5e3b351b570544bd17bc44ebf;hpb=befeb8f188a4a533bb8761d8cb4b02f2241c3bee;p=silc.git diff --git a/lib/silcutil/silcstrutil.c b/lib/silcutil/silcstrutil.c index e7344ad5..3251c2b0 100644 --- a/lib/silcutil/silcstrutil.c +++ b/lib/silcutil/silcstrutil.c @@ -121,13 +121,13 @@ int silc_string_compare(char *string1, char *string2) char **silc_string_split(const char *string, char ch, int *ret_count) { - char **splitted = NULL, sep[1], *item, *cp; + char **splitted = NULL, sep[2], *item, *cp; int i = 0, len; - if (!string) - return NULL; - if (!ret_count) + if (!string || !ret_count) { + silc_set_errno(SILC_ERR_INVALID_ARGUMENT); return NULL; + } splitted = silc_calloc(1, sizeof(*splitted)); if (!splitted) @@ -140,9 +140,13 @@ char **silc_string_split(const char *string, char ch, int *ret_count) } sep[0] = ch; + sep[1] = '\0'; cp = (char *)string; - while(cp) { + while (cp) { len = strcspn(cp, sep); + if (!len) + break; + item = silc_memdup(cp, len); if (!item) { silc_free(splitted); @@ -155,10 +159,13 @@ char **silc_string_split(const char *string, char ch, int *ret_count) else cp++; - splitted = silc_realloc(splitted, (i + 1) * sizeof(*splitted)); - if (!splitted) - return NULL; splitted[i++] = item; + + if (cp) { + splitted = silc_realloc(splitted, (i + 1) * sizeof(*splitted)); + if (!splitted) + return NULL; + } } *ret_count = i; @@ -174,8 +181,10 @@ char *silc_string_regexify(const char *string) int i, len, count; char *regex; - if (!string) + if (!string) { + silc_set_errno(SILC_ERR_INVALID_ARGUMENT); return NULL; + } len = strlen(string); count = 4; @@ -224,8 +233,10 @@ char *silc_string_regex_combine(const char *string1, const char *string2) char *tmp; int len1, len2; - if (!string1 || !string2) + if (!string1 || !string2) { + silc_set_errno(SILC_ERR_INVALID_ARGUMENT); return NULL; + } len1 = strlen(string1); len2 = strlen(string2); @@ -238,24 +249,6 @@ char *silc_string_regex_combine(const char *string1, const char *string2) return tmp; } -/* Matches the two strings and returns TRUE if the strings match. */ - -int silc_string_regex_match(const char *regex, const char *string) -{ - regex_t preg; - int ret = FALSE; - - if (regcomp(&preg, regex, REG_NOSUB | REG_EXTENDED) != 0) - return FALSE; - - if (regexec(&preg, string, 0, NULL, 0) == 0) - ret = TRUE; - - regfree(&preg); - - return ret; -} - /* Do regex match to the two strings `string1' and `string2'. If the `string2' matches the `string1' this returns TRUE. */ @@ -264,8 +257,10 @@ int silc_string_match(const char *string1, const char *string2) char *s1; int ret = FALSE; - if (!string1 || !string2) + if (!string1 || !string2) { + silc_set_errno(SILC_ERR_INVALID_ARGUMENT); return ret; + } s1 = silc_string_regexify(string1); ret = silc_string_regex_match(s1, string2);