xref: /optee_os/lib/libutils/ext/strlcpy.c (revision b01047730e77127c23a36591643eeb8bb0487d68)
1*b0104773SPascal Brand /* This file is copied from newlib-1.19 */
2*b0104773SPascal Brand 
3*b0104773SPascal Brand /*      $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $        */
4*b0104773SPascal Brand 
5*b0104773SPascal Brand /*
6*b0104773SPascal Brand  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
7*b0104773SPascal Brand  * All rights reserved.
8*b0104773SPascal Brand  *
9*b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
10*b0104773SPascal Brand  * modification, are permitted provided that the following conditions
11*b0104773SPascal Brand  * are met:
12*b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright
13*b0104773SPascal Brand  *    notice, this list of conditions and the following disclaimer.
14*b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright
15*b0104773SPascal Brand  *    notice, this list of conditions and the following disclaimer in the
16*b0104773SPascal Brand  *    documentation and/or other materials provided with the distribution.
17*b0104773SPascal Brand  * 3. The name of the author may not be used to endorse or promote products
18*b0104773SPascal Brand  *    derived from this software without specific prior written permission.
19*b0104773SPascal Brand  *
20*b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21*b0104773SPascal Brand  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22*b0104773SPascal Brand  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23*b0104773SPascal Brand  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24*b0104773SPascal Brand  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25*b0104773SPascal Brand  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26*b0104773SPascal Brand  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27*b0104773SPascal Brand  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28*b0104773SPascal Brand  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29*b0104773SPascal Brand  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*b0104773SPascal Brand  */
31*b0104773SPascal Brand 
32*b0104773SPascal Brand #if defined(LIBC_SCCS) && !defined(lint)
33*b0104773SPascal Brand static char *rcsid =
34*b0104773SPascal Brand 	"$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
35*b0104773SPascal Brand #endif /* LIBC_SCCS and not lint */
36*b0104773SPascal Brand 
37*b0104773SPascal Brand #include <sys/types.h>
38*b0104773SPascal Brand #include <string_ext.h>
39*b0104773SPascal Brand 
40*b0104773SPascal Brand /*
41*b0104773SPascal Brand  * Copy src to string dst of size siz.  At most siz-1 characters
42*b0104773SPascal Brand  * will be copied.  Always NUL terminates (unless siz == 0).
43*b0104773SPascal Brand  * Returns strlen(src); if retval >= siz, truncation occurred.
44*b0104773SPascal Brand  */
45*b0104773SPascal Brand size_t strlcpy(char *dst, const char *src, size_t siz)
46*b0104773SPascal Brand {
47*b0104773SPascal Brand 	register char *d = dst;
48*b0104773SPascal Brand 	register const char *s = src;
49*b0104773SPascal Brand 	register size_t n = siz;
50*b0104773SPascal Brand 
51*b0104773SPascal Brand 	/* Copy as many bytes as will fit */
52*b0104773SPascal Brand 	if (n != 0 && --n != 0) {
53*b0104773SPascal Brand 		do {
54*b0104773SPascal Brand 			if ((*d++ = *s++) == 0)
55*b0104773SPascal Brand 				break;
56*b0104773SPascal Brand 		} while (--n != 0);
57*b0104773SPascal Brand 	}
58*b0104773SPascal Brand 
59*b0104773SPascal Brand 	/* Not enough room in dst, add NUL and traverse rest of src */
60*b0104773SPascal Brand 	if (n == 0) {
61*b0104773SPascal Brand 		if (siz != 0)
62*b0104773SPascal Brand 			*d = '\0';	/* NUL-terminate dst */
63*b0104773SPascal Brand 		while (*s++)
64*b0104773SPascal Brand 			;
65*b0104773SPascal Brand 	}
66*b0104773SPascal Brand 
67*b0104773SPascal Brand 	return s - src - 1;	/* count does not include NUL */
68*b0104773SPascal Brand }
69