summaryrefslogtreecommitdiff
path: root/src/login
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-06-08 19:25:38 +0200
committerLennart Poettering <lennart@poettering.net>2016-06-14 19:50:38 +0200
commit9184ca48ea43e009cd6f379f319926109a30926c (patch)
tree0db1fa083fb240966e47d621d861cd7cb45501e6 /src/login
parentd9ab2bcf0591b496f1a4750c7ff790b33f9c7e59 (diff)
util-lib: introduce parse_percent() for parsing percent specifications
And port a couple of users over to it.
Diffstat (limited to 'src/login')
-rw-r--r--src/login/logind-user.c29
1 files changed, 8 insertions, 21 deletions
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
index a826321bf0..6d0904f5ca 100644
--- a/src/login/logind-user.c
+++ b/src/login/logind-user.c
@@ -843,7 +843,6 @@ int config_parse_tmpfs_size(
void *userdata) {
size_t *sz = data;
- const char *e;
int r;
assert(filename);
@@ -851,29 +850,17 @@ int config_parse_tmpfs_size(
assert(rvalue);
assert(data);
- e = endswith(rvalue, "%");
- if (e) {
- unsigned long ul;
- char *f;
-
- errno = 0;
- ul = strtoul(rvalue, &f, 10);
- if (errno > 0 || f != e) {
- log_syntax(unit, LOG_ERR, filename, line, errno, "Failed to parse percentage value, ignoring: %s", rvalue);
- return 0;
- }
-
- if (ul <= 0 || ul >= 100) {
- log_syntax(unit, LOG_ERR, filename, line, 0, "Percentage value out of range, ignoring: %s", rvalue);
- return 0;
- }
-
- *sz = PAGE_ALIGN((size_t) ((physical_memory() * (uint64_t) ul) / (uint64_t) 100));
- } else {
+ /* First, try to parse as percentage */
+ r = parse_percent(rvalue);
+ if (r > 0 && r < 100)
+ *sz = PAGE_ALIGN((size_t) ((physical_memory() * (uint64_t) r) / 100U));
+ else {
uint64_t k;
+ /* If the passed argument was not a percentage, or out of range, parse as byte size */
+
r = parse_size(rvalue, 1024, &k);
- if (r < 0 || (uint64_t) (size_t) k != k) {
+ if (r < 0 || k <= 0 || (uint64_t) (size_t) k != k) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
return 0;
}