From 581b79e0b9097592ab32bb00edce33c06d72917b Mon Sep 17 00:00:00 2001 From: Sven Michael Klose Date: Sun, 7 Jul 2024 14:04:49 +0200 Subject: [PATCH 1/4] Add stpcpy(). Like strcpy() but returning pointer to ending zero of copied string. --- include/string.h | 1 + libsrc/common/stpcpy.c | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 libsrc/common/stpcpy.c diff --git a/include/string.h b/include/string.h index b19f44e31..06c97d464 100644 --- a/include/string.h +++ b/include/string.h @@ -52,6 +52,7 @@ char* __fastcall__ strchr (const char* s, int c); int __fastcall__ strcmp (const char* s1, const char* s2); int __fastcall__ strcoll (const char* s1, const char* s2); char* __fastcall__ strcpy (char* dest, const char* src); +char* __fastcall__ stpcpy (char* dest, const char* src); size_t __fastcall__ strcspn (const char* s1, const char* s2); char* __fastcall__ strerror (int errcode); size_t __fastcall__ strlen (const char* s); diff --git a/libsrc/common/stpcpy.c b/libsrc/common/stpcpy.c new file mode 100644 index 000000000..153a3e361 --- /dev/null +++ b/libsrc/common/stpcpy.c @@ -0,0 +1,8 @@ +#include + +char * __fastcall__ +stpcpy (char * dst, const char * src) +{ + strcpy (dst, src); + return dst + strlen (src); +} From af3ac423733637c74400818d5281b3d3ec643df4 Mon Sep 17 00:00:00 2001 From: Sven Michael Klose Date: Sun, 7 Jul 2024 19:48:44 +0200 Subject: [PATCH 2/4] Move stpcpy() to non-standard section. --- include/string.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/string.h b/include/string.h index 06c97d464..3b7ece1d9 100644 --- a/include/string.h +++ b/include/string.h @@ -52,7 +52,6 @@ char* __fastcall__ strchr (const char* s, int c); int __fastcall__ strcmp (const char* s1, const char* s2); int __fastcall__ strcoll (const char* s1, const char* s2); char* __fastcall__ strcpy (char* dest, const char* src); -char* __fastcall__ stpcpy (char* dest, const char* src); size_t __fastcall__ strcspn (const char* s1, const char* s2); char* __fastcall__ strerror (int errcode); size_t __fastcall__ strlen (const char* s); @@ -91,6 +90,7 @@ char* __fastcall__ strlower (char* s); char* __fastcall__ strupr (char* s); char* __fastcall__ strupper (char* s); char* __fastcall__ strqtok (char* s1, const char* s2); +char* __fastcall__ stpcpy (char* dest, const char* src); #endif const char* __fastcall__ __stroserror (unsigned char errcode); From 9558ebad624ed7c03eb7ddee12445482d1749a19 Mon Sep 17 00:00:00 2001 From: Sven Michael Klose Date: Mon, 15 Jul 2024 17:35:28 +0200 Subject: [PATCH 3/4] Add test for stpcpy(). --- test/val/stpcpy.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/val/stpcpy.c diff --git a/test/val/stpcpy.c b/test/val/stpcpy.c new file mode 100644 index 000000000..8bdbfb926 --- /dev/null +++ b/test/val/stpcpy.c @@ -0,0 +1,44 @@ +// 2024-07-15 Sven Michael Klose + +#include +#include +#include +#include + +#define STR_SHORT "Hello, World!" +#define STR_LONG "This is a longer test string for stpcpy." + +int +main () +{ + char dest[50]; + const char *src_empty; + const char *src_short; + const char *src_long; + char *end; + + src_empty = ""; + end = stpcpy (dest, src_empty); + assert(!strcmp (dest, src_empty)); + assert(!*end); + assert(end == dest); + printf ("Test 1 passed.\n"); + + src_short = STR_SHORT; + end = stpcpy (dest, src_short); + assert(!strcmp (dest, src_short)); + assert(!*end); + assert(end == &dest[sizeof (STR_SHORT) - 1]); + printf ("Test 2 passed.\n"); + + src_long = STR_LONG; + end = stpcpy (dest, src_long); + assert(!strcmp (dest, src_long)); + assert(!*end); + assert(end == &dest[sizeof (STR_LONG) - 1]); + printf ("Test 3 passed.\n"); + + printf ("All tests passed.\n"); + return EXIT_SUCCESS; +} + From aed94d2dae9755a77040fec396d84edc883892ef Mon Sep 17 00:00:00 2001 From: Sven Michael Klose Date: Tue, 16 Jul 2024 01:33:48 +0200 Subject: [PATCH 4/4] Fix code style. Have type, function name and argument declaration on a single line. --- libsrc/common/stpcpy.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libsrc/common/stpcpy.c b/libsrc/common/stpcpy.c index 153a3e361..12af47f2f 100644 --- a/libsrc/common/stpcpy.c +++ b/libsrc/common/stpcpy.c @@ -1,7 +1,6 @@ #include -char * __fastcall__ -stpcpy (char * dst, const char * src) +char * __fastcall__ stpcpy (char * dst, const char * src) { strcpy (dst, src); return dst + strlen (src);