-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstrcat.c
58 lines (55 loc) · 1.45 KB
/
dstrcat.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dstrcat.h"
#ifdef DEBUG_LVL
#if DEBUG_LVL == 0
#define DEBUG1(x) ;
#define DEBUG2(x) ;
#elif DEBUG_LVL == 1
#define DEBUG1(x) x;
#define DEBUG2(x) ;
#elif DEBUG_LVL == 2
#define DEBUG1(x) x;
#define DEBUG2(x) x;
#endif
#else
#warning DEBUG_LVL undefined; defaulting to 0
#define DEBUG1(x) ;
#define DEBUG2(x) ;
#endif
// dynamically appends a string to another heap-allocated string
char *dstrcat(char *dest, const char *str_to_append) {
DEBUG1(
fprintf(stderr, "~~ Calling dstrcat(");
if (dest) {
fprintf(stderr, "\"%s\", \"%s\").\n", dest, str_to_append);
} else {
fprintf(stderr, "%p, \"%s\").\n", dest, str_to_append);
}
fprintf(stderr, "~~ ============== ~~\n");
);
size_t bufsize;
if (dest != NULL)
bufsize = 1 + snprintf(NULL, 0, "%s", str_to_append) + strlen(dest);
else
bufsize = 1 + snprintf(NULL, 0, "%s", str_to_append);
DEBUG2(
fprintf(
stderr, "~~ Allocating %lu chars to dest.\n", bufsize
);
fprintf(
stderr,
"~~ strlen(dest): %lu\n",
dest == NULL ? 0 : strlen(dest)
);
);
_Bool should_not_strlen_dest = dest == NULL;
dest = realloc(dest, bufsize);
if (should_not_strlen_dest)
sprintf(dest, "%s", str_to_append);
else
sprintf(dest + strlen(dest), "%s", str_to_append);
DEBUG2(fprintf(stderr, "~~ Result: \"%s\"\n", dest));
return dest;
}