chiark / gitweb /
util: introduce join() to speed up simple string concatenations
authorLennart Poettering <lennart@poettering.net>
Sun, 31 Jul 2011 23:28:01 +0000 (01:28 +0200)
committerLennart Poettering <lennart@poettering.net>
Sun, 31 Jul 2011 23:28:01 +0000 (01:28 +0200)
src/load-dropin.c
src/strv.c
src/util.c
src/util.h

index d30865c..984a47a 100644 (file)
@@ -78,7 +78,8 @@ static int process_dir(Unit *u, const char *unit_path, const char *name, const c
         assert(name);
         assert(suffix);
 
-        if (asprintf(&path, "%s/%s%s", unit_path, name, suffix) < 0)
+        path = join(unit_path, "/", name, suffix, NULL);
+        if (!path)
                 return -ENOMEM;
 
         if (u->meta.manager->unit_path_cache &&
index f15aa87..066dd09 100644 (file)
@@ -105,7 +105,6 @@ char **strv_new_ap(const char *x, va_list ap) {
         unsigned n = 0, i = 0;
         va_list aq;
 
-
         if (x) {
                 n = 1;
 
index 1a61a26..b533b26 100644 (file)
@@ -5493,6 +5493,54 @@ finish:
         return r;
 }
 
+char *join(const char *x, ...) {
+        va_list ap;
+        size_t l;
+        char *r, *p;
+
+        va_start(ap, x);
+
+        if (x) {
+                l = strlen(x);
+
+                for (;;) {
+                        const char *t;
+
+                        t = va_arg(ap, const char *);
+                        if (!t)
+                                break;
+
+                        l += strlen(t);
+                }
+        } else
+                l = 0;
+
+        va_end(ap);
+
+        r = new(char, l+1);
+        if (!r)
+                return NULL;
+
+        if (x) {
+                p = stpcpy(r, x);
+
+                va_start(ap, x);
+
+                for (;;) {
+                        const char *t;
+
+                        t = va_arg(ap, const char *);
+                        if (!t)
+                                break;
+
+                        p = stpcpy(p, t);
+                }
+        } else
+                r[0] = 0;
+
+        return r;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
index bf5703c..eb0061c 100644 (file)
@@ -458,6 +458,8 @@ int dirent_ensure_type(DIR *d, struct dirent *de);
 int in_search_path(const char *path, char **search);
 int get_files_in_directory(const char *path, char ***list);
 
+char *join(const char *x, ...) _sentinel_;
+
 #define NULSTR_FOREACH(i, l)                                    \
         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)