PageRenderTime 23ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/strlcpy.c

https://bitbucket.org/muegamma/rome3
C | 106 lines | 49 code | 14 blank | 43 comment | 23 complexity | a3016b0aa23f66e90aafe465e9d3779d 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 3 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. #ifndef strlcpy
  29. size_t strlcpy(char *dst, const char *src, size_t size)
  30. {
  31. char *d = dst;
  32. const char *s = src;
  33. size_t n = size;
  34. /* Copy as many bytes as will fit */
  35. if (n != 0 && --n != 0) {
  36. do {
  37. if ((*d++ = *s++) == 0)
  38. break;
  39. } while (--n != 0);
  40. }
  41. /* Not enough room in dst, add NUL and traverse rest of src */
  42. if (n == 0) {
  43. if (size != 0)
  44. *d = '\0'; /* NUL-terminate dst */
  45. while (*s++);
  46. }
  47. return (s - src - 1); /* count does not include NUL */
  48. }
  49. #endif
  50. /*-------------------------------------------------------------------*/
  51. /*
  52. * Appends src to string dst of size siz (unlike strncat, siz is the
  53. * full size of dst, not space left). At most siz-1 characters
  54. * will be copied. Always NUL terminates (unless size <= strlen(dst)).
  55. * Returns strlen(src) + MIN(size, strlen(initial dst)).
  56. * If retval >= size, truncation occurred.
  57. */
  58. #ifndef strlcat
  59. size_t strlcat(char *dst, const char *src, size_t size)
  60. {
  61. char *d = dst;
  62. const char *s = src;
  63. size_t n = size;
  64. size_t dlen;
  65. /* Find the end of dst and adjust bytes left but don't go past end */
  66. while (n-- != 0 && *d != '\0')
  67. d++;
  68. dlen = d - dst;
  69. n = size - dlen;
  70. if (n == 0)
  71. return (dlen + strlen(s));
  72. while (*s != '\0') {
  73. if (n != 1) {
  74. *d++ = *s;
  75. n--;
  76. }
  77. s++;
  78. }
  79. *d = '\0';
  80. return (dlen + (s - src)); /* count does not include NUL */
  81. }
  82. /*-------------------------------------------------------------------*/
  83. #endif
  84. #endif // STRLCPY_DEFINED