-
Notifications
You must be signed in to change notification settings - Fork 1
/
strcatdup.c
72 lines (61 loc) · 1.26 KB
/
strcatdup.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
/**
* \file appstrcatdup.c
* \brief app_strcatdup function
*
* stolen from glib
*
* include LICENSE
*/
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <appmem.h>
#include <strcatdup.h>
#ifdef TRACE_MEM
#include <tracemem.h>
#endif
/*
* concatanate all arguments of a NULL terminated list
* and strdup the result
*/
char *app_strcatdup (const char *str, ...)
{
char *buffer, *cur;
const char *p;
unsigned int len = 0;
va_list args;
if ((p = str) == NULL) {
return NULL;
}
va_start (args, str);
while (p != NULL) {
len += strlen (p);
p = va_arg (args, const char *);
}
va_end (args);
cur = buffer = app_new(char, len + 1);
va_start (args, str);
p = str;
while (p != NULL) {
strcpy (cur, p); /* safe */
p = va_arg (args, const char *);
cur += strlen (cur);
}
va_end (args);
return buffer;
}
/*
* concatenate and dup <res> = <ptr> <sep> <str>
* ptr and str must be duped before.
* res should be freed after usage.
*/
char *app_strappend ( char *ptr, char *str, char *sep)
{
if ( ptr == NULL ) {
return str;
}
char *newptr = app_strcatdup( ptr, sep, str, NULL );
app_free( ptr );
app_free( str );
return newptr;
}