parse-util.c revision 11c3a36649e5e5e77db499c92f3cdcbd619efd3a
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xlocale.h>
#include "alloc-util.h"
#include "extract-word.h"
#include "parse-util.h"
#include "macro.h"
#include "string-util.h"
int parse_boolean(const char *v) {
assert(v);
if (streq(v, "1") || strcaseeq(v, "yes") || strcaseeq(v, "y") || strcaseeq(v, "true") || strcaseeq(v, "t") || strcaseeq(v, "on"))
return 1;
else if (streq(v, "0") || strcaseeq(v, "no") || strcaseeq(v, "n") || strcaseeq(v, "false") || strcaseeq(v, "f") || strcaseeq(v, "off"))
return 0;
return -EINVAL;
}
unsigned long ul = 0;
int r;
assert(s);
r = safe_atolu(s, &ul);
if (r < 0)
return r;
return -ERANGE;
if (pid <= 0)
return -ERANGE;
return 0;
}
char *x;
long l;
assert(s);
s += strspn(s, WHITESPACE);
if (s[0] == '-')
return -ERANGE;
errno = 0;
l = strtol(s, &x, 8);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if (l < 0 || l > 07777)
return -ERANGE;
return 0;
}
int parse_ifindex(const char *s, int *ret) {
int ifi, r;
if (r < 0)
return r;
if (ifi <= 0)
return -EINVAL;
return 0;
}
/* Soo, sometimes we want to parse IEC binary suffixes, and
* sometimes SI decimal suffixes. This function can parse
* both. Which one is the right way depends on the
* context. Wikipedia suggests that SI is customary for
* hardware metrics and network speeds, while IEC is
* customary for most data sizes used by software and volatile
* (RAM) memory. Hence be careful which one you pick!
*
* In either case we use just K, M, G as suffix, and not Ki,
* Mi, Gi or so (as IEC would suggest). That's because that's
* frickin' ugly. But this means you really need to make sure
* to document which base you are parsing when you use this
* call. */
struct table {
const char *suffix;
unsigned long long factor;
};
{ "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
{ "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
{ "T", 1024ULL*1024ULL*1024ULL*1024ULL },
{ "G", 1024ULL*1024ULL*1024ULL },
{ "M", 1024ULL*1024ULL },
{ "K", 1024ULL },
{ "B", 1ULL },
{ "", 1ULL },
};
{ "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
{ "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
{ "T", 1000ULL*1000ULL*1000ULL*1000ULL },
{ "G", 1000ULL*1000ULL*1000ULL },
{ "M", 1000ULL*1000ULL },
{ "K", 1000ULL },
{ "B", 1ULL },
{ "", 1ULL },
};
const char *p;
unsigned long long r = 0;
assert(t);
if (base == 1000) {
} else {
}
p = t;
do {
unsigned long long l, tmp;
double frac = 0;
char *e;
unsigned i;
p += strspn(p, WHITESPACE);
errno = 0;
l = strtoull(p, &e, 10);
if (errno != 0)
return -errno;
if (e == p)
return -EINVAL;
if (*p == '-')
return -ERANGE;
if (*e == '.') {
e++;
/* strtoull() itself would accept space/+/- */
if (*e >= '0' && *e <= '9') {
unsigned long long l2;
char *e2;
if (errno != 0)
return -errno;
/* Ignore failure. E.g. 10.M is valid */
for (; e < e2; e++)
frac /= 10;
}
}
e += strspn(e, WHITESPACE);
break;
if (i >= n_entries)
return -EINVAL;
return -ERANGE;
if (tmp > ULLONG_MAX - r)
return -ERANGE;
r += tmp;
if ((unsigned long long) (uint64_t) r != r)
return -ERANGE;
start_pos = i + 1;
} while (*p);
*size = r;
return 0;
}
unsigned l, u;
int r;
/* Extract the lower bound. */
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
if (r < 0)
return r;
/* Check for the upper bound and extract it if needed */
if (!t)
/* Single number with no dashes. */
u = l;
else if (!*t)
/* Trailing dash is an error. */
return -EINVAL;
else {
r = safe_atou(t, &u);
if (r < 0)
return r;
}
*lower = l;
*upper = u;
return 0;
}
unsigned i;
/* This only does IEC units so far */
static const struct {
const char *suffix;
} table[] = {
};
if (t == (uint64_t) -1)
return NULL;
for (i = 0; i < ELEMENTSOF(table); i++) {
goto finish;
}
}
buf[l-1] = 0;
return buf;
}
char *x = NULL;
unsigned long l;
assert(s);
/* strtoul() is happy to parse negative values, and silently
* converts them to unsigned values without generating an
* error. We want a clean error, hence let's look for the "-"
* prefix on our own, and generate an error. But let's do so
* only after strtoul() validated that the string is clean
* otherwise, so that we return EINVAL preferably over
* ERANGE. */
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoul(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if (s[0] == '-')
return -ERANGE;
if ((unsigned long) (unsigned) l != l)
return -ERANGE;
*ret_u = (unsigned) l;
return 0;
}
char *x = NULL;
long l;
assert(s);
errno = 0;
l = strtol(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if ((long) (int) l != l)
return -ERANGE;
*ret_i = (int) l;
return 0;
}
int safe_atollu(const char *s, long long unsigned *ret_llu) {
char *x = NULL;
unsigned long long l;
assert(s);
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoull(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if (*s == '-')
return -ERANGE;
*ret_llu = l;
return 0;
}
int safe_atolli(const char *s, long long int *ret_lli) {
char *x = NULL;
long long l;
assert(s);
errno = 0;
l = strtoll(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
*ret_lli = l;
return 0;
}
char *x = NULL;
unsigned long l;
assert(s);
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoul(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if (s[0] == '-')
return -ERANGE;
if ((unsigned long) (uint8_t) l != l)
return -ERANGE;
return 0;
}
char *x = NULL;
unsigned long l;
assert(s);
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoul(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if (s[0] == '-')
return -ERANGE;
if ((unsigned long) (uint16_t) l != l)
return -ERANGE;
return 0;
}
char *x = NULL;
long l;
assert(s);
errno = 0;
l = strtol(s, &x, 0);
if (errno != 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if ((long) (int16_t) l != l)
return -ERANGE;
return 0;
}
char *x = NULL;
double d = 0;
assert(s);
return -errno;
errno = 0;
if (errno != 0) {
return -errno;
}
if (!x || x == s || *x) {
return -EINVAL;
}
*ret_d = (double) d;
return 0;
}
size_t i;
unsigned val = 0;
const char *s;
s = *p;
/* accept any number of digits, strtoull is limted to 19 */
for(i=0; i < digits; i++,s++) {
if (*s < '0' || *s > '9') {
if (i == 0)
return -EINVAL;
/* too few digits, pad with 0 */
for (; i < digits; i++)
val *= 10;
break;
}
val *= 10;
val += *s - '0';
}
/* maybe round up */
if (*s >= '5' && *s <= '9')
val++;
*p = s;
return 0;
}