1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
12 usec_t now(clockid_t clock) {
15 assert_se(clock_gettime(clock, &ts) == 0);
17 return timespec_load(&ts);
20 usec_t timespec_load(const struct timespec *ts) {
24 (usec_t) ts->tv_sec * USEC_PER_SEC +
25 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
28 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
31 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
32 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
37 usec_t timeval_load(const struct timeval *tv) {
41 (usec_t) tv->tv_sec * USEC_PER_SEC +
45 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
48 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
49 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
54 bool endswith(const char *s, const char *postfix) {
66 return memcmp(s + sl - pl, postfix, pl) == 0;
69 bool startswith(const char *s, const char *prefix) {
81 return memcmp(s, prefix, pl) == 0;
84 int nointr_close(int fd) {
90 if ((r = close(fd)) >= 0)
98 int parse_boolean(const char *v) {
101 if (!strcmp(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
103 else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
109 int safe_atou(const char *s, unsigned *ret_u) {
117 l = strtoul(s, &x, 0);
119 if (!x || *x || errno)
120 return errno ? -errno : -EINVAL;
122 if ((unsigned) l != l)
125 *ret_u = (unsigned) l;
129 int safe_atoi(const char *s, int *ret_i) {
137 l = strtol(s, &x, 0);
139 if (!x || *x || errno)
140 return errno ? -errno : -EINVAL;
145 *ret_i = (unsigned) l;