daandemeyer / rpms / systemd

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