-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
56 lines (42 loc) · 1.69 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
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <sys/queue.h>
#include "attrs.h"
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
// TAILQ_FOREACH_SAFE and TAILQ_FOREACH_REVERSE_SAFE, which allow elements to
// be removed or freed during the loop, are available on BSDs but not in glibc,
// so shim them here. The implementation is copied from sys/queue.h on macOS.
#ifndef TAILQ_FOREACH_SAFE
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#endif
#ifndef TAILQ_FOREACH_REVERSE_SAFE
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
for ((var) = TAILQ_LAST((head), headname); \
(var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
(var) = (tvar))
#endif
ATTR_PRINTFLIKE(1, 2)
void debug(const char *format, ...);
void *xmalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *s);
bool strtoi(char *s, int *result);
// Returns a new string where all occurrences of 'from' are replaced with 'to'.
char *strrep(char *s, char *from, char *to);
size_t strcnt(char *s, char c);
// Returns absolute path, with unnecessary //, ./ or ../ removed.
// Also expands a leading ~ to the home directory.
// Unlike realpath(3), does not resolve symlinks.
char *abspath(const char *path);
const char *relpath(const char *path, const char *start);
const char *homedir(void);
struct region {
size_t start;
size_t end;
};
struct region *region_set(struct region *region, size_t start, size_t end);