strlcpy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #include "php.h"
  19. #ifdef USE_STRLCPY_PHP_IMPL
  20. /* $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $ */
  21. /*
  22. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  23. * All rights reserved.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. * 3. The name of the author may not be used to endorse or promote products
  34. * derived from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  38. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  39. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  40. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  41. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  42. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  43. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  44. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. #if defined(LIBC_SCCS) && !defined(lint)
  48. static char *rcsid = "$OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $";
  49. #endif /* LIBC_SCCS and not lint */
  50. #include <sys/types.h>
  51. #include <string.h>
  52. /*
  53. * Copy src to string dst of size siz. At most siz-1 characters
  54. * will be copied. Always NUL terminates (unless siz == 0).
  55. * Returns strlen(src); if retval >= siz, truncation occurred.
  56. */
  57. PHPAPI size_t php_strlcpy(dst, src, siz)
  58. char *dst;
  59. const char *src;
  60. size_t siz;
  61. {
  62. const char *s = src;
  63. size_t n = siz;
  64. /* Copy as many bytes as will fit */
  65. if (n != 0) {
  66. while (--n != 0) {
  67. if ((*dst++ = *src++) == 0)
  68. break;
  69. }
  70. }
  71. /* Not enough room in dst, add NUL and traverse rest of src */
  72. if (n == 0) {
  73. if (siz != 0)
  74. *dst = '\0'; /* NUL-terminate dst */
  75. while (*src++)
  76. ;
  77. }
  78. /*
  79. * Cast pointers to unsigned type before calculation, to avoid signed
  80. * overflow when the string ends where the MSB has changed.
  81. * Return value does not include NUL.
  82. */
  83. return((uintptr_t)src - (uintptr_t)s - 1);
  84. }
  85. #endif /* !HAVE_STRLCPY */
  86. /*
  87. * Local variables:
  88. * tab-width: 4
  89. * c-basic-offset: 4
  90. * End:
  91. * vim600: sw=4 ts=4 fdm=marker
  92. * vim<600: sw=4 ts=4
  93. */