PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext_zend_compat/php-src/main/strlcpy.cpp

http://github.com/facebook/hiphop-php
C++ | 93 lines | 29 code | 12 blank | 52 comment | 14 complexity | f80e88d485c85b1052ba24f0fa69fd8e MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 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. /* $Id$ */
  19. #include "php.h"
  20. #ifndef HAVE_STRLCPY
  21. /* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */
  22. /*
  23. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. * 1. Redistributions of source code must retain the above copyright
  30. * notice, this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright
  32. * notice, this list of conditions and the following disclaimer in the
  33. * documentation and/or other materials provided with the distribution.
  34. * 3. The name of the author may not be used to endorse or promote products
  35. * derived from this software without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  38. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  39. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  40. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  41. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  42. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  43. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  44. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  45. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  46. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. #if defined(LIBC_SCCS) && !defined(lint)
  49. static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
  50. #endif /* LIBC_SCCS and not lint */
  51. #include <sys/types.h>
  52. #include <string.h>
  53. /*
  54. * Copy src to string dst of size siz. At most siz-1 characters
  55. * will be copied. Always NUL terminates (unless siz == 0).
  56. * Returns strlen(src); if retval >= siz, truncation occurred.
  57. */
  58. PHPAPI size_t php_strlcpy(
  59. char *dst,
  60. const char *src,
  61. size_t siz) {
  62. register char *d = dst;
  63. register const char *s = src;
  64. register size_t n = siz;
  65. /* Copy as many bytes as will fit */
  66. if (n != 0 && --n != 0) {
  67. do {
  68. if ((*d++ = *s++) == 0)
  69. break;
  70. } while (--n != 0);
  71. }
  72. /* Not enough room in dst, add NUL and traverse rest of src */
  73. if (n == 0) {
  74. if (siz != 0)
  75. *d = '\0'; /* NUL-terminate dst */
  76. while (*s++)
  77. ;
  78. }
  79. return(s - src - 1); /* count does not include NUL */
  80. }
  81. #endif /* !HAVE_STRLCPY */