1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-22 22:03:43 +03:00

Merge pull request #12245 from poettering/empty-or-dash

introduce empty_or_dash() helper
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-08 15:22:44 +02:00 committed by GitHub
commit afae22ca41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 53 additions and 66 deletions

View File

@ -0,0 +1,5 @@
@@
expression s;
@@
- (isempty(s) || streq(s, "-"))
+ empty_or_dash(s)

View File

@ -57,6 +57,16 @@ static inline const char *empty_to_dash(const char *str) {
return isempty(str) ? "-" : str; return isempty(str) ? "-" : str;
} }
static inline bool empty_or_dash(const char *str) {
return !str ||
str[0] == 0 ||
(str[0] == '-' && str[1] == 0);
}
static inline const char *empty_or_dash_to_null(const char *p) {
return empty_or_dash(p) ? NULL : p;
}
static inline char *startswith(const char *s, const char *prefix) { static inline char *startswith(const char *s, const char *prefix) {
size_t l; size_t l;

View File

@ -78,8 +78,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
if (argc >= 3) if (argc >= 3)
path = argv[2]; path = argv[2];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
determine_compression_from_filename(path); determine_compression_from_filename(path);
@ -155,8 +154,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
if (argc >= 3) if (argc >= 3)
path = argv[2]; path = argv[2];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
determine_compression_from_filename(path); determine_compression_from_filename(path);

View File

@ -117,15 +117,13 @@ static int import_fs(int argc, char *argv[], void *userdata) {
if (argc >= 2) if (argc >= 2)
path = argv[1]; path = argv[1];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
if (argc >= 3) if (argc >= 3)
local = argv[2]; local = argv[2];
else if (path) else if (path)
local = basename(path); local = basename(path);
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
if (!machine_name_is_valid(local)) { if (!machine_name_is_valid(local)) {

View File

@ -49,15 +49,13 @@ static int import_tar(int argc, char *argv[], void *userdata) {
if (argc >= 2) if (argc >= 2)
path = argv[1]; path = argv[1];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
if (argc >= 3) if (argc >= 3)
local = argv[2]; local = argv[2];
else if (path) else if (path)
local = basename(path); local = basename(path);
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
r = tar_strip_suffixes(local, &ll); r = tar_strip_suffixes(local, &ll);
@ -145,15 +143,13 @@ static int import_raw(int argc, char *argv[], void *userdata) {
if (argc >= 2) if (argc >= 2)
path = argv[1]; path = argv[1];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
if (argc >= 3) if (argc >= 3)
local = argv[2]; local = argv[2];
else if (path) else if (path)
local = basename(path); local = basename(path);
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
r = raw_strip_suffixes(local, &ll); r = raw_strip_suffixes(local, &ll);

View File

@ -64,8 +64,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
local = l; local = l;
} }
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
r = tar_strip_suffixes(local, &ll); r = tar_strip_suffixes(local, &ll);
@ -151,8 +150,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
local = l; local = l;
} }
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
r = raw_strip_suffixes(local, &ll); r = raw_strip_suffixes(local, &ll);

View File

@ -32,10 +32,6 @@ static bool startswith_comma(const char *s, const char *prefix) {
return IN_SET(*s, ',', '\0'); return IN_SET(*s, ',', '\0');
} }
static const char* strnulldash(const char *s) {
return isempty(s) || streq(s, "-") ? NULL : s;
}
static const char* systemd_kbd_model_map(void) { static const char* systemd_kbd_model_map(void) {
const char* s; const char* s;
@ -551,15 +547,15 @@ int vconsole_convert_to_x11(Context *c) {
if (!streq(c->vc_keymap, a[0])) if (!streq(c->vc_keymap, a[0]))
continue; continue;
if (!streq_ptr(c->x11_layout, strnulldash(a[1])) || if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
!streq_ptr(c->x11_model, strnulldash(a[2])) || !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
!streq_ptr(c->x11_variant, strnulldash(a[3])) || !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
!streq_ptr(c->x11_options, strnulldash(a[4]))) { !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
if (free_and_strdup(&c->x11_layout, strnulldash(a[1])) < 0 || if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
free_and_strdup(&c->x11_model, strnulldash(a[2])) < 0 || free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
free_and_strdup(&c->x11_variant, strnulldash(a[3])) < 0 || free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
free_and_strdup(&c->x11_options, strnulldash(a[4])) < 0) free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
return -ENOMEM; return -ENOMEM;
modified = true; modified = true;

View File

@ -1993,16 +1993,6 @@ static int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
return -r; return -r;
} }
static const char *nullify_dash(const char *p) {
if (isempty(p))
return NULL;
if (streq(p, "-"))
return NULL;
return p;
}
static int import_tar(int argc, char *argv[], void *userdata) { static int import_tar(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_free_ char *ll = NULL, *fn = NULL; _cleanup_free_ char *ll = NULL, *fn = NULL;
@ -2014,10 +2004,10 @@ static int import_tar(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
if (argc >= 2) if (argc >= 2)
path = nullify_dash(argv[1]); path = empty_or_dash_to_null(argv[1]);
if (argc >= 3) if (argc >= 3)
local = nullify_dash(argv[2]); local = empty_or_dash_to_null(argv[2]);
else if (path) { else if (path) {
r = path_extract_filename(path, &fn); r = path_extract_filename(path, &fn);
if (r < 0) if (r < 0)
@ -2081,10 +2071,10 @@ static int import_raw(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
if (argc >= 2) if (argc >= 2)
path = nullify_dash(argv[1]); path = empty_or_dash_to_null(argv[1]);
if (argc >= 3) if (argc >= 3)
local = nullify_dash(argv[2]); local = empty_or_dash_to_null(argv[2]);
else if (path) { else if (path) {
r = path_extract_filename(path, &fn); r = path_extract_filename(path, &fn);
if (r < 0) if (r < 0)
@ -2148,10 +2138,10 @@ static int import_fs(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
if (argc >= 2) if (argc >= 2)
path = nullify_dash(argv[1]); path = empty_or_dash_to_null(argv[1]);
if (argc >= 3) if (argc >= 3)
local = nullify_dash(argv[2]); local = empty_or_dash_to_null(argv[2]);
else if (path) { else if (path) {
r = path_extract_filename(path, &fn); r = path_extract_filename(path, &fn);
if (r < 0) if (r < 0)
@ -2230,8 +2220,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
if (argc >= 3) if (argc >= 3)
path = argv[2]; path = argv[2];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
if (path) { if (path) {
determine_compression_from_filename(path); determine_compression_from_filename(path);
@ -2280,8 +2269,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
if (argc >= 3) if (argc >= 3)
path = argv[2]; path = argv[2];
if (isempty(path) || streq(path, "-")) path = empty_or_dash_to_null(path);
path = NULL;
if (path) { if (path) {
determine_compression_from_filename(path); determine_compression_from_filename(path);
@ -2338,8 +2326,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
local = l; local = l;
} }
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
r = tar_strip_suffixes(local, &ll); r = tar_strip_suffixes(local, &ll);
@ -2402,8 +2389,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
local = l; local = l;
} }
if (isempty(local) || streq(local, "-")) local = empty_or_dash_to_null(local);
local = NULL;
if (local) { if (local) {
r = raw_strip_suffixes(local, &ll); r = raw_strip_suffixes(local, &ll);

View File

@ -1411,7 +1411,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
"[%s:%u] Unknown command type '%c'.", fname, line, action[0]); "[%s:%u] Unknown command type '%c'.", fname, line, action[0]);
/* Verify name */ /* Verify name */
if (isempty(name) || streq(name, "-")) if (empty_or_dash(name))
name = mfree(name); name = mfree(name);
if (name) { if (name) {
@ -1426,7 +1426,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
} }
/* Verify id */ /* Verify id */
if (isempty(id) || streq(id, "-")) if (empty_or_dash(id))
id = mfree(id); id = mfree(id);
if (id) { if (id) {
@ -1437,7 +1437,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
} }
/* Verify description */ /* Verify description */
if (isempty(description) || streq(description, "-")) if (empty_or_dash(description))
description = mfree(description); description = mfree(description);
if (description) { if (description) {
@ -1453,7 +1453,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
} }
/* Verify home */ /* Verify home */
if (isempty(home) || streq(home, "-")) if (empty_or_dash(home))
home = mfree(home); home = mfree(home);
if (home) { if (home) {
@ -1469,7 +1469,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
} }
/* Verify shell */ /* Verify shell */
if (isempty(shell) || streq(shell, "-")) if (empty_or_dash(shell))
shell = mfree(shell); shell = mfree(shell);
if (shell) { if (shell) {

View File

@ -2507,7 +2507,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
return -EIO; return -EIO;
} }
if (!isempty(buffer) && !streq(buffer, "-")) { if (!empty_or_dash(buffer)) {
i.argument = strdup(buffer); i.argument = strdup(buffer);
if (!i.argument) if (!i.argument)
return log_oom(); return log_oom();
@ -2704,7 +2704,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
free_and_replace(i.path, p); free_and_replace(i.path, p);
} }
if (!isempty(user) && !streq(user, "-")) { if (!empty_or_dash(user)) {
const char *u = user; const char *u = user;
r = get_user_creds(&u, &i.uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING); r = get_user_creds(&u, &i.uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
@ -2716,7 +2716,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
i.uid_set = true; i.uid_set = true;
} }
if (!isempty(group) && !streq(group, "-")) { if (!empty_or_dash(group)) {
const char *g = group; const char *g = group;
r = get_group_creds(&g, &i.gid, USER_CREDS_ALLOW_MISSING); r = get_group_creds(&g, &i.gid, USER_CREDS_ALLOW_MISSING);
@ -2729,7 +2729,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
i.gid_set = true; i.gid_set = true;
} }
if (!isempty(mode) && !streq(mode, "-")) { if (!empty_or_dash(mode)) {
const char *mm = mode; const char *mm = mode;
unsigned m; unsigned m;
@ -2749,7 +2749,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
} else } else
i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644; i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644;
if (!isempty(age) && !streq(age, "-")) { if (!empty_or_dash(age)) {
const char *a = age; const char *a = age;
if (*a == '~') { if (*a == '~') {