X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=unit-name.c;h=c5901cacfa3bf9c14fd95b91e6a2c8e1a3d42110;hb=4f4a1dbf2171aa62da04d2e3b6945e8992139d14;hp=219997b681579581b69b7c955707f125770c9c93;hpb=9e2f7c11fb6ba35ffec2274da3e2d08b10d23965;p=elogind.git diff --git a/unit-name.c b/unit-name.c index 219997b68..c5901cacf 100644 --- a/unit-name.c +++ b/unit-name.c @@ -29,7 +29,7 @@ "0123456789" \ "abcdefghijklmnopqrstuvwxyz" \ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ - "-_.\\" + ":-_.\\" UnitType unit_name_to_type(const char *n) { UnitType t; @@ -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,54 @@ char *unit_name_template(const char *f) { return r; } + +char *unit_name_from_path(const char *path, const char *suffix) { + char *p, *r; + + assert(path); + assert(suffix); + + if (!(p = strdup(path))) + return NULL; + + path_kill_slashes(p); + + path = p[0] == '/' ? p + 1 : p; + + if (path[0] == 0) { + free(p); + return strappend("-", suffix); + } + + r = unit_name_build_escape(path, NULL, suffix); + free(p); + + return r; +} + +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; +}