From c5ae1a41da5dc76b9bd355b2bddf46c6e7e4c0ef Mon Sep 17 00:00:00 2001 From: Sven Schnelle Date: Wed, 20 Oct 2021 08:48:21 +0200 Subject: [PATCH] zipl/boot: add strlcpy implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add strlcpy from libutil to the zipl boot mini libc. Signed-off-by: Sven Schnelle Reviewed-by: Stefan Haberland Signed-off-by: Jan Höppner --- zipl/boot/libc.c | 27 +++++++++++++++++++++++++++ zipl/boot/libc.h | 1 + 2 files changed, 28 insertions(+) diff --git a/zipl/boot/libc.c b/zipl/boot/libc.c index f4ddbd46..7c0a476b 100644 --- a/zipl/boot/libc.c +++ b/zipl/boot/libc.c @@ -97,6 +97,33 @@ char *strcpy(char *dest, const char *src) return dest; } +/** + * Copy \a src to buffer \a dest of size \a size. At most size - 1 + * chars will be copied. \a dest will always be NUL terminated. + * + * Note: If the return value is greater than or equal to size truncation + * occurred. + * + * @param[in] dest Destination buffer + * @param[in] src Source string + * @param[in] size Size of destination buffer + * + * @returns strlen Length of \a src string + */ +size_t strlcpy(char *dest, const char *src, size_t size) +{ + size_t str_len = strlen(src); + size_t len; + + if (size) { + len = MIN(size - 1, str_len); + memcpy(dest, src, len); + dest[len] = '\0'; + } + + return str_len; +} + /* * Return string length */ diff --git a/zipl/boot/libc.h b/zipl/boot/libc.h index 1125fec3..9cf3b485 100644 --- a/zipl/boot/libc.h +++ b/zipl/boot/libc.h @@ -54,6 +54,7 @@ char *strcat(char *, const char *); int strncmp(const char *, const char *, unsigned long); int strlen(const char *); char *strcpy(char *, const char *); +size_t strlcpy(char *dest, const char *src, size_t size); char *strchr(const char *str, char c); unsigned int strhash(unsigned char *str, unsigned int hash_size); unsigned long get_zeroed_page(void);