aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Brown <mcb30@ipxe.org>2024-05-23 13:18:16 +0100
committerMichael Brown <mcb30@ipxe.org>2024-05-31 10:11:22 +0100
commite965f179e1654103eca33feed7a9cc4c51d91be6 (patch)
treefe55a32f263b5836b0310e8ad5a5184112079899
parentdc118c53696af6a0b1a8ee78fc9a4d28a217fb21 (diff)
downloadipxe-e965f179e1654103eca33feed7a9cc4c51d91be6.zip
ipxe-e965f179e1654103eca33feed7a9cc4c51d91be6.tar.gz
ipxe-e965f179e1654103eca33feed7a9cc4c51d91be6.tar.bz2
[libc] Add stpcpy()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/core/string.c17
-rw-r--r--src/include/string.h1
-rw-r--r--src/tests/string_test.c18
3 files changed, 34 insertions, 2 deletions
diff --git a/src/core/string.c b/src/core/string.c
index 9a1b9b7..364c4cf 100644
--- a/src/core/string.c
+++ b/src/core/string.c
@@ -321,9 +321,9 @@ char * strstr ( const char *haystack, const char *needle ) {
*
* @v dest Destination string
* @v src Source string
- * @ret dest Destination string
+ * @ret dnul Terminating NUL of destination string
*/
-char * strcpy ( char *dest, const char *src ) {
+char * stpcpy ( char *dest, const char *src ) {
const uint8_t *src_bytes = ( ( const uint8_t * ) src );
uint8_t *dest_bytes = ( ( uint8_t * ) dest );
@@ -333,6 +333,19 @@ char * strcpy ( char *dest, const char *src ) {
if ( ! *dest_bytes )
break;
}
+ return ( ( char * ) dest_bytes );
+}
+
+/**
+ * Copy string
+ *
+ * @v dest Destination string
+ * @v src Source string
+ * @ret dest Destination string
+ */
+char * strcpy ( char *dest, const char *src ) {
+
+ stpcpy ( dest, src );
return dest;
}
diff --git a/src/include/string.h b/src/include/string.h
index 5f5aecb..4ee9c73 100644
--- a/src/include/string.h
+++ b/src/include/string.h
@@ -43,6 +43,7 @@ extern char * __pure strchr ( const char *src, int character ) __nonnull;
extern char * __pure strrchr ( const char *src, int character ) __nonnull;
extern char * __pure strstr ( const char *haystack,
const char *needle ) __nonnull;
+extern char * stpcpy ( char *dest, const char *src ) __nonnull;
extern char * strcpy ( char *dest, const char *src ) __nonnull;
extern char * strncpy ( char *dest, const char *src, size_t max ) __nonnull;
extern char * strcat ( char *dest, const char *src ) __nonnull;
diff --git a/src/tests/string_test.c b/src/tests/string_test.c
index 3afb8de..c0436c3 100644
--- a/src/tests/string_test.c
+++ b/src/tests/string_test.c
@@ -204,6 +204,24 @@ static void string_test_exec ( void ) {
free ( dup );
}
+ /* Test stpcpy() */
+ {
+ const char longer[12] = "duplicateme";
+ const char shorter[6] = "hello";
+ char dest[12];
+ char *dnul;
+
+ dnul = stpcpy ( dest, longer );
+ ok ( *dnul == '\0' );
+ ok ( dnul == &dest[11] );
+ ok ( memcmp ( dest, longer, 12 ) == 0 );
+ dnul = stpcpy ( dest, shorter );
+ ok ( *dnul == '\0' );
+ ok ( dnul == &dest[5] );
+ ok ( memcmp ( dest, shorter, 6 ) == 0 );
+ ok ( memcmp ( ( dest + 6 ), ( longer + 6 ), 6 ) == 0 );
+ }
+
/* Test strcpy() */
{
const char longer[7] = "copyme";