[systemd-devel] [PATCH] util: allow strappenda to take any number of args

Dave Reisner dreisner at archlinux.org
Wed Aug 13 13:35:31 PDT 2014


This makes strappenda3 redundant, so we remove its usage and definition.
---
Cleaning out some old branches I had laying around, found this...

 src/dbus1-generator/dbus1-generator.c |  2 +-
 src/fstab-generator/fstab-generator.c |  2 +-
 src/getty-generator/getty-generator.c |  2 +-
 src/shared/install.c                  |  2 +-
 src/shared/util.h                     | 36 +++++++++++++----------------------
 src/systemctl/systemctl.c             |  2 +-
 src/test/test-util.c                  |  7 +++++++
 7 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/src/dbus1-generator/dbus1-generator.c b/src/dbus1-generator/dbus1-generator.c
index dcfceec..e1ffc55 100644
--- a/src/dbus1-generator/dbus1-generator.c
+++ b/src/dbus1-generator/dbus1-generator.c
@@ -169,7 +169,7 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
         assert(path);
         assert(fname);
 
-        p = strappenda3(path, "/", fname);
+        p = strappenda(path, "/", fname);
         r = config_parse(NULL, p, NULL,
                          "D-BUS Service\0",
                          config_item_table_lookup, table,
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index 418e8b1..2c38ab9 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -432,7 +432,7 @@ static int add_root_mount(void) {
         else if (arg_root_rw >= 0 ||
                  (!mount_test_option(arg_root_options, "ro") &&
                   !mount_test_option(arg_root_options, "rw")))
-                opts = strappenda3(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
+                opts = strappenda(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
         else
                 opts = arg_root_options;
 
diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c
index c78a511..06ca9b9 100644
--- a/src/getty-generator/getty-generator.c
+++ b/src/getty-generator/getty-generator.c
@@ -42,7 +42,7 @@ static int add_symlink(const char *fservice, const char *tservice) {
         assert(tservice);
 
         from = strappenda(SYSTEM_DATA_UNIT_PATH "/", fservice);
-        to = strappenda3(arg_dest, "/getty.target.wants/", tservice);
+        to = strappenda(arg_dest, "/getty.target.wants/", tservice);
 
         mkdir_parents_label(to, 0755);
 
diff --git a/src/shared/install.c b/src/shared/install.c
index 276ca3e..0fe1371 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1061,7 +1061,7 @@ static int unit_file_load(
         assert(path);
 
         if (!isempty(root_dir))
-                path = strappenda3(root_dir, "/", path);
+                path = strappenda(root_dir, "/", path);
 
         fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
         if (fd < 0)
diff --git a/src/shared/util.h b/src/shared/util.h
index 8231cf2..101d2df 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -845,29 +845,19 @@ int unlink_noerrno(const char *path);
                 (void *) memset(_new_, 0, _len_);       \
         })
 
-#define strappenda(a, b)                                \
-        ({                                              \
-                const char *_a_ = (a), *_b_ = (b);      \
-                char *_c_;                              \
-                size_t _x_, _y_;                        \
-                _x_ = strlen(_a_);                      \
-                _y_ = strlen(_b_);                      \
-                _c_ = alloca(_x_ + _y_ + 1);            \
-                strcpy(stpcpy(_c_, _a_), _b_);          \
-                _c_;                                    \
-        })
-
-#define strappenda3(a, b, c)                                    \
-        ({                                                      \
-                const char *_a_ = (a), *_b_ = (b), *_c_ = (c);  \
-                char *_d_;                                      \
-                size_t _x_, _y_, _z_;                           \
-                _x_ = strlen(_a_);                              \
-                _y_ = strlen(_b_);                              \
-                _z_ = strlen(_c_);                              \
-                _d_ = alloca(_x_ + _y_ + _z_ + 1);              \
-                strcpy(stpcpy(stpcpy(_d_, _a_), _b_), _c_);     \
-                _d_;                                            \
+#define strappenda(a, ...)                                       \
+        ({                                                       \
+                int _len = strlen(a);                            \
+                unsigned _i;                                     \
+                char *_d_, *_p_;                                 \
+                const char *_appendees_[] = { __VA_ARGS__ };     \
+                for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
+                        _len += strlen(_appendees_[_i]);         \
+                _d_ = alloca(_len + 1);                          \
+                _p_ = stpcpy(_d_, a);                            \
+                for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
+                        _p_ = stpcpy(_p_, _appendees_[_i]);      \
+                _d_;                                             \
         })
 
 #define procfs_file_alloca(pid, field)                                  \
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 540b4a6..36db652 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -4827,7 +4827,7 @@ static int switch_root(sd_bus *bus, char **args) {
                 const char *root_systemd_path = NULL, *root_init_path = NULL;
 
                 root_systemd_path = strappenda(root, "/" SYSTEMD_BINARY_PATH);
-                root_init_path = strappenda3(root, "/", init);
+                root_init_path = strappenda(root, "/", init);
 
                 /* If the passed init is actually the same as the
                  * systemd binary, then let's suppress it. */
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 16f89b4..8776899 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -907,6 +907,12 @@ static void test_strshorten(void) {
         assert_se(strlen(strshorten(s, 0)) == 0);
 }
 
+static void test_strappenda(void) {
+        assert_se(streq(strappenda("", "foo", "bar"), "foobar"));
+        assert_se(streq(strappenda("foo", "bar", "baz"), "foobarbaz"));
+        assert_se(streq(strappenda("foo", "", "bar", "baz"), "foobarbaz"));
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -965,6 +971,7 @@ int main(int argc, char *argv[]) {
         test_read_one_char();
         test_ignore_signals();
         test_strshorten();
+        test_strappenda();
 
         return 0;
 }
-- 
2.0.4


More information about the systemd-devel mailing list