diff options
author | Alejandro Colomar <alx@kernel.org> | 2023-12-04 00:03:23 +0100 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2023-12-04 00:37:01 +0100 |
commit | c68dc81c7a68f6ba83eb956ccaa4d89b31a31fd8 (patch) | |
tree | db2297db0c5e3e0e446500b8278ad33ed6e6298a | |
parent | 790795ec4e5eb5f841dfb946b30c43b7cde44086 (diff) |
string_copying.7: Reimplement stpecpy()
This implementation resembles more strtcpy(), so the differences are
more visible. Also, it has no -1's, so it's a bit easier to implement
correctly.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | man7/string_copying.7 | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/man7/string_copying.7 b/man7/string_copying.7 index d79812a6a..0b1562e4c 100644 --- a/man7/string_copying.7 +++ b/man7/string_copying.7 @@ -734,19 +734,20 @@ ssize_t char * .IR stpecpy "(char *dst, char end[0], const char *restrict src)" { - char *p; + bool trunc; + size_t dsize, dlen, slen; \& - if (dst == NULL) - return NULL; if (dst == end) return end; + if (dst == NULL) + return NULL; \& - p = memccpy(dst, src, \[aq]\e0\[aq], end \- dst); - if (p != NULL) - return p \- 1; + dsize = end \- dst; + slen = strnlen(src, dsize); + trunc = (slen == dsize); + dlen = slen \- trunc; \& - /* truncation detected */ - return stpcpy(end\-1, ""); + return stpcpy(mempcpy(dst, src, dlen), "") + trunc; } .\" ----- SEE ALSO :: -------------------------------------------------/ .SH SEE ALSO |