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
|
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <assert.h>
#include "util.h"
#define __FILENAME__ "util.c"
#include "dbg.h"
#include "../config.h"
/* A safe malloc wrapper. */
void *ec_malloc(size_t size) {
void *ptr = malloc(size);
if (!ptr)
fatal("redo: cannot allocate %zu bytes", size);
return ptr;
}
/* For concating multiple strings into a single larger one. */
char *concat(size_t count, ...) {
assert(count > 0);
va_list ap, ap2;
va_start(ap, count);
va_copy(ap2, ap);
size_t i, size = 0, args_len[count];
for (i = 0; i < count; ++i) {
args_len[i] = strlen(va_arg(ap, char*));
size += args_len[i];
}
size++;
char *result = ec_malloc(size);
/* debug("Allocated %zu bytes at %p\n", size, result); */
uintptr_t offset = 0;
for (i = 0; i < count; ++i) {
strcpy(&result[offset], va_arg(ap2, char*));
offset += args_len[i];
}
va_end(ap);
va_end(ap2);
return result;
}
char *xbasename(const char *path) {
assert(path);
char *ptr = strrchr(path, '/');
return ptr? ptr+1 : (char*) path;
}
char *ec_strdup(const char* str) {
assert(str);
size_t len = strlen(str) + 1;
char *ptr = malloc(len);
if (!ptr)
fatal("redo: failed to duplicate string");
return memcpy(ptr, str, len);;
}
|