teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0484-locale-util-add-new-helper-locale_is_installed.patch

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