From adaf5cc31199b8b70404f30b51a0e4ef822d6fab Mon Sep 17 00:00:00 2001 From: Timerix Date: Thu, 13 Nov 2025 06:16:57 +0500 Subject: [PATCH] cstr_copy --- include/tlibc/string/cstr.h | 2 ++ src/string/cstr.c | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/tlibc/string/cstr.h b/include/tlibc/string/cstr.h index 4712d39..d0081e4 100755 --- a/include/tlibc/string/cstr.h +++ b/include/tlibc/string/cstr.h @@ -1,6 +1,8 @@ #pragma once #include "../std.h" +char* cstr_copy(cstr self); + #define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__) char* _strcat_malloc(u64 n, cstr str0, ...); char* _vstrcat_malloc(u64 n, cstr str0, va_list args); diff --git a/src/string/cstr.c b/src/string/cstr.c index b8425a3..9ab8dd2 100755 --- a/src/string/cstr.c +++ b/src/string/cstr.c @@ -1,5 +1,15 @@ #include "tlibc/string/cstr.h" +char* cstr_copy(cstr self){ + if(self == NULL) + return NULL; + + u64 len_with_zero = strlen(self) + 1; + char* copy = (char*)malloc(len_with_zero); + memcpy(copy, self, len_with_zero); + return copy; +} + char* _strcat_malloc(u64 n, cstr str0, ...){ va_list args; va_start(args, str0);