summaryrefslogtreecommitdiff
path: root/lib/string.c
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2021-04-13 09:50:45 -0400
committerTom Rini <trini@konsulko.com>2021-04-13 09:50:45 -0400
commita94ab561e2f49a80d8579930e840b810ab1a1330 (patch)
tree77913e7bd9309afa6b2ddc6f3e3e49827da2025c /lib/string.c
parent3b676a1662ac6b54d1e97ea40a0c41ee0925ffe3 (diff)
parent8c4e3b79bd0bb76eea16869e9666e19047c0d005 (diff)
Merge branch '2021-04-13-assorted-improvements'
- A large assortment of bug fixes, code cleanups and a few feature enhancements.
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c31
1 files changed, 27 insertions, 4 deletions
diff --git a/lib/string.c b/lib/string.c
index 73b984123dc..a0cff8fe88e 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -114,17 +114,21 @@ char * strncpy(char * dest,const char *src,size_t count)
* NUL-terminated string that fits in the buffer (unless,
* of course, the buffer size is zero). It does not pad
* out the result like strncpy() does.
+ *
+ * Return: the number of bytes copied
*/
size_t strlcpy(char *dest, const char *src, size_t size)
{
- size_t ret = strlen(src);
-
if (size) {
- size_t len = (ret >= size) ? size - 1 : ret;
+ size_t srclen = strlen(src);
+ size_t len = (srclen >= size) ? size - 1 : srclen;
+
memcpy(dest, src, len);
dest[len] = '\0';
+ return len + 1;
}
- return ret;
+
+ return 0;
}
#endif
@@ -176,6 +180,25 @@ char * strncat(char *dest, const char *src, size_t count)
}
#endif
+#ifndef __HAVE_ARCH_STRLCAT
+/**
+ * strlcat - Append a length-limited, %NUL-terminated string to another
+ * @dest: The string to be appended to
+ * @src: The string to append to it
+ * @size: The size of @dest
+ *
+ * Compatible with *BSD: the result is always a valid NUL-terminated string that
+ * fits in the buffer (unless, of course, the buffer size is zero). It does not
+ * write past @size like strncat() does.
+ */
+size_t strlcat(char *dest, const char *src, size_t size)
+{
+ size_t len = strnlen(dest, size);
+
+ return len + strlcpy(dest + len, src, size - len);
+}
+#endif
+
#ifndef __HAVE_ARCH_STRCMP
/**
* strcmp - Compare two strings