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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
/**
* Standard library wrappers and polyfills.
*/
#ifndef BFS_BFSTD_H
#define BFS_BFSTD_H
#include "prelude.h"
#include "sanity.h"
#include <stddef.h>
#include <ctype.h>
/**
* Work around https://github.com/llvm/llvm-project/issues/65532 by forcing a
* function, not a macro, to be called.
*/
#if __FreeBSD__ && SANITIZE_MEMORY
# define BFS_INTERCEPT(fn) (fn)
#else
# define BFS_INTERCEPT(fn) fn
#endif
/**
* Wrap isalpha()/isdigit()/etc.
*/
#define BFS_ISCTYPE(fn, c) BFS_INTERCEPT(fn)((unsigned char)(c))
#define xisalnum(c) BFS_ISCTYPE(isalnum, c)
#define xisalpha(c) BFS_ISCTYPE(isalpha, c)
#define xisascii(c) BFS_ISCTYPE(isascii, c)
#define xiscntrl(c) BFS_ISCTYPE(iscntrl, c)
#define xisdigit(c) BFS_ISCTYPE(isdigit, c)
#define xislower(c) BFS_ISCTYPE(islower, c)
#define xisgraph(c) BFS_ISCTYPE(isgraph, c)
#define xisprint(c) BFS_ISCTYPE(isprint, c)
#define xispunct(c) BFS_ISCTYPE(ispunct, c)
#define xisspace(c) BFS_ISCTYPE(isspace, c)
#define xisupper(c) BFS_ISCTYPE(isupper, c)
#define xisxdigit(c) BFS_ISCTYPE(isxdigit, c)
// #include <errno.h>
/**
* Check if an error code is "like" another one. For example, ENOTDIR is
* like ENOENT because they can both be triggered by non-existent paths.
*
* @param error
* The error code to check.
* @param category
* The category to test for. Known categories include ENOENT and
* ENAMETOOLONG.
* @return
* Whether the error belongs to the given category.
*/
bool error_is_like(int error, int category);
/**
* Equivalent to error_is_like(errno, category).
*/
bool errno_is_like(int category);
/**
* Apply the "negative errno" convention.
*
* @param ret
* The return value of the attempted operation.
* @return
* ret, if non-negative, otherwise -errno.
*/
int try(int ret);
#include <fcntl.h>
#ifndef O_EXEC
# ifdef O_PATH
# define O_EXEC O_PATH
# else
# define O_EXEC O_RDONLY
# endif
#endif
#ifndef O_SEARCH
# ifdef O_PATH
# define O_SEARCH O_PATH
# else
# define O_SEARCH O_RDONLY
# endif
#endif
#ifndef O_DIRECTORY
# define O_DIRECTORY 0
#endif
#include <fnmatch.h>
#if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE)
# define FNM_CASEFOLD FNM_IGNORECASE
#endif
// #include <libgen.h>
/**
* Re-entrant dirname() variant that always allocates a copy.
*
* @param path
* The path in question.
* @return
* The parent directory of the path.
*/
char *xdirname(const char *path);
/**
* Re-entrant basename() variant that always allocates a copy.
*
* @param path
* The path in question.
* @return
* The final component of the path.
*/
char *xbasename(const char *path);
/**
* Find the offset of the final component of a path.
*
* @param path
* The path in question.
* @return
* The offset of the basename.
*/
size_t xbaseoff(const char *path);
#include <stdio.h>
/**
* 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);
/**
* 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);
/**
* Open the controlling terminal.
*
* @param flags
* The open() flags.
* @return
* An open file descriptor, or -1 on failure.
*/
int open_cterm(int flags);
// #include <stdlib.h>
/**
* Wrapper for getprogname() or equivalent functionality.
*
* @return
* The basename of the currently running program.
*/
const char *xgetprogname(void);
/**
* Wrapper for strtoll() that forbids leading spaces.
*
* @param str
* The string to parse.
* @param end
* If non-NULL, will hold a pointer to the first invalid character.
* If NULL, the entire string must be valid.
* @param base
* The base for the conversion, or 0 to auto-detect.
* @param value
* Will hold the parsed integer value, on success.
* @return
* 0 on success, -1 on failure.
*/
int xstrtoll(const char *str, char **end, int base, long long *value);
/**
* Process a yes/no prompt.
*
* @return 1 for yes, 0 for no, and -1 for unknown.
*/
int ynprompt(void);
// #include <string.h>
/**
* Get the length of the pure-ASCII prefix of a string.
*/
size_t asciilen(const char *str);
/**
* Get the length of the pure-ASCII prefix of a string.
*
* @param str
* The string to check.
* @param n
* The maximum prefix length.
*/
size_t asciinlen(const char *str, size_t n);
/**
* Allocate a copy of a region of memory.
*
* @param src
* The memory region to copy.
* @param size
* The size of the memory region.
* @return
* A copy of the region, allocated with malloc(), or NULL on failure.
*/
void *xmemdup(const void *src, size_t size);
/**
* A nice string copying function.
*
* @param dest
* The NUL terminator of the destination string, or `end` if it is
* already truncated.
* @param end
* The end of the destination buffer.
* @param src
* The string to copy from.
* @return
* The new NUL terminator of the destination, or `end` on truncation.
*/
char *xstpecpy(char *dest, char *end, const char *src);
/**
* A nice string copying function.
*
* @param dest
* The NUL terminator of the destination string, or `end` if it is
* already truncated.
* @param end
* The end of the destination buffer.
* @param src
* The string to copy from.
* @param n
* The maximum number of characters to copy.
* @return
* The new NUL terminator of the destination, or `end` on truncation.
*/
char *xstpencpy(char *dest, char *end, const char *src, size_t n);
/**
* Thread-safe strerror().
*
* @param errnum
* An error number.
* @return
* A string describing that error, which remains valid until the next
* xstrerror() call in the same thread.
*/
const char *xstrerror(int errnum);
/**
* Shorthand for xstrerror(errno).
*/
const char *errstr(void);
/**
* 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]);
#include <sys/resource.h>
/**
* Compare two rlim_t values, accounting for infinite limits.
*/
int rlim_cmp(rlim_t a, rlim_t b);
#include <sys/types.h>
/**
* Portable version of makedev().
*/
dev_t xmakedev(int ma, int mi);
/**
* Portable version of major().
*/
int xmajor(dev_t dev);
/**
* Portable version of minor().
*/
int xminor(dev_t dev);
// #include <sys/stat.h>
/**
* Get the access/change/modification time from a struct stat.
*/
#if BFS_HAS_ST_ACMTIM
# define ST_ATIM(sb) (sb).st_atim
# define ST_CTIM(sb) (sb).st_ctim
# define ST_MTIM(sb) (sb).st_mtim
#elif BFS_HAS_ST_ACMTIMESPEC
# define ST_ATIM(sb) (sb).st_atimespec
# define ST_CTIM(sb) (sb).st_ctimespec
# define ST_MTIM(sb) (sb).st_mtimespec
#else
# define ST_ATIM(sb) ((struct timespec) { .tv_sec = (sb).st_atime })
# define ST_CTIM(sb) ((struct timespec) { .tv_sec = (sb).st_ctime })
# define ST_MTIM(sb) ((struct timespec) { .tv_sec = (sb).st_mtime })
#endif
// #include <sys/wait.h>
/**
* waitpid() wrapper that handles EINTR.
*/
pid_t xwaitpid(pid_t pid, int *status, int flags);
// #include <unistd.h>
/**
* 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]);
/**
* 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);
/**
* close() variant that preserves errno.
*
* @param fd
* The file descriptor to close.
*/
void close_quietly(int fd);
/**
* 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);
/**
* Wrapper for faccessat() that handles some portability issues.
*/
int xfaccessat(int fd, const char *path, int amode);
/**
* 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);
/**
* 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);
/**
* 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);
/**
* Wrapper for sysconf() that works around an MSan bug.
*/
long xsysconf(int name);
/**
* Check for a POSIX option[1] at runtime.
*
* [1]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap02.html#tag_02_01_06
*
* @param name
* The symbolic name of the POSIX option (e.g. SPAWN).
* @return
* The value of the option, either -1 or a date like 202405.
*/
#define sysoption(name) \
(_POSIX_##name == 0 ? xsysconf(_SC_##name) : _POSIX_##name)
#include <wchar.h>
/**
* Error-recovering mbrtowc() wrapper.
*
* @param str
* The string to convert.
* @param i
* The current index.
* @param len
* The length of the string.
* @param mb
* The multi-byte decoding state.
* @return
* The wide character at index *i, or WEOF if decoding fails. In either
* case, *i will be advanced to the next multi-byte character.
*/
wint_t xmbrtowc(const char *str, size_t *i, size_t len, mbstate_t *mb);
/**
* 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);
/**
* wcwidth() wrapper that works around LLVM bug #65532.
*/
#define xwcwidth BFS_INTERCEPT(wcwidth)
#include <wctype.h>
/**
* Wrap iswalpha()/iswdigit()/etc.
*/
#define BFS_ISWCTYPE(fn, c) BFS_INTERCEPT(fn)(c)
#define xiswalnum(c) BFS_ISWCTYPE(iswalnum, c)
#define xiswalpha(c) BFS_ISWCTYPE(iswalpha, c)
#define xiswcntrl(c) BFS_ISWCTYPE(iswcntrl, c)
#define xiswdigit(c) BFS_ISWCTYPE(iswdigit, c)
#define xiswlower(c) BFS_ISWCTYPE(iswlower, c)
#define xiswgraph(c) BFS_ISWCTYPE(iswgraph, c)
#define xiswprint(c) BFS_ISWCTYPE(iswprint, c)
#define xiswpunct(c) BFS_ISWCTYPE(iswpunct, c)
#define xiswspace(c) BFS_ISWCTYPE(iswspace, c)
#define xiswupper(c) BFS_ISWCTYPE(iswupper, c)
#define xiswxdigit(c) BFS_ISWCTYPE(iswxdigit, c)
// #include <wordexp.h>
/**
* Flags for wordesc().
*/
enum wesc_flags {
/**
* Escape special characters so that the shell will treat the escaped
* string as a single word.
*/
WESC_SHELL = 1 << 0,
/**
* Escape special characters so that the escaped string is safe to print
* to a TTY.
*/
WESC_TTY = 1 << 1,
};
/**
* Escape a string as a single shell word.
*
* @param dest
* The destination string to fill.
* @param end
* The end of the destination buffer.
* @param src
* The string to escape.
* @param flags
* Controls which characters to escape.
* @return
* The new NUL terminator of the destination, or `end` on truncation.
*/
char *wordesc(char *dest, char *end, const char *str, enum wesc_flags flags);
/**
* Escape a string as a single shell word.
*
* @param dest
* The destination string to fill.
* @param end
* The end of the destination buffer.
* @param src
* The string to escape.
* @param n
* The maximum length of the string.
* @param flags
* Controls which characters to escape.
* @return
* The new NUL terminator of the destination, or `end` on truncation.
*/
char *wordnesc(char *dest, char *end, const char *str, size_t n, enum wesc_flags flags);
#endif // BFS_BFSTD_H
|