add strlcpy, remove estrlcat
authorgit <redacted>
Sun, 26 Apr 2026 14:58:53 +0000 (10:58 -0400)
committergit <redacted>
Sun, 26 Apr 2026 14:58:53 +0000 (10:58 -0400)
README.md
strlcat.h

index 762d93f9480be66c2ebaeed8b9723b1988440cab..a5126fda69da03a2caf422fd3b627bee7f6e52cc 100644 (file)
--- a/README.md
+++ b/README.md
@@ -56,6 +56,6 @@ permuting, defining those enables it per function (GNU behaviour, can still be d
 
 ### strlcat
 
-includes `strlcat()` and `estrlcat()`
+includes `strlcat()` and `strlcpy()`
 
 ---
index 78951553b437da2a0f9de589128b0b00db9dd887..1235ae32000e0f67b16d423de5ec4f264a22b735 100644 (file)
--- a/strlcat.h
+++ b/strlcat.h
@@ -18,7 +18,7 @@
 #ifndef STRLCAT_H_
  #define STRLCAT_H_     1
 
- #ifndef STRLCAT_INCLUDE_LIBC
+ #ifdef STRLCAT_INCLUDE_LIBC
   #include <stddef.h>
   #include <stdio.h>
   #include <string.h>
@@ -27,9 +27,9 @@
  #ifndef HAVE_STRLCAT
   #define HAVE_STRLCAT  1
   #define strlcat       i_strlcat_
-  #define estrlcat      i_estrlcat_
+  #define strlcpy       i_strlcpy_
 size_t i_strlcat_(char *dst, const char *src, size_t siz);
-size_t i_estrlcat_(char *dst, const char *src, size_t siz);
+size_t i_strlcpy_(char *dst, const char *src, size_t siz);
  #endif /* !HAVE_STRLCAT */
 
  #ifdef STRLCAT_IMPLEMENTATION
@@ -65,15 +65,32 @@ i_strlcat_(char *dst, const char *src, size_t siz)
        return (dlen + (s - src)); /* count does not include NUL */
 }
 
+/*
+ * Copy src to string dst of size siz. At most siz-1 characters
+ * will be copied. Always NUL terminates (unless siz == 0).
+ * Returns strlen(src); if retval >= siz, truncation occurred.
+ */
 size_t
-i_estrlcat_(char *dst, const char *src, size_t siz)
+i_strlcpy_(char *dst, const char *src, size_t siz)
 {
-       size_t ret;
-
-       if ((ret = i_strlcat_(dst, src, siz)) >= siz)
-               fprintf(stderr, "strlcat: input string too long\n");
-
-       return ret;
+       char *d = dst;
+       const char *s = src;
+       size_t n = siz;
+       /* Copy as many bytes as will fit */
+       if (n != 0) {
+               while (--n != 0) {
+                       if ((*d++ = *s++) == '\0')
+                               break;
+               }
+       }
+       /* Not enough room in dst, add NUL and traverse rest of src */
+       if (n == 0) {
+               if (siz != 0)
+                       *d = '\0'; /* NUL-terminate dst */
+               while (*s++)
+                       ;
+       }
+       return(s - src - 1); /* count does not include NUL */
 }
  #endif /* STRLCAT_IMPLEMENTATION */
 #endif /* !STRLCAT_H_ */