util: rework strappenda(), and rename it strjoina()
After all it is now much more like strjoin() than strappend(). At the same time, add support for NULL sentinels, even if they are normally not necessary.
This commit is contained in:
@@ -93,7 +93,7 @@ static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigne
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
fn = strappenda(p, "/cgroup.procs");
|
||||
fn = strjoina(p, "/cgroup.procs");
|
||||
f = fopen(fn, "re");
|
||||
if (!f)
|
||||
return -errno;
|
||||
|
||||
@@ -510,7 +510,7 @@ static int check_hierarchy(const char *p) {
|
||||
return 0;
|
||||
|
||||
/* Check if this controller actually really exists */
|
||||
cc = strappenda("/sys/fs/cgroup/", p);
|
||||
cc = strjoina("/sys/fs/cgroup/", p);
|
||||
if (laccess(cc, F_OK) < 0)
|
||||
return -errno;
|
||||
|
||||
|
||||
@@ -288,7 +288,7 @@ static int condition_test_needs_update(Condition *c) {
|
||||
if (!path_is_absolute(c->parameter))
|
||||
return true;
|
||||
|
||||
p = strappenda(c->parameter, "/.updated");
|
||||
p = strjoina(c->parameter, "/.updated");
|
||||
if (lstat(p, &other) < 0)
|
||||
return true;
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
|
||||
assert(path);
|
||||
assert(suffix);
|
||||
|
||||
dirpath = strappenda(root ? root : "", path);
|
||||
dirpath = strjoina(root ? root : "", path);
|
||||
|
||||
dir = opendir(dirpath);
|
||||
if (!dir) {
|
||||
|
||||
@@ -62,7 +62,7 @@ int generator_write_fsck_deps(
|
||||
if (streq(where, "/")) {
|
||||
char *lnk;
|
||||
|
||||
lnk = strappenda(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
|
||||
lnk = strjoina(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
|
||||
|
||||
mkdir_parents(lnk, 0755);
|
||||
if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0)
|
||||
|
||||
@@ -1048,7 +1048,7 @@ static int unit_file_load(
|
||||
assert(path);
|
||||
|
||||
if (!isempty(root_dir))
|
||||
path = strappenda(root_dir, "/", path);
|
||||
path = strjoina(root_dir, "/", path);
|
||||
|
||||
if (!load) {
|
||||
r = access(path, F_OK) ? -errno : 0;
|
||||
@@ -1730,7 +1730,7 @@ int unit_file_set_default(
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
path = strappenda(config_path, "/" SPECIAL_DEFAULT_TARGET);
|
||||
path = strjoina(config_path, "/" SPECIAL_DEFAULT_TARGET);
|
||||
|
||||
r = create_symlink(i->path, path, force, changes, n_changes);
|
||||
if (r < 0)
|
||||
|
||||
@@ -1033,10 +1033,10 @@ int add_matches_for_unit(sd_journal *j, const char *unit) {
|
||||
assert(j);
|
||||
assert(unit);
|
||||
|
||||
m1 = strappenda("_SYSTEMD_UNIT=", unit);
|
||||
m2 = strappenda("COREDUMP_UNIT=", unit);
|
||||
m3 = strappenda("UNIT=", unit);
|
||||
m4 = strappenda("OBJECT_SYSTEMD_UNIT=", unit);
|
||||
m1 = strjoina("_SYSTEMD_UNIT=", unit);
|
||||
m2 = strjoina("COREDUMP_UNIT=", unit);
|
||||
m3 = strjoina("UNIT=", unit);
|
||||
m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit);
|
||||
|
||||
(void)(
|
||||
/* Look for messages from the service itself */
|
||||
@@ -1080,10 +1080,10 @@ int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
|
||||
assert(j);
|
||||
assert(unit);
|
||||
|
||||
m1 = strappenda("_SYSTEMD_USER_UNIT=", unit);
|
||||
m2 = strappenda("USER_UNIT=", unit);
|
||||
m3 = strappenda("COREDUMP_USER_UNIT=", unit);
|
||||
m4 = strappenda("OBJECT_SYSTEMD_USER_UNIT=", unit);
|
||||
m1 = strjoina("_SYSTEMD_USER_UNIT=", unit);
|
||||
m2 = strjoina("USER_UNIT=", unit);
|
||||
m3 = strjoina("COREDUMP_USER_UNIT=", unit);
|
||||
m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit);
|
||||
sprintf(muid, "_UID="UID_FMT, uid);
|
||||
|
||||
(void) (
|
||||
|
||||
@@ -425,7 +425,7 @@ int image_rename(Image *i, const char *new_name) {
|
||||
case IMAGE_RAW: {
|
||||
const char *fn;
|
||||
|
||||
fn = strappenda(new_name, ".raw");
|
||||
fn = strjoina(new_name, ".raw");
|
||||
new_path = file_in_same_dir(i->path, fn);
|
||||
break;
|
||||
}
|
||||
@@ -486,13 +486,13 @@ int image_clone(Image *i, const char *new_name, bool read_only) {
|
||||
|
||||
case IMAGE_SUBVOLUME:
|
||||
case IMAGE_DIRECTORY:
|
||||
new_path = strappenda("/var/lib/machines/", new_name);
|
||||
new_path = strjoina("/var/lib/machines/", new_name);
|
||||
|
||||
r = btrfs_subvol_snapshot(i->path, new_path, read_only, true);
|
||||
break;
|
||||
|
||||
case IMAGE_RAW:
|
||||
new_path = strappenda("/var/lib/machines/", new_name, ".raw");
|
||||
new_path = strjoina("/var/lib/machines/", new_name, ".raw");
|
||||
|
||||
r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
|
||||
break;
|
||||
@@ -629,7 +629,7 @@ int image_name_lock(const char *name, int operation, LockFile *ret) {
|
||||
return -EBUSY;
|
||||
|
||||
mkdir_p("/run/systemd/nspawn/locks", 0600);
|
||||
p = strappenda("/run/systemd/nspawn/locks/name-", name);
|
||||
p = strjoina("/run/systemd/nspawn/locks/name-", name);
|
||||
|
||||
return make_lock_file(p, operation, ret);
|
||||
}
|
||||
|
||||
+1
-1
@@ -91,7 +91,7 @@ int pager_open(bool jump_to_end) {
|
||||
if (!less_opts)
|
||||
less_opts = "FRSXMK";
|
||||
if (jump_to_end)
|
||||
less_opts = strappenda(less_opts, " +G");
|
||||
less_opts = strjoina(less_opts, " +G");
|
||||
setenv("LESS", less_opts, 1);
|
||||
|
||||
/* Make sure the pager goes away when the parent dies */
|
||||
|
||||
@@ -560,14 +560,14 @@ int path_is_os_tree(const char *path) {
|
||||
int r;
|
||||
|
||||
/* We use /usr/lib/os-release as flag file if something is an OS */
|
||||
p = strappenda(path, "/usr/lib/os-release");
|
||||
p = strjoina(path, "/usr/lib/os-release");
|
||||
r = access(p, F_OK);
|
||||
|
||||
if (r >= 0)
|
||||
return 1;
|
||||
|
||||
/* Also check for the old location in /etc, just in case. */
|
||||
p = strappenda(path, "/etc/os-release");
|
||||
p = strjoina(path, "/etc/os-release");
|
||||
r = access(p, F_OK);
|
||||
|
||||
return r >= 0;
|
||||
@@ -665,7 +665,7 @@ int fsck_exists(const char *fstype) {
|
||||
const char *checker;
|
||||
int r;
|
||||
|
||||
checker = strappenda("fsck.", fstype);
|
||||
checker = strjoina("fsck.", fstype);
|
||||
|
||||
r = find_binary(checker, true, &p);
|
||||
if (r < 0)
|
||||
|
||||
@@ -51,7 +51,7 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot,
|
||||
if (path_equal(new_root, "/"))
|
||||
return 0;
|
||||
|
||||
temporary_old_root = strappenda(new_root, oldroot);
|
||||
temporary_old_root = strjoina(new_root, oldroot);
|
||||
mkdir_p_label(temporary_old_root, 0755);
|
||||
|
||||
old_root_remove = in_initrd();
|
||||
|
||||
@@ -965,7 +965,7 @@ bool timezone_is_valid(const char *name) {
|
||||
if (slash)
|
||||
return false;
|
||||
|
||||
t = strappenda("/usr/share/zoneinfo/", name);
|
||||
t = strjoina("/usr/share/zoneinfo/", name);
|
||||
if (stat(t, &st) < 0)
|
||||
return false;
|
||||
|
||||
|
||||
+3
-3
@@ -6452,7 +6452,7 @@ int container_get_leader(const char *machine, pid_t *pid) {
|
||||
assert(machine);
|
||||
assert(pid);
|
||||
|
||||
p = strappenda("/run/systemd/machines/", machine);
|
||||
p = strjoina("/run/systemd/machines/", machine);
|
||||
r = parse_env_file(p, NEWLINE, "LEADER", &s, "CLASS", &class, NULL);
|
||||
if (r == -ENOENT)
|
||||
return -EHOSTDOWN;
|
||||
@@ -6691,7 +6691,7 @@ int open_tmpfile(const char *path, int flags) {
|
||||
#endif
|
||||
|
||||
/* Fall back to unguessable name + unlinking */
|
||||
p = strappenda(path, "/systemd-tmp-XXXXXX");
|
||||
p = strjoina(path, "/systemd-tmp-XXXXXX");
|
||||
|
||||
fd = mkostemp_safe(p, flags);
|
||||
if (fd < 0)
|
||||
@@ -7220,7 +7220,7 @@ int take_password_lock(const char *root) {
|
||||
* awfully racy, and thus we just won't do them. */
|
||||
|
||||
if (root)
|
||||
path = strappenda(root, "/etc/.pwd.lock");
|
||||
path = strjoina(root, "/etc/.pwd.lock");
|
||||
else
|
||||
path = "/etc/.pwd.lock";
|
||||
|
||||
|
||||
+13
-13
@@ -931,19 +931,19 @@ int unlink_noerrno(const char *path);
|
||||
(void*)memset(_new_, 0, _size_); \
|
||||
})
|
||||
|
||||
#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 strjoina(a, ...) \
|
||||
({ \
|
||||
const char *_appendees_[] = { a, __VA_ARGS__ }; \
|
||||
char *_d_, *_p_; \
|
||||
int _len_ = 0; \
|
||||
unsigned _i_; \
|
||||
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
|
||||
_len_ += strlen(_appendees_[_i_]); \
|
||||
_p_ = _d_ = alloca(_len_ + 1); \
|
||||
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
|
||||
_p_ = stpcpy(_p_, _appendees_[_i_]); \
|
||||
*_p_ = 0; \
|
||||
_d_; \
|
||||
})
|
||||
|
||||
#define procfs_file_alloca(pid, field) \
|
||||
|
||||
Reference in New Issue
Block a user