Index: misc.c =================================================================== RCS file: /cvsroot/gnokii/gnokii/common/misc.c,v retrieving revision 1.97 diff -u -p -r1.97 misc.c --- misc.c 18 Jul 2005 20:55:36 -0000 1.97 +++ misc.c 4 Aug 2005 17:11:12 -0000 @@ -527,22 +527,28 @@ char **gnokii_strsplit(const char *strin if (!string || !delimiter || !tokens) return NULL; - strings = calloc(tokens + 1, sizeof(char *)); + strings = NULL; - while ((tmp = strstr(left, delimiter)) != NULL && (count < tokens)) { + while ((tmp = strstr(left, delimiter)) != NULL) { + if (tokens > 0 && count == tokens) + break; str = malloc((tmp - left) + 1); memset(str, 0, (tmp - left) + 1); memcpy(str, left, tmp - left); + + /* +3 = + * +1 for the current string + * +1 for the remainder + * +1 for the null termination + */ + strings = realloc(strings, sizeof(char *) * (count + 3)); strings[count] = str; left = tmp + strlen(delimiter); count++; } strings[count] = strdup(left); - - for (count = 0; count < tokens; count++) { - dprintf("strings[%d] = %s\n", count, strings[count]); - } + strings[count+1] = NULL; return strings; } @@ -552,16 +558,14 @@ char **gnokii_strsplit(const char *strin */ void gnokii_strfreev(char **str_array) { - char **tmp = str_array; + int i; if (!str_array) return; - while (*tmp) { - free(*tmp); - tmp++; - } - free(str_array); + for (i = 0; str_array[i] != NULL; i++) + free (str_array[i]); + free (str_array); } /*