strlcat.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #ifdef USE_STRLCAT_PHP_IMPL
  18. /* $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $ */
  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. #if defined(LIBC_SCCS) && !defined(lint)
  46. static const char *rcsid = "$OpenBSD: strlcat.c,v 1.17 2016/10/14 18:19:04 dtucker Exp $";
  47. #endif /* LIBC_SCCS and not lint */
  48. #include <sys/types.h>
  49. #include <string.h>
  50. /*
  51. * Appends src to string dst of size siz (unlike strncat, siz is the
  52. * full size of dst, not space left). At most siz-1 characters
  53. * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
  54. * Returns strlen(src) + MIN(siz, strlen(initial dst).
  55. * If retval >= siz, truncation occurred.
  56. */
  57. PHPAPI size_t php_strlcat(dst, src, siz)
  58. char *dst;
  59. const char *src;
  60. size_t siz;
  61. {
  62. const char *d = dst;
  63. const char *s = src;
  64. size_t n = siz;
  65. size_t dlen;
  66. /* Find the end of dst and adjust bytes left but don't go past end */
  67. while (n-- != 0 && *dst != '\0')
  68. dst++;
  69. dlen = dst - d;
  70. n = siz - dlen;
  71. if (n-- == 0)
  72. return(dlen + strlen(src));
  73. while (*src != '\0') {
  74. if (n != 0) {
  75. *dst++ = *src;
  76. n--;
  77. }
  78. src++;
  79. }
  80. *dst = '\0';
  81. return(dlen + (src - s));
  82. }
  83. #endif /* !HAVE_STRLCAT */