-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatdup.c
182 lines (147 loc) · 3.51 KB
/
catdup.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* SPDX-FileCopyrightText: Copyright © 2009-2023 Nedko Arnaudov
* SPDX-License-Identifier: GPL-2.0-or-later
*
**************************************************************************
* This file contains implementation of the catdup() functions
**************************************************************************
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "catdup.h"
char * catdup(const char * s1, const char * s2)
{
char * buffer;
size_t s1_len, s2_len;
//ASSERT(s1 != NULL && s2 != NULL);
s1_len = strlen(s1);
s2_len = strlen(s2);
buffer = malloc(s1_len + s2_len + 1);
if (buffer == NULL)
{
//log_error("malloc() failed.");
return NULL;
}
memcpy(buffer, s1, s1_len);
memcpy(buffer + s1_len, s2, s2_len);
buffer[s1_len + s2_len] = 0;
return buffer;
}
char * catdup3(const char * s1, const char * s2, const char * s3)
{
char * buffer;
size_t s1_len, s2_len, s3_len;
//ASSERT(s1 != NULL && s2 != NULL && s3 != NULL);
s1_len = strlen(s1);
s2_len = strlen(s2);
s3_len = strlen(s3);
buffer = malloc(s1_len + s2_len + s3_len + 1);
if (buffer == NULL)
{
//log_error("malloc() failed.");
return NULL;
}
memcpy(buffer, s1, s1_len);
memcpy(buffer + s1_len, s2, s2_len);
memcpy(buffer + s1_len + s2_len, s3, s3_len);
buffer[s1_len + s2_len + s3_len] = 0;
return buffer;
}
char * catdup4(const char * s1, const char * s2, const char * s3, const char * s4)
{
char * buffer;
size_t s1_len, s2_len, s3_len, s4_len;
//ASSERT(s1 != NULL && s2 != NULL && s3 != NULL && s4 != NULL);
s1_len = strlen(s1);
s2_len = strlen(s2);
s3_len = strlen(s3);
s4_len = strlen(s4);
buffer = malloc(s1_len + s2_len + s3_len + s4_len + 1);
if (buffer == NULL)
{
//log_error("malloc() failed.");
return NULL;
}
memcpy(buffer, s1, s1_len);
memcpy(buffer + s1_len, s2, s2_len);
memcpy(buffer + s1_len + s2_len, s3, s3_len);
memcpy(buffer + s1_len + s2_len + s3_len, s4, s4_len);
buffer[s1_len + s2_len + s3_len + s4_len] = 0;
return buffer;
}
char * catdupv(const char * s1, const char * s2, ...)
{
va_list ap;
const char * str;
size_t len;
char * buffer;
char * p;
len = strlen(s1) + strlen(s2);
va_start(ap, s2);
while ((str = va_arg(ap, const char *)) != NULL)
{
len += strlen(str);
}
va_end(ap);
len++;
buffer = malloc(len);
if (buffer == NULL)
{
//log_error("malloc(%zu) failed.", len);
return NULL;
}
len = strlen(s1);
memcpy(buffer, s1, len);
p = buffer + len;
len = strlen(s2);
memcpy(p, s2, len);
p += len;
va_start(ap, s2);
while ((str = va_arg(ap, const char *)) != NULL)
{
len = strlen(str);
memcpy(p, str, len);
p += len;
}
va_end(ap);
*p = 0;
return buffer;
}
char * catdup_array(const char * const * array, const char * delimiter)
{
size_t len;
size_t i;
size_t delimiter_length;
char * buffer;
char * p;
delimiter_length = delimiter != NULL ? strlen(delimiter) : 0;
len = 0;
for (i = 0; array[i] != NULL; i++)
{
len += strlen(array[i]);
len += delimiter_length;
}
len++;
buffer = malloc(len);
if (buffer == NULL)
{
//log_error("malloc(%zu) failed.", len);
return NULL;
}
p = buffer;
for (i = 0; array[i] != NULL; i++)
{
len = strlen(array[i]);
memcpy(p, array[i], len);
p += len;
if (delimiter_length > 0)
{
memcpy(p, delimiter, delimiter_length);
p += delimiter_length;
}
}
*p = 0;
return buffer;
}