Skip to content

Commit

Permalink
sys/string_utils: add strscpy()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 26, 2022
1 parent 6db9960 commit cdaf715
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions sys/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rsource "fs/Kconfig"
rsource "hashes/Kconfig"
rsource "iolist/Kconfig"
rsource "isrpipe/Kconfig"
rsource "libc/Kconfig"

menu "Libc"

Expand Down
24 changes: 23 additions & 1 deletion sys/include/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
* @author Marian Buschsieweke <[email protected]>
*/

#include <errno.h>
#include <stdint.h>
/* 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 <stdint.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>

#include "kernel_defines.h"

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions sys/libc/Kconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions sys/libc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
40 changes: 40 additions & 0 deletions sys/libc/string.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#include <errno.h>
#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;
}
}
/** @} */

0 comments on commit cdaf715

Please sign in to comment.