chiark / gitweb /
add missing newlines
[elogind.git] / util.c
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 #include <stdlib.h>
8
9 #include "macro.h"
10 #include "util.h"
11
12 usec_t now(clockid_t clock) {
13         struct timespec ts;
14
15         assert_se(clock_gettime(clock, &ts) == 0);
16
17         return timespec_load(&ts);
18 }
19
20 usec_t timespec_load(const struct timespec *ts) {
21         assert(ts);
22
23         return
24                 (usec_t) ts->tv_sec * USEC_PER_SEC +
25                 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
26 }
27
28 struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
29         assert(ts);
30
31         ts->tv_sec = (time_t) (u / USEC_PER_SEC);
32         ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
33
34         return ts;
35 }
36
37 usec_t timeval_load(const struct timeval *tv) {
38         assert(tv);
39
40         return
41                 (usec_t) tv->tv_sec * USEC_PER_SEC +
42                 (usec_t) tv->tv_usec;
43 }
44
45 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
46         assert(tv);
47
48         tv->tv_sec = (time_t) (u / USEC_PER_SEC);
49         tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
50
51         return tv;
52 }
53
54 bool endswith(const char *s, const char *postfix) {
55         size_t sl, pl;
56
57         assert(s);
58         assert(postfix);
59
60         sl = strlen(s);
61         pl = strlen(postfix);
62
63         if (sl < pl)
64                 return false;
65
66         return memcmp(s + sl - pl, postfix, pl) == 0;
67 }
68
69 bool startswith(const char *s, const char *prefix) {
70         size_t sl, pl;
71
72         assert(s);
73         assert(prefix);
74
75         sl = strlen(s);
76         pl = strlen(prefix);
77
78         if (sl < pl)
79                 return false;
80
81         return memcmp(s, prefix, pl) == 0;
82 }
83
84 int nointr_close(int fd) {
85         assert(fd >= 0);
86
87         for (;;) {
88                 int r;
89
90                 if ((r = close(fd)) >= 0)
91                         return r;
92
93                 if (errno != EINTR)
94                         return r;
95         }
96 }
97
98 int parse_boolean(const char *v) {
99         assert(v);
100
101         if (!strcmp(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
102                 return 1;
103         else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
104                 return 0;
105
106         return -EINVAL;
107 }
108
109 int safe_atou(const char *s, unsigned *ret_u) {
110         char *x = NULL;
111         unsigned l;
112
113         assert(s);
114         assert(ret_u);
115
116         errno = 0;
117         l = strtoul(s, &x, 0);
118
119         if (!x || *x || errno)
120                 return errno ? -errno : -EINVAL;
121
122         if ((unsigned) l != l)
123                 return -ERANGE;
124
125         *ret_u = (unsigned) l;
126         return 0;
127 }
128
129 int safe_atoi(const char *s, int *ret_i) {
130         char *x = NULL;
131         int l;
132
133         assert(s);
134         assert(ret_i);
135
136         errno = 0;
137         l = strtol(s, &x, 0);
138
139         if (!x || *x || errno)
140                 return errno ? -errno : -EINVAL;
141
142         if ((int) l != l)
143                 return -ERANGE;
144
145         *ret_i = (unsigned) l;
146         return 0;
147 }
148
149 /* What is interpreted as whitespace? */
150 #define WHITESPACE " \t\n"
151
152 /* Split a string into words. */
153 char *split_spaces(const char *c, size_t *l, char **state) {
154         char *current;
155
156         current = *state ? *state : (char*) c;
157
158         if (!*current || *c == 0)
159                 return NULL;
160
161         current += strspn(current, WHITESPACE);
162         *l = strcspn(current, WHITESPACE);
163         *state = current+*l;
164
165         return (char*) current;
166 }