1
0

string_catn.c 845 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * string_cat.c
  3. */
  4. #include "private.h"
  5. #include <string.h>
  6. #include <stdlib.h>
  7. /*--------------------------------------------------------- */
  8. void lub_string_catn(char **string, const char *text, size_t len)
  9. {
  10. if (text) {
  11. char *q;
  12. size_t length, initlen, textlen = strlen(text);
  13. /* make sure the client cannot give us duff details */
  14. len = (len < textlen) ? len : textlen;
  15. /* remember the size of the original string */
  16. initlen = *string ? strlen(*string) : 0;
  17. /* account for '\0' */
  18. length = initlen + len + 1;
  19. /* allocate the memory for the result */
  20. q = realloc(*string, length);
  21. if (NULL != q) {
  22. *string = q;
  23. /* move to the end of the initial string */
  24. q += initlen;
  25. while (len--) {
  26. *q++ = *text++;
  27. }
  28. *q = '\0';
  29. }
  30. }
  31. }
  32. /*--------------------------------------------------------- */