ii

My fork of https://tools.suckless.org/ii/
git clone https://git.inz.fi/ii
Log | Files | Refs | README | LICENSE

strlcpy.c (713B)


      1 /* Taken from OpenBSD */
      2 #include <sys/types.h>
      3 #include <string.h>
      4 
      5 /*
      6  * Copy src to string dst of size siz.  At most siz-1 characters
      7  * will be copied.  Always NUL terminates (unless siz == 0).
      8  * Returns strlen(src); if retval >= siz, truncation occurred.
      9  */
     10 size_t
     11 strlcpy(char *dst, const char *src, size_t siz)
     12 {
     13 	char *d = dst;
     14 	const char *s = src;
     15 	size_t n = siz;
     16 
     17 	/* Copy as many bytes as will fit */
     18 	if (n != 0) {
     19 		while (--n != 0) {
     20 			if ((*d++ = *s++) == '\0')
     21 				break;
     22 		}
     23 	}
     24 	/* Not enough room in dst, add NUL and traverse rest of src */
     25 	if (n == 0) {
     26 		if (siz != 0)
     27 			*d = '\0'; /* NUL-terminate dst */
     28 		while (*s++)
     29 			;
     30 	}
     31 	return(s - src - 1); /* count does not include NUL */
     32 }