Hi.
Ls command produce file list formatted incorrectly when filenames
contain unicode characters: columns are shifted to the left when
multibyte chars are present in file names. There is the patch that
solve this issue. New option in configuration was added to support
widechars as well... wchar.h and ctype.h are needed to be included in
libbb.h (mayby) but they were placed here since it's used only in
ls.c.
--
Thanks
Volodymyr
Index: coreutils/ls.c
===================================================================
--- coreutils/ls.c (revision 22297)
+++ coreutils/ls.c (working copy)
@@ -31,6 +31,11 @@
#include "libbb.h"
+#if ENABLE_FEATURE_LS_WIDECHARS
+#include <wchar.h>
+#include <ctype.h>
+#endif
+
/* This is a NOEXEC applet. Be very careful! */
@@ -141,7 +146,10 @@
static struct dnode **list_dir(const char *);
static struct dnode **dnalloc(int);
-static int list_single(struct dnode *);
+static int list_single(const struct dnode *);
+#if ENABLE_FEATURE_LS_WIDECHARS
+int mbstrlen(const char *string, size_t nbytes);
+#endif
static unsigned all_fmt;
@@ -395,7 +403,11 @@
} else {
/* find the longest file name, use that as the column width */
for (i = 0; i < nfiles; i++) {
+#if ENABLE_FEATURE_LS_WIDECHARS
+ int len = mbstrlen(dn[i]->name, strlen(dn[i]->name));
+#else
int len = strlen(dn[i]->name);
+#endif
if (column_width < len)
column_width = len;
}
@@ -544,7 +556,7 @@
static time_t current_time_t;
#endif
-static int list_single(struct dnode *dn)
+static int list_single(const struct dnode *dn)
{
int i, column = 0;
@@ -651,16 +663,23 @@
break;
#endif
case LIST_FILENAME:
- errno = 0;
+ {
+ errno = 0;
#if ENABLE_FEATURE_LS_COLOR
- if (show_color && !lstat(dn->fullname, &info)) {
- printf("\033[%d;%dm", bgcolor(info.st_mode),
+ if (show_color && !lstat(dn->fullname, &info)) {
+ printf("\033[%d;%dm", bgcolor(info.st_mode),
fgcolor(info.st_mode));
- }
+ }
#endif
- column += printf("%s", dn->name);
- if (show_color) {
- printf("\033[0m");
+#if ENABLE_FEATURE_LS_WIDECHARS
+ printf("%s", dn->name);
+ column += mbstrlen(dn->name, strlen(dn->name));
+#else
+ column += printf("%s", dn->name);
+#endif
+ if (show_color) {
+ printf("\033[0m");
+ }
}
break;
case LIST_SYMLINK:
@@ -701,6 +720,19 @@
return column;
}
+
+#if ENABLE_FEATURE_LS_WIDECHARS
+int mbstrlen(const char *string, size_t nbytes)
+{
+ wchar_t buf[nbytes];
+ int width = mbstowcs(buf, string, nbytes);
+ if (width == -1)
+ return nbytes;
+
+ return width;
+}
+#endif
+
/* "[-]Cadil1", POSIX mandated options, busybox always supports */
/* "[-]gnsx", POSIX non-mandated options, busybox always supports */
/* "[-]Ak" GNU options, busybox always supports */
Index: coreutils/Config.in
===================================================================
--- coreutils/Config.in (revision 22297)
+++ coreutils/Config.in (working copy)
@@ -340,6 +340,13 @@
help
Allow ls to sort file names alphabetically.
+config FEATURE_LS_WIDECHARS
+ bool "Better handling of unicode chars"
+ default y
+ depends on LS
+ help
+ Allow ls to produce better column formatting for unicode filenames.
+
config FEATURE_LS_TIMESTAMPS
bool "Show file timestamps"
default y
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox