Skip to content

Commit 8085f16

Browse files
committed
util: allow strappenda to take any number of args
This makes strappenda3 redundant, so we remove its usage and definition. Add a few tests along the way for sanity.
1 parent d06441d commit 8085f16

File tree

7 files changed

+32
-28
lines changed

7 files changed

+32
-28
lines changed

src/dbus1-generator/dbus1-generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
169169
assert(path);
170170
assert(fname);
171171

172-
p = strappenda3(path, "/", fname);
172+
p = strappenda(path, "/", fname);
173173
r = config_parse(NULL, p, NULL,
174174
"D-BUS Service\0",
175175
config_item_table_lookup, table,

src/fstab-generator/fstab-generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ static int add_root_mount(void) {
432432
else if (arg_root_rw >= 0 ||
433433
(!mount_test_option(arg_root_options, "ro") &&
434434
!mount_test_option(arg_root_options, "rw")))
435-
opts = strappenda3(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
435+
opts = strappenda(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
436436
else
437437
opts = arg_root_options;
438438

src/getty-generator/getty-generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int add_symlink(const char *fservice, const char *tservice) {
4242
assert(tservice);
4343

4444
from = strappenda(SYSTEM_DATA_UNIT_PATH "/", fservice);
45-
to = strappenda3(arg_dest, "/getty.target.wants/", tservice);
45+
to = strappenda(arg_dest, "/getty.target.wants/", tservice);
4646

4747
mkdir_parents_label(to, 0755);
4848

src/shared/install.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ static int unit_file_load(
10611061
assert(path);
10621062

10631063
if (!isempty(root_dir))
1064-
path = strappenda3(root_dir, "/", path);
1064+
path = strappenda(root_dir, "/", path);
10651065

10661066
fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
10671067
if (fd < 0)

src/shared/util.h

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -845,29 +845,19 @@ int unlink_noerrno(const char *path);
845845
(void *) memset(_new_, 0, _len_); \
846846
})
847847

848-
#define strappenda(a, b) \
849-
({ \
850-
const char *_a_ = (a), *_b_ = (b); \
851-
char *_c_; \
852-
size_t _x_, _y_; \
853-
_x_ = strlen(_a_); \
854-
_y_ = strlen(_b_); \
855-
_c_ = alloca(_x_ + _y_ + 1); \
856-
strcpy(stpcpy(_c_, _a_), _b_); \
857-
_c_; \
858-
})
859-
860-
#define strappenda3(a, b, c) \
861-
({ \
862-
const char *_a_ = (a), *_b_ = (b), *_c_ = (c); \
863-
char *_d_; \
864-
size_t _x_, _y_, _z_; \
865-
_x_ = strlen(_a_); \
866-
_y_ = strlen(_b_); \
867-
_z_ = strlen(_c_); \
868-
_d_ = alloca(_x_ + _y_ + _z_ + 1); \
869-
strcpy(stpcpy(stpcpy(_d_, _a_), _b_), _c_); \
870-
_d_; \
848+
#define strappenda(a, ...) \
849+
({ \
850+
int _len = strlen(a); \
851+
unsigned _i; \
852+
char *_d_, *_p_; \
853+
const char *_appendees_[] = { __VA_ARGS__ }; \
854+
for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
855+
_len += strlen(_appendees_[_i]); \
856+
_d_ = alloca(_len + 1); \
857+
_p_ = stpcpy(_d_, a); \
858+
for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
859+
_p_ = stpcpy(_p_, _appendees_[_i]); \
860+
_d_; \
871861
})
872862

873863
#define procfs_file_alloca(pid, field) \

src/systemctl/systemctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4827,7 +4827,7 @@ static int switch_root(sd_bus *bus, char **args) {
48274827
const char *root_systemd_path = NULL, *root_init_path = NULL;
48284828

48294829
root_systemd_path = strappenda(root, "/" SYSTEMD_BINARY_PATH);
4830-
root_init_path = strappenda3(root, "/", init);
4830+
root_init_path = strappenda(root, "/", init);
48314831

48324832
/* If the passed init is actually the same as the
48334833
* systemd binary, then let's suppress it. */

src/test/test-util.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,19 @@ static void test_strshorten(void) {
907907
assert_se(strlen(strshorten(s, 0)) == 0);
908908
}
909909

910+
static void test_strappenda(void) {
911+
char *actual;
912+
913+
actual = strappenda("", "foo", "bar");
914+
assert_se(streq(actual, "foobar"));
915+
916+
actual = strappenda("foo", "bar", "baz");
917+
assert_se(streq(actual, "foobarbaz"));
918+
919+
actual = strappenda("foo", "", "bar", "baz");
920+
assert_se(streq(actual, "foobarbaz"));
921+
}
922+
910923
int main(int argc, char *argv[]) {
911924
log_parse_environment();
912925
log_open();
@@ -965,6 +978,7 @@ int main(int argc, char *argv[]) {
965978
test_read_one_char();
966979
test_ignore_signals();
967980
test_strshorten();
981+
test_strappenda();
968982

969983
return 0;
970984
}

0 commit comments

Comments
 (0)