string_catn.c 1.0 KB

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