From 4fb48e69126058c09a93a102970d182db3be31b9 Mon Sep 17 00:00:00 2001 From: xavi Date: Tue, 17 Sep 2024 20:52:42 -0700 Subject: [PATCH] Fixed bug that missed the null terminator on the dest in x_strcpy --- x_string/src/x_string.c | 9 ++++++--- x_string/src/x_string.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/x_string/src/x_string.c b/x_string/src/x_string.c index 4c75ae5..20d1674 100644 --- a/x_string/src/x_string.c +++ b/x_string/src/x_string.c @@ -12,15 +12,15 @@ int x_strlen(char* str){ // copy one string to another destination // must include the size of the destination -void x_strcpy(char* dest, char* src, const int size_dest){ +// if greater than destination than stop and return 1 +int x_strcpy(char* dest, char* src, const int size_dest){ char* dest_ptr = dest; char* src_ptr = src; int i = 0; while(*src_ptr != '\0'){ if (i >= size_dest){ - fprintf(stderr, "Error: x_strcpy\nSource string exeeds destination memory\n"); - exit(1); + return 1; } *dest_ptr = *src_ptr; dest_ptr++; @@ -28,6 +28,9 @@ void x_strcpy(char* dest, char* src, const int size_dest){ i++; } + *dest_ptr = *src_ptr; + return 0; + } // compare two strings // return 0 if equal to eachother diff --git a/x_string/src/x_string.h b/x_string/src/x_string.h index 8ccf9d3..2b6d621 100644 --- a/x_string/src/x_string.h +++ b/x_string/src/x_string.h @@ -2,7 +2,7 @@ #define X_STRING_H int x_strlen(char*); -void x_strcpy(char*, char*, const int); +int x_strcpy(char*, char*, const int); int x_strcmp(const char*, const char*); char* x_strconcat(char*, char*);