diff options
-rw-r--r-- | src/util.c | 28 |
1 files changed, 10 insertions, 18 deletions
@@ -28,35 +28,27 @@ char* slv2_strjoin(const char* first, ...) { - /* FIXME: This is, in fact, as stupid as it looks */ - size_t len = strlen(first); - char* result = NULL; - va_list args; - - va_start(args, first); - while (1) { - const char* const s = va_arg(args, const char *); - if (s == NULL) - break; - len += strlen(s); - } - va_end(args); + char* result = malloc(len + 1); - result = malloc(len + 1); - if (!result) - return NULL; + memcpy(result, first, len); - strcpy(result, first); + va_list args; va_start(args, first); while (1) { const char* const s = va_arg(args, const char *); if (s == NULL) break; - strcat(result, s); + + const size_t this_len = strlen(s); + result = realloc(result, len + this_len + 1); + memcpy(result + len, s, this_len); + len += this_len; } va_end(args); + result[len] = '\0'; + return result; } |