journalctl and vconsole-setup both implement utf8 locale detection.
Let's have a common function for it.
The next patch will add another use.
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
-#include <locale.h>
-#include <langinfo.h>
#include <systemd/sd-journal.h>
#ifdef HAVE_QRENCODE
/* If this is not an UTF-8 system don't print any QR codes */
- setlocale(LC_CTYPE, "");
-
- if (streq_ptr(nl_langinfo(CODESET), "UTF-8")) {
+ if (is_locale_utf8()) {
fputs("\nTo transfer the verification key to your phone please scan the QR code below:\n\n", stderr);
print_qr_code(stderr, seed, seed_size, n, arg_interval, hn, machine);
}
#include <sys/vfs.h>
#include <linux/magic.h>
#include <limits.h>
+#include <langinfo.h>
+#include <locale.h>
#include "macro.h"
#include "util.h"
}
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;
+}
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);
#include <stdbool.h>
#include <stdarg.h>
#include <limits.h>
-#include <locale.h>
-#include <langinfo.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <linux/tiocl.h>
return ioctl(fd, TIOCLINUX, data) >= 0;
}
-static bool is_locale_utf8(void) {
- const char *set;
-
- if (!setlocale(LC_ALL, ""))
- return true;
-
- set = nl_langinfo(CODESET);
- if (!set)
- return true;
-
- return streq(set, "UTF-8");
-}
-
static int disable_utf8(int fd) {
int r = 0, k;