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