X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=unit-name.c;h=5d428dd446af79e1049846e3491dacf4309c804c;hp=eb2f704e2f7f4d61319d1a1f8de230dc8b3c4515;hb=25e870b5f79f158ba6ac0b715248b0c3d3549325;hpb=4f2d528d3bb25cebf8d3ebe83d8193ab4016cb90 diff --git a/unit-name.c b/unit-name.c index eb2f704e2..5d428dd44 100644 --- a/unit-name.c +++ b/unit-name.c @@ -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; +}