lt__strl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* lt__strl.c -- size-bounded string copying and concatenation
  2. Copyright (C) 2004, 2011-2015 Free Software Foundation, Inc.
  3. Written by Bob Friesenhahn, 2004
  4. NOTE: The canonical source of this file is maintained with the
  5. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  6. GNU Libltdl is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. As a special exception to the GNU Lesser General Public License,
  11. if you distribute this file as part of a program or library that
  12. is built using GNU Libtool, you may include this file under the
  13. same distribution terms that you use for the rest of that program.
  14. GNU Libltdl is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  20. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  21. or obtained by writing to the Free Software Foundation, Inc.,
  22. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. */
  24. #include <assert.h>
  25. #include <string.h>
  26. #include "lt__strl.h"
  27. /*
  28. lt_strlcat appends the NULL-terminated string src to the end of dst.
  29. It will append at most dstsize - strlen(dst) - 1 bytes,
  30. NULL-terminating the result. The total length of the string that
  31. would have been created given sufficient buffer size (may be longer
  32. than dstsize) is returned. This function substitutes for strlcat(),
  33. which is available under NetBSD, FreeBSD and Solaris 9.
  34. Buffer overflow can be checked as follows:
  35. if (lt_strlcat(dst, src, dstsize) >= dstsize)
  36. return -1;
  37. */
  38. #if !defined HAVE_STRLCAT
  39. size_t
  40. lt_strlcat(char *dst, const char *src, const size_t dstsize)
  41. {
  42. size_t length;
  43. char *p;
  44. const char *q;
  45. assert(dst != NULL);
  46. assert(src != (const char *) NULL);
  47. assert(dstsize >= 1);
  48. length=strlen(dst);
  49. /*
  50. Copy remaining characters from src while constraining length to
  51. size - 1.
  52. */
  53. for ( p = dst + length, q = src;
  54. (*q != 0) && (length < dstsize - 1);
  55. length++, p++, q++ )
  56. *p = *q;
  57. dst[length]='\0';
  58. /*
  59. Add remaining length of src to length.
  60. */
  61. while (*q++)
  62. length++;
  63. return length;
  64. }
  65. #endif /* !defined HAVE_STRLCAT */
  66. /*
  67. lt_strlcpy copies up to dstsize - 1 characters from the NULL-terminated
  68. string src to dst, NULL-terminating the result. The total length of
  69. the string that would have been created given sufficient buffer
  70. size (may be longer than dstsize) is returned. This function
  71. substitutes for strlcpy(), which is available under OpenBSD, FreeBSD
  72. and Solaris 9.
  73. Buffer overflow can be checked as follows:
  74. if (lt_strlcpy(dst, src, dstsize) >= dstsize)
  75. return -1;
  76. */
  77. #if !defined HAVE_STRLCPY
  78. size_t
  79. lt_strlcpy(char *dst, const char *src, const size_t dstsize)
  80. {
  81. size_t length=0;
  82. char *p;
  83. const char *q;
  84. assert(dst != NULL);
  85. assert(src != (const char *) NULL);
  86. assert(dstsize >= 1);
  87. /*
  88. Copy src to dst within bounds of size-1.
  89. */
  90. for ( p=dst, q=src, length=0;
  91. (*q != 0) && (length < dstsize-1);
  92. length++, p++, q++ )
  93. *p = *q;
  94. dst[length]='\0';
  95. /*
  96. Add remaining length of src to length.
  97. */
  98. while (*q++)
  99. length++;
  100. return length;
  101. }
  102. #endif /* !defined HAVE_STRLCPY */