util: add is_locale_utf8()

journalctl and vconsole-setup both implement utf8 locale detection.
Let's have a common function for it.
The next patch will add another use.
This commit is contained in:
Michal Schmidt
2012-11-02 17:27:15 +01:00
parent 4940c64240
commit 0901758558
4 changed files with 28 additions and 20 deletions
+25
View File
@@ -57,6 +57,8 @@
#include <sys/vfs.h>
#include <linux/magic.h>
#include <limits.h>
#include <langinfo.h>
#include <locale.h>
#include "macro.h"
#include "util.h"
@@ -6115,3 +6117,26 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
}
return NULL;
}
bool is_locale_utf8(void) {
const char *set;
static int cached_answer = -1;
if (cached_answer >= 0)
goto out;
if (!setlocale(LC_ALL, "")) {
cached_answer = true;
goto out;
}
set = nl_langinfo(CODESET);
if (!set) {
cached_answer = true;
goto out;
}
cached_answer = streq(set, "UTF-8");
out:
return (bool)cached_answer;
}
+2
View File
@@ -598,3 +598,5 @@ int parse_timestamp(const char *t, usec_t *usec);
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *, void *),
void *arg);
bool is_locale_utf8(void);