From 784d654db95ac27ab45db53f9258b324125b3cfd Mon Sep 17 00:00:00 2001 From: Anser Date: Mon, 27 Jul 2026 21:56:05 +0200 Subject: utils: added string concatination function --- src/modules/utils/utils.c | 19 +++++++++++++++++++ src/modules/utils/utils.h | 7 +++++++ 2 files changed, 26 insertions(+) (limited to 'src/modules/utils') 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 #include #include @@ -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 -- cgit v1.2.3