-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostprogs.h
252 lines (218 loc) · 6.56 KB
/
hostprogs.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* microfs - Minimally Improved Compressed Read Only File System
* Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, ..., +%Y
* Erik Edlund <erik.edlund@32767.se>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef HOSTPROGS_H
#define HOSTPROGS_H
#if ( \
!defined(__STDC__) || __STDC__ == 0 || \
!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L \
)
#error "An ISO C11 compiler and preprocessor is required."
#endif
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <linux/types.h>
#ifndef HOSTPROG_PRINT_LOCK
#define HOSTPROG_PRINT_LOCK 0
#endif
void hostprog_print_lock(void);
void hostprog_print_unlock(void);
#define do_error(Exit, CanExit, ...) \
do { \
if (HOSTPROG_PRINT_LOCK) hostprog_print_lock(); \
fprintf(stderr, "!!! error: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " (%s:%d)", __FILE__, __LINE__); \
fprintf(stderr, "\n"); \
if (HOSTPROG_PRINT_LOCK) hostprog_print_unlock(); \
if (CanExit) \
Exit; \
} while (0)
#define error(...) do_error(exit(EXIT_FAILURE), 1, __VA_ARGS__)
#define do_warning(Exit, CanExit, ...) \
do { \
if (HOSTPROG_PRINT_LOCK) hostprog_print_lock(); \
fprintf(stderr, "*** warning: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " (%s:%d)", __FILE__, __LINE__); \
fprintf(stderr, "\n"); \
if (HOSTPROG_PRINT_LOCK) hostprog_print_unlock(); \
if (hostprog_werror) \
do_error(Exit, CanExit, "warnings treated as errors"); \
} while (0)
#define warning(...) do_warning(exit(EXIT_FAILURE), 1, __VA_ARGS__)
enum {
VERBOSITY_0 = 0,
VERBOSITY_1,
VERBOSITY_2
};
#define message(Level, ...) \
do { \
if (hostprog_verbosity >= Level) { \
if (HOSTPROG_PRINT_LOCK) hostprog_print_lock(); \
fprintf(stdout, __VA_ARGS__); \
fprintf(stdout, "\n"); \
if (HOSTPROG_PRINT_LOCK) hostprog_print_unlock(); \
} \
} while (0)
#define opt_strtolx(Postfix, Opt, Arg, Var) \
do { \
char* endptr; \
errno = 0; \
Var = strto##Postfix(Arg, &endptr, 10); \
if (*endptr != '\0') { \
error("arg %s is not an integer (%s=%s)", \
Opt, Opt, Arg); \
} else if (errno) { \
error("arg %s: %s (%s=%s)", Opt, strerror(errno), \
Opt, Arg); \
} \
} while (0)
#define max(a, b) \
({ \
__typeof__ (a) a_ = a; \
__typeof__ (b) b_ = b; \
a_ < b_ ? b_ : a_; \
})
#define min(a, b) \
({ \
__typeof__ (a) a_ = a; \
__typeof__ (b) b_ = b; \
a_ < b_ ? a_ : b_; \
})
static inline char* optiontostr(int option, char* buffer)
{
buffer[0] = '-';
buffer[1] = option;
buffer[2] = '\0';
return buffer;
}
static inline char nodtype(const mode_t mode)
{
switch (mode & S_IFMT) {
case S_IFREG:
return 'f';
case S_IFDIR:
return 'd';
case S_IFLNK:
return 'l';
case S_IFCHR:
return 'c';
case S_IFBLK:
return 'b';
case S_IFSOCK:
return 's';
case S_IFIFO:
return 'p';
default:
return '?';
}
}
/* A naive implementation of the Fisher-Yates(-Knuth) shuffle.
*/
int fykshuffle(void** slots, size_t length);
/* Skewed number in [min, max].
*/
static inline int rand_nonuniform_range(int min, int max)
{
return rand() % (max - min + 1) + min;
}
extern int hostprog_werror;
extern int hostprog_verbosity;
int hostprog_scandirsort(const struct dirent** a, const struct dirent** b);
/* A simplistic stack implementation.
*/
struct hostprog_stack {
/* Next free slot. */
__u64 st_index;
/* Size of %st_slots. */
__u64 st_size;
/* Factor to grow %st_slots with. */
__u64 st_growth;
/* The objects held by the stack. */
void** st_slots;
};
/* An integer type which can be stored on a stack without
* a wrapper structure.
*/
typedef ptrdiff_t hostprog_stack_int_t;
#if __WORDSIZE == 64
#define HOSTPROG_STACK_INT_T_ZERO 0ULL
#else
#define HOSTPROG_STACK_INT_T_ZERO 0UL
#endif
#define HOSTPROG_STACK_INT_T_MAX \
((hostprog_stack_int_t)(~HOSTPROG_STACK_INT_T_ZERO >> 1))
#define HOSTPROG_STACK_INT_T_MIN \
(-HOSTPROG_STACK_INT_T_MAX - 1)
int hostprog_stack_create(struct hostprog_stack** const s,
const __u64 size, const __u64 growth);
int hostprog_stack_destroy(struct hostprog_stack* const s);
int hostprog_stack_size(const struct hostprog_stack* const s);
int _hostprog_stack_push(struct hostprog_stack* s, void* v);
int _hostprog_stack_top(struct hostprog_stack* const s, void** v);
int _hostprog_stack_pop(struct hostprog_stack* const s, void** v);
#define hostprog_stack_push(Stack, Value) \
_hostprog_stack_push(Stack, (void*)Value)
#define hostprog_stack_top(Stack, Value) \
_hostprog_stack_top(Stack, (void**)Value)
#define hostprog_stack_pop(Stack, Value) \
_hostprog_stack_pop(Stack, (void**)Value)
#define HOSTPROG_PATH_MAXNAMELEN 255
/* A very simplistic filesystem path representation.
*/
struct hostprog_path {
/* The path, a NULL-terminated string. */
char* p_path;
/* Number of bytes allocated for %p_path. */
__u64 p_pathsz;
/* Number of characters in %p_path. */
__u64 p_pathlen;
/* Maximum allowed name length. */
__u64 p_maxnamelen;
/* Minimi growth of %p_path when it runs out space. */
__u64 p_mingrowth;
/* All path separator indexes (excluding the root dir). */
struct hostprog_stack* p_separators;
};
int hostprog_path_create(struct hostprog_path** dpath, const char* path,
const __u64 maxnamelen, const __u64 mingrowth);
int hostprog_path_append(struct hostprog_path* const dpath, const char* dname);
int hostprog_path_lvls(struct hostprog_path* const dpath);
int hostprog_path_dirname(struct hostprog_path* const dpath);
int hostprog_path_dirnamelvl(struct hostprog_path* const dpath, const int lvl);
int hostprog_path_reset(struct hostprog_path* const dpath);
int hostprog_path_destroy(struct hostprog_path* const dpath);
int hostprog_path_dotdir(const char* dname);
#endif