As a different approach to ls, I wrote this a while ago.  This uses the
wchar_t functions, but only in putname().



On 2015 Oct 23 (Fri) at 08:42:52 -0400 (-0400), Ted Unangst wrote:
:So, third diff to ponder as we evaluate this approach. This one also uses a
:u8len() function to help get the column widths correct.
:
:(Still not dealing with combining or otherwise not 1 width glyphs.)
:

Index: ls.c
===================================================================
RCS file: /cvs/src/bin/ls/ls.c,v
retrieving revision 1.43
diff -u -p -u -p -r1.43 ls.c
--- ls.c        9 Oct 2015 01:37:06 -0000       1.43
+++ ls.c        19 Oct 2015 10:03:04 -0000
@@ -50,6 +50,8 @@
 #include <limits.h>
 #include <util.h>
 
+#include <locale.h>
+
 #include "ls.h"
 #include "extern.h"
 
@@ -102,6 +104,8 @@ ls_main(int argc, char *argv[])
        int ch, fts_options, notused;
        int kflag = 0, width = 0;
        char *p;
+
+       setlocale(LC_CTYPE, "");
 
        /* Terminal defaults to -Cq, non-terminal defaults to -1. */
        if (isatty(STDOUT_FILENO)) {
Index: util.c
===================================================================
RCS file: /cvs/src/bin/ls/util.c,v
retrieving revision 1.16
diff -u -p -u -p -r1.16 util.c
--- util.c      21 Nov 2013 15:54:45 -0000      1.16
+++ util.c      19 Oct 2015 10:03:04 -0000
@@ -42,17 +42,32 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <wchar.h>
+#include <wctype.h>
+
 #include "ls.h"
 #include "extern.h"
 
 int
 putname(char *name)
 {
-       int len;
+       wchar_t wname;
+       mbstate_t state;
+       size_t n = strlen(name);
+       int len, wlen = 0;
+
+       /* initialize the conversion state */
+       memset(&state, 0, sizeof(state));
+
+       while ((len = mbrtowc(&wname, name, n, &state)) > 0) {
+               wprintf(L"%lc",
+                   (!iswprint((wchar_t)wname) && f_nonprint) ? '?' : wname);
+               name += len;
+               if (len == 1)
+                       wlen++;
+       }
 
-       for (len = 0; *name; len++, name++)
-               putchar((!isprint((unsigned char)*name) && f_nonprint) ? '?' : 
*name);
-       return len;
+       return (wlen);
 }
 
 void



-- 
"Do not meddle in the affairs of wizards, for you are crunchy and good
with ketchup."

Reply via email to