转载:http://blog.csdn.net/wavemcu/article/details/7202908

***************************************************************************************************************************
作者:EasyWave                                                                                 时间:2012.01.15

类别:linux驱动开发                                                                           声明:转载,请保留链接

***************************************************************************************************************************

在嵌入式linux系统中,busybox是最常见的用来构建文件系统的。可是从busybox1.17.0以上之后,对ls命令不做修改是无法显
示中文的。就算是内核设置了支持中文的话,在shell下用ls命令也是无法显示中文的,这是因为busybox1.17.0以后版本对中文的支持进行了
限制。现在就来讲讲如何修改让busybox1.17.0以上版本支持中文,要想让busybox1.17.0以上支持中文,需要修改两个文
件:printable_string.c以及unicode.c
。下面来分析,为什么ls命令无法显示中文。请看printable_string.c未修改过的代码:

const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str)
{
static char *saved[];
static unsigned cur_saved; /* = 0 */ char *dst;
const char *s; s = str;
while () {
unsigned char c = *s;
if (c == '\0') {
/* 99+% of inputs do not need conversion */
if (stats) {
stats->byte_count = (s - str);
stats->unicode_count = (s - str);
stats->unicode_width = (s - str);
}
return str;
}
if (c < ' ')
break;
if (c >= 0x7f)
break;
s++;
} #if ENABLE_UNICODE_SUPPORT
dst = unicode_conv_to_printable(stats, str);
#else
{
char *d = dst = xstrdup(str);
while () {
unsigned char c = *d;
if (c == '\0')
break;
if (c < ' ' || c >= 0x7f)
*d = '?';
d++;
}
if (stats) {
stats->byte_count = (d - dst);
stats->unicode_count = (d - dst);
stats->unicode_width = (d - dst);
}
}
#endif free(saved[cur_saved]);
saved[cur_saved] = dst;
cur_saved = (cur_saved + ) & (ARRAY_SIZE(saved)-); return dst;
}

从上面代码23和24行以及37和38行可以看出:大于0x7F的字符直接被break掉,或者直接被“?”代替了。所以就算是linux内核设置了支持中文,也是无法显示出来的,被“?”代替了。修改红色加粗的代码如下:

const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str)
{
static char *saved[];
static unsigned cur_saved; /* = 0 */ char *dst;
const char *s; s = str;
while () {
unsigned char c = *s;
if (c == '\0') {
/* 99+% of inputs do not need conversion */
if (stats) {
stats->byte_count = (s - str);
stats->unicode_count = (s - str);
stats->unicode_width = (s - str);
}
return str;
}
if (c < ' ')
break;
/*
if (c >= 0x7f)
break;
*/
s++;
} #if ENABLE_UNICODE_SUPPORT
dst = unicode_conv_to_printable(stats, str);
#else
{
char *d = dst = xstrdup(str);
while () {
unsigned char c = *d;
if (c == '\0')
break;
if (c < ' ' /*|| c >= 0x7f */)
*d = '?';
d++;
}
if (stats) {
stats->byte_count = (d - dst);
stats->unicode_count = (d - dst);
stats->unicode_width = (d - dst);
}
}
#endif free(saved[cur_saved]);
saved[cur_saved] = dst;
cur_saved = (cur_saved + ) & (ARRAY_SIZE(saved)-); return dst;
}

经过以上的修改之后,同时busybox1.17.0配置的时候没有选中[] Support Unicode的话,那么采用ls命令是可以看到中文的,这个我自己已经亲自测试过的。可是还有一种情况:busybox1.17.0在配置的时候选中了:[*] Support Unicode,见下:

在配置里,有Support Unicode选上的:
Busybox Settings->General Configuration->
│ │[ ] Enable locale support (system needs locale for this to work) │ │
│ │[*] Support Unicode │ │
│ │[*] Support for --long-options │ │

那么这样还需要修改一个文件,这个文件就是:unicode.c。如果不修改这个文件,ls命令也是无法显示出中文的。见下未修改的代码:

static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
{
char *dst;
unsigned dst_len;
unsigned uni_count;
unsigned uni_width; if (unicode_status != UNICODE_ON) {
char *d;
if (flags & UNI_FLAG_PAD) {
d = dst = xmalloc(width + );
while ((int)--width >= ) {
unsigned char c = *src;
if (c == '\0') {
do
*d++ = ' ';
while ((int)--width >= );
break;
}
*d++ = (c >= ' ' && c < 0x7f) ? c : '?';
src++;
}
*d = '\0';
} else {
d = dst = xstrndup(src, width);
while (*d) {
unsigned char c = *d;
if (c < ' ' || c >= 0x7f)
*d = '?';
d++;
}
}
if (stats) {
stats->byte_count = (d - dst);
stats->unicode_count = (d - dst);
stats->unicode_width = (d - dst);
}
return dst;
} dst = NULL;
uni_count = uni_width = ;
dst_len = ;
while () {
int w;
wchar_t wc; #if ENABLE_UNICODE_USING_LOCALE
{
mbstate_t mbst = { };
ssize_t rc = mbsrtowcs(&wc, &src, , &mbst);
/* If invalid sequence is seen: -1 is returned,
* src points to the invalid sequence, errno = EILSEQ.
* Else number of wchars (excluding terminating L'\0')
* written to dest is returned.
* If len (here: 1) non-L'\0' wchars stored at dest,
* src points to the next char to be converted.
* If string is completely converted: src = NULL.
*/
if (rc == ) /* end-of-string */
break;
if (rc < ) { /* error */
src++;
goto subst;
}
if (!iswprint(wc))
goto subst;
}
#else
src = mbstowc_internal(&wc, src);
/* src is advanced to next mb char
* wc == ERROR_WCHAR: invalid sequence is seen
* else: wc is set
*/
if (wc == ERROR_WCHAR) /* error */
goto subst;
if (wc == ) /* end-of-string */
break;
#endif
if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
goto subst;
w = wcwidth(wc);
if ((ENABLE_UNICODE_COMBINING_WCHARS && w < ) /* non-printable wchar */
|| (!ENABLE_UNICODE_COMBINING_WCHARS && w <= )
|| (!ENABLE_UNICODE_WIDE_WCHARS && w > )
) {
subst:
wc = CONFIG_SUBST_WCHAR;
w = ;
}
width -= w;
/* Note: if width == 0, we still may add more chars,
* they may be zero-width or combining ones */
if ((int)width < ) {
/* can't add this wc, string would become longer than width */
width += w;
break;
} uni_count++;
uni_width += w;
dst = xrealloc(dst, dst_len + MB_CUR_MAX);
#if ENABLE_UNICODE_USING_LOCALE
{
mbstate_t mbst = { };
dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
}
#else
dst_len += wcrtomb_internal(&dst[dst_len], wc);
#endif
} /* Pad to remaining width */
if (flags & UNI_FLAG_PAD) {
dst = xrealloc(dst, dst_len + width + );
uni_count += width;
uni_width += width;
while ((int)--width >= ) {
dst[dst_len++] = ' ';
}
}
dst[dst_len] = '\0';
if (stats) {
stats->byte_count = dst_len;
stats->unicode_count = uni_count;
stats->unicode_width = uni_width;
} return dst;
}

见上面20行和28行,需要修改一下,修改后的代码见下:

static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
{
char *dst;
unsigned dst_len;
unsigned uni_count;
unsigned uni_width; if (unicode_status != UNICODE_ON) {
char *d;
if (flags & UNI_FLAG_PAD) {
d = dst = xmalloc(width + );
while ((int)--width >= ) {
unsigned char c = *src;
if (c == '\0') {
do
*d++ = ' ';
while ((int)--width >= );
break;
}
*d++ = (c >= ' '/* && c < 0x7f */) ? c : '?';
src++;
}
*d = '\0';
} else {
d = dst = xstrndup(src, width);
while (*d) {
unsigned char c = *d;
if (c < ' '/* || c >= 0x7f */)
*d = '?';
d++;
}
}
if (stats) {
stats->byte_count = (d - dst);
stats->unicode_count = (d - dst);
stats->unicode_width = (d - dst);
}
return dst;
} dst = NULL;
uni_count = uni_width = ;
dst_len = ;
while () {
int w;
wchar_t wc; #if ENABLE_UNICODE_USING_LOCALE
{
mbstate_t mbst = { };
ssize_t rc = mbsrtowcs(&wc, &src, , &mbst);
/* If invalid sequence is seen: -1 is returned,
* src points to the invalid sequence, errno = EILSEQ.
* Else number of wchars (excluding terminating L'\0')
* written to dest is returned.
* If len (here: 1) non-L'\0' wchars stored at dest,
* src points to the next char to be converted.
* If string is completely converted: src = NULL.
*/
if (rc == ) /* end-of-string */
break;
if (rc < ) { /* error */
src++;
goto subst;
}
if (!iswprint(wc))
goto subst;
}
#else
src = mbstowc_internal(&wc, src);
/* src is advanced to next mb char
* wc == ERROR_WCHAR: invalid sequence is seen
* else: wc is set
*/
if (wc == ERROR_WCHAR) /* error */
goto subst;
if (wc == ) /* end-of-string */
break;
#endif
if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
goto subst;
w = wcwidth(wc);
if ((ENABLE_UNICODE_COMBINING_WCHARS && w < ) /* non-printable wchar */
|| (!ENABLE_UNICODE_COMBINING_WCHARS && w <= )
|| (!ENABLE_UNICODE_WIDE_WCHARS && w > )
) {
subst:
wc = CONFIG_SUBST_WCHAR;
w = ;
}
width -= w;
/* Note: if width == 0, we still may add more chars,
* they may be zero-width or combining ones */
if ((int)width < ) {
/* can't add this wc, string would become longer than width */
width += w;
break;
} uni_count++;
uni_width += w;
dst = xrealloc(dst, dst_len + MB_CUR_MAX);
#if ENABLE_UNICODE_USING_LOCALE
{
mbstate_t mbst = { };
dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
}
#else
dst_len += wcrtomb_internal(&dst[dst_len], wc);
#endif
} /* Pad to remaining width */
if (flags & UNI_FLAG_PAD) {
dst = xrealloc(dst, dst_len + width + );
uni_count += width;
uni_width += width;
while ((int)--width >= ) {
dst[dst_len++] = ' ';
}
}
dst[dst_len] = '\0';
if (stats) {
stats->byte_count = dst_len;
stats->unicode_count = uni_count;
stats->unicode_width = uni_width;
} return dst;
}

经过以上修改之后,就算配置支持Unicode,ls命令也是可以支持中文的。同时也可以进入中文目录可以文件夹。

Busybox支持中文的解决办法的更多相关文章

  1. mac中matplotlib不支持中文的解决办法

    参考:https://blog.csdn.net/kaizei_pao/article/details/80795377 首先查看matplotlib已加载的字体: import matplotlib ...

  2. python---不支持中文注释解决办法

    很神奇的一件事儿,pycharm不支持中文注释,具体解决办法: #-*- coding: utf- -*- 具体使用:

  3. JqueryQrcode生成二维码不支持中文的解决办法

    JqueryQrcode.js有一个小小的缺点,就是默认不支持中文. 这跟js的机制有关系,jquery-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的, 而这个方法默认会 ...

  4. 使用iTextSharp 解析html生成pdf,xmlworker不支持中文的解决办法

    http://www.micmiu.com/opensource/expdoc/itext-xml-worker-cn/ 参考上面的文章,虽然是java的,但是和.net是对应的. 下载   html ...

  5. IDLE3.6.3 Mac版不支持中文输入解决办法

    最近安装了IDLE 3.6.3版本 但是在IDLE中要输入中文注释时发现虽然输入法切换到了中文,但输入的还是英文.然后我在IDLE外试了下,输入中文没问题,于是就确认应该是IDLE的问题. 网上查询到 ...

  6. koala不支持中文的解决办法(问题出现在使用中文字体时报错)

    C:\Program Files\Koala\rubygems\gems\sass-3.4.9\lib\sass 这是我的koala的安装路径,在sass文件夹下打开engine.rb(文本文档打开即 ...

  7. Ubantu里面的Sublime Text3不支持中文的解决办法

    参考的大佬链接:https://github.com/lyfeyaj/sublime-text-imfix 更新然后将系统升级到最新版本,在linux终端输入 sudo apt-get update ...

  8. [Linux] - CentOS中文乱码解决办法

    CentOS 7 终端中文乱码解决办法: 1.使用vim编辑locale.config文件: vim /etc/locale.conf 2.将LANG="en_US.UTF-8"修 ...

  9. Oracle导入中文乱码解决办法

    Oracle导入中文乱码解决办法 一.确保各个客户端字符集的编码同服务器字符集编码一致 1-       确定sqlplus字符集编码,如果是windows设置环境变量. 2-       确保Sec ...

随机推荐

  1. 【多线程同步案例】Race Condition引起的性能问题

    Race Condition(也叫做资源竞争),是多线程编程中比较头疼的问题.特别是Java多线程模型当中,经常会因为多个线程同时访问相同的共享数据,而造成数据的不一致性.为了解决这个问题,通常来说需 ...

  2. maven 部署到tomcat 的 resource问题

    1.maven resource结构 如图,我将resoures下建立四个子文件夹,base存放的是不随环境变化的配置项,而其他三个均是对应环境的配置文件. 2.问题 我执行maven命令是没有的,但 ...

  3. labview中的文件格式

     

  4. utf8 和 UTF-8 的区别

    只有在MySQL中可以使用“utf-8”的别名“utf8”,但是在其他地方一律使用大写“UTF-8”.

  5. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. 【转】删除已经存在的 TFS Workspace

    删除已经存在的 TFS Workspace 分类: TFS2010-03-03 16:59 1239人阅读 评论(2) 收藏 举报 serverpathcommandcachefilegoogle 工 ...

  7. junit4学习(Annotation)

    在一个测试类中,所有被@Test注解修饰的public,void方法都是testcase,可以被JUNIT执行. @Retention(value=RUNTIME) @Target(value=MET ...

  8. C++的优秀特性2:inline 函数

    (转载请注明原创于潘多拉盒子) Inline函数是C++的一个很小的特性,在不计较效率的情况下,这个特性似乎可有可无.然而,C++天生是为最为广泛的应用场景设计的,因此,总会有关于效率的问题.其实,除 ...

  9. [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果

    大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 在<仙剑奇侠传>.<古剑奇谭>等游戏中,常常须要玩家在一个3D场景中 ...

  10. CSS中filter滤镜学习笔记

    1.CSS静态滤镜样式 (filter)(只有IE4.0以上支持)  CSS静态滤镜样式的使用方法:{ filter : filtername( parameters1, parameters2, . ...