diff options
author | Willy Tarreau <w@1wt.eu> | 2022-02-07 19:23:42 +0300 |
---|---|---|
committer | Paul E. McKenney <paulmck@kernel.org> | 2022-04-21 03:05:44 +0300 |
commit | b312eb0b8711dbfbe2b45681926eb553e8ac8de3 (patch) | |
tree | b24cf7c9f79183c1a70ec3a7a44a970b1fc82f3c /tools/include/nolibc | |
parent | d76232ff8be662b8851e975d13d59cad4bf423d3 (diff) | |
download | linux-b312eb0b8711dbfbe2b45681926eb553e8ac8de3.tar.xz |
tools/nolibc/string: add strncpy() and strlcpy()
These are minimal variants. strncpy() always fills the destination for
<size> chars, while strlcpy() copies no more than <size> including the
zero and returns the source's length. The respective sizes on various
archs are:
strncpy(): x86:0x1f mips:0x30 arm:0x20
strlcpy(): x86:0x17 mips:0x34 arm:0x1a
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Diffstat (limited to 'tools/include/nolibc')
-rw-r--r-- | tools/include/nolibc/string.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index b831a02de83f..7c274efcdfae 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -122,6 +122,34 @@ size_t nolibc_strlen(const char *str) }) static __attribute__((unused)) +size_t strlcpy(char *dst, const char *src, size_t size) +{ + size_t len; + char c; + + for (len = 0;;) { + c = src[len]; + if (len < size) + dst[len] = c; + if (!c) + break; + len++; + } + return len; +} + +static __attribute__((unused)) +char *strncpy(char *dst, const char *src, size_t size) +{ + size_t len; + + for (len = 0; len < size; len++) + if ((dst[len] = *src)) + src++; + return dst; +} + +static __attribute__((unused)) char *strrchr(const char *s, int c) { const char *ret = NULL; |