summaryrefslogtreecommitdiff
path: root/inline.h
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2022-12-23 05:06:54 -0700
committerYves Orton <demerphq@gmail.com>2023-01-04 10:59:40 +0100
commita2887f2772e70a589141ab27c29a0b324260ce76 (patch)
treef9b105c2c2425d6621ca74840acd54ffc3511f5b /inline.h
parent01a4a6089e5123dac13f1d1eeae071fc6cec251b (diff)
downloadperl-a2887f2772e70a589141ab27c29a0b324260ce76.tar.gz
Revert "Revert "Inline strlcat(), strlcpy()""
This reverts commit 81620fbedd5f5cb87f240d7809dc669cd60d0139. The original commit was wrongly blamed for breakage, when it was the harness parallelism that was at fault; since fixed, mostly.
Diffstat (limited to 'inline.h')
-rw-r--r--inline.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/inline.h b/inline.h
index c72957a97b..38de751bd6 100644
--- a/inline.h
+++ b/inline.h
@@ -3625,5 +3625,79 @@ Perl_savesharedsvpv(pTHX_ SV *sv)
}
/*
+=for apidoc my_strlcat
+
+The C library C<strlcat> if available, or a Perl implementation of it.
+This operates on C C<NUL>-terminated strings.
+
+C<my_strlcat()> appends string C<src> to the end of C<dst>. It will append at
+most S<C<size - strlen(dst) - 1>> characters. It will then C<NUL>-terminate,
+unless C<size> is 0 or the original C<dst> string was longer than C<size> (in
+practice this should not happen as it means that either C<size> is incorrect or
+that C<dst> is not a proper C<NUL>-terminated string).
+
+Note that C<size> is the full size of the destination buffer and
+the result is guaranteed to be C<NUL>-terminated if there is room. Note that
+room for the C<NUL> should be included in C<size>.
+
+The return value is the total length that C<dst> would have if C<size> is
+sufficiently large. Thus it is the initial length of C<dst> plus the length of
+C<src>. If C<size> is smaller than the return, the excess was not appended.
+
+=cut
+
+Description stolen from http://man.openbsd.org/strlcat.3
+*/
+#ifndef HAS_STRLCAT
+PERL_STATIC_INLINE Size_t
+Perl_my_strlcat(char *dst, const char *src, Size_t size)
+{
+ Size_t used, length, copy;
+
+ used = strlen(dst);
+ length = strlen(src);
+ if (size > 0 && used < size - 1) {
+ copy = (length >= size - used) ? size - used - 1 : length;
+ memcpy(dst + used, src, copy);
+ dst[used + copy] = '\0';
+ }
+ return used + length;
+}
+#endif
+
+
+/*
+=for apidoc my_strlcpy
+
+The C library C<strlcpy> if available, or a Perl implementation of it.
+This operates on C C<NUL>-terminated strings.
+
+C<my_strlcpy()> copies up to S<C<size - 1>> characters from the string C<src>
+to C<dst>, C<NUL>-terminating the result if C<size> is not 0.
+
+The return value is the total length C<src> would be if the copy completely
+succeeded. If it is larger than C<size>, the excess was not copied.
+
+=cut
+
+Description stolen from http://man.openbsd.org/strlcpy.3
+*/
+#ifndef HAS_STRLCPY
+PERL_STATIC_INLINE Size_t
+Perl_my_strlcpy(char *dst, const char *src, Size_t size)
+{
+ Size_t length, copy;
+
+ length = strlen(src);
+ if (size > 0) {
+ copy = (length >= size) ? size - 1 : length;
+ memcpy(dst, src, copy);
+ dst[copy] = '\0';
+ }
+ return length;
+}
+#endif
+
+/*
* ex: set ts=8 sts=4 sw=4 et:
*/