diff options
author | Lennart Poettering <lennart@poettering.net> | 2011-08-01 01:28:01 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2011-08-01 01:28:01 +0200 |
commit | 911a4828e054a531be961cea34de89b666bda710 (patch) | |
tree | 4ba6454e7e3b08b578a91d1728062c4efbf233bc | |
parent | 57a8eca84a1eda99c8cfb63889fa300ba982cb77 (diff) |
util: introduce join() to speed up simple string concatenations
-rw-r--r-- | src/load-dropin.c | 3 | ||||
-rw-r--r-- | src/strv.c | 1 | ||||
-rw-r--r-- | src/util.c | 48 | ||||
-rw-r--r-- | src/util.h | 2 |
4 files changed, 52 insertions, 2 deletions
diff --git a/src/load-dropin.c b/src/load-dropin.c index d30865cbc5..984a47a297 100644 --- a/src/load-dropin.c +++ b/src/load-dropin.c @@ -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 && diff --git a/src/strv.c b/src/strv.c index f15aa8736a..066dd09276 100644 --- a/src/strv.c +++ b/src/strv.c @@ -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; diff --git a/src/util.c b/src/util.c index 1a61a26046..b533b26daf 100644 --- a/src/util.c +++ b/src/util.c @@ -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", diff --git a/src/util.h b/src/util.h index bf5703c5aa..eb0061cdb5 100644 --- a/src/util.h +++ b/src/util.h @@ -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) |