basic/unit-name: allow unit_name_to_instance() to be used to classify units

This could already be done by calling unit_name_is_*(), but if we don't know
if the argument is a valid unit name, it is more convenient to have a single
function which returns the type or possibly an error if the unit name is not
valid.

The values in the enum are sorted "by length". Not really important, but it
seems more natural to me.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2019-07-19 16:49:41 +02:00
parent 69de5c16ff
commit b124d3f2ce
3 changed files with 25 additions and 19 deletions
+11 -10
View File
@@ -140,12 +140,10 @@ int unit_name_to_prefix(const char *n, char **ret) {
return 0;
}
int unit_name_to_instance(const char *n, char **instance) {
int unit_name_to_instance(const char *n, char **ret) {
const char *p, *d;
char *i;
assert(n);
assert(instance);
if (!unit_name_is_valid(n, UNIT_NAME_ANY))
return -EINVAL;
@@ -153,8 +151,9 @@ int unit_name_to_instance(const char *n, char **instance) {
/* Everything past the first @ and before the last . is the instance */
p = strchr(n, '@');
if (!p) {
*instance = NULL;
return 0;
if (ret)
*ret = NULL;
return UNIT_NAME_PLAIN;
}
p++;
@@ -163,12 +162,14 @@ int unit_name_to_instance(const char *n, char **instance) {
if (!d)
return -EINVAL;
i = strndup(p, d-p);
if (!i)
return -ENOMEM;
if (ret) {
char *i = strndup(p, d-p);
if (!i)
return -ENOMEM;
*instance = i;
return 1;
*ret = i;
}
return d > p ? UNIT_NAME_INSTANCE : UNIT_NAME_TEMPLATE;
}
int unit_name_to_prefix_and_instance(const char *n, char **ret) {