-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
427 lines (359 loc) · 8.3 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <sys/types.h>
#include <dev/evdev/input.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xlocale.h>
#include "util.h"
#include "util-list.h"
#define HAVE_LOCALE_H 1
bool
streq(const char *str1, const char *str2)
{
/* one NULL, one not NULL is always false */
if (str1 && str2)
return strcmp(str1, str2) == 0;
return str1 == str2;
}
bool
strneq(const char *str1, const char *str2, int n)
{
/* one NULL, one not NULL is always false */
if (str1 && str2)
return strncmp(str1, str2, n) == 0;
return str1 == str2;
}
void *
zalloc(size_t size)
{
void *p;
/* We never need to alloc anything more than 1,5 MB so we can assume
* if we ever get above that something's going wrong */
if (size > 1536 * 1024)
assert(!"bug: internal malloc size limit exceeded");
p = calloc(1, size);
if (!p)
abort();
return p;
}
/**
* strdup guaranteed to succeed. If the input string is NULL, the output
* string is NULL. If the input string is a string pointer, we strdup or
* abort on failure.
*/
char *
safe_strdup(const char *str)
{
char *s;
if (!str)
return NULL;
s = strdup(str);
if (!s)
abort();
return s;
}
/**
* Simple wrapper for asprintf that ensures the passed in-pointer is set
* to NULL upon error.
* The standard asprintf() call does not guarantee the passed in pointer
* will be NULL'ed upon failure, whereas this wrapper does.
*
* @param strp pointer to set to newly allocated string.
* This pointer should be passed to free() to release when done.
* @param fmt the format string to use for printing.
* @return The number of bytes printed (excluding the null byte terminator)
* upon success or -1 upon failure. In the case of failure the pointer is set
* to NULL.
*/
__attribute__ ((format (printf, 2, 3)))
int
xasprintf(char **strp, const char *fmt, ...)
{
int rc = 0;
va_list args;
va_start(args, fmt);
rc = vasprintf(strp, fmt, args);
va_end(args);
if ((rc == -1) && strp)
*strp = NULL;
return rc;
}
bool
safe_atoi_base(const char *str, int *val, int base)
{
char *endptr;
long v;
assert(base == 10 || base == 16 || base == 8);
errno = 0;
v = strtol(str, &endptr, base);
if (errno > 0)
return false;
if (str == endptr)
return false;
if (*str != '\0' && *endptr != '\0')
return false;
if (v > INT_MAX || v < INT_MIN)
return false;
*val = v;
return true;
}
bool
safe_atou_base(const char *str, unsigned int *val, int base)
{
char *endptr;
unsigned long v;
assert(base == 10 || base == 16 || base == 8);
errno = 0;
v = strtoul(str, &endptr, base);
if (errno > 0)
return false;
if (str == endptr)
return false;
if (*str != '\0' && *endptr != '\0')
return false;
if ((long)v < 0)
return false;
*val = v;
return true;
}
bool
safe_atod(const char *str, double *val)
{
char *endptr;
double v;
#ifdef HAVE_LOCALE_H
locale_t c_locale;
#endif
size_t slen = strlen(str);
/* We don't have a use-case where we want to accept hex for a double
* or any of the other values strtod can parse */
for (size_t i = 0; i < slen; i++) {
char c = str[i];
if (isdigit(c))
continue;
switch(c) {
case '+':
case '-':
case '.':
break;
default:
return false;
}
}
#ifdef HAVE_LOCALE_H
/* Create a "C" locale to force strtod to use '.' as separator */
c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
if (c_locale == (locale_t)0)
return false;
errno = 0;
v = strtod_l(str, &endptr, c_locale);
freelocale(c_locale);
#else
/* No locale support in provided libc, assume it already uses '.' */
errno = 0;
v = strtod(str, &endptr);
#endif
if (errno > 0)
return false;
if (str == endptr)
return false;
if (*str != '\0' && *endptr != '\0')
return false;
if (v != 0.0 && !isnormal(v))
return false;
*val = v;
return true;
}
void
strv_free(char **strv) {
char **s = strv;
if (!strv)
return;
while (*s != NULL) {
free(*s);
*s = (char*)0x1; /* detect use-after-free */
s++;
}
free (strv);
}
/**
* Return the next word in a string pointed to by state before the first
* separator character. Call repeatedly to tokenize a whole string.
*
* @param state Current state
* @param len String length of the word returned
* @param separators List of separator characters
*
* @return The first word in *state, NOT null-terminated
*/
const char *
next_word(const char **state, size_t *len, const char *separators)
{
const char *next = *state;
size_t l;
if (!*next)
return NULL;
next += strspn(next, separators);
if (!*next) {
*state = next;
return NULL;
}
l = strcspn(next, separators);
*state = next + l;
*len = l;
return next;
}
/**
* Return a null-terminated string array with the tokens in the input
* string, e.g. "one two\tthree" with a separator list of " \t" will return
* an array [ "one", "two", "three", NULL ] and num elements 3.
*
* Use strv_free() to free the array.
*
* Another example:
* result = strv_from_string("+1-2++3--4++-+5-+-", "+-", &nelem)
* result == [ "1", "2", "3", "4", "5", NULL ] and nelem == 5
*
* @param in Input string
* @param separators List of separator characters
* @param num_elements Number of elements found in the input string
*
* @return A null-terminated string array or NULL on errors
*/
char **
strv_from_string(const char *in, const char *separators, size_t *num_elements)
{
assert(in != NULL);
assert(separators != NULL);
assert(num_elements != NULL);
const char *s = in;
size_t l, nelems = 0;
while (next_word(&s, &l, separators) != NULL)
nelems++;
if (nelems == 0) {
*num_elements = 0;
return NULL;
}
size_t strv_len = nelems + 1; /* NULL-terminated */
char **strv = zalloc(strv_len * sizeof *strv);
size_t idx = 0;
const char *word;
s = in;
while ((word = next_word(&s, &l, separators)) != NULL) {
char *copy = strndup(word, l);
if (!copy) {
strv_free(strv);
*num_elements = 0;
return NULL;
}
strv[idx++] = copy;
}
*num_elements = nelems;
return strv;
}
/**
* Return true if str ends in suffix, false otherwise. If the suffix is the
* empty string, strendswith() always returns false.
*/
bool
strendswith(const char *str, const char *suffix)
{
size_t slen = strlen(str);
size_t suffixlen = strlen(suffix);
size_t offset;
if (slen == 0 || suffixlen == 0 || suffixlen > slen)
return false;
offset = slen - suffixlen;
return strneq(&str[offset], suffix, suffixlen);
}
/**
* Parses a simple dimension string in the form of "10x40". The two
* numbers must be positive integers in decimal notation.
* On success, the two numbers are stored in w and h. On failure, w and h
* are unmodified.
*
* @param prop The value of the property
* @param w Returns the first component of the dimension
* @param h Returns the second component of the dimension
* @return true on success, false otherwise
*/
bool
parse_dimension_property(const char *prop, size_t *w, size_t *h)
{
int x, y;
if (!prop)
return false;
if (sscanf(prop, "%dx%d", &x, &y) != 2)
return false;
if (x <= 0 || y <= 0)
return false;
*w = (size_t)x;
*h = (size_t)y;
return true;
}
/**
* Parses a string of the format "a:b" where both a and b must be integer
* numbers and a > b. Also allowed is the special string value "none" which
* amounts to unsetting the property.
*
* @param prop The value of the property
* @param hi Set to the first digit or 0 in case of 'none'
* @param lo Set to the second digit or 0 in case of 'none'
* @return true on success, false otherwise
*/
bool
parse_range_property(const char *prop, int *hi, int *lo)
{
int first, second;
if (!prop)
return false;
if (streq(prop, "none")) {
*hi = 0;
*lo = 0;
return true;
}
if (sscanf(prop, "%d:%d", &first, &second) != 2)
return false;
if (second >= first)
return false;
*hi = first;
*lo = second;
return true;
}
bool
parse_boolean_property(const char *prop, bool *b)
{
if (!prop)
return false;
if (streq(prop, "1"))
*b = true;
else if (streq(prop, "0"))
*b = false;
else
return false;
return true;
}
/*
* NOT IMPLEMENTED
*/
bool
parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents)
{
return true;
}
/*
* NOT IMPLEMENTED
*/
bool
parse_input_prop_property(const char *prop, struct input_prop *props_out, size_t *nprops)
{
return true;
}