util-lib: make localed's nonempty() generic, rename it to empty_to_null() and make use of it everywhere

This commit is contained in:
Lennart Poettering 2016-05-30 17:59:43 +02:00
parent c6f8d17de0
commit 3c6f7c3402
7 changed files with 26 additions and 52 deletions

View file

@ -66,6 +66,10 @@ static inline bool isempty(const char *p) {
return !p || !p[0];
}
static inline const char *empty_to_null(const char *p) {
return isempty(p) ? NULL : p;
}
static inline char *startswith(const char *s, const char *prefix) {
size_t l;

View file

@ -479,8 +479,7 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
if (r < 0)
return r;
if (isempty(name))
name = NULL;
name = empty_to_null(name);
if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
return sd_bus_reply_method_return(m, NULL);
@ -499,9 +498,9 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (isempty(name)) {
if (isempty(name))
c->data[PROP_STATIC_HOSTNAME] = mfree(c->data[PROP_STATIC_HOSTNAME]);
} else {
else {
char *h;
if (!hostname_is_valid(name, false))
@ -546,8 +545,7 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess
if (r < 0)
return r;
if (isempty(name))
name = NULL;
name = empty_to_null(name);
if (streq_ptr(name, c->data[prop]))
return sd_bus_reply_method_return(m, NULL);
@ -570,9 +568,9 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (isempty(name)) {
if (isempty(name))
c->data[prop] = mfree(c->data[prop]);
} else {
else {
char *h;
/* The icon name might ultimately be used as file

View file

@ -97,10 +97,6 @@ typedef struct Context {
Hashmap *polkit_registry;
} Context;
static const char* nonempty(const char *s) {
return isempty(s) ? NULL : s;
}
static bool startswith_comma(const char *s, const char *prefix) {
const char *t;
@ -171,8 +167,7 @@ static int locale_read_data(Context *c) {
for (p = 0; p < _LOCALE_MAX; p++) {
assert(names[p]);
r = free_and_strdup(&c->locale[p],
nonempty(getenv(names[p])));
r = free_and_strdup(&c->locale[p], empty_to_null(getenv(names[p])));
if (r < 0)
return r;
}
@ -1041,11 +1036,8 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
if (r < 0)
return r;
if (isempty(keymap))
keymap = NULL;
if (isempty(keymap_toggle))
keymap_toggle = NULL;
keymap = empty_to_null(keymap);
keymap_toggle = empty_to_null(keymap_toggle);
if (!streq_ptr(keymap, c->vc_keymap) ||
!streq_ptr(keymap_toggle, c->vc_keymap_toggle)) {
@ -1214,17 +1206,10 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
if (r < 0)
return r;
if (isempty(layout))
layout = NULL;
if (isempty(model))
model = NULL;
if (isempty(variant))
variant = NULL;
if (isempty(options))
options = NULL;
layout = empty_to_null(layout);
model = empty_to_null(model);
variant = empty_to_null(variant);
options = empty_to_null(options);
if (!streq_ptr(layout, c->x11_layout) ||
!streq_ptr(model, c->x11_model) ||

View file

@ -655,8 +655,7 @@ int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bu
r = sd_bus_message_read(message, "ss", &user, &path);
if (r < 0)
return r;
if (isempty(user))
user = NULL;
user = empty_to_null(user);
if (isempty(path))
path = "/bin/sh";
if (!path_is_absolute(path))

View file

@ -658,10 +658,8 @@ static int resolve_service(sd_bus *bus, const char *name, const char *type, cons
assert(bus);
assert(domain);
if (isempty(name))
name = NULL;
if (isempty(type))
type = NULL;
name = empty_to_null(name);
type = empty_to_null(type);
if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
@ -820,10 +818,8 @@ static int resolve_service(sd_bus *bus, const char *name, const char *type, cons
if (r < 0)
return bus_log_parse_error(r);
if (isempty(canonical_name))
canonical_name = NULL;
if (isempty(canonical_type))
canonical_type = NULL;
canonical_name = empty_to_null(canonical_name);
canonical_type = empty_to_null(canonical_type);
if (!streq_ptr(name, canonical_name) ||
!streq_ptr(type, canonical_type) ||

View file

@ -5177,9 +5177,7 @@ static int switch_root(int argc, char *argv[], void *userdata) {
init = cmdline_init;
}
if (isempty(init))
init = NULL;
init = empty_to_null(init);
if (init) {
const char *root_systemd_path = NULL, *root_init_path = NULL;

View file

@ -527,9 +527,7 @@ static int load_sysv(SysvStub *s) {
t[k-1] = 0;
}
j = strstrip(t+12);
if (isempty(j))
j = NULL;
j = empty_to_null(strstrip(t+12));
r = free_and_strdup(&chkconfig_description, j);
if (r < 0)
@ -605,9 +603,7 @@ static int load_sysv(SysvStub *s) {
state = LSB_DESCRIPTION;
j = strstrip(t+12);
if (isempty(j))
j = NULL;
j = empty_to_null(strstrip(t+12));
r = free_and_strdup(&long_description, j);
if (r < 0)
@ -618,9 +614,7 @@ static int load_sysv(SysvStub *s) {
state = LSB;
j = strstrip(t+18);
if (isempty(j))
j = NULL;
j = empty_to_null(strstrip(t+18));
r = free_and_strdup(&short_description, j);
if (r < 0)