]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
locale-util: add new helper locale_is_installed()
authorLennart Poettering <lennart@poettering.net>
Thu, 30 Apr 2020 16:30:56 +0000 (18:30 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 7 May 2020 15:23:23 +0000 (17:23 +0200)
This new helper checks whether the specified locale is installed. It's
distinct from locale_is_valid() which just superficially checks if a
string looks like something that could be a valid locale.

Heavily inspired by @jsynacek's #13964.

Replaces: #13964

src/basic/locale-util.c
src/basic/locale-util.h

index e4ce289c519ed23f79c5b39b8859b625ad717cd3..ab5f86f54a308ecc68c0a2cd939fe44f78282080 100644 (file)
@@ -254,6 +254,21 @@ bool locale_is_valid(const char *name) {
         return true;
 }
 
+int locale_is_installed(const char *name) {
+        if (!locale_is_valid(name))
+                return false;
+
+        if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
+                return true;
+
+        _cleanup_(freelocalep) locale_t loc =
+                newlocale(LC_ALL_MASK, name, 0);
+        if (loc == (locale_t) 0)
+                return errno == ENOMEM ? -ENOMEM : false;
+
+        return true;
+}
+
 void init_gettext(void) {
         setlocale(LC_ALL, "");
         textdomain(GETTEXT_PACKAGE);
index 7d77fa2bdae05de4bc650d90805fdca6d99cc621..6bd377205963aa82b32b3bd8a492d55e8e08f172 100644 (file)
@@ -31,6 +31,7 @@ typedef enum LocaleVariable {
 
 int get_locales(char ***l);
 bool locale_is_valid(const char *name);
+int locale_is_installed(const char *name);
 
 #define _(String) gettext(String)
 #define N_(String) String