-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.h
126 lines (103 loc) · 2.84 KB
/
common.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef COMMON_H
#define COMMON_H
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CONFIG_DIR ".moc"
/* Maximal string length sent/received. */
#define MAX_SEND_STRING 4096
/* Maximum path length, we don't consider exceptions like mounted NFS */
#ifndef PATH_MAX
# if defined(_POSIX_PATH_MAX)
# define PATH_MAX _POSIX_PATH_MAX /* Posix */
# elif defined(MAXPATHLEN)
# define PATH_MAX MAXPATHLEN /* Solaris? Also linux...*/
# else
# define PATH_MAX 4096 /* Suppose, we have 4096 */
# endif
#endif
/* Exit status on fatal exit. */
#define EXIT_FATAL 2
#define LOCK(mutex) pthread_mutex_lock (&mutex)
#define UNLOCK(mutex) pthread_mutex_unlock (&mutex)
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef LIMIT
#define LIMIT(val, lim) ((val) >= 0 && (val) < (lim))
#endif
#ifndef RANGE
#define RANGE(min, val, max) ((val) >= (min) && (val) <= (max))
#endif
#ifndef CLAMP
#define CLAMP(min, val, max) ((val) < (min) ? (min) : \
(val) > (max) ? (max) : (val))
#endif
#ifdef HAVE__ATTRIBUTE__
# define ATTR_UNUSED __attribute__((unused))
#else
# define ATTR_UNUSED
#endif
#if HAVE_STDBOOL_H
# include <stdbool.h>
#else
# if ! HAVE__BOOL
# ifdef __cplusplus
typedef bool _Bool;
# else
typedef unsigned char _Bool;
# endif
# endif
# define bool _Bool
# define false 0
# define true 1
# define __bool_true_false_are_defined 1
#endif
/* isblank() is a GNU extension */
#ifndef isblank
#define isblank(c) ((c) == ' ' || (c) == '\t')
#endif
/* __FUNCTION__ is a gcc extension */
#ifndef HAVE__FUNCTION__
# define __FUNCTION__ "UNKNOWN_FUNC"
#endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
void *xmalloc (size_t size);
void *xcalloc (size_t nmemb, size_t size);
void *xrealloc (void *ptr, const size_t size);
char *xstrdup (const char *s);
char *str_repl (char *target, const char *oldstr, const char *newstr);
#ifdef NDEBUG
#define fatal(format, ...) \
internal_fatal (NULL, 0, NULL, format, ## __VA_ARGS__)
#else
#define fatal(format, ...) \
internal_fatal (__FILE__, __LINE__, __FUNCTION__, format, \
## __VA_ARGS__)
#endif
#ifdef HAVE__ATTRIBUTE__
void internal_fatal (const char *file, int line, const char *function,
const char *format, ...)
__attribute__ ((format (printf, 4, 5), noreturn));
void error (const char *format, ...) __attribute__((format (printf, 1, 2)));
#else
void internal_fatal (const char *file, int line, const char *function,
const char *format, ...);
void error (const char *format, ...);
#endif
void set_me_server ();
char *trim (const char *src, size_t len);
bool is_valid_symbol (const char *candidate);
char *create_file_name (const char *file);
void sec_to_min (char *buff, const int seconds);
const char *get_home ();
#ifdef __cplusplus
}
#endif
#endif