summaryrefslogtreecommitdiff
path: root/userland/libc/string/strlcpy.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-22 19:50:38 +0200
committerAnton Kling <anton@kling.gg>2023-10-22 19:50:38 +0200
commit4e09bca9e34c226b6d7e34b4fa11248405fd988e (patch)
tree80f156b7940d9d19971395f335530170c69516c7 /userland/libc/string/strlcpy.c
Move everything into a new repo.
Diffstat (limited to 'userland/libc/string/strlcpy.c')
-rw-r--r--userland/libc/string/strlcpy.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/userland/libc/string/strlcpy.c b/userland/libc/string/strlcpy.c
new file mode 100644
index 0000000..a2d3dd9
--- /dev/null
+++ b/userland/libc/string/strlcpy.c
@@ -0,0 +1,20 @@
+#include <string.h>
+
+// Copy string s2 to buffer s1 of size n. At most n-1
+// chars will be copied. Always NUL terminates (unless n == 0).
+// Returns strlen(s2); if retval >= n, truncation occurred.
+size_t *strlcpy(char *s1, const char *s2, size_t n) {
+ size_t tmp_n = n;
+ const char *os2 = s2;
+ for (; tmp_n; tmp_n--) {
+ if ((*s1++ = *s2++) == '\0')
+ break;
+ }
+ if (tmp_n == 0) {
+ if (n != 0)
+ *s1 = '\0'; /* NUL-terminate s1 */
+ while (*s2++)
+ ;
+ }
+ return s2 - os2 - 1;
+}