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
65
66
67
68
69
70
71
72
73
74
|
/* $Id: strconcat.c,v 1.1 2004-05-03 05:17:48 behdad Exp $
* concatenate a list of strings, storing them in a malloc'ed region
*/
#include "c2man.h"
#include "strconcat.h"
#ifdef I_STDARG
#include <stdarg.h>
#endif
#ifdef I_VARARGS
#include <varargs.h>
#endif
extern void outmem();
#ifdef I_STDARG
char *strconcat(const char *first, ...)
#else
char *strconcat(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
#ifdef DEBUG
fprintf(stderr,"strconcat: \"%s\"",first);
#endif
totallen = strlen(first);
while ((s = va_arg(argp,char *)) != NULL)
{
totallen += strlen(s);
#ifdef DEBUG
fprintf(stderr,",\"%s\"",s);
#endif
}
#ifdef DEBUG
fprintf(stderr,"\nstrlen = %ld\n",(long)totallen);
#endif
va_end(argp);
/* malloc the memory */
if ((retstring = malloc(totallen + 1)) == 0)
outmem();
#ifdef I_STDARG
va_start(argp,first);
#else
va_start(argp);
first = va_arg(argp, char *);
#endif
/* copy the stuff in */
strcpy(retstring,first);
while ((s = va_arg(argp,char *)) != NULL)
strcat(retstring,s);
va_end(argp);
#ifdef DEBUG
fprintf(stderr,"strconcat returns \"%s\"\n",retstring);
#endif
return retstring;
}
|