21255d
From f0d9e0cb24958bc11c8d83f0a3de651def2aa1d6 Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Thu, 30 Apr 2020 18:30:56 +0200
21255d
Subject: [PATCH] locale-util: add new helper locale_is_installed()
21255d
21255d
This new helper checks whether the specified locale is installed. It's
21255d
distinct from locale_is_valid() which just superficially checks if a
21255d
string looks like something that could be a valid locale.
21255d
21255d
Heavily inspired by @jsynacek's #13964.
21255d
21255d
Replaces: #13964
21255d
(cherry picked from commit 23fa786ca67ed3a32930ff1a7b175ac823db187c)
21255d
21255d
Related: #1755287
21255d
---
21255d
 src/basic/locale-util.c | 15 +++++++++++++++
21255d
 src/basic/locale-util.h |  1 +
21255d
 2 files changed, 16 insertions(+)
21255d
21255d
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
21255d
index 7cd143ea6f..42ef309ebd 100644
21255d
--- a/src/basic/locale-util.c
21255d
+++ b/src/basic/locale-util.c
21255d
@@ -204,6 +204,21 @@ bool locale_is_valid(const char *name) {
21255d
         return true;
21255d
 }
21255d
 
21255d
+int locale_is_installed(const char *name) {
21255d
+        if (!locale_is_valid(name))
21255d
+                return false;
21255d
+
21255d
+        if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
21255d
+                return true;
21255d
+
21255d
+        _cleanup_(freelocalep) locale_t loc =
21255d
+                newlocale(LC_ALL_MASK, name, 0);
21255d
+        if (loc == (locale_t) 0)
21255d
+                return errno == ENOMEM ? -ENOMEM : false;
21255d
+
21255d
+        return true;
21255d
+}
21255d
+
21255d
 void init_gettext(void) {
21255d
         setlocale(LC_ALL, "");
21255d
         textdomain(GETTEXT_PACKAGE);
21255d
diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h
21255d
index 368675f286..b40f9c641a 100644
21255d
--- a/src/basic/locale-util.h
21255d
+++ b/src/basic/locale-util.h
21255d
@@ -31,6 +31,7 @@ typedef enum LocaleVariable {
21255d
 
21255d
 int get_locales(char ***l);
21255d
 bool locale_is_valid(const char *name);
21255d
+int locale_is_installed(const char *name);
21255d
 
21255d
 #define _(String) gettext(String)
21255d
 #define N_(String) String