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