str.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * zebra string function
  3. *
  4. * XXX This version of snprintf does not check bounds!
  5. */
  6. /*
  7. The implementations of strlcpy and strlcat are copied from rsync (GPL):
  8. Copyright (C) Andrew Tridgell 1998
  9. Copyright (C) 2002 by Martin Pool
  10. Note that these are not terribly efficient, since they make more than one
  11. pass over the argument strings. At some point, they should be optimized.
  12. The implementation of strndup is copied from glibc-2.3.5:
  13. Copyright (C) 1996, 1997, 1998, 2001, 2002 Free Software Foundation, Inc.
  14. */
  15. /*
  16. * This file is part of Quagga.
  17. *
  18. * Quagga is free software; you can redistribute it and/or modify it
  19. * under the terms of the GNU General Public License as published by the
  20. * Free Software Foundation; either version 2, or (at your option) any
  21. * later version.
  22. *
  23. * Quagga is distributed in the hope that it will be useful, but
  24. * WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with Quagga; see the file COPYING. If not, write to the Free
  30. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  31. * 02111-1307, USA.
  32. */
  33. #include <zebra.h>
  34. #ifndef HAVE_SNPRINTF
  35. /*
  36. * snprint() is a real basic wrapper around the standard sprintf()
  37. * without any bounds checking
  38. */
  39. int
  40. snprintf(char *str, size_t size, const char *format, ...)
  41. {
  42. va_list args;
  43. va_start (args, format);
  44. return vsprintf (str, format, args);
  45. }
  46. #endif
  47. #ifndef HAVE_STRLCPY
  48. /**
  49. * Like strncpy but does not 0 fill the buffer and always null
  50. * terminates.
  51. *
  52. * @param bufsize is the size of the destination buffer.
  53. *
  54. * @return index of the terminating byte.
  55. **/
  56. size_t
  57. strlcpy(char *d, const char *s, size_t bufsize)
  58. {
  59. size_t len = strlen(s);
  60. size_t ret = len;
  61. if (bufsize > 0) {
  62. if (len >= bufsize)
  63. len = bufsize-1;
  64. memcpy(d, s, len);
  65. d[len] = 0;
  66. }
  67. return ret;
  68. }
  69. #endif
  70. #ifndef HAVE_STRLCAT
  71. /**
  72. * Like strncat() but does not 0 fill the buffer and always null
  73. * terminates.
  74. *
  75. * @param bufsize length of the buffer, which should be one more than
  76. * the maximum resulting string length.
  77. **/
  78. size_t
  79. strlcat(char *d, const char *s, size_t bufsize)
  80. {
  81. size_t len1 = strlen(d);
  82. size_t len2 = strlen(s);
  83. size_t ret = len1 + len2;
  84. if (len1 < bufsize - 1) {
  85. if (len2 >= bufsize - len1)
  86. len2 = bufsize - len1 - 1;
  87. memcpy(d+len1, s, len2);
  88. d[len1+len2] = 0;
  89. }
  90. return ret;
  91. }
  92. #endif
  93. #ifndef HAVE_STRNLEN
  94. size_t
  95. strnlen(const char *s, size_t maxlen)
  96. {
  97. const char *p;
  98. return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen;
  99. }
  100. #endif
  101. #ifndef HAVE_STRNDUP
  102. char *
  103. strndup (const char *s, size_t maxlen)
  104. {
  105. size_t len = strnlen (s, maxlen);
  106. char *new = (char *) malloc (len + 1);
  107. if (new == NULL)
  108. return NULL;
  109. new[len] = '\0';
  110. return (char *) memcpy (new, s, len);
  111. }
  112. #endif