xref: /PHP-7.4/main/strlcat.c (revision 92ac598a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author:                                                              |
16   +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 
21 #ifdef USE_STRLCAT_PHP_IMPL
22 
23 /*     $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $    */
24 
25 /*
26  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  *    notice, this list of conditions and the following disclaimer in the
36  *    documentation and/or other materials provided with the distribution.
37  * 3. The name of the author may not be used to endorse or promote products
38  *    derived from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
42  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
43  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
46  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  */
51 
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char *rcsid = "$OpenBSD: strlcat.c,v 1.17 2016/10/14 18:19:04 dtucker Exp $";
54 #endif /* LIBC_SCCS and not lint */
55 
56 #include <sys/types.h>
57 #include <string.h>
58 
59 /*
60  * Appends src to string dst of size siz (unlike strncat, siz is the
61  * full size of dst, not space left).  At most siz-1 characters
62  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
63  * Returns strlen(src) + MIN(siz, strlen(initial dst).
64  * If retval >= siz, truncation occurred.
65  */
php_strlcat(dst,src,siz)66 PHPAPI size_t php_strlcat(dst, src, siz)
67 	char *dst;
68 	const char *src;
69 	size_t siz;
70 {
71 	const char *d = dst;
72 	const char *s = src;
73 	size_t n = siz;
74 	size_t dlen;
75 
76 	/* Find the end of dst and adjust bytes left but don't go past end */
77 	while (n-- != 0 && *dst != '\0')
78 		dst++;
79 	dlen = dst - d;
80 	n = siz - dlen;
81 
82 	if (n-- == 0)
83 		return(dlen + strlen(src));
84 	while (*src != '\0') {
85 		if (n != 0) {
86 			*dst++ = *src;
87 			n--;
88 		}
89 		src++;
90 	}
91 	*dst = '\0';
92 
93 	return(dlen + (src - s));
94 }
95 
96 #endif /* !HAVE_STRLCAT */
97