PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/strlcpy.cxx

https://bitbucket.org/muegamma/rome3
C++ | 101 lines | 45 code | 13 blank | 43 comment | 23 complexity | 10c153a1995647a51a6ab2fd708bc2d7 MD5 | raw file
  1. /********************************************************************\
  2. Name: strlcpy.c
  3. Created by: Stefan Ritt
  4. Copyright 2000 + Stefan Ritt
  5. Contents: Contains strlcpy and strlcat which are versions of
  6. strcpy and strcat, but which avoid buffer overflows
  7. This file is part of MIDAS XML Library.
  8. MIDAS XML Library is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 2 of the License, or
  11. (at your option) any later version.
  12. MIDAS XML Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with MIDAS XML Library. If not, see <http://www.gnu.org/licenses/>.
  18. \********************************************************************/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "strlcpy.h"
  22. /*
  23. * Copy src to string dst of size siz. At most siz-1 characters
  24. * will be copied. Always NUL terminates (unless size == 0).
  25. * Returns strlen(src); if retval >= siz, truncation occurred.
  26. */
  27. #ifndef STRLCPY_DEFINED
  28. size_t strlcpy(char *dst, const char *src, size_t size)
  29. {
  30. char *d = dst;
  31. const char *s = src;
  32. size_t n = size;
  33. /* Copy as many bytes as will fit */
  34. if (n != 0 && --n != 0) {
  35. do {
  36. if ((*d++ = *s++) == 0)
  37. break;
  38. } while (--n != 0);
  39. }
  40. /* Not enough room in dst, add NUL and traverse rest of src */
  41. if (n == 0) {
  42. if (size != 0)
  43. *d = '\0'; /* NUL-terminate dst */
  44. while (*s++);
  45. }
  46. return (s - src - 1); /* count does not include NUL */
  47. }
  48. /*-------------------------------------------------------------------*/
  49. /*
  50. * Appends src to string dst of size siz (unlike strncat, siz is the
  51. * full size of dst, not space left). At most siz-1 characters
  52. * will be copied. Always NUL terminates (unless size <= strlen(dst)).
  53. * Returns strlen(src) + MIN(size, strlen(initial dst)).
  54. * If retval >= size, truncation occurred.
  55. */
  56. size_t strlcat(char *dst, const char *src, size_t size)
  57. {
  58. char *d = dst;
  59. const char *s = src;
  60. size_t n = size;
  61. size_t dlen;
  62. /* Find the end of dst and adjust bytes left but don't go past end */
  63. while (n-- != 0 && *d != '\0')
  64. d++;
  65. dlen = d - dst;
  66. n = size - dlen;
  67. if (n == 0)
  68. return (dlen + strlen(s));
  69. while (*s != '\0') {
  70. if (n != 1) {
  71. *d++ = *s;
  72. n--;
  73. }
  74. s++;
  75. }
  76. *d = '\0';
  77. return (dlen + (s - src)); /* count does not include NUL */
  78. }
  79. /*-------------------------------------------------------------------*/
  80. #endif // STRLCPY_DEFINED