diff options
author | Lennart Poettering <lennart@poettering.net> | 2009-11-18 00:42:52 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2009-11-18 00:42:52 +0100 |
commit | 6091827530d6dd43479d6709fb6e9f745c11e900 (patch) | |
tree | 3edf7ccdeeb60b634d63e1abcaa8132a6259a331 /util.c |
initial commit
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/util.c b/util.c new file mode 100644 index 0000000000..3ef190fa2a --- /dev/null +++ b/util.c @@ -0,0 +1,95 @@ +/*-*- Mode: C; c-basic-offset: 8 -*-*/ + +#include <assert.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "macro.h" +#include "util.h" + +usec_t now(clockid_t clock) { + struct timespec ts; + + assert_se(clock_gettime(clock, &ts) == 0); + + return timespec_load(&ts); +} + +usec_t timespec_load(const struct timespec *ts) { + assert(ts); + + return + (usec_t) ts->tv_sec * USEC_PER_SEC + + (usec_t) ts->tv_nsec / NSEC_PER_USEC; +} + +struct timespec *timespec_store(struct timespec *ts, usec_t u) { + assert(ts); + + ts->tv_sec = (time_t) (u / USEC_PER_SEC); + ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); + + return ts; +} + +usec_t timeval_load(const struct timeval *tv) { + assert(tv); + + return + (usec_t) tv->tv_sec * USEC_PER_SEC + + (usec_t) tv->tv_usec; +} + +struct timeval *timeval_store(struct timeval *tv, usec_t u) { + assert(tv); + + tv->tv_sec = (time_t) (u / USEC_PER_SEC); + tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); + + return tv; +} + +bool endswith(const char *s, const char *postfix) { + size_t sl, pl; + + assert(s); + assert(postfix); + + sl = strlen(s); + pl = strlen(postfix); + + if (sl < pl) + return false; + + return memcmp(s + sl - pl, postfix, pl) == 0; +} + +bool startswith(const char *s, const char *prefix) { + size_t sl, pl; + + assert(s); + assert(prefix); + + sl = strlen(s); + pl = strlen(prefix); + + if (sl < pl) + return false; + + return memcmp(s, prefix, pl) == 0; +} + +int nointr_close(int fd) { + assert(fd >= 0); + + for (;;) { + int r; + + if ((r = close(fd)) >= 0) + return r; + + if (errno != EINTR) + return r; + } +} |