-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.h
79 lines (59 loc) · 1.74 KB
/
util.h
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
#ifndef CEO_UTIL_H
#define CEO_UTIL_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <sys/types.h>
#include <dirent.h>
#ifdef __GNUC__
#define NORETURN __attribute__((__noreturn__))
#define PRINTF_LIKE(extra) __attribute__((format(printf, extra+1, extra+2)))
#else
#define NORETURN
#define PRINTF_LIKE(extra)
#endif
PRINTF_LIKE(0) NORETURN void fatal(const char *, ...);
PRINTF_LIKE(0) NORETURN void fatalpe(const char *, ...);
PRINTF_LIKE(0) NORETURN void badconf(const char *, ...);
PRINTF_LIKE(0) NORETURN void deny(const char *, ...);
PRINTF_LIKE(0) void error(const char *, ...);
PRINTF_LIKE(0) void warn(const char *, ...);
PRINTF_LIKE(0) void notice(const char *, ...);
PRINTF_LIKE(0) void debug(const char *, ...);
PRINTF_LIKE(1) void debugn(int level, const char *, ...);
PRINTF_LIKE(0) void errorpe(const char *, ...);
PRINTF_LIKE(0) void warnpe(const char *, ...);
static inline void *xmalloc(size_t size) {
void *alloc = malloc(size);
if (alloc == NULL)
fatal("out of memory");
return alloc;
}
static inline void *xrealloc(void *ptr, size_t size) {
void *alloc = realloc(ptr, size);
if (alloc == NULL)
fatal("out of memory");
return alloc;
}
static inline void *xcalloc(size_t nmemb, size_t size) {
void *alloc = calloc(nmemb, size);
if (alloc == NULL)
fatal("out of memory");
return alloc;
}
static inline char *xstrdup(const char *s) {
char *dup = strdup(s);
if (dup == NULL)
fatal("out of memory");
return dup;
}
static inline char *xstrndup(const char *s, size_t n) {
char *dup = strndup(s, n);
if (dup == NULL)
fatal("out of memory");
return dup;
}
extern int debug_level;
#endif