forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
/** @} */ |