-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
39 lines (28 loc) · 749 Bytes
/
util.c
File metadata and controls
39 lines (28 loc) · 749 Bytes
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
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "util.h"
char * strdup(const char* s) {
if (s == NULL) return NULL;
char *d = calloc(strlen (s) + 1, sizeof(char));
if (d == NULL) return NULL;
strcpy (d,s);
return d;
}
char* vstrcat(int n, ...) {
va_list va;
char** strings = calloc(n, sizeof(char*));
size_t stringLength = 0;
va_start(va, n);
for(int i = 0; i<n; i++) {
strings[i] = va_arg(va, char*);
stringLength += strlen(strings[i]);
}
va_end(va);
char* newStr = calloc(stringLength+1, sizeof(char));
for(int i = 0; i<n; i++) {
strcat(newStr, strings[i]);
}
free(strings);
return newStr;
}