-
Notifications
You must be signed in to change notification settings - Fork 0
/
strconcat.c
97 lines (72 loc) · 1.91 KB
/
strconcat.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* $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 /* I_STDARG */
#ifdef I_VARARGS
#include <varargs.h>
#endif /* I_VARARGS */
extern void outmem();
#ifdef I_STDARG
char *strconcat(const char *first, ...)
#else
char *strconcat(va_alist)
va_dcl
#endif /* I_STDARG */
{
size_t totallen;
va_list argp;
char *s = (char *)NULL,
*retstring = (char *)NULL;
#ifndef I_STDARG
char *first = (char *)NULL;
#endif /* I_STDARG */
/*-------------------------*/
/* add up the total length */
/*-------------------------*/
#ifdef I_STDARG
va_start(argp,first);
#else
va_start(argp);
first = va_arg(argp, char *);
#endif /* I_STDARG */
#ifdef DEBUG
(void)fprintf(stderr,"strconcat: \"%s\"",first);
#endif /* DEBUG */
totallen = strlen(first);
while ((s = va_arg(argp,char *)) != NULL)
{ totallen += strlen(s);
#ifdef DEBUG
(void)fprintf(stderr,",\"%s\"",s);
#endif /* DEBUG */
}
#ifdef DEBUG
(void)fprintf(stderr,"\nstrlen = %ld\n",(long)totallen);
#endif /* DEBUG */
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 /* I_STDARG */
/*---------------*/
/* copy stuff in */
/*---------------*/
(void)strcpy(retstring,first);
while ((s = va_arg(argp,char *)) != (char *)NULL)
(void)strcat(retstring,s);
va_end(argp);
#ifdef DEBUG
(void)fprintf(stderr,"strconcat returns \"%s\"\n",retstring);
#endif /* DEBUG */
return retstring;
}