1 #ifndef __SUBCMD_UTIL_H
2 #define __SUBCMD_UTIL_H
8 #define NORETURN __attribute__((__noreturn__))
10 static inline void report(const char *prefix
, const char *err
, va_list params
)
13 vsnprintf(msg
, sizeof(msg
), err
, params
);
14 fprintf(stderr
, " %s%s\n", prefix
, msg
);
17 static NORETURN
inline void die(const char *err
, ...)
21 va_start(params
, err
);
22 report(" Fatal: ", err
, params
);
27 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
29 #define alloc_nr(x) (((x)+16)*3/2)
32 * Realloc the buffer pointed at by variable 'x' so that it can hold
33 * at least 'nr' entries; the number of entries currently allocated
34 * is 'alloc', using the standard growing factor alloc_nr() macro.
36 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
38 #define ALLOC_GROW(x, nr, alloc) \
41 if (alloc_nr(alloc) < (nr)) \
44 alloc = alloc_nr(alloc); \
45 x = xrealloc((x), alloc * sizeof(*(x))); \
49 static inline void *xrealloc(void *ptr
, size_t size
)
51 void *ret
= realloc(ptr
, size
);
53 ret
= realloc(ptr
, 1);
55 ret
= realloc(ptr
, size
);
57 ret
= realloc(ptr
, 1);
59 die("Out of memory, realloc failed");
64 #define astrcatf(out, fmt, ...) \
67 if (asprintf((out), "%s" fmt, tmp ?: "", ## __VA_ARGS__) == -1) \
68 die("asprintf failed"); \
72 static inline void astrcat(char **out
, const char *add
)
76 if (asprintf(out
, "%s%s", tmp
?: "", add
) == -1)
77 die("asprintf failed");
82 static inline int prefixcmp(const char *str
, const char *prefix
)
84 for (; ; str
++, prefix
++)
87 else if (*str
!= *prefix
)
88 return (unsigned char)*prefix
- (unsigned char)*str
;
91 #endif /* __SUBCMD_UTIL_H */