summaryrefslogtreecommitdiff
path: root/src/basic/parse-util.c
diff options
context:
space:
mode:
authorSven Eden <yamakuzure@gmx.net>2018-03-13 19:11:43 +0100
committerSven Eden <yamakuzure@gmx.net>2018-03-26 18:25:45 +0200
commit5a1765290e87f45b970657cfc8ac1e7ca5c50d6a (patch)
tree316b8b753c02e894a621976a888d8741e3cf9156 /src/basic/parse-util.c
parent869eeae5727fa42bbb442ef10c0dde985fcdce4d (diff)
Prep v236 : Add missing SPDX-License-Identifier (2/9) src/basic
Diffstat (limited to 'src/basic/parse-util.c')
-rw-r--r--src/basic/parse-util.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index f47258edf..37f6a08c3 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd.
@@ -25,6 +26,7 @@
#include <string.h>
#include "alloc-util.h"
+#include "errno-list.h"
//#include "extract-word.h"
#include "macro.h"
#include "parse-util.h"
@@ -273,6 +275,64 @@ int parse_range(const char *t, unsigned *lower, unsigned *upper) {
}
#endif // 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;