-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
91 lines (77 loc) · 2.31 KB
/
util.h
File metadata and controls
91 lines (77 loc) · 2.31 KB
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
#ifndef _edna_util_
#define _edna_util_
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#define argv(...) ((char *[]){__VA_ARGS__})
#define arr_len(arr) (sizeof (arr) / sizeof *(arr))
#define fozin(P, F) ((P) ? (P)->F : 0)
#define write_str(fd, str) write(fd, str, strlen(str))
#define repeat(NTIMES) for (size_t _i=0; _i<NTIMES; ++i)
#define iterate(VAR, NITER) for (size_t VAR=0; VAR<NITER; ++VAR)
#ifndef LONG_BIT
# define LONG_BIT sizeof (long) * 8
#endif
typedef unsigned long ulong;
typedef unsigned int uint;
static inline bool in_range(ulong b, ulong e, ulong n);
static inline void plshift(void *l, void *m, void *r);
static inline void *memswp(void *A, void *B, size_t n);
static inline size_t next_line(char *s, size_t n);
static inline void pswp(void *A, void *B);
static inline int to_sign(bool b);
static inline int ucmp(ulong a, ulong b);
static inline int uclz(ulong a);
static inline int ufls(ulong a);
static inline void ulrotate(ulong *l, ulong *m, ulong *r);
static inline void ulshift(ulong *l, ulong *m, ulong r);
static inline ulong umax(ulong a, ulong b);
static inline ulong umin(ulong a, ulong b);
static inline bool uunder(ulong n, ulong i);
bool in_range(ulong a, ulong z, ulong n) { return z > n - a; }
int to_sign(bool b) { return 2*b - 1; }
void ulshift(ulong *l, ulong *m, ulong r) {*l = *m, *m = r;}
int ucmp(ulong a, ulong b) { return (a >= b) - (a <= b); }
int uclz(ulong a) { return a ? __builtin_clzl(a) : LONG_BIT; }
int ufls(ulong a) { return LONG_BIT - uclz(a); }
ulong umax(ulong a, ulong b) { return a > b ? a : b; }
ulong umin(ulong a, ulong b) { return a < b ? a : b; }
bool uunder(ulong n, ulong a) { return n + a < a; }
void
ulrotate(ulong *l, ulong *m, ulong *r)
{
ulong t = *l;
*l=*m, *m=*r, *r=t;
}
void
plshift(void *L, void *M, void *r)
{
void **l=L, **m=M;
*l=*m, *m=r;
}
void *
memswp(void *A, void *B, size_t n)
{
char *a=A, *b=B, m;
while (n --> 0) m=a[n], a[n]=b[n], b[n]=m;
return a;
}
size_t
next_line(char *s, size_t n)
{
char *l;
return (l = memchr(s, '\n', n)) ? l - s + 1 : n;
}
void
pswp(void *A, void *B)
{
void **a=A, **b=B, *p;
p=*a, *a=*b, *b=p;
}
char *asprintf(char *fmt, ...) __attribute__((format (printf, 1, 2)));
char *vasprintf(char *fmt, va_list args);
int msleep(size_t);
int mk_pty(void);
int open_pty(int);
#endif