/* See LICENSE file for copyright and license details. */ #include #include char ** strsplit(char *str, char delim, int *size) { char **lines = NULL; char *s = str; int count = 0; for (int start = 0, end = 0; ; s++, end++) { if (*s == delim || *s == '\0') { int _size = end - start; lines = realloc(lines, sizeof(char *) * ++count); int idx = count - 1; if (_size > 1) { lines[idx] = calloc(_size + 1, sizeof(char *)); strncpy(lines[idx], str + start, _size); } else { lines[idx] = strdup(""); } start = end + 1; /* plus 1 - skip the delim */ } if (*s == '\0') break; } *size = count; return lines; }