summaryrefslogtreecommitdiff
path: root/src/basic/parse-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-10-28 13:06:56 +0100
committerLennart Poettering <lennart@poettering.net>2015-10-28 13:06:56 +0100
commite3d285e6c0fb5a63e48d97f9bec1ba6d4ec132aa (patch)
treec2afa8ccfccfa7e464792f52e871e99281597f3f /src/basic/parse-util.c
parentdf5b3e1840a373dca1e3da5b81540b7862994ab4 (diff)
parent71b1c27a406271b71f64487ae70b58f44a4a37f0 (diff)
Merge pull request #1699 from filbranden/cpuaffinity9
cpu-set-util: Support ranges in parse_cpu_set_and_warn (v4)
Diffstat (limited to 'src/basic/parse-util.c')
-rw-r--r--src/basic/parse-util.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 2437fee60c..1ee5783680 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -19,6 +19,8 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
+#include "extract-word.h"
#include "parse-util.h"
#include "string-util.h"
#include "util.h"
@@ -207,6 +209,43 @@ int parse_size(const char *t, uint64_t base, uint64_t *size) {
return 0;
}
+int parse_range(const char *t, unsigned *lower, unsigned *upper) {
+ _cleanup_free_ char *word = NULL;
+ unsigned l, u;
+ int r;
+
+ assert(lower);
+ assert(upper);
+
+ /* Extract the lower bound. */
+ r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -EINVAL;
+
+ r = safe_atou(word, &l);
+ 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;
+}
+
char *format_bytes(char *buf, size_t l, uint64_t t) {
unsigned i;