diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2017-11-11 13:29:17 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2017-11-11 13:29:17 +0100 |
commit | cf26c4a7225a56c53eb0550379f1946c5aaf1532 (patch) | |
tree | 07491725b1777a0e04627fb83dc5a964898850e9 /src/basic/parse-util.c | |
parent | basic/errno-list: remove errno_max() and define ERRNO_MAX as 4095 (diff) | |
download | systemd-cf26c4a7225a56c53eb0550379f1946c5aaf1532.tar.xz systemd-cf26c4a7225a56c53eb0550379f1946c5aaf1532.zip |
parse-util: add parse_errno() and parse_syscall_and_errno()
Diffstat (limited to 'src/basic/parse-util.c')
-rw-r--r-- | src/basic/parse-util.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 4ae07b0a8e..9108820ca8 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -25,6 +25,7 @@ #include <string.h> #include "alloc-util.h" +#include "errno-list.h" #include "extract-word.h" #include "macro.h" #include "parse-util.h" @@ -268,6 +269,64 @@ int parse_range(const char *t, unsigned *lower, unsigned *upper) { return 0; } +int parse_errno(const char *t) { + int r, e; + + assert(t); + + r = errno_from_name(t); + if (r > 0) + return r; + + r = safe_atoi(t, &e); + if (r < 0) + return r; + + if (e < 0 || e > ERRNO_MAX) + return -ERANGE; + + return e; +} + +int parse_syscall_and_errno(const char *in, char **name, int *error) { + _cleanup_free_ char *n = NULL; + char *p; + int e = -1; + + assert(in); + assert(name); + assert(error); + + /* + * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255". + * If errno is omitted, then error is set to -1. + * Empty syscall name is not allowed. + * Here, we do not check that the syscall name is valid or not. + */ + + p = strchr(in, ':'); + if (p) { + e = parse_errno(p + 1); + if (e < 0) + return e; + + n = strndup(in, p - in); + } else + n = strdup(in); + + if (!n) + return -ENOMEM; + + if (isempty(n)) + return -EINVAL; + + *error = e; + *name = n; + n = NULL; + + return 0; +} + char *format_bytes(char *buf, size_t l, uint64_t t) { unsigned i; |