1 #ifndef _UTILS_H
2 #define _UTILS_H
3 #include <stdbool.h>
4 #include <string.h>
5 #include <stdint.h>
6
7 /* Is A == B ? */
8 #define streq(a,b) (strcmp((a),(b)) == 0)
9
10 /* Does A start with B ? */
11 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
12
13 /* Does A end in B ? */
strends(const char * a,const char * b)14 static inline bool strends(const char *a, const char *b)
15 {
16 if (strlen(a) < strlen(b))
17 return false;
18
19 return streq(a + strlen(a) - strlen(b), b);
20 }
21
22 #ifndef ARRAY_SIZE
23 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
24 #endif
25
26 void barf(const char *fmt, ...) __attribute__((noreturn));
27 void barf_perror(const char *fmt, ...) __attribute__((noreturn));
28
29 void (*xprintf)(const char *fmt, ...);
30
31 #define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
32
33 /*
34 * Mux errno values onto returned pointers.
35 */
36
ERR_PTR(long error)37 static inline void *ERR_PTR(long error)
38 {
39 return (void *)error;
40 }
41
PTR_ERR(const void * ptr)42 static inline long PTR_ERR(const void *ptr)
43 {
44 return (long)ptr;
45 }
46
IS_ERR(const void * ptr)47 static inline long IS_ERR(const void *ptr)
48 {
49 return ((unsigned long)ptr > (unsigned long)-1000L);
50 }
51
52
53 #endif /* _UTILS_H */
54
55 /*
56 * Local variables:
57 * c-file-style: "linux"
58 * indent-tabs-mode: t
59 * c-indent-level: 8
60 * c-basic-offset: 8
61 * tab-width: 8
62 * End:
63 */
64