mirror of
https://github.com/systemd/systemd.git
synced 2025-01-12 13:18:14 +03:00
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:
parent
4940c64240
commit
0901758558
@ -33,8 +33,6 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <locale.h>
|
|
||||||
#include <langinfo.h>
|
|
||||||
|
|
||||||
#include <systemd/sd-journal.h>
|
#include <systemd/sd-journal.h>
|
||||||
|
|
||||||
@ -719,9 +717,7 @@ static int setup_keys(void) {
|
|||||||
|
|
||||||
#ifdef HAVE_QRENCODE
|
#ifdef HAVE_QRENCODE
|
||||||
/* If this is not an UTF-8 system don't print any QR codes */
|
/* If this is not an UTF-8 system don't print any QR codes */
|
||||||
setlocale(LC_CTYPE, "");
|
if (is_locale_utf8()) {
|
||||||
|
|
||||||
if (streq_ptr(nl_langinfo(CODESET), "UTF-8")) {
|
|
||||||
fputs("\nTo transfer the verification key to your phone please scan the QR code below:\n\n", stderr);
|
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);
|
print_qr_code(stderr, seed, seed_size, n, arg_interval, hn, machine);
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,8 @@
|
|||||||
#include <sys/vfs.h>
|
#include <sys/vfs.h>
|
||||||
#include <linux/magic.h>
|
#include <linux/magic.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <langinfo.h>
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
#include "util.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;
|
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;
|
||||||
|
}
|
||||||
|
@ -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,
|
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
|
||||||
int (*compar) (const void *, const void *, void *),
|
int (*compar) (const void *, const void *, void *),
|
||||||
void *arg);
|
void *arg);
|
||||||
|
|
||||||
|
bool is_locale_utf8(void);
|
||||||
|
@ -29,8 +29,6 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <locale.h>
|
|
||||||
#include <langinfo.h>
|
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <linux/tiocl.h>
|
#include <linux/tiocl.h>
|
||||||
@ -48,19 +46,6 @@ static bool is_vconsole(int fd) {
|
|||||||
return ioctl(fd, TIOCLINUX, data) >= 0;
|
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) {
|
static int disable_utf8(int fd) {
|
||||||
int r = 0, k;
|
int r = 0, k;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user