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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/****************************************************************************
* bfs *
* Copyright (C) 2016-2022 Tavian Barnes <tavianator@tavianator.com> *
* *
* Permission to use, copy, modify, and/or distribute this software for any *
* purpose with or without fee is hereby granted. *
* *
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF *
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR *
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES *
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN *
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
****************************************************************************/
/**
* Assorted utilities that don't belong anywhere else.
*/
#ifndef BFS_UTIL_H
#define BFS_UTIL_H
#include <fcntl.h>
#include <fnmatch.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
// Some portability concerns
#ifndef __has_feature
# define __has_feature(feat) false
#endif
#ifndef __has_c_attribute
# define __has_c_attribute(attr) false
#endif
#ifndef __has_attribute
# define __has_attribute(attr) false
#endif
#ifdef __has_include
#if __has_include(<mntent.h>)
# define BFS_HAS_MNTENT_H true
#endif
#if __has_include(<paths.h>)
# define BFS_HAS_PATHS_H true
#endif
#if __has_include(<sys/acl.h>)
# define BFS_HAS_SYS_ACL_H true
#endif
#if __has_include(<sys/capability.h>)
# define BFS_HAS_SYS_CAPABILITY_H true
#endif
#if __has_include(<sys/extattr.h>)
# define BFS_HAS_SYS_EXTATTR_H true
#endif
#if __has_include(<sys/mkdev.h>)
# define BFS_HAS_SYS_MKDEV_H true
#endif
#if __has_include(<sys/param.h>)
# define BFS_HAS_SYS_PARAM_H true
#endif
#if __has_include(<sys/sysmacros.h>)
# define BFS_HAS_SYS_SYSMACROS_H true
#endif
#if __has_include(<sys/xattr.h>)
# define BFS_HAS_SYS_XATTR_H true
#endif
#if __has_include(<util.h>)
# define BFS_HAS_UTIL_H true
#endif
#else // !__has_include
#define BFS_HAS_MNTENT_H __GLIBC__
#define BFS_HAS_PATHS_H true
#define BFS_HAS_SYS_ACL_H true
#define BFS_HAS_SYS_CAPABILITY_H __linux__
#define BFS_HAS_SYS_EXTATTR_H __FreeBSD__
#define BFS_HAS_SYS_MKDEV_H false
#define BFS_HAS_SYS_PARAM_H true
#define BFS_HAS_SYS_SYSMACROS_H __GLIBC__
#define BFS_HAS_SYS_XATTR_H __linux__
#define BFS_HAS_UTIL_H __NetBSD__
#endif // !__has_include
#ifndef BFS_USE_MNTENT_H
# define BFS_USE_MNTENT_H BFS_HAS_MNTENT_H
#endif
#ifndef BFS_USE_PATHS_H
# define BFS_USE_PATHS_H BFS_HAS_PATHS_H
#endif
#ifndef BFS_USE_SYS_ACL_H
# define BFS_USE_SYS_ACL_H BFS_HAS_SYS_ACL_H
#endif
#ifndef BFS_USE_SYS_CAPABILITY_H
# define BFS_USE_SYS_CAPABILITY_H BFS_HAS_SYS_CAPABILITY_H
#endif
#ifndef BFS_USE_SYS_EXTATTR_H
# define BFS_USE_SYS_EXTATTR_H BFS_HAS_SYS_EXTATTR_H
#endif
#ifndef BFS_USE_SYS_MKDEV_H
# define BFS_USE_SYS_MKDEV_H BFS_HAS_SYS_MKDEV_H
#endif
#ifndef BFS_USE_SYS_PARAM_H
# define BFS_USE_SYS_PARAM_H BFS_HAS_SYS_PARAM_H
#endif
#ifndef BFS_USE_SYS_SYSMACROS_H
# define BFS_USE_SYS_SYSMACROS_H BFS_HAS_SYS_SYSMACROS_H
#endif
#ifndef BFS_USE_SYS_XATTR_H
# define BFS_USE_SYS_XATTR_H BFS_HAS_SYS_XATTR_H
#endif
#ifndef BFS_USE_UTIL_H
# define BFS_USE_UTIL_H BFS_HAS_UTIL_H
#endif
#ifndef __GLIBC_PREREQ
# define __GLIBC_PREREQ(maj, min) false
#endif
#if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE)
# define FNM_CASEFOLD FNM_IGNORECASE
#endif
#ifndef O_DIRECTORY
# define O_DIRECTORY 0
#endif
#if __has_c_attribute(fallthrough)
# define BFS_FALLTHROUGH [[fallthrough]]
#elif __has_attribute(fallthrough)
# define BFS_FALLTHROUGH __attribute__((fallthrough))
#else
# define BFS_FALLTHROUGH ((void)0)
#endif
/**
* Adds compiler warnings for bad printf()-style function calls, if supported.
*/
#if __has_attribute(format)
# define BFS_FORMATTER(fmt, args) __attribute__((format(printf, fmt, args)))
#else
# define BFS_FORMATTER(fmt, args)
#endif
// Lower bound on BFS_FLEX_SIZEOF()
#define BFS_FLEX_LB(type, member, length) (offsetof(type, member) + sizeof(((type *)NULL)->member[0]) * (length))
// Maximum macro for BFS_FLEX_SIZE()
#define BFS_FLEX_MAX(a, b) ((a) > (b) ? (a) : (b))
/**
* Computes the size of a struct containing a flexible array member of the given
* length.
*
* @param type
* The type of the struct containing the flexible array.
* @param member
* The name of the flexible array member.
* @param length
* The length of the flexible array.
*/
#define BFS_FLEX_SIZEOF(type, member, length) \
(sizeof(type) <= BFS_FLEX_LB(type, member, 0) \
? BFS_FLEX_LB(type, member, length) \
: BFS_FLEX_MAX(sizeof(type), BFS_FLEX_LB(type, member, length)))
/**
* readlinkat() wrapper that dynamically allocates the result.
*
* @param fd
* The base directory descriptor.
* @param path
* The path to the link, relative to fd.
* @param size
* An estimate for the size of the link name (pass 0 if unknown).
* @return The target of the link, allocated with malloc(), or NULL on failure.
*/
char *xreadlinkat(int fd, const char *path, size_t size);
/**
* Like dup(), but set the FD_CLOEXEC flag.
*
* @param fd
* The file descriptor to duplicate.
* @return A duplicated file descriptor, or -1 on failure.
*/
int dup_cloexec(int fd);
/**
* Like pipe(), but set the FD_CLOEXEC flag.
*
* @param pipefd
* The array to hold the two file descriptors.
* @return 0 on success, -1 on failure.
*/
int pipe_cloexec(int pipefd[2]);
/**
* Format a mode like ls -l (e.g. -rw-r--r--).
*
* @param mode
* The mode to format.
* @param str
* The string to hold the formatted mode.
*/
void xstrmode(mode_t mode, char str[11]);
/**
* basename() variant that doesn't modify the input.
*
* @param path
* The path in question.
* @return A pointer into path at the base name offset.
*/
const char *xbasename(const char *path);
/**
* Wrapper for faccessat() that handles some portability issues.
*/
int xfaccessat(int fd, const char *path, int amode);
/**
* Portability wrapper for strtofflags().
*
* @param str
* The string to parse. The pointee will be advanced to the first
* invalid position on error.
* @param set
* The flags that are set in the string.
* @param clear
* The flags that are cleared in the string.
* @return
* 0 on success, -1 on failure.
*/
int xstrtofflags(const char **str, unsigned long long *set, unsigned long long *clear);
/**
* wcswidth() variant that works on narrow strings.
*
* @param str
* The string to measure.
* @return
* The likely width of that string in a terminal.
*/
size_t xstrwidth(const char *str);
/**
* Return whether an error code is due to a path not existing.
*/
bool is_nonexistence_error(int error);
/**
* Process a yes/no prompt.
*
* @return 1 for yes, 0 for no, and -1 for unknown.
*/
int ynprompt(void);
/**
* Portable version of makedev().
*/
dev_t bfs_makedev(int ma, int mi);
/**
* Portable version of major().
*/
int bfs_major(dev_t dev);
/**
* Portable version of minor().
*/
int bfs_minor(dev_t dev);
/**
* A safe version of read() that handles interrupted system calls and partial
* reads.
*
* @return
* The number of bytes read. A value != nbytes indicates an error
* (errno != 0) or end of file (errno == 0).
*/
size_t xread(int fd, void *buf, size_t nbytes);
/**
* A safe version of write() that handles interrupted system calls and partial
* writes.
*
* @return
The number of bytes written. A value != nbytes indicates an error.
*/
size_t xwrite(int fd, const void *buf, size_t nbytes);
/**
* Wrapper for confstr() that allocates with malloc().
*
* @param name
* The ID of the confstr to look up.
* @return
* The value of the confstr, or NULL on failure.
*/
char *xconfstr(int name);
/**
* Convenience wrapper for getdelim().
*
* @param file
* The file to read.
* @param delim
* The delimiter character to split on.
* @return
* The read chunk (without the delimiter), allocated with malloc().
* NULL is returned on error (errno != 0) or end of file (errno == 0).
*/
char *xgetdelim(FILE *file, char delim);
/**
* fopen() variant that takes open() style flags.
*
* @param path
* The path to open.
* @param flags
* Flags to pass to open().
*/
FILE *xfopen(const char *path, int flags);
/**
* close() wrapper that asserts the file descriptor is valid.
*
* @param fd
* The file descriptor to close.
* @return
* 0 on success, or -1 on error.
*/
int xclose(int fd);
/**
* close() variant that preserves errno.
*
* @param fd
* The file descriptor to close.
*/
void close_quietly(int fd);
#endif // BFS_UTIL_H
|