summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/lineedit.c2
-rw-r--r--libbb/progress.c2
-rw-r--r--libbb/unicode.c46
3 files changed, 29 insertions, 21 deletions
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index c73e6b7..b73f1d6 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -1763,7 +1763,7 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li
return len;
}
- check_unicode_in_env();
+ init_unicode();
// FIXME: audit & improve this
if (maxsize > MAX_LINELEN)
diff --git a/libbb/progress.c b/libbb/progress.c
index f6f2692..3a245ae 100644
--- a/libbb/progress.c
+++ b/libbb/progress.c
@@ -78,7 +78,7 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
}
#if ENABLE_FEATURE_ASSUME_UNICODE
- check_unicode_in_env();
+ init_unicode();
/* libbb candidate? */
{
wchar_t wbuf21[21];
diff --git a/libbb/unicode.c b/libbb/unicode.c
index 544528a..9d316df 100644
--- a/libbb/unicode.c
+++ b/libbb/unicode.c
@@ -7,7 +7,9 @@
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
*/
#include "libbb.h"
-# include "unicode.h"
+#include "unicode.h"
+
+uint8_t unicode_status;
size_t FAST_FUNC bb_mbstrlen(const char *string)
{
@@ -17,32 +19,38 @@ size_t FAST_FUNC bb_mbstrlen(const char *string)
return width;
}
-#if !ENABLE_LOCALE_SUPPORT
+#if ENABLE_LOCALE_SUPPORT
+
+/* Unicode support using libc */
+
+void FAST_FUNC init_unicode(void)
+{
+ /* In unicode, this is a one character string */
+ static const char unicode_0x394[] = { 0xce, 0x94, 0 };
+
+ if (unicode_status != UNICODE_UNKNOWN)
+ return;
+
+ unicode_status = bb_mbstrlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF;
+}
+
+#else
/* Crude "locale support" which knows only C and Unicode locales */
-/* unicode_is_enabled:
- * 0: not known yet,
- * 1: not unicode (IOW: assuming one char == one byte)
- * 2: unicode
- */
-# if !ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
-# define unicode_is_enabled 2
-# else
-static smallint unicode_is_enabled;
-void FAST_FUNC check_unicode_in_env(void)
+# if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
+void FAST_FUNC init_unicode(void)
{
char *lang;
- if (unicode_is_enabled)
+ if (unicode_status != UNICODE_UNKNOWN)
return;
- unicode_is_enabled = 1;
+ unicode_status = UNICODE_OFF;
lang = getenv("LANG");
if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF")))
return;
-
- unicode_is_enabled = 2;
+ unicode_status = UNICODE_ON;
}
# endif
@@ -85,7 +93,7 @@ static size_t wcrtomb_internal(char *s, wchar_t wc)
size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
{
- if (unicode_is_enabled != 2) {
+ if (unicode_status != UNICODE_ON) {
*s = wc;
return 1;
}
@@ -97,7 +105,7 @@ size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
{
size_t org_n = n;
- if (unicode_is_enabled != 2) {
+ if (unicode_status != UNICODE_ON) {
while (n) {
wchar_t c = *src++;
*dest++ = c;
@@ -137,7 +145,7 @@ size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
{
size_t org_n = n;
- if (unicode_is_enabled != 2) {
+ if (unicode_status != UNICODE_ON) {
while (n) {
unsigned char c = *src++;