1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include "alloc-util.h"
16 #include "errno-list.h"
17 //#include "extract-word.h"
18 #include "locale-util.h"
20 #include "parse-util.h"
21 #include "process-util.h"
22 #include "string-util.h"
24 /// Additional includes needed by elogind
25 #include "musl_missing.h"
27 int parse_boolean(const char *v) {
30 if (streq(v, "1") || strcaseeq(v, "yes") || strcaseeq(v, "y") || strcaseeq(v, "true") || strcaseeq(v, "t") || strcaseeq(v, "on"))
32 else if (streq(v, "0") || strcaseeq(v, "no") || strcaseeq(v, "n") || strcaseeq(v, "false") || strcaseeq(v, "f") || strcaseeq(v, "off"))
38 int parse_pid(const char *s, pid_t* ret_pid) {
46 r = safe_atolu(s, &ul);
52 if ((unsigned long) pid != ul)
55 if (!pid_is_valid(pid))
62 int parse_mode(const char *s, mode_t *ret) {
69 s += strspn(s, WHITESPACE);
77 if (!x || x == s || *x != 0)
79 if (l < 0 || l > 07777)
86 int parse_ifindex(const char *s, int *ret) {
89 r = safe_atoi(s, &ifi);
99 int parse_size(const char *t, uint64_t base, uint64_t *size) {
101 /* Soo, sometimes we want to parse IEC binary suffixes, and
102 * sometimes SI decimal suffixes. This function can parse
103 * both. Which one is the right way depends on the
104 * context. Wikipedia suggests that SI is customary for
105 * hardware metrics and network speeds, while IEC is
106 * customary for most data sizes used by software and volatile
107 * (RAM) memory. Hence be careful which one you pick!
109 * In either case we use just K, M, G as suffix, and not Ki,
110 * Mi, Gi or so (as IEC would suggest). That's because that's
111 * frickin' ugly. But this means you really need to make sure
112 * to document which base you are parsing when you use this
117 unsigned long long factor;
120 static const struct table iec[] = {
121 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
122 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
123 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
124 { "G", 1024ULL*1024ULL*1024ULL },
125 { "M", 1024ULL*1024ULL },
131 static const struct table si[] = {
132 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
133 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
134 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
135 { "G", 1000ULL*1000ULL*1000ULL },
136 { "M", 1000ULL*1000ULL },
142 const struct table *table;
144 unsigned long long r = 0;
145 unsigned n_entries, start_pos = 0;
148 assert(IN_SET(base, 1000, 1024));
153 n_entries = ELEMENTSOF(si);
156 n_entries = ELEMENTSOF(iec);
161 unsigned long long l, tmp;
166 p += strspn(p, WHITESPACE);
169 l = strtoull(p, &e, 10);
180 /* strtoull() itself would accept space/+/- */
181 if (*e >= '0' && *e <= '9') {
182 unsigned long long l2;
185 l2 = strtoull(e, &e2, 10);
189 /* Ignore failure. E.g. 10.M is valid */
196 e += strspn(e, WHITESPACE);
198 for (i = start_pos; i < n_entries; i++)
199 if (startswith(e, table[i].suffix))
205 if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
208 tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
209 if (tmp > ULLONG_MAX - r)
213 if ((unsigned long long) (uint64_t) r != r)
216 p = e + strlen(table[i].suffix);
227 #if 0 /// UNNEEDED by elogind
228 int parse_range(const char *t, unsigned *lower, unsigned *upper) {
229 _cleanup_free_ char *word = NULL;
236 /* Extract the lower bound. */
237 r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
243 r = safe_atou(word, &l);
247 /* Check for the upper bound and extract it if needed */
249 /* Single number with no dashes. */
252 /* Trailing dash is an error. */
255 r = safe_atou(t, &u);
266 int parse_errno(const char *t) {
271 r = errno_from_name(t);
275 r = safe_atoi(t, &e);
279 /* 0 is also allowed here */
280 if (!errno_is_valid(e) && e != 0)
286 int parse_syscall_and_errno(const char *in, char **name, int *error) {
287 _cleanup_free_ char *n = NULL;
296 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
297 * If errno is omitted, then error is set to -1.
298 * Empty syscall name is not allowed.
299 * Here, we do not check that the syscall name is valid or not.
304 e = parse_errno(p + 1);
308 n = strndup(in, p - in);
324 char *format_bytes(char *buf, size_t l, uint64_t t) {
327 /* This only does IEC units so far */
329 static const struct {
333 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
334 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
335 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
336 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
337 { "M", UINT64_C(1024)*UINT64_C(1024) },
338 { "K", UINT64_C(1024) },
341 if (t == (uint64_t) -1)
344 for (i = 0; i < ELEMENTSOF(table); i++) {
346 if (t >= table[i].factor) {
348 "%" PRIu64 ".%" PRIu64 "%s",
350 ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10),
357 snprintf(buf, l, "%" PRIu64 "B", t);
365 int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
373 /* strtoul() is happy to parse negative values, and silently
374 * converts them to unsigned values without generating an
375 * error. We want a clean error, hence let's look for the "-"
376 * prefix on our own, and generate an error. But let's do so
377 * only after strtoul() validated that the string is clean
378 * otherwise, so that we return EINVAL preferably over
381 s += strspn(s, WHITESPACE);
384 l = strtoul(s, &x, base);
387 if (!x || x == s || *x != 0)
391 if ((unsigned long) (unsigned) l != l)
394 *ret_u = (unsigned) l;
398 int safe_atoi(const char *s, int *ret_i) {
406 l = strtol(s, &x, 0);
409 if (!x || x == s || *x != 0)
411 if ((long) (int) l != l)
418 int safe_atollu(const char *s, long long unsigned *ret_llu) {
420 unsigned long long l;
425 s += strspn(s, WHITESPACE);
428 l = strtoull(s, &x, 0);
431 if (!x || x == s || *x != 0)
440 int safe_atolli(const char *s, long long int *ret_lli) {
448 l = strtoll(s, &x, 0);
451 if (!x || x == s || *x != 0)
458 int safe_atou8(const char *s, uint8_t *ret) {
465 s += strspn(s, WHITESPACE);
468 l = strtoul(s, &x, 0);
471 if (!x || x == s || *x != 0)
475 if ((unsigned long) (uint8_t) l != l)
482 int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
490 s += strspn(s, WHITESPACE);
493 l = strtoul(s, &x, base);
496 if (!x || x == s || *x != 0)
500 if ((unsigned long) (uint16_t) l != l)
507 int safe_atoi16(const char *s, int16_t *ret) {
515 l = strtol(s, &x, 0);
518 if (!x || x == s || *x != 0)
520 if ((long) (int16_t) l != l)
527 int safe_atod(const char *s, double *ret_d) {
528 _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
535 loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
536 if (loc == (locale_t) 0)
540 d = strtod_l(s, &x, loc);
543 if (!x || x == s || *x != 0)
550 int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
557 /* accept any number of digits, strtoull is limted to 19 */
558 for (i=0; i < digits; i++,s++) {
559 if (*s < '0' || *s > '9') {
563 /* too few digits, pad with 0 */
564 for (; i < digits; i++)
575 if (*s >= '5' && *s <= '9')
578 s += strspn(s, DIGITS);
586 int parse_percent_unbounded(const char *p) {
590 pc = endswith(p, "%");
594 n = strndupa(p, pc - p);
595 r = safe_atoi(n, &v);
604 int parse_percent(const char *p) {
607 v = parse_percent_unbounded(p);
614 #if 0 /// UNNEEDED by elogind
615 int parse_nice(const char *p, int *ret) {
618 r = safe_atoi(p, &n);
622 if (!nice_is_valid(n))
629 int parse_ip_port(const char *s, uint16_t *ret) {
633 r = safe_atou16(s, &l);
646 int parse_dev(const char *s, dev_t *ret) {
650 if (sscanf(s, "%u:%u", &x, &y) != 2)
654 if ((unsigned) major(d) != x || (unsigned) minor(d) != y)