locale-util: introduce common helper locale_variables_free() for freeing locale variable arrays

This commit is contained in:
Lennart Poettering 2018-11-12 16:50:58 +01:00
parent 13df9c398d
commit e6755a3350
5 changed files with 32 additions and 41 deletions

View File

@ -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",

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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) {

View File

@ -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);