]> git.ipfire.org Git - thirdparty/systemd.git/blame - util.h
add simple event loop
[thirdparty/systemd.git] / util.h
CommitLineData
60918275
LP
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>
5cb5a6ff 10#include <stdlib.h>
60918275
LP
11
12typedef uint64_t usec_t;
13
14#define USEC_PER_SEC 1000000ULL
15#define NSEC_PER_USEC 1000ULL
16
17usec_t now(clockid_t clock);
18
19usec_t timespec_load(const struct timespec *ts);
20struct timespec *timespec_store(struct timespec *ts, usec_t u);
21
22usec_t timeval_load(const struct timeval *tv);
23struct 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
33static inline const char* yes_no(bool b) {
34 return b ? "yes" : "no";
35}
36
37static inline const char* strempty(const char *s) {
38 return s ? s : "";
39}
40
41static inline const char* strnull(const char *s) {
42 return s ? s : "(null)";
43}
44
04fd6fe4
LP
45static inline const char *strna(const char *s) {
46 return s ? s : "n/a";
47}
48
49static inline bool is_path_absolute(const char *p) {
50 return *p == '/';
51}
52
60918275
LP
53bool endswith(const char *s, const char *postfix);
54bool startswith(const char *s, const char *prefix);
55
42f4e3c4 56int close_nointr(int fd);
60918275 57
85261803
LP
58int parse_boolean(const char *v);
59
60int safe_atou(const char *s, unsigned *ret_u);
61int safe_atoi(const char *s, int *ret_i);
62
a41e8209
LP
63char *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
60918275 68#endif