parse-util.c revision 28cb17ef0281efc3a46e5d0e702b0b0ddeaafaa4
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2010 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint parse_boolean(const char *v) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (streq(v, "1") || strcaseeq(v, "yes") || strcaseeq(v, "y") || strcaseeq(v, "true") || strcaseeq(v, "t") || strcaseeq(v, "on"))
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poettering else if (streq(v, "0") || strcaseeq(v, "no") || strcaseeq(v, "n") || strcaseeq(v, "false") || strcaseeq(v, "f") || strcaseeq(v, "off"))
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringint parse_pid(const char *s, pid_t* ret_pid) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen unsigned long ul = 0;
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersenint parse_mode(const char *s, mode_t *ret) {
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering if (!x || x == s || *x)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (l < 0 || l > 07777)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint parse_size(const char *t, uint64_t base, uint64_t *size) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Soo, sometimes we want to parse IEC binary suffixes, and
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * sometimes SI decimal suffixes. This function can parse
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * both. Which one is the right way depends on the
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * context. Wikipedia suggests that SI is customary for
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * hardware metrics and network speeds, while IEC is
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * customary for most data sizes used by software and volatile
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * (RAM) memory. Hence be careful which one you pick!
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * In either case we use just K, M, G as suffix, and not Ki,
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * Mi, Gi or so (as IEC would suggest). That's because that's
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * frickin' ugly. But this means you really need to make sure
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering * to document which base you are parsing when you use this
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering unsigned long long factor;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *p;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering unsigned long long r = 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen unsigned long long l, tmp;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering if (*p == '-')
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (*e == '.') {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* strtoull() itself would accept space/+/- */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering unsigned long long l2;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* Ignore failure. E.g. 10.M is valid */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering for (; e < e2; e++)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if ((unsigned long long) (uint64_t) r != r)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringint parse_range(const char *t, unsigned *lower, unsigned *upper) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering unsigned l, u;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /* Extract the lower bound. */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Check for the upper bound and extract it if needed */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /* Single number with no dashes. */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Trailing dash is an error. */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringchar *format_bytes(char *buf, size_t l, uint64_t t) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* This only does IEC units so far */
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering static const struct {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering for (i = 0; i < ELEMENTSOF(table); i++) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10),
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringint safe_atou(const char *s, unsigned *ret_u) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering unsigned long l;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!x || x == s || *x || errno)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if ((unsigned long) (unsigned) l != l)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering *ret_u = (unsigned) l;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poetteringint safe_atoi(const char *s, int *ret_i) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering l = strtol(s, &x, 0);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (!x || x == s || *x || errno)
8937e7b68940d0fa0d0aab90eb7425fa7dccebc9Lennart Poettering if ((long) (int) l != l)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringint safe_atollu(const char *s, long long unsigned *ret_llu) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering unsigned long long l;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (!x || x == s || *x || errno)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringint safe_atolli(const char *s, long long int *ret_lli) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (!x || x == s || *x || errno)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringint safe_atou8(const char *s, uint8_t *ret) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering unsigned long l;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (!x || x == s || *x || errno)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if ((unsigned long) (uint8_t) l != l)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringint safe_atou16(const char *s, uint16_t *ret) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering unsigned long l;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!x || x == s || *x || errno)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if ((unsigned long) (uint16_t) l != l)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringint safe_atoi16(const char *s, int16_t *ret) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering l = strtol(s, &x, 0);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!x || x == s || *x || errno)
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if ((long) (int16_t) l != l)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringint safe_atod(const char *s, double *ret_d) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!x || x == s || *x || errno) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering *ret_d = (double) d;