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
|
#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 *safe_malloc_(size_t size, const char *file, unsigned int line) {
void *ptr = malloc(size);
if (!ptr)
fatal_(file, line, "redo: cannot allocate %zu bytes", size);
return ptr;
}
void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned int line) {
void *ptr2 = realloc(ptr, size);
if (!ptr2)
fatal_(file, line, "redo: cannot reallocate %zu bytes", size);
return ptr2;
}
FILE *safe_fopen_(const char *path, const char *mode, const char *file,
unsigned int line) {
FILE *temp = fopen(path, mode);
if (!temp)
fatal_(file, line, "redo: failed to open %s", path);
return temp;
}
/* 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 = safe_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;
}
/* TODO: REIMPLEMENT */
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);;
}
|