chiark / gitweb /
first attempt in implementinging execution logic
[elogind.git] / util.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef fooutilhfoo
4 #define fooutilhfoo
5
6 #include <inttypes.h>
7 #include <time.h>
8 #include <sys/time.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11
12 typedef uint64_t usec_t;
13
14 #define USEC_PER_SEC 1000000ULL
15 #define NSEC_PER_USEC 1000ULL
16
17 usec_t now(clockid_t clock);
18
19 usec_t timespec_load(const struct timespec *ts);
20 struct timespec *timespec_store(struct timespec *ts, usec_t u);
21
22 usec_t timeval_load(const struct timeval *tv);
23 struct timeval *timeval_store(struct timeval *tv, usec_t u);
24
25 #define streq(a,b) (strcmp((a),(b)) == 0)
26
27 #define new(t, n) ((t*) malloc(sizeof(t)*(n)))
28
29 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
30
31 #define malloc0(n) (calloc((n), 1))
32
33 static inline const char* yes_no(bool b) {
34         return b ? "yes" : "no";
35 }
36
37 static inline const char* strempty(const char *s) {
38         return s ? s : "";
39 }
40
41 static inline const char* strnull(const char *s) {
42         return s ? s : "(null)";
43 }
44
45 static inline const char *strna(const char *s) {
46         return s ? s : "n/a";
47 }
48
49 static inline bool is_path_absolute(const char *p) {
50         return *p == '/';
51 }
52
53 bool endswith(const char *s, const char *postfix);
54 bool startswith(const char *s, const char *prefix);
55
56 int close_nointr(int fd);
57
58 int parse_boolean(const char *v);
59
60 int safe_atou(const char *s, unsigned *ret_u);
61 int safe_atoi(const char *s, int *ret_i);
62
63 char *split_spaces(const char *c, size_t *l, char **state);
64
65 #define FOREACH_WORD(word, length, s, state)    \
66         for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
67
68 #endif