[systemd-commits] 5 commits - src/core src/journal src/modules-load src/shared src/sysctl src/systemctl src/test
Zbigniew JÄdrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Sun Jan 5 06:37:09 PST 2014
src/core/main.c | 25 ++----
src/journal/journal-file.c | 4 -
src/journal/journal-verify.c | 6 -
src/modules-load/modules-load.c | 7 -
src/shared/install.c | 14 +--
src/shared/install.h | 14 +--
src/shared/path-lookup.c | 83 +++++----------------
src/shared/strv.c | 158 +++++++---------------------------------
src/shared/strv.h | 6 -
src/shared/util.c | 88 ++++++----------------
src/shared/util.h | 15 ++-
src/sysctl/sysctl.c | 7 -
src/systemctl/systemctl.c | 17 +---
src/test/test-strv.c | 77 +++++++------------
14 files changed, 152 insertions(+), 369 deletions(-)
New commits:
commit af13a6b0606f166c93cafc9892343f167f880281
Author: George McCollister <george.mccollister at gmail.com>
Date: Tue Dec 31 14:37:32 2013 -0600
journal: Add missing byte order conversions
Convert entry_array.items[0] to host byte order prior to passing it to
chain_cache_put().
[zj: also use le64toh in journal-verify.c]
https://bugs.freedesktop.org/show_bug.cgi?id=73194
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index 121b40a..275324b 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -1452,7 +1452,7 @@ static int generic_array_get(
found:
/* Let's cache this item for the next invocation */
- chain_cache_put(f->chain_cache, ci, first, a, o->entry_array.items[0], t, i);
+ chain_cache_put(f->chain_cache, ci, first, a, le64toh(o->entry_array.items[0]), t, i);
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
if (r < 0)
@@ -1685,7 +1685,7 @@ found:
return 0;
/* Let's cache this item for the next invocation */
- chain_cache_put(f->chain_cache, ci, first, a, array->entry_array.items[0], t, subtract_one ? (i > 0 ? i-1 : (uint64_t) -1) : i);
+ chain_cache_put(f->chain_cache, ci, first, a, le64toh(array->entry_array.items[0]), t, subtract_one ? (i > 0 ? i-1 : (uint64_t) -1) : i);
if (subtract_one && i == 0)
p = last_p;
diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c
index 3405811..82b0f0a 100644
--- a/src/journal/journal-verify.c
+++ b/src/journal/journal-verify.c
@@ -249,12 +249,12 @@ static int journal_file_object_verify(JournalFile *f, uint64_t offset, Object *o
}
for (i = 0; i < journal_file_entry_array_n_items(o); i++)
- if (o->entry_array.items[i] != 0 &&
- !VALID64(o->entry_array.items[i])) {
+ if (le64toh(o->entry_array.items[i]) != 0 &&
+ !VALID64(le64toh(o->entry_array.items[i]))) {
log_error(OFSfmt": invalid object entry array item (%"PRIu64"/%"PRIu64"): "OFSfmt,
offset,
i, journal_file_entry_array_n_items(o),
- o->entry_array.items[i]);
+ le64toh(o->entry_array.items[i]));
return -EBADMSG;
}
commit 7195aa42e3d3c2b51da5f0ddefa74bd0925441be
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Dec 27 00:16:39 2013 -0500
shared/install: use char** convention for strvs
diff --git a/src/shared/install.c b/src/shared/install.c
index 62151d9..1f9d7e2 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -556,7 +556,7 @@ int unit_file_mask(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
@@ -625,7 +625,7 @@ int unit_file_unmask(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
UnitFileChange **changes,
unsigned *n_changes) {
@@ -690,7 +690,7 @@ int unit_file_link(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
@@ -1462,7 +1462,7 @@ int unit_file_enable(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
@@ -1502,7 +1502,7 @@ int unit_file_disable(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
UnitFileChange **changes,
unsigned *n_changes) {
@@ -1543,7 +1543,7 @@ int unit_file_reenable(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
@@ -1820,7 +1820,7 @@ int unit_file_preset(
UnitFileScope scope,
bool runtime,
const char *root_dir,
- char *files[],
+ char **files,
bool force,
UnitFileChange **changes,
unsigned *n_changes) {
diff --git a/src/shared/install.h b/src/shared/install.h
index e87c57e..5d57b1b 100644
--- a/src/shared/install.h
+++ b/src/shared/install.h
@@ -73,13 +73,13 @@ typedef struct {
char **required_by;
} InstallInfo;
-int unit_file_enable(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_disable(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], UnitFileChange **changes, unsigned *n_changes);
-int unit_file_reenable(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_link(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_preset(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_mask(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_unmask(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], UnitFileChange **changes, unsigned *n_changes);
+int unit_file_enable(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
+int unit_file_disable(UnitFileScope scope, bool runtime, const char *root_dir, char **files, UnitFileChange **changes, unsigned *n_changes);
+int unit_file_reenable(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
+int unit_file_link(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
+int unit_file_preset(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
+int unit_file_mask(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
+int unit_file_unmask(UnitFileScope scope, bool runtime, const char *root_dir, char **files, UnitFileChange **changes, unsigned *n_changes);
int unit_file_set_default(UnitFileScope scope, const char *root_dir, const char *file, bool force, UnitFileChange **changes, unsigned *n_changes);
int unit_file_get_default(UnitFileScope scope, const char *root_dir, char **name);
commit cc3d61683d877791e6b2ce0b17940d4d77cd6550
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Sun Jan 5 00:32:43 2014 -0500
Remove unused variables
diff --git a/src/core/main.c b/src/core/main.c
index 6db4299..1b1741a 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -599,8 +599,6 @@ static int config_parse_join_controllers(const char *unit,
for (a = arg_join_controllers; *a; a++) {
if (strv_overlap(*a, l)) {
- char **c;
-
if (strv_extend_strv(&l, *a) < 0) {
strv_free(l);
strv_free_free(t);
diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c
index 01987f2..3ac25fa 100644
--- a/src/modules-load/modules-load.c
+++ b/src/modules-load/modules-load.c
@@ -57,7 +57,6 @@ static void systemd_kmod_log(void *data, int priority, const char *file, int lin
#pragma GCC diagnostic pop
static int add_modules(const char *p) {
- char **t;
_cleanup_strv_free_ char **k = NULL;
k = strv_split(p, ",");
diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c
index 57112a7..76efacb 100644
--- a/src/sysctl/sysctl.c
+++ b/src/sysctl/sysctl.c
@@ -245,7 +245,6 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_PREFIX: {
char *p;
- char **l;
for (p = optarg; *p; p++)
if (*p == '.')
commit e3e45d4f82daa5cd85ba40dde9127df900096c0c
Author: Simon Peeters <peeters.simon at gmail.com>
Date: Sat Jan 4 02:35:27 2014 +0100
strv: multiple cleanups
- turn strv_merge into strv_extend_strv.
appending strv b to the end of strv a instead of creating a new strv
- strv_append: remove in favor of strv_extend and strv_push.
- strv_remove: write slightly more elegant
- strv_remove_prefix: remove unused function
- strv_overlap: use strv_contains
- strv_printf: STRV_FOREACH handles NULL correctly
diff --git a/src/core/main.c b/src/core/main.c
index d052c8d..6db4299 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -601,15 +601,12 @@ static int config_parse_join_controllers(const char *unit,
if (strv_overlap(*a, l)) {
char **c;
- c = strv_merge(*a, l);
- if (!c) {
+ if (strv_extend_strv(&l, *a) < 0) {
strv_free(l);
strv_free_free(t);
return log_oom();
}
- strv_free(l);
- l = c;
} else {
char **c;
@@ -1853,10 +1850,11 @@ finish:
shutdown_verb,
NULL
};
- char **env_block;
+ _cleanup_strv_free_ char **env_block = NULL;
+ env_block = strv_copy(environ);
if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
- char e[32];
+ char *e;
/* If we reboot let's set the shutdown
* watchdog and tell the shutdown binary to
@@ -1864,15 +1862,11 @@ finish:
watchdog_set_timeout(&arg_shutdown_watchdog);
watchdog_close(false);
- /* Tell the binary how often to ping */
- snprintf(e, sizeof(e), "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog);
- char_array_0(e);
-
- env_block = strv_append(environ, e);
- } else {
- env_block = strv_copy(environ);
+ /* Tell the binary how often to ping, ignore failure */
+ if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
+ strv_push(&env_block, e);
+ } else
watchdog_close(true);
- }
/* Avoid the creation of new processes forked by the
* kernel; at this point, we will not listen to the
@@ -1881,7 +1875,6 @@ finish:
cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
- free(env_block);
log_error("Failed to execute shutdown binary, freezing: %m");
}
diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c
index 5d141a8..01987f2 100644
--- a/src/modules-load/modules-load.c
+++ b/src/modules-load/modules-load.c
@@ -64,13 +64,9 @@ static int add_modules(const char *p) {
if (!k)
return log_oom();
- t = strv_merge(arg_proc_cmdline_modules, k);
- if (!t)
+ if (strv_extend_strv(&arg_proc_cmdline_modules, k) < 0)
return log_oom();
- strv_free(arg_proc_cmdline_modules);
- arg_proc_cmdline_modules = t;
-
return 0;
}
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
index 1a47ea9..e2ca942 100644
--- a/src/shared/path-lookup.c
+++ b/src/shared/path-lookup.c
@@ -90,9 +90,9 @@ static char** user_dirs(
};
const char *home, *e;
- char *config_home = NULL, *data_home = NULL;
- char **config_dirs = NULL, **data_dirs = NULL;
- char **r = NULL, **t;
+ _cleanup_free_ char *config_home = NULL, *data_home = NULL;
+ _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
+ char **r = NULL;
/* Implement the mechanisms defined in
*
@@ -150,89 +150,48 @@ static char** user_dirs(
goto fail;
/* Now merge everything we found. */
- if (generator_early) {
- t = strv_append(r, generator_early);
- if (!t)
+ if (generator_early)
+ if (strv_extend(&r, generator_early) < 0)
goto fail;
- strv_free(r);
- r = t;
- }
- if (config_home) {
- t = strv_append(r, config_home);
- if (!t)
+ if (config_home)
+ if (strv_extend(&r, config_home) < 0)
goto fail;
- strv_free(r);
- r = t;
- }
- if (!strv_isempty(config_dirs)) {
- t = strv_merge_concat(r, config_dirs, "/systemd/user");
- if (!t)
- goto finish;
- strv_free(r);
- r = t;
- }
+ if (!strv_isempty(config_dirs))
+ if (strv_extend_strv_concat(&r, config_dirs, "/systemd/user") < 0)
+ goto fail;
- t = strv_merge(r, (char**) config_unit_paths);
- if (!t)
+ if (strv_extend_strv(&r, (char**) config_unit_paths) < 0)
goto fail;
- strv_free(r);
- r = t;
- if (generator) {
- t = strv_append(r, generator);
- if (!t)
+ if (generator)
+ if (strv_extend(&r, generator) < 0)
goto fail;
- strv_free(r);
- r = t;
- }
- if (data_home) {
- t = strv_append(r, data_home);
- if (!t)
+ if (data_home)
+ if (strv_extend(&r, data_home) < 0)
goto fail;
- strv_free(r);
- r = t;
- }
- if (!strv_isempty(data_dirs)) {
- t = strv_merge_concat(r, data_dirs, "/systemd/user");
- if (!t)
+ if (!strv_isempty(data_dirs))
+ if (strv_extend_strv_concat(&r, data_dirs, "/systemd/user") < 0)
goto fail;
- strv_free(r);
- r = t;
- }
- t = strv_merge(r, (char**) data_unit_paths);
- if (!t)
+ if (strv_extend_strv(&r, (char**) data_unit_paths) < 0)
goto fail;
- strv_free(r);
- r = t;
- if (generator_late) {
- t = strv_append(r, generator_late);
- if (!t)
+ if (generator_late)
+ if (strv_extend(&r, generator_late) < 0)
goto fail;
- strv_free(r);
- r = t;
- }
if (!path_strv_make_absolute_cwd(r))
goto fail;
-finish:
- free(config_home);
- strv_free(config_dirs);
- free(data_home);
- strv_free(data_dirs);
-
return r;
fail:
strv_free(r);
- r = NULL;
- goto finish;
+ return NULL;
}
int lookup_paths_init(
diff --git a/src/shared/strv.c b/src/shared/strv.c
index 607c221..13deba7 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -166,72 +166,38 @@ char **strv_new(const char *x, ...) {
return r;
}
-char **strv_merge(char **a, char **b) {
- char **r, **k;
-
- if (!a)
- return strv_copy(b);
-
- if (!b)
- return strv_copy(a);
-
- r = new(char*, strv_length(a) + strv_length(b) + 1);
- if (!r)
- return NULL;
-
- for (k = r; *a; k++, a++) {
- *k = strdup(*a);
- if (!*k)
- goto fail;
- }
+int strv_extend_strv(char ***a, char **b) {
+ int r;
+ char **s;
- for (; *b; k++, b++) {
- *k = strdup(*b);
- if (!*k)
- goto fail;
+ STRV_FOREACH(s, b) {
+ r = strv_extend(a, *s);
+ if (r < 0)
+ return r;
}
- *k = NULL;
- return r;
-
-fail:
- strv_free(r);
- return NULL;
+ return 0;
}
-char **strv_merge_concat(char **a, char **b, const char *suffix) {
- char **r, **k;
-
- /* Like strv_merge(), but appends suffix to all strings in b, before adding */
+int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
+ int r;
+ char **s;
- if (!b)
- return strv_copy(a);
+ STRV_FOREACH(s, b) {
+ char *v;
- r = new(char*, strv_length(a) + strv_length(b) + 1);
- if (!r)
- return NULL;
+ v = strappend(*s, suffix);
+ if (!v)
+ return -ENOMEM;
- k = r;
- if (a)
- for (; *a; k++, a++) {
- *k = strdup(*a);
- if (!*k)
- goto fail;
+ r = strv_push(a, v);
+ if (r < 0) {
+ free(v);
+ return r;
}
-
- for (; *b; k++, b++) {
- *k = strappend(*b, suffix);
- if (!*k)
- goto fail;
}
- *k = NULL;
- return r;
-
-fail:
- strv_free(r);
- return NULL;
-
+ return 0;
}
char **strv_split(const char *s, const char *separator) {
@@ -393,37 +359,6 @@ char *strv_join_quoted(char **l) {
return NULL;
}
-char **strv_append(char **l, const char *s) {
- char **r, **k;
-
- if (!l)
- return strv_new(s, NULL);
-
- if (!s)
- return strv_copy(l);
-
- r = new(char*, strv_length(l)+2);
- if (!r)
- return NULL;
-
- for (k = r; *l; k++, l++) {
- *k = strdup(*l);
- if (!*k)
- goto fail;
- }
-
- k[0] = strdup(s);
- if (!k[0])
- goto fail;
-
- k[1] = NULL;
- return r;
-
-fail:
- strv_free(r);
- return NULL;
-}
-
int strv_push(char ***l, char *value) {
char **c;
unsigned n;
@@ -484,40 +419,11 @@ char **strv_remove(char **l, const char *s) {
/* Drops every occurrence of s in the string list, edits
* in-place. */
- for (f = t = l; *f; f++) {
-
- if (streq(*f, s)) {
- free(*f);
- continue;
- }
-
- *(t++) = *f;
- }
-
- *t = NULL;
- return l;
-}
-
-char **strv_remove_prefix(char **l, const char *s) {
- char **f, **t;
-
- if (!l)
- return NULL;
-
- assert(s);
-
- /* Drops every occurrence of a string prefixed with s in the
- * string list, edits in-place. */
-
- for (f = t = l; *f; f++) {
-
- if (startswith(*f, s)) {
+ for (f = t = l; *f; f++)
+ if (streq(*f, s))
free(*f);
- continue;
- }
-
- *(t++) = *f;
- }
+ else
+ *(t++) = *f;
*t = NULL;
return l;
@@ -586,14 +492,11 @@ char **strv_split_nulstr(const char *s) {
}
bool strv_overlap(char **a, char **b) {
- char **i, **j;
+ char **i;
- STRV_FOREACH(i, a) {
- STRV_FOREACH(j, b) {
- if (streq(*i, *j))
- return true;
- }
- }
+ STRV_FOREACH(i, a)
+ if (strv_contains(b, *i))
+ return true;
return false;
}
@@ -616,9 +519,6 @@ char **strv_sort(char **l) {
void strv_print(char **l) {
char **s;
- if (!l)
- return;
-
STRV_FOREACH(s, l)
puts(*s);
}
diff --git a/src/shared/strv.h b/src/shared/strv.h
index daf9ad0..715bc54 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -36,14 +36,12 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
char **strv_copy(char * const *l);
unsigned strv_length(char * const *l) _pure_;
-char **strv_merge(char **a, char **b);
-char **strv_merge_concat(char **a, char **b, const char *suffix);
-char **strv_append(char **l, const char *s);
+int strv_extend_strv(char ***a, char **b);
+int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
int strv_extend(char ***l, const char *value);
int strv_push(char ***l, char *value);
char **strv_remove(char **l, const char *s);
-char **strv_remove_prefix(char **l, const char *s);
char **strv_uniq(char **l);
#define strv_contains(l, s) (!!strv_find((l), (s)))
diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c
index 7ebe1e2..57112a7 100644
--- a/src/sysctl/sysctl.c
+++ b/src/sysctl/sysctl.c
@@ -251,13 +251,9 @@ static int parse_argv(int argc, char *argv[]) {
if (*p == '.')
*p = '/';
- l = strv_append(arg_prefixes, optarg);
- if (!l)
+ if (strv_extend(&arg_prefixes, optarg) < 0)
return log_oom();
- strv_free(arg_prefixes);
- arg_prefixes = l;
-
break;
}
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 01a4489..dd95df1 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -1327,7 +1327,7 @@ static int list_dependencies_one(
char ***units,
unsigned int branches) {
- _cleanup_strv_free_ char **deps = NULL, **u;
+ _cleanup_strv_free_ char **deps = NULL;
char **c;
int r = 0;
@@ -1335,8 +1335,8 @@ static int list_dependencies_one(
assert(name);
assert(units);
- u = strv_append(*units, name);
- if (!u)
+ r = strv_extend(units, name);
+ if (r < 0)
return log_oom();
r = list_dependencies_get_dependencies(bus, name, &deps);
@@ -1348,7 +1348,7 @@ static int list_dependencies_one(
STRV_FOREACH(c, deps) {
int state;
- if (strv_contains(u, *c)) {
+ if (strv_contains(*units, *c)) {
if (!arg_plain) {
r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1);
if (r < 0)
@@ -1368,17 +1368,14 @@ static int list_dependencies_one(
return r;
if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
- r = list_dependencies_one(bus, *c, level + 1, &u, (branches << 1) | (c[1] == NULL ? 0 : 1));
+ r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1));
if (r < 0)
return r;
}
}
- if (arg_plain) {
- strv_free(*units);
- *units = u;
- u = NULL;
- }
+ if (!arg_plain)
+ strv_remove(*units, name);
return 0;
}
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index d58d99c..c8c5fc9 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -214,22 +214,6 @@ static void test_strv_split_nulstr(void) {
assert_se(streq(l[3], "str3"));
}
-static void test_strv_remove_prefix(void) {
- unsigned i = 0;
- char **s;
- _cleanup_strv_free_ char **l = NULL;
-
- l = strv_new("_one", "_two", "_three", NULL);
- assert(l);
-
- l = strv_remove_prefix(l, "_");
- assert(l);
-
- STRV_FOREACH(s, l) {
- assert_se(streq(*s, input_table_multiple[i++]));
- }
-}
-
static void test_strv_parse_nulstr(void) {
_cleanup_strv_free_ char **l = NULL;
const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
@@ -289,58 +273,54 @@ static void test_strv_sort(void) {
assert_se(streq(input_table[4], "durian"));
}
-static void test_strv_merge_concat(void) {
- _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
+static void test_strv_extend_strv_concat(void) {
+ _cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("without", "suffix", NULL);
b = strv_new("with", "suffix", NULL);
assert_se(a);
assert_se(b);
- c = strv_merge_concat(a, b, "_suffix");
- assert_se(c);
+ assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0);
- assert_se(streq(c[0], "without"));
- assert_se(streq(c[1], "suffix"));
- assert_se(streq(c[2], "with_suffix"));
- assert_se(streq(c[3], "suffix_suffix"));
+ assert_se(streq(a[0], "without"));
+ assert_se(streq(a[1], "suffix"));
+ assert_se(streq(a[2], "with_suffix"));
+ assert_se(streq(a[3], "suffix_suffix"));
}
-static void test_strv_merge(void) {
- _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
+static void test_strv_extend_strv(void) {
+ _cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("abc", "def", "ghi", NULL);
b = strv_new("jkl", "mno", "pqr", NULL);
assert_se(a);
assert_se(b);
- c = strv_merge(a, b);
- assert_se(c);
+ assert_se(strv_extend_strv(&a, b) >= 0);
- assert_se(streq(c[0], "abc"));
- assert_se(streq(c[1], "def"));
- assert_se(streq(c[2], "ghi"));
- assert_se(streq(c[3], "jkl"));
- assert_se(streq(c[4], "mno"));
- assert_se(streq(c[5], "pqr"));
+ assert_se(streq(a[0], "abc"));
+ assert_se(streq(a[1], "def"));
+ assert_se(streq(a[2], "ghi"));
+ assert_se(streq(a[3], "jkl"));
+ assert_se(streq(a[4], "mno"));
+ assert_se(streq(a[5], "pqr"));
- assert_se(strv_length(c) == 6);
+ assert_se(strv_length(a) == 6);
}
-static void test_strv_append(void) {
- _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
+static void test_strv_extend(void) {
+ _cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("test", "test1", NULL);
assert_se(a);
- b = strv_append(a, "test2");
- c = strv_append(NULL, "test3");
- assert_se(b);
- assert_se(c);
+ assert_se(strv_extend(&a, "test2") >= 0);
+ assert_se(strv_extend(&b, "test3") >= 0);
- assert_se(streq(b[0], "test"));
- assert_se(streq(b[1], "test1"));
- assert_se(streq(b[2], "test2"));
- assert_se(streq(c[0], "test3"));
+ assert_se(streq(a[0], "test"));
+ assert_se(streq(a[1], "test1"));
+ assert_se(streq(a[2], "test2"));
+ assert_se(streq(b[0], "test3"));
}
static void test_strv_foreach(void) {
@@ -436,12 +416,11 @@ int main(int argc, char *argv[]) {
test_strv_split_newlines();
test_strv_split_nulstr();
test_strv_parse_nulstr();
- test_strv_remove_prefix();
test_strv_overlap();
test_strv_sort();
- test_strv_merge();
- test_strv_merge_concat();
- test_strv_append();
+ test_strv_extend_strv();
+ test_strv_extend_strv_concat();
+ test_strv_extend();
test_strv_from_stdarg_alloca();
return 0;
commit bf85c24daaf63f72562bbe4c627ca8b963dfb964
Author: Simon Peeters <peeters.simon at gmail.com>
Date: Sat Jan 4 02:35:25 2014 +0100
shared: util.c: unify split and split_quoted
diff --git a/src/shared/util.c b/src/shared/util.c
index c824c39..5551714 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -359,8 +359,23 @@ int safe_atod(const char *s, double *ret_d) {
return 0;
}
+static size_t strcspn_escaped(const char *s, const char *reject) {
+ bool escaped = false;
+ size_t n;
+
+ for (n=0; s[n]; n++) {
+ if (escaped)
+ escaped = false;
+ else if (s[n] == '\\')
+ escaped = true;
+ else if (strchr(reject, s[n]))
+ return n;
+ }
+ return n;
+}
+
/* Split a string into words. */
-char *split(const char *c, size_t *l, const char *separator, char **state) {
+char *split(const char *c, size_t *l, const char *separator, bool quoted, char **state) {
char *current;
current = *state ? *state : (char*) c;
@@ -369,70 +384,19 @@ char *split(const char *c, size_t *l, const char *separator, char **state) {
return NULL;
current += strspn(current, separator);
- *l = strcspn(current, separator);
- *state = current+*l;
-
- return (char*) current;
-}
-
-/* Split a string into words, but consider strings enclosed in '' and
- * "" as words even if they include spaces. */
-char *split_quoted(const char *c, size_t *l, char **state) {
- const char *current, *e;
- bool escaped = false;
-
- assert(c);
- assert(l);
- assert(state);
-
- current = *state ? *state : c;
-
- current += strspn(current, WHITESPACE);
-
- if (*current == 0)
+ if (!*current)
return NULL;
- else if (*current == '\'') {
- current ++;
-
- for (e = current; *e; e++) {
- if (escaped)
- escaped = false;
- else if (*e == '\\')
- escaped = true;
- else if (*e == '\'')
- break;
- }
-
- *l = e-current;
- *state = (char*) (*e == 0 ? e : e+1);
-
- } else if (*current == '\"') {
- current ++;
-
- for (e = current; *e; e++) {
- if (escaped)
- escaped = false;
- else if (*e == '\\')
- escaped = true;
- else if (*e == '\"')
- break;
- }
-
- *l = e-current;
- *state = (char*) (*e == 0 ? e : e+1);
-
+ if (quoted && strchr("\'\"", *current)) {
+ char quotechar = *(current++);
+ *l = strcspn_escaped(current, (char[]){quotechar, '\0'});
+ *state = current+*l+1;
+ } else if (quoted) {
+ *l = strcspn_escaped(current, separator);
+ *state = current+*l;
} else {
- for (e = current; *e; e++) {
- if (escaped)
- escaped = false;
- else if (*e == '\\')
- escaped = true;
- else if (strchr(WHITESPACE, *e))
- break;
- }
- *l = e-current;
- *state = (char*) e;
+ *l = strcspn(current, separator);
+ *state = current+*l;
}
return (char*) current;
diff --git a/src/shared/util.h b/src/shared/util.h
index 8645654..ebc765d 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -198,17 +198,22 @@ static inline int safe_atoi64(const char *s, int64_t *ret_i) {
return safe_atolli(s, (long long int*) ret_i);
}
-char *split(const char *c, size_t *l, const char *separator, char **state);
-char *split_quoted(const char *c, size_t *l, char **state);
+char *split(const char *c, size_t *l, const char *separator, bool quoted, char **state);
#define FOREACH_WORD(word, length, s, state) \
- for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
+ _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
- for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
+ _FOREACH_WORD(word, length, s, separator, false, state)
#define FOREACH_WORD_QUOTED(word, length, s, state) \
- for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
+ _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
+
+#define FOREACH_WORD_SEPARATOR_QUOTED(word, length, s, separator, state) \
+ _FOREACH_WORD(word, length, s, separator, true, state)
+
+#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
+ for ((state) = NULL, (word) = split((s), &(length), (separator), (quoted), &(state)); (word); (word) = split((s), &(length), (separator), (quoted), &(state)))
pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
int get_starttime_of_pid(pid_t pid, unsigned long long *st);
More information about the systemd-commits
mailing list