chiark / gitweb /
build fix for opensuse
[elogind.git] / unit-name.c
index eb2f704e2f7f4d61319d1a1f8de230dc8b3c4515..5d428dd446af79e1049846e3491dacf4309c804c 100644 (file)
@@ -207,8 +207,8 @@ static char* do_escape(const char *f, char *t) {
 
         for (; *f; f++) {
                 if (*f == '/')
-                        *(t++) = '.';
-                else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
+                        *(t++) = '-';
+                else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
                         *(t++) = '\\';
                         *(t++) = 'x';
                         *(t++) = hexchar(*f > 4);
@@ -286,7 +286,7 @@ char *unit_name_unescape(const char *f) {
                 return NULL;
 
         for (t = r; *f; f++) {
-                if (*f == '.')
+                if (*f == '-')
                         *(t++) = '/';
                 else if (*f == '\\') {
                         int a, b;
@@ -371,3 +371,43 @@ char *unit_name_template(const char *f) {
         return r;
 
 }
+
+char *unit_name_from_path(const char *path, const char *suffix) {
+        assert(path);
+        assert(suffix);
+
+        if (path[0] == '/')
+                path++;
+
+        if (path[0] == 0)
+                return strappend("-", suffix);
+
+        return unit_name_build_escape(path, NULL, suffix);
+}
+
+char *unit_name_to_path(const char *name) {
+        char *w, *e;
+
+        assert(name);
+
+        if (!(w = unit_name_to_prefix(name)))
+                return NULL;
+
+        e = unit_name_unescape(w);
+        free(w);
+
+        if (!e)
+                return NULL;
+
+        if (e[0] != '/') {
+                w = strappend("/", e);
+                free(e);
+
+                if (!w)
+                        return NULL;
+
+                e = w;
+        }
+
+        return e;
+}