switch the "namespace" to "lce"
[libcext.git] / strlcat.h
index 78951553b437da2a0f9de589128b0b00db9dd887..f1a035d46245481962496912706c1e0adb406c5b 100644 (file)
--- a/strlcat.h
+++ b/strlcat.h
  */
 
 /* https://git.datadissipation.net */
  */
 
 /* https://git.datadissipation.net */
-#ifndef STRLCAT_H_
- #define STRLCAT_H_     1
+#ifndef LCE_STRLCAT_H
+ #define LCE_STRLCAT_H     1
 
 
- #ifndef STRLCAT_INCLUDE_LIBC
-  #include <stddef.h>
+ #ifdef LCE_STRLCAT_INCLUDE_LIBC
   #include <stdio.h>
   #include <string.h>
   #include <stdio.h>
   #include <string.h>
- #endif /* !STRLCAT_INCLUDE_LIBC */
+ #endif /* !LCE_STRLCAT_INCLUDE_LIBC */
 
  #ifndef HAVE_STRLCAT
   #define HAVE_STRLCAT  1
 
  #ifndef HAVE_STRLCAT
   #define HAVE_STRLCAT  1
-  #define strlcat       i_strlcat_
-  #define estrlcat      i_estrlcat_
-size_t i_strlcat_(char *dst, const char *src, size_t siz);
-size_t i_estrlcat_(char *dst, const char *src, size_t siz);
+  #define strlcat       lce_strlcat_
+  #define strlcpy       lce_strlcpy_
+size_t lce_strlcat_(char *dst, const char *src, size_t siz);
+size_t lce_strlcpy_(char *dst, const char *src, size_t siz);
  #endif /* !HAVE_STRLCAT */
 
  #endif /* !HAVE_STRLCAT */
 
- #ifdef STRLCAT_IMPLEMENTATION
+ #ifdef LCE_STRLCAT_IMPLEMENTATION
 /*
  * Appends src to string dst of size siz (unlike strncat, siz is the
  * full size of dst, not space left). At most siz-1 characters
 /*
  * Appends src to string dst of size siz (unlike strncat, siz is the
  * full size of dst, not space left). At most siz-1 characters
@@ -41,7 +40,7 @@ size_t i_estrlcat_(char *dst, const char *src, size_t siz);
  * If retval >= siz, truncation occurred.
  */
 size_t
  * If retval >= siz, truncation occurred.
  */
 size_t
-i_strlcat_(char *dst, const char *src, size_t siz)
+lce_strlcat_(char *dst, const char *src, size_t siz)
 {
        char *d = dst;
        const char *s = src;
 {
        char *d = dst;
        const char *s = src;
@@ -65,15 +64,32 @@ i_strlcat_(char *dst, const char *src, size_t siz)
        return (dlen + (s - src)); /* count does not include NUL */
 }
 
        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
 size_t
-i_estrlcat_(char *dst, const char *src, size_t siz)
+lce_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_ */
+ #endif /* LCE_STRLCAT_IMPLEMENTATION */
+#endif /* !LCE_STRLCAT_H */