1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 //#include "extract-word.h"
24 #include "parse-util.h"
25 #include "string-util.h"
28 int parse_boolean(const char *v) {
31 if (streq(v, "1") || strcaseeq(v, "yes") || strcaseeq(v, "y") || strcaseeq(v, "true") || strcaseeq(v, "t") || strcaseeq(v, "on"))
33 else if (streq(v, "0") || strcaseeq(v, "no") || strcaseeq(v, "n") || strcaseeq(v, "false") || strcaseeq(v, "f") || strcaseeq(v, "off"))
39 int parse_pid(const char *s, pid_t* ret_pid) {
47 r = safe_atolu(s, &ul);
53 if ((unsigned long) pid != ul)
63 int parse_mode(const char *s, mode_t *ret) {
70 s += strspn(s, WHITESPACE);
78 if (!x || x == s || *x)
80 if (l < 0 || l > 07777)
87 int parse_ifindex(const char *s, int *ret) {
90 r = safe_atoi(s, &ifi);
100 int parse_size(const char *t, uint64_t base, uint64_t *size) {
102 /* Soo, sometimes we want to parse IEC binary suffixes, and
103 * sometimes SI decimal suffixes. This function can parse
104 * both. Which one is the right way depends on the
105 * context. Wikipedia suggests that SI is customary for
106 * hardware metrics and network speeds, while IEC is
107 * customary for most data sizes used by software and volatile
108 * (RAM) memory. Hence be careful which one you pick!
110 * In either case we use just K, M, G as suffix, and not Ki,
111 * Mi, Gi or so (as IEC would suggest). That's because that's
112 * frickin' ugly. But this means you really need to make sure
113 * to document which base you are parsing when you use this
118 unsigned long long factor;
121 static const struct table iec[] = {
122 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
123 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
124 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
125 { "G", 1024ULL*1024ULL*1024ULL },
126 { "M", 1024ULL*1024ULL },
132 static const struct table si[] = {
133 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
134 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
135 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
136 { "G", 1000ULL*1000ULL*1000ULL },
137 { "M", 1000ULL*1000ULL },
143 const struct table *table;
145 unsigned long long r = 0;
146 unsigned n_entries, start_pos = 0;
149 assert(base == 1000 || base == 1024);
154 n_entries = ELEMENTSOF(si);
157 n_entries = ELEMENTSOF(iec);
162 unsigned long long l, tmp;
167 p += strspn(p, WHITESPACE);
170 l = strtoull(p, &e, 10);
181 /* strtoull() itself would accept space/+/- */
182 if (*e >= '0' && *e <= '9') {
183 unsigned long long l2;
186 l2 = strtoull(e, &e2, 10);
190 /* Ignore failure. E.g. 10.M is valid */
197 e += strspn(e, WHITESPACE);
199 for (i = start_pos; i < n_entries; i++)
200 if (startswith(e, table[i].suffix))
206 if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
209 tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
210 if (tmp > ULLONG_MAX - r)
214 if ((unsigned long long) (uint64_t) r != r)
217 p = e + strlen(table[i].suffix);
228 /// UNNEEDED by elogind
230 int parse_range(const char *t, unsigned *lower, unsigned *upper) {
231 _cleanup_free_ char *word = NULL;
238 /* Extract the lower bound. */
239 r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
245 r = safe_atou(word, &l);
249 /* Check for the upper bound and extract it if needed */
251 /* Single number with no dashes. */
254 /* Trailing dash is an error. */
257 r = safe_atou(t, &u);
267 char *format_bytes(char *buf, size_t l, uint64_t t) {
270 /* This only does IEC units so far */
272 static const struct {
276 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
277 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
278 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
279 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
280 { "M", UINT64_C(1024)*UINT64_C(1024) },
281 { "K", UINT64_C(1024) },
284 if (t == (uint64_t) -1)
287 for (i = 0; i < ELEMENTSOF(table); i++) {
289 if (t >= table[i].factor) {
291 "%" PRIu64 ".%" PRIu64 "%s",
293 ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10),
300 snprintf(buf, l, "%" PRIu64 "B", t);
309 int safe_atou(const char *s, unsigned *ret_u) {
316 /* strtoul() is happy to parse negative values, and silently
317 * converts them to unsigned values without generating an
318 * error. We want a clean error, hence let's look for the "-"
319 * prefix on our own, and generate an error. But let's do so
320 * only after strtoul() validated that the string is clean
321 * otherwise, so that we return EINVAL preferably over
324 s += strspn(s, WHITESPACE);
327 l = strtoul(s, &x, 0);
330 if (!x || x == s || *x)
334 if ((unsigned long) (unsigned) l != l)
337 *ret_u = (unsigned) l;
341 int safe_atoi(const char *s, int *ret_i) {
349 l = strtol(s, &x, 0);
352 if (!x || x == s || *x)
354 if ((long) (int) l != l)
361 int safe_atollu(const char *s, long long unsigned *ret_llu) {
363 unsigned long long l;
368 s += strspn(s, WHITESPACE);
371 l = strtoull(s, &x, 0);
374 if (!x || x == s || *x)
383 int safe_atolli(const char *s, long long int *ret_lli) {
391 l = strtoll(s, &x, 0);
394 if (!x || x == s || *x)
401 int safe_atou8(const char *s, uint8_t *ret) {
408 s += strspn(s, WHITESPACE);
411 l = strtoul(s, &x, 0);
414 if (!x || x == s || *x)
418 if ((unsigned long) (uint8_t) l != l)
425 int safe_atou16(const char *s, uint16_t *ret) {
432 s += strspn(s, WHITESPACE);
435 l = strtoul(s, &x, 0);
438 if (!x || x == s || *x)
442 if ((unsigned long) (uint16_t) l != l)
449 int safe_atoi16(const char *s, int16_t *ret) {
457 l = strtol(s, &x, 0);
460 if (!x || x == s || *x)
462 if ((long) (int16_t) l != l)
469 int safe_atod(const char *s, double *ret_d) {
477 loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
478 if (loc == (locale_t) 0)
482 d = strtod_l(s, &x, loc);
487 if (!x || x == s || *x) {