dupcat.c 898 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Implementation function behind dupcat() in misc.h.
  3. *
  4. * This function is called with an arbitrary number of 'const char *'
  5. * parameters, of which the last one is a null pointer. The wrapper
  6. * macro puts on the null pointer itself, so normally callers don't
  7. * have to.
  8. */
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include "defs.h"
  12. #include "misc.h"
  13. char *dupcat_fn(const char *s1, ...)
  14. {
  15. int len;
  16. char *p, *q, *sn;
  17. va_list ap;
  18. len = strlen(s1);
  19. va_start(ap, s1);
  20. while (1) {
  21. sn = va_arg(ap, char *);
  22. if (!sn)
  23. break;
  24. len += strlen(sn);
  25. }
  26. va_end(ap);
  27. p = snewn(len + 1, char);
  28. strcpy(p, s1);
  29. q = p + strlen(p);
  30. va_start(ap, s1);
  31. while (1) {
  32. sn = va_arg(ap, char *);
  33. if (!sn)
  34. break;
  35. strcpy(q, sn);
  36. q += strlen(q);
  37. }
  38. va_end(ap);
  39. return p;
  40. }