diff --git a/sys/Kconfig b/sys/Kconfig index 4a6c84c21e..73354e3f5a 100644 --- a/sys/Kconfig +++ b/sys/Kconfig @@ -37,6 +37,7 @@ rsource "fs/Kconfig" rsource "hashes/Kconfig" rsource "iolist/Kconfig" rsource "isrpipe/Kconfig" +rsource "libc/Kconfig" menu "Libc" diff --git a/sys/include/string_utils.h b/sys/include/string_utils.h index eb0ec8641a..50f3db8a47 100644 --- a/sys/include/string_utils.h +++ b/sys/include/string_utils.h @@ -22,11 +22,13 @@ * @author Marian Buschsieweke */ +#include +#include /* if explicit_bzero() is provided by standard C lib, it may be defined in * either `string.h` or `strings.h`, so just include both here */ -#include #include #include +#include #include "kernel_defines.h" @@ -70,6 +72,26 @@ static inline void explicit_bzero(void *dest, size_t n_bytes) } #endif +/** + * @brief Copy the string, or as much of it as fits, into the dest buffer. + * + * Preferred to `strncpy` since it always returns a valid string, and doesn't + * unnecessarily force the tail of the destination buffer to be zeroed. + * If the zeroing is desired, it's likely cleaner to use `strscpy` with an + * overflow test, then just memset the tail of the dest buffer. + * + * @param[out] dest Where to copy the string to + * @param[in] src Where to copy the string from + * @param[in] count Size of destination buffer + * + * @pre The destination buffer is at least one byte large, as + * otherwise the terminating zero byte won't fit + * + * @return the number of characters copied (not including the trailing zero) + * @retval -E2BIG the destination buffer wasn't big enough + */ +ssize_t strscpy(char *dest, const char *src, size_t count); + #ifdef __cplusplus } #endif diff --git a/sys/libc/Kconfig b/sys/libc/Kconfig new file mode 100644 index 0000000000..eaa4895518 --- /dev/null +++ b/sys/libc/Kconfig @@ -0,0 +1,11 @@ +# Copyright (c) 2021 HAW Hamburg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. +# + +config MODULE_LIBC + bool "C Library helper functions" + default y + depends on TEST_KCONFIG diff --git a/sys/libc/Makefile b/sys/libc/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/sys/libc/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/libc/string.c b/sys/libc/string.c new file mode 100644 index 0000000000..c3c1cc661a --- /dev/null +++ b/sys/libc/string.c @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + * + * @author Benjamin Valentin + */ + +#include +#include "string_utils.h" + +ssize_t strscpy(char *dest, const char *src, size_t count) +{ + const char *start = dest; + + if (!count) { + return -E2BIG; + } + + while (--count && *src) { + *dest++ = *src++; + } + + *dest = 0; + + if (*src == 0) { + return dest - start; + } else { + return -E2BIG; + } +} +/** @} */