File: util.h

package info (click to toggle)
pcaputils 0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: ansic: 2,945; sh: 44; makefile: 38
file content (112 lines) | stat: -rw-r--r-- 2,469 bytes parent folder | download | duplicates (4)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef RSEUTIL_UTIL_H
#define RSEUTIL_UTIL_H

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

extern bool util_flag_daemonized;
extern bool util_flag_verbose;

/* macros */

#define DEBUG(format, ...) do{ \
	if(util_flag_daemonized) syslog(LOG_DAEMON | LOG_DEBUG, "%s] " format, __func__, ## __VA_ARGS__); \
	else fprintf(stderr, "%s] " format "\n", __func__, ## __VA_ARGS__); \
}while(0)

#define ERROR(format, ...) do{ \
	DEBUG("Error: " format, ## __VA_ARGS__); \
	exit(EXIT_FAILURE); \
}while(0)

#define VERBOSE(format, ...) do{ \
	if(util_flag_verbose) DEBUG(format, ## __VA_ARGS__); \
}while(0)

#define MALLOC(target, size) do{ \
	target = malloc(size); \
	if(target == NULL){ \
		DEBUG("malloc() failed, aborting"); \
		abort(); \
	} \
}while(0)

#define CALLOC(target, size) do{ \
	target = calloc(1, size); \
	if(target == NULL){ \
		DEBUG("calloc() failed, aborting"); \
		abort(); \
	} \
}while(0)

#define REALLOC(target, size) do { \
	target = realloc(target, size); \
	if(target == NULL){ \
		DEBUG("realloc() failed, aborting"); \
		abort(); \
	} \
}while(0)

#define NEW(target) do{ \
	target = malloc(sizeof(*target)); \
	if(target == NULL){ \
		DEBUG("malloc() failed, aborting"); \
		abort(); \
	} \
}while(0)

#define NEW0(target) do{ \
	target = calloc(1, sizeof(*target)); \
	if(target == NULL){ \
		DEBUG("malloc() failed, aborting"); \
		abort(); \
	} \
}while(0)

#define FREE(target) do{ \
	if(target){ \
		free(target); \
		target = NULL; \
	} \
}while(0)

#define Chdir(dir) do{ \
	if(chdir(dir) != 0) ERROR("unable to chdir(%s): %s", dir, strerror(errno)); \
}while(0)

#define Chroot(dir) do{ \
	if(chroot(dir) != 0) ERROR("unable to chroot(%s): %s", dir, strerror(errno)); \
}while(0)

#define ZERO(target) memset(&target, 0, sizeof(target));

/* General-purpose swap macro from http://www.spinellis.gr/blog/20060130/index.html */
#define SWAP(a, b) do{\
	char c[sizeof(a)]; \
	memcpy((void *)&c, (void *)&a, sizeof(c)); \
	memcpy((void *)&a, (void *)&b, sizeof(a)); \
	memcpy((void *)&b, (void *)&c, sizeof(b)); \
}while(0)

#define likely(x)	__builtin_expect((x), 1)
#define unlikely(x)	__builtin_expect((x), 0)

#if __GNUC__ >= 3
# define __unused __attribute__((unused))
#else
# define __unused
#endif

#if __GNUC_GNU_INLINE__
# define __inline inline __attribute__((gnu_inline))
#else
# define __inline inline
#endif

void util_print_backtrace(int);

#endif