xref: /freebsd/contrib/libfido2/openbsd-compat/strlcpy.c (revision 0afa8e065e14bb8fd338d75690e0238c00167d40)
1*0afa8e06SEd Maste /*	$OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $	*/
2*0afa8e06SEd Maste 
3*0afa8e06SEd Maste /*
4*0afa8e06SEd Maste  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5*0afa8e06SEd Maste  *
6*0afa8e06SEd Maste  * Permission to use, copy, modify, and distribute this software for any
7*0afa8e06SEd Maste  * purpose with or without fee is hereby granted, provided that the above
8*0afa8e06SEd Maste  * copyright notice and this permission notice appear in all copies.
9*0afa8e06SEd Maste  *
10*0afa8e06SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*0afa8e06SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*0afa8e06SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*0afa8e06SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*0afa8e06SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*0afa8e06SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*0afa8e06SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*0afa8e06SEd Maste  */
18*0afa8e06SEd Maste 
19*0afa8e06SEd Maste /* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
20*0afa8e06SEd Maste 
21*0afa8e06SEd Maste #include "openbsd-compat.h"
22*0afa8e06SEd Maste 
23*0afa8e06SEd Maste #if !defined(HAVE_STRLCPY)
24*0afa8e06SEd Maste 
25*0afa8e06SEd Maste #include <sys/types.h>
26*0afa8e06SEd Maste #include <string.h>
27*0afa8e06SEd Maste 
28*0afa8e06SEd Maste /*
29*0afa8e06SEd Maste  * Copy src to string dst of size siz.  At most siz-1 characters
30*0afa8e06SEd Maste  * will be copied.  Always NUL terminates (unless siz == 0).
31*0afa8e06SEd Maste  * Returns strlen(src); if retval >= siz, truncation occurred.
32*0afa8e06SEd Maste  */
33*0afa8e06SEd Maste size_t
strlcpy(char * dst,const char * src,size_t siz)34*0afa8e06SEd Maste strlcpy(char *dst, const char *src, size_t siz)
35*0afa8e06SEd Maste {
36*0afa8e06SEd Maste 	char *d = dst;
37*0afa8e06SEd Maste 	const char *s = src;
38*0afa8e06SEd Maste 	size_t n = siz;
39*0afa8e06SEd Maste 
40*0afa8e06SEd Maste 	/* Copy as many bytes as will fit */
41*0afa8e06SEd Maste 	if (n != 0) {
42*0afa8e06SEd Maste 		while (--n != 0) {
43*0afa8e06SEd Maste 			if ((*d++ = *s++) == '\0')
44*0afa8e06SEd Maste 				break;
45*0afa8e06SEd Maste 		}
46*0afa8e06SEd Maste 	}
47*0afa8e06SEd Maste 
48*0afa8e06SEd Maste 	/* Not enough room in dst, add NUL and traverse rest of src */
49*0afa8e06SEd Maste 	if (n == 0) {
50*0afa8e06SEd Maste 		if (siz != 0)
51*0afa8e06SEd Maste 			*d = '\0';		/* NUL-terminate dst */
52*0afa8e06SEd Maste 		while (*s++)
53*0afa8e06SEd Maste 			;
54*0afa8e06SEd Maste 	}
55*0afa8e06SEd Maste 
56*0afa8e06SEd Maste 	return(s - src - 1);	/* count does not include NUL */
57*0afa8e06SEd Maste }
58*0afa8e06SEd Maste 
59*0afa8e06SEd Maste #endif /* !defined(HAVE_STRLCPY) */
60