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