1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}
|