} while (0)
-#if ENABLE_FEATURE_ASSUME_UNICODE
-/* libbb candidate */
-static size_t mbstrlen(const char *string)
-{
- size_t width = mbstowcs(NULL, string, INT_MAX);
- if (width == (size_t)-1L)
- return strlen(string);
- return width;
-}
-#else
-#define mbstrlen(string) strlen(string)
-#endif
-
-
static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
{
struct stat dstat;
} else {
/* find the longest file name, use that as the column width */
for (i = 0; i < nfiles; i++) {
- int len = mbstrlen(dn[i]->name);
+ int len = bb_mbstrlen(dn[i]->name);
if (column_width < len)
column_width = len;
}
{
if (option_mask32 & OPT_Q) {
#if ENABLE_FEATURE_ASSUME_UNICODE
- int len = 2 + mbstrlen(name);
+ int len = 2 + bb_mbstrlen(name);
#else
int len = 2;
#endif
/* No -Q: */
#if ENABLE_FEATURE_ASSUME_UNICODE
fputs(name, stdout);
- return mbstrlen(name);
+ return bb_mbstrlen(name);
#else
return printf("%s", name);
#endif
#if !ENABLE_FEATURE_ASSUME_UNICODE
+# define bb_mbstrlen(string) strlen(string)
# define check_unicode_in_env() ((void)0)
#else
+size_t bb_mbstrlen(const char *string) FAST_FUNC;
+
# if ENABLE_LOCALE_SUPPORT
# include <wchar.h>
# else
+/* Crude "locale support" which knows only C and Unicode locales */
+
# if !ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
# define check_unicode_in_env() ((void)0)
# else
int iswalnum(wint_t wc) FAST_FUNC;
int iswpunct(wint_t wc) FAST_FUNC;
-# endif
+# endif /* !LOCALE_SUPPORT */
-#endif
+#endif /* FEATURE_ASSUME_UNICODE */
#endif
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
*/
#include "libbb.h"
+# include "unicode.h"
-/* if LOCALE_SUPPORT, libc locale stuff takes care of it, else: */
+size_t FAST_FUNC bb_mbstrlen(const char *string)
+{
+ size_t width = mbstowcs(NULL, string, INT_MAX);
+ if (width == (size_t)-1L)
+ return strlen(string);
+ return width;
+}
#if !ENABLE_LOCALE_SUPPORT
-#include "unicode.h"
-/* 0: not known yet,
+/* 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
*/
static size_t wcrtomb_internal(char *s, wchar_t wc)
{
+ int n;
uint32_t v = wc;
if (v <= 0x7f) {
return 1;
}
- /* 80-7FF -> 110yyyxx 10xxxxxx */
- if (v <= 0x7ff) {
- s[1] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[0] = v | 0xc0;
- return 2;
- }
+ /* RFC 3629 says that Unicode ends at 10FFFF,
+ * but we cover entire 32 bits */
- /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
- if (v <= 0xffff) {
- s[2] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[1] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[0] = v | 0xe0;
- return 3;
+ n = 2;
+ /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
+ if (v >= 0x4000000) {
+ s[5] = (wc & 0x3f) | 0x80;
+ wc = (uint32_t)wc >> 6; /* ensuring that high bits are 0 */
+ n++;
+ }
+ /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
+ if (v >= 0x200000) {
+ s[4] = (wc & 0x3f) | 0x80;
+ wc >>= 6;
+ n++;
}
-
- /* RFC 3629 says that Unicode ends at 10FFFF */
-
/* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
- if (v <= 0x1fffff) {
- s[3] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[2] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[1] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[0] = v | 0xf0;
- return 4;
+ if (v >= 0x10000) {
+ s[3] = (wc & 0x3f) | 0x80;
+ wc >>= 6;
+ n++;
}
-
- /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
- if (v <= 0x3ffffff) {
- s[4] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[3] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[2] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[1] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[0] = v | 0xf8;
- return 5;
+ /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
+ if (v >= 0x800) {
+ s[2] = (wc & 0x3f) | 0x80;
+ wc >>= 6;
+ n++;
}
-
- /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
- s[5] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[4] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[3] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[2] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[1] = (v & 0x3f) | 0x80;
- v >>= 6;
- s[0] = v | 0xfc;
- return 6;
+ s[1] = (wc & 0x3f) | 0x80;
+ wc >>= 6;
+ s[0] = wc | (uint8_t)(0x3f00 >> n);
+ return n;
}
size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
if (unicode_is_enabled != 2) {
while (n) {
unsigned char c = *src++;
- *dest++ = c;
+
+ if (dest)
+ *dest++ = c;
if (c == 0)
break;
n--;
unsigned c = (unsigned char) *src++;
if (c <= 0x7f) {
- *dest++ = c;
+ if (dest)
+ *dest++ = c;
if (c == '\0')
break;
n--;
//or maybe: c = 0xfffd; /* replacement character */
}
- *dest++ = c;
+ if (dest)
+ *dest++ = c;
n--;
}