-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
194 lines (172 loc) · 5 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#if !defined(_UTIL_H)
#define _UTIL_H
#include <assert.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#if !defined(BUFSIZE)
#define BUFSIZE 256
#endif
#if !defined(EXTRA_LEN_PRINT_ERROR)
#define EXTRA_LEN_PRINT_ERROR 512
#endif
#if !defined(MAX_FILENAME_LENGTH)
#define MAX_FILENAME_LENGTH 255
#endif
#define SYSCALL_EXIT(name, r, sc, str, ...) \
if ((r=sc) == -1) { \
perror(#name); \
int errno_copy = errno; \
print_prefix(str,"ERROR:", ##__VA_ARGS__); \
exit(errno_copy); \
}
#define SYSCALL_PRINT(name, r, sc, str, ...) \
if ((r=sc) == -1) { \
perror(#name); \
int errno_copy = errno; \
print_prefix(str,"ERROR:", ##__VA_ARGS__); \
errno = errno_copy; \
}
#define SYSCALL_RETURN(name, r, sc, str, ...) \
if ((r=sc) == -1) { \
perror(#name); \
int errno_copy = errno; \
print_prefix(str,"ERROR:", ##__VA_ARGS__); \
errno = errno_copy; \
return r; \
}
#define CHECK_EQ_EXIT(name, X, val, str, ...) \
if ((X)==val) { \
perror(#name); \
int errno_copy = errno; \
print_prefix(str,"ERROR:", ##__VA_ARGS__); \
exit(errno_copy); \
}
#define CHECK_NEQ_EXIT(name, X, val, str, ...) \
if ((X)!=val) { \
perror(#name); \
int errno_copy = errno; \
print_prefix(str,"ERROR:",##__VA_ARGS__); \
exit(errno_copy); \
}
#if defined(DEBUG)
#define DBG(str, ...) \
print_prefix(str, "DBG:", ##__VA_ARGS__)
#else
#define DBG(str, ...)
#endif
static inline void print_prefix(const char * str, const char *prefix, ...) {
va_list argp;
char * p=(char *)malloc(strlen(str)+strlen(prefix)+EXTRA_LEN_PRINT_ERROR);
if (!p) {
perror("malloc");
fprintf(stderr,"FATAL ERROR in print_prefix\n");
return;
}
strcpy(p,prefix);
strcpy(p+strlen(prefix), str);
va_start(argp, prefix);
vfprintf(stderr, p, argp);
va_end(argp);
free(p);
}
/**
* @function isNumber
* @brief Check if the string is a number.
* It returns 0 if ok, 1 if it is not a number, and 2 if overflow/underflow occurs.
*/
static inline int isNumber(const char* s, long* n) {
if (s==NULL) return 1;
if (strlen(s)==0) return 1;
char* e = NULL;
errno=0;
long val = strtol(s, &e, 10);
if (errno == ERANGE) return 2; // overflow/underflow
if (e != NULL && *e == (char)0) {
*n = val;
return 0; // ok
}
return 1; // it's not a number
}
/**
* @function isRegular
* @brief Check if the name provided as first argument is a regular file.
* It returns 1 if success, 0 if it is not a regular file, and -1 if error.
* The argument size is set only if name is a regular file and if size is not NULL.
*/
static inline int isRegular(const char name[], size_t *size) {
struct stat statbuf;
if (stat(name, &statbuf) == -1) return -1;
if (!S_ISREG(statbuf.st_mode)) return 0;
if (size != NULL) {
*size = statbuf.st_size;
}
return 1;
}
#define LOCK(l) if (pthread_mutex_lock(l)!=0) { \
fprintf(stderr, "FATAL ERROR lock\n"); \
pthread_exit((void*)EXIT_FAILURE); \
}
#define LOCK_RETURN(l, r) if (pthread_mutex_lock(l)!=0) { \
fprintf(stderr, "FATAL ERROR lock\n"); \
return r; \
}
#define UNLOCK(l) if (pthread_mutex_unlock(l)!=0) { \
fprintf(stderr, "FATAL ERROR unlock\n"); \
pthread_exit((void*)EXIT_FAILURE); \
}
#define UNLOCK_RETURN(l,r) if (pthread_mutex_unlock(l)!=0) { \
fprintf(stderr, "FATAL ERROR unlock\n"); \
return r; \
}
#define WAIT(c,l) if (pthread_cond_wait(c,l)!=0) { \
fprintf(stderr, "FATAL ERROR wait\n"); \
pthread_exit((void*)EXIT_FAILURE); \
}
/* ATTENZIONE: t e' un tempo assoluto! */
#define TWAIT(c,l,t) { \
int r=0; \
if ((r=pthread_cond_timedwait(c,l,t))!=0 && r!=ETIMEDOUT) { \
fprintf(stderr, "FATAL ERROR timed wait\n"); \
pthread_exit((void*)EXIT_FAILURE); \
} \
}
#define SIGNAL(c) if (pthread_cond_signal(c)!=0) { \
fprintf(stderr, "FATAL ERROR signal\n"); \
pthread_exit((void*)EXIT_FAILURE); \
}
#define BCAST(c) if (pthread_cond_broadcast(c)!=0) { \
fprintf(stderr, "FATAL ERROR broadcast\n"); \
pthread_exit((void*)EXIT_FAILURE); \
}
static inline int TRYLOCK(pthread_mutex_t* l) {
int r=0;
if ((r=pthread_mutex_trylock(l))!=0 && r!=EBUSY) {
fprintf(stderr, "FATAL ERROR unlock\n");
pthread_exit((void*)EXIT_FAILURE);
}
return r;
}
static inline int P(sem_t* sem) {
if (sem_wait(sem) == -1) {
perror("sem_wait");
return -1;
}
return 0;
}
static inline int V(sem_t* sem) {
if (sem_post(sem) == -1) {
perror("sem_post");
return -1;
}
return 0;
}
#endif /* _UTIL_H */