diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-02-17 15:33:05 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-02-18 22:36:34 +0100 |
commit | 38d0c270063c9d7379b45b80b20b179a713edc6e (patch) | |
tree | 77260670f21cc95564d914649dbdc1c3d7e3bea0 /src/basic | |
parent | util: move percent/permille/permyriad parser into percent-util.[ch] (diff) | |
download | systemd-38d0c270063c9d7379b45b80b20b179a713edc6e.tar.xz systemd-38d0c270063c9d7379b45b80b20b179a713edc6e.zip |
percent-util: when parsing permyriads, permit percents too with 1 place after the dot
Previously, when parsing myriads, we'd support:
x% → percent, no places after the dot
x.yz% → percent, two places after the dot
x‰ → permille, no places after the dot
x.y‰ → permille, one place after the dot
x‱ → permyriad, no places after the dot
What's missing is:
x.y% → percent, one place after the dot
Let's add it in.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/percent-util.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/basic/percent-util.c b/src/basic/percent-util.c index f58a51dcf9..06f20fd61e 100644 --- a/src/basic/percent-util.c +++ b/src/basic/percent-util.c @@ -64,11 +64,23 @@ static int parse_parts_value_with_hundredths_place(const char *p, const char *sy dot = memchr(p, '.', pc - p); if (dot) { - if (dot + 3 != pc) - return -EINVAL; - if (dot[1] < '0' || dot[1] > '9' || dot[2] < '0' || dot[2] > '9') + if (dot + 3 == pc) { + /* Support two places after the dot */ + + if (dot[1] < '0' || dot[1] > '9' || dot[2] < '0' || dot[2] > '9') + return -EINVAL; + q = (dot[1] - '0') * 10 + (dot[2] - '0'); + + } else if (dot + 2 == pc) { + /* Support one place after the dot */ + + if (dot[1] < '0' || dot[1] > '9') + return -EINVAL; + q = (dot[1] - '0') * 10; + } else + /* We do not support zero or more than two places */ return -EINVAL; - q = (dot[1] - '0') * 10 + (dot[2] - '0'); + n = strndupa(p, dot - p); } else { q = 0; |