+
+char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
+ char *r, *t;
+ const char *f;
+ size_t a, b, c;
+
+ assert(path);
+
+ /* Takes a path and a suffix and prefix and makes a nice
+ * string suitable as unit name of it, escaping all weird
+ * chars on the way.
+ *
+ * / becomes ., and all chars not alloweed in a unit name get
+ * escaped as \xFF, including \ and ., of course. This
+ * escaping is hence reversible.
+ */
+
+ if (!prefix)
+ prefix = "";
+
+ if (!suffix)
+ suffix = "";
+
+ a = strlen(prefix);
+ b = strlen(path);
+ c = strlen(suffix);
+
+ if (!(r = new(char, a+b*4+c+1)))
+ return NULL;
+
+ memcpy(r, prefix, a);
+
+ for (f = path, t = r+a; *f; f++) {
+ if (*f == '/')
+ *(t++) = '.';
+ else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = hexchar(*f > 4);
+ *(t++) = hexchar(*f);
+ } else
+ *(t++) = *f;
+ }
+
+ memcpy(t, suffix, c+1);
+
+ return r;
+}