1 #ifndef _UTILS_H
2 #define _UTILS_H
3 #include <stdbool.h>
4 #include <string.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 
8 #include <xen-tools/common-macros.h>
9 
10 #include "xenstore_lib.h"
11 
12 /* Is A == B ? */
13 #define streq(a,b) (strcmp((a),(b)) == 0)
14 
15 /* Does A start with B ? */
16 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
17 
18 /* Does A end in B ? */
strends(const char * a,const char * b)19 static inline bool strends(const char *a, const char *b)
20 {
21 	if (strlen(a) < strlen(b))
22 		return false;
23 
24 	return streq(a + strlen(a) - strlen(b), b);
25 }
26 
27 /*
28  * Write NUL bytes for aligning state data to 8 bytes.
29  */
30 const char *dump_state_align(FILE *fp);
31 
32 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))
33 
34 #define __noreturn __attribute__((noreturn))
35 
36 void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
37 void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
38 
39 /* Function pointer as xprintf() can be configured at runtime. */
40 extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
41 
42 #define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
43 
44 /*
45  * Mux errno values onto returned pointers.
46  */
47 
ERR_PTR(long error)48 static inline void *ERR_PTR(long error)
49 {
50 	return (void *)error;
51 }
52 
PTR_ERR(const void * ptr)53 static inline long PTR_ERR(const void *ptr)
54 {
55 	return (long)ptr;
56 }
57 
IS_ERR(const void * ptr)58 static inline long IS_ERR(const void *ptr)
59 {
60 	return ((unsigned long)ptr > (unsigned long)-1000L);
61 }
62 
63 
64 #endif /* _UTILS_H */
65 
66 /*
67  * Local variables:
68  *  mode: C
69  *  c-file-style: "linux"
70  *  indent-tabs-mode: t
71  *  c-basic-offset: 8
72  *  tab-width: 8
73  * End:
74  */
75