]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #include <assert.h> | |
4 | #include <string.h> | |
5 | #include <unistd.h> | |
6 | #include <errno.h> | |
7 | ||
8 | #include "macro.h" | |
9 | #include "util.h" | |
10 | ||
11 | usec_t now(clockid_t clock) { | |
12 | struct timespec ts; | |
13 | ||
14 | assert_se(clock_gettime(clock, &ts) == 0); | |
15 | ||
16 | return timespec_load(&ts); | |
17 | } | |
18 | ||
19 | usec_t timespec_load(const struct timespec *ts) { | |
20 | assert(ts); | |
21 | ||
22 | return | |
23 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
24 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
25 | } | |
26 | ||
27 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { | |
28 | assert(ts); | |
29 | ||
30 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
31 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
32 | ||
33 | return ts; | |
34 | } | |
35 | ||
36 | usec_t timeval_load(const struct timeval *tv) { | |
37 | assert(tv); | |
38 | ||
39 | return | |
40 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
41 | (usec_t) tv->tv_usec; | |
42 | } | |
43 | ||
44 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
45 | assert(tv); | |
46 | ||
47 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
48 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
49 | ||
50 | return tv; | |
51 | } | |
52 | ||
53 | bool endswith(const char *s, const char *postfix) { | |
54 | size_t sl, pl; | |
55 | ||
56 | assert(s); | |
57 | assert(postfix); | |
58 | ||
59 | sl = strlen(s); | |
60 | pl = strlen(postfix); | |
61 | ||
62 | if (sl < pl) | |
63 | return false; | |
64 | ||
65 | return memcmp(s + sl - pl, postfix, pl) == 0; | |
66 | } | |
67 | ||
68 | bool startswith(const char *s, const char *prefix) { | |
69 | size_t sl, pl; | |
70 | ||
71 | assert(s); | |
72 | assert(prefix); | |
73 | ||
74 | sl = strlen(s); | |
75 | pl = strlen(prefix); | |
76 | ||
77 | if (sl < pl) | |
78 | return false; | |
79 | ||
80 | return memcmp(s, prefix, pl) == 0; | |
81 | } | |
82 | ||
83 | int nointr_close(int fd) { | |
84 | assert(fd >= 0); | |
85 | ||
86 | for (;;) { | |
87 | int r; | |
88 | ||
89 | if ((r = close(fd)) >= 0) | |
90 | return r; | |
91 | ||
92 | if (errno != EINTR) | |
93 | return r; | |
94 | } | |
95 | } |