-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
61 lines (55 loc) · 1.29 KB
/
util.c
File metadata and controls
61 lines (55 loc) · 1.29 KB
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
#include <execinfo.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "all.h"
void check(int cond, const char *msg, ...) {
va_list args;
if (cond) return;
va_start(args, msg);
vfprintf(stderr, msg, args);
fputc('\n', stderr);
va_end(args);
dump_stacktrace();
exit(1);
}
void fail(const char *msg, ...) {
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
fputc('\n', stderr);
va_end(args);
dump_stacktrace();
exit(1);
}
void dump_stacktrace(void) {
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
printf("\nstacktrace:\n");
for (i = 0; i < frames; ++i) {
printf(" %s\n", strs[i]);
}
free(strs);
}
char *w_strdup(const char *s) {
size_t sz = strlen(s);
char *r = malloc(sz + 1);
check(r != 0, "malloc failed");
memcpy(r, s, sz+1);
return r;
}
int w_snprintf(char *buf, size_t sz, const char *fmt, ...) {
int r;
va_list args;
va_start(args, fmt);
#if defined(__linux__) && !defined(__USE_ISOC99)
/* linux stdio.h hides (v)snprintf under C89. */
(void) sz;
r = vsprintf(buf, fmt, args);
#else
r = vsnprintf(buf, sz, fmt, args);
#endif
va_end(args);
return r;
}