diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/modules/utils/utils.c | 19 | ||||
| -rw-r--r-- | src/modules/utils/utils.h | 7 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/modules/utils/utils.c b/src/modules/utils/utils.c index d5a3a96..2e8aa5c 100644 --- a/src/modules/utils/utils.c +++ b/src/modules/utils/utils.c @@ -1,3 +1,4 @@ +#include <stdio.h> #include <string.h> #include <stdlib.h> @@ -93,3 +94,21 @@ char *string_format(char* string) { return ptr; } + +// input two char pointer +// output: one char pointer +// misc: caller needs to free the memory +char *concat(const char *s1, const char *s2) { + const size_t len1 = strlen(s1); + const size_t len2 = strlen(s2); + char *result = malloc(len1 + len2 + 1); // +1 for '\0' + + if (result == NULL) { + perror("allocating memory for strings failed"); + return NULL; + } + + memcpy(result, s1, len1); + memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator + return result; //caller needs to free memory +} diff --git a/src/modules/utils/utils.h b/src/modules/utils/utils.h index 1dfbb51..7a0ab02 100644 --- a/src/modules/utils/utils.h +++ b/src/modules/utils/utils.h @@ -11,4 +11,11 @@ */ char *string_format(char* string); +// function: char *concat(const char *str1, const char *str2) +// input two char pointer +// return: pointer to string +// on-error: returns NULL +// misc: caller needs to free the memory +char *concat(const char *str1, const char *str2); + #endif |
