summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-11-12 16:50:58 +0100
committerLennart Poettering <lennart@poettering.net>2018-11-14 17:01:55 +0100
commite6755a33509f425463ddbbf99839ba3ae6979216 (patch)
tree0aedbd7362da192e07dd7819ee86a99700bd39e4 /src
parent13df9c398d60e441a65b11ada491f750947649bf (diff)
downloadsystemd-e6755a33509f425463ddbbf99839ba3ae6979216.tar.gz
locale-util: introduce common helper locale_variables_free() for freeing locale variable arrays
Diffstat (limited to 'src')
-rw-r--r--src/basic/locale-util.c10
-rw-r--r--src/basic/locale-util.h5
-rw-r--r--src/core/locale-setup.c34
-rw-r--r--src/locale/localectl.c11
-rw-r--r--src/locale/localed.c13
5 files changed, 32 insertions, 41 deletions
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
index 8b89bd0024..9ad51a1972 100644
--- a/src/basic/locale-util.c
+++ b/src/basic/locale-util.c
@@ -389,6 +389,16 @@ const char *special_glyph(SpecialGlyph code) {
return draw_table[is_locale_utf8()][code];
}
+void locale_variables_free(char*l[_VARIABLE_LC_MAX]) {
+ LocaleVariable i;
+
+ if (!l)
+ return;
+
+ for (i = 0; i < _VARIABLE_LC_MAX; i++)
+ l[i] = mfree(l[i]);
+}
+
static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
[VARIABLE_LANG] = "LANG",
[VARIABLE_LANGUAGE] = "LANGUAGE",
diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h
index 7762254940..14beece6d8 100644
--- a/src/basic/locale-util.h
+++ b/src/basic/locale-util.h
@@ -66,3 +66,8 @@ static inline void freelocalep(locale_t *p) {
freelocale(*p);
}
+
+void locale_variables_free(char* l[_VARIABLE_LC_MAX]);
+static inline void locale_variables_freep(char*(*l)[_VARIABLE_LC_MAX]) {
+ locale_variables_free(*l);
+}
diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c
index 64fddca48c..fff3a798c4 100644
--- a/src/core/locale-setup.c
+++ b/src/core/locale-setup.c
@@ -15,7 +15,7 @@
#include "virt.h"
int locale_setup(char ***environment) {
- char *variables[_VARIABLE_LC_MAX] = {};
+ _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
_cleanup_strv_free_ char **add = NULL;
LocaleVariable i;
int r;
@@ -66,25 +66,19 @@ int locale_setup(char ***environment) {
continue;
s = strjoin(locale_variable_to_string(i), "=", variables[i]);
- if (!s) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!s)
+ return -ENOMEM;
- if (strv_consume(&add, s) < 0) {
- r = -ENOMEM;
- goto finish;
- }
+ if (strv_consume(&add, s) < 0)
+ return -ENOMEM;
}
if (strv_isempty(add)) {
/* If no locale is configured then default to C.UTF-8. */
add = strv_new("LANG=C.UTF-8");
- if (!add) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!add)
+ return -ENOMEM;
}
if (strv_isempty(*environment))
@@ -93,19 +87,11 @@ int locale_setup(char ***environment) {
char **merged;
merged = strv_env_merge(2, *environment, add);
- if (!merged) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!merged)
+ return -ENOMEM;
strv_free_and_replace(*environment, merged);
}
- r = 0;
-
-finish:
- for (i = 0; i < _VARIABLE_LC_MAX; i++)
- free(variables[i]);
-
- return r;
+ return 0;
}
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index c8278a383d..75e7a55676 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -49,10 +49,10 @@ static void status_info_clear(StatusInfo *info) {
}
static void print_overridden_variables(void) {
- int r;
- char *variables[_VARIABLE_LC_MAX] = {};
- LocaleVariable j;
+ _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
bool print_warning = true;
+ LocaleVariable j;
+ int r;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return;
@@ -75,7 +75,7 @@ static void print_overridden_variables(void) {
"locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
if (r < 0 && r != -ENOENT) {
log_warning_errno(r, "Failed to read /proc/cmdline: %m");
- goto finish;
+ return;
}
for (j = 0; j < _VARIABLE_LC_MAX; j++)
@@ -88,9 +88,6 @@ static void print_overridden_variables(void) {
} else
log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
}
- finish:
- for (j = 0; j < _VARIABLE_LC_MAX; j++)
- free(variables[j]);
}
static void print_status_info(StatusInfo *i) {
diff --git a/src/locale/localed.c b/src/locale/localed.c
index 043fffaccb..21d1ded65c 100644
--- a/src/locale/localed.c
+++ b/src/locale/localed.c
@@ -255,20 +255,13 @@ static int property_get_xkb(
return -EINVAL;
}
-static void locale_free(char ***l) {
- int p;
-
- for (p = 0; p < _VARIABLE_LC_MAX; p++)
- (*l)[p] = mfree((*l)[p]);
-}
-
static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *error) {
- Context *c = userdata;
+ _cleanup_(locale_variables_freep) char *new_locale[_VARIABLE_LC_MAX] = {};
_cleanup_strv_free_ char **settings = NULL, **l = NULL;
- char *new_locale[_VARIABLE_LC_MAX] = {}, **i;
- _cleanup_(locale_free) _unused_ char **dummy = new_locale;
+ Context *c = userdata;
bool modified = false;
int interactive, p, r;
+ char **i;
assert(m);
assert(c);