1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
From 899173e241f781ad4dfb6b40e7c5f35104164435 Mon Sep 17 00:00:00 2001
From: Brad Bishop <bradleyb@fuzziesquirrel.com>
Date: Thu, 12 Jan 2017 09:56:54 -0500
Subject: [PATCH 1/2] basic: Factor out string checking from
name_to_prefix/instance
Add two new functions: string_to_prefix/instance that enable
prefix/instance extraction from a name before the name is
mangled.
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
---
src/basic/unit-name.c | 48 +++++++++++++++++++++++++++++++-----------------
1 file changed, 31 insertions(+), 17 deletions(-)
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
index 0a6efa4..93c4838 100644
--- a/src/basic/unit-name.c
+++ b/src/basic/unit-name.c
@@ -135,42 +135,46 @@ bool unit_suffix_is_valid(const char *s) {
return true;
}
-int unit_name_to_prefix(const char *n, char **ret) {
+static int string_to_prefix(const char *s, char **ret) {
const char *p;
- char *s;
+ char *r;
- assert(n);
+ assert(s);
assert(ret);
- if (!unit_name_is_valid(n, UNIT_NAME_ANY))
- return -EINVAL;
-
- p = strchr(n, '@');
+ p = strchr(s, '@');
if (!p)
- p = strrchr(n, '.');
+ p = strrchr(s, '.');
assert_se(p);
- s = strndup(n, p - n);
- if (!s)
+ r = strndup(s, p - s);
+ if (!r)
return -ENOMEM;
- *ret = s;
+ *ret = r;
return 0;
}
-int unit_name_to_instance(const char *n, char **instance) {
- const char *p, *d;
- char *i;
-
+int unit_name_to_prefix(const char *n, char **ret) {
assert(n);
- assert(instance);
+ assert(ret);
if (!unit_name_is_valid(n, UNIT_NAME_ANY))
return -EINVAL;
+ return string_to_prefix(n, ret);
+}
+
+static int string_to_instance(const char *s, char **instance) {
+ const char *p, *d;
+ char *i;
+
+ assert(s);
+ assert(instance);
+
/* Everything past the first @ and before the last . is the instance */
- p = strchr(n, '@');
+ p = strchr(s, '@');
if (!p) {
*instance = NULL;
return 0;
@@ -190,6 +194,16 @@ int unit_name_to_instance(const char *n, char **instance) {
return 1;
}
+int unit_name_to_instance(const char *n, char **instance) {
+ assert(n);
+ assert(instance);
+
+ if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+ return -EINVAL;
+
+ return string_to_instance(n, instance);
+}
+
int unit_name_to_prefix_and_instance(const char *n, char **ret) {
const char *d;
char *s;
--
1.8.3.1
|