diff options
| author | Anser <rostgans@tutamail.com> | 2026-07-27 21:56:05 +0200 |
|---|---|---|
| committer | Anser <rostgans@tutamail.com> | 2026-07-31 14:27:56 +0200 |
| commit | 784d654db95ac27ab45db53f9258b324125b3cfd (patch) | |
| tree | 6184845bcac6ed2a8cc9e4fd733114ba4db55aed /src/modules | |
| parent | b8969bf91a034fe848f77c6e670a5a158438ea6e (diff) | |
utils: added string concatination function
Diffstat (limited to 'src/modules')
| -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 |
