summaryrefslogtreecommitdiff
path: root/strappend.c
diff options
context:
space:
mode:
authorbehdad <behdad>2004-05-03 05:17:48 +0000
committerbehdad <behdad>2004-05-03 05:17:48 +0000
commit577ed4095383ef5284225d45709e6b5f0598a064 (patch)
tree6c7d0ce55124a688b4d7050e684d9d7a1e3aa71d /strappend.c
Initial revisionHEADoriginmaster
Diffstat (limited to 'strappend.c')
-rw-r--r--strappend.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/strappend.c b/strappend.c
new file mode 100644
index 0000000..c8cc997
--- /dev/null
+++ b/strappend.c
@@ -0,0 +1,64 @@
+/* $Id: strappend.c,v 1.1 2004-05-03 05:17:48 behdad Exp $
+ */
+#include "c2man.h"
+#include "strappend.h"
+
+#ifdef I_STDARG
+#include <stdarg.h>
+#endif
+#ifdef I_VARARGS
+#include <varargs.h>
+#endif
+
+extern void outmem();
+
+/*
+ * append a list of strings to another, storing them in a malloc'ed region.
+ * The first string may be NULL, in which case the rest are simply concatenated.
+ */
+#ifdef I_STDARG
+char *strappend(char *first, ...)
+#else
+char *strappend(va_alist)
+ va_dcl
+#endif
+{
+ size_t totallen;
+ va_list argp;
+ char *s, *retstring;
+#ifndef I_STDARG
+ char *first;
+#endif
+ /* add up the total length */
+#ifdef I_STDARG
+ va_start(argp,first);
+#else
+ va_start(argp);
+ first = va_arg(argp, char *);
+#endif
+ totallen = first ? strlen(first) : 0;
+ while ((s = va_arg(argp,char *)) != NULL)
+ totallen += strlen(s);
+ va_end(argp);
+
+ /* malloc the memory */
+ totallen++; /* add space for the nul terminator */
+ if ((retstring = first ? realloc(first,totallen) : malloc(totallen)) == 0)
+ outmem();
+
+ if (first == NULL) *retstring = '\0';
+
+#ifdef I_STDARG
+ va_start(argp,first);
+#else
+ va_start(argp);
+ first = va_arg(argp, char *); /* skip the first arg */
+#endif
+
+ while ((s = va_arg(argp,char *)) != NULL)
+ strcat(retstring,s);
+
+ va_end(argp);
+
+ return retstring;
+}