diff --git a/include/nasmlib.h b/include/nasmlib.h index e57d0e6d..202c9f03 100644 --- a/include/nasmlib.h +++ b/include/nasmlib.h @@ -80,6 +80,7 @@ void nasm_free(void *); char * safe_alloc nasm_strdup(const char *); char * safe_alloc nasm_strndup(const char *, size_t); char * safe_alloc nasm_strcat(const char *one, const char *two); +char * never_null nasm_strappend(char *one, const char *two); char * safe_alloc end_with_null nasm_strcatn(const char *one, ...); /* Assert the argument is a pointer without evaluating it */ diff --git a/nasmlib/malloc.c b/nasmlib/malloc.c index ccbc0c75..5903f9a5 100644 --- a/nasmlib/malloc.c +++ b/nasmlib/malloc.c @@ -110,6 +110,25 @@ char *nasm_strcat(const char *one, const char *two) return rslt; } +/* Unlike nasm_strcat() this frees the initial string, which can be NULL */ +char *nasm_strappend(char *one, const char *two) +{ + size_t l1, l2; + + if (!two) + return one; + + if (!one) + return nasm_strdup(two); + + l1 = strlen(one); + l2 = strlen(two) + 1; + one = nasm_realloc(one, l1+l2); + memcpy(one + l1, two, l2); + + return one; +} + char *nasm_strcatn(const char *str1, ...) { va_list ap;