Not pulling in 2 localization functions (One from libc, one from lib.c) reduces 
executable
size, also more portable on glibc systems because locale installation nonsense. 
No
typecasting to int's either.

15 bytes saved in bloatcheck. tests pass for everything with changes applied 
(Except the shell).

-   Oliver Webb <aquahobby...@proton.me>
From abc35e8f7a9cde4db5d23bc7a1a480540b173539 Mon Sep 17 00:00:00 2001
From: Oliver Webb <aquahobby...@proton.me>
Date: Tue, 9 Apr 2024 16:49:53 -0500
Subject: [PATCH] Nuke wcrtomb() since we have our own wctoutf8()

---
 lib/lib.c         | 2 +-
 toys/pending/sh.c | 2 +-
 toys/posix/echo.c | 2 +-
 toys/posix/find.c | 2 +-
 toys/posix/ls.c   | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index 6a4a77dd..79ae2a1d 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -426,7 +426,7 @@ char *strlower(char *s)
     // if we had a valid utf8 sequence, convert it to lower case, and can't
     // encode back to utf8, something is wrong with your libc. But just
     // in case somebody finds an exploit...
-    len = wcrtomb(new, c, 0);
+    len = wctoutf8(new, c);
     if (len < 1) error_exit("bad utf8 %x", (int)c);
     new += len;
 
diff --git a/toys/pending/sh.c b/toys/pending/sh.c
index 46390724..c0e605b0 100644
--- a/toys/pending/sh.c
+++ b/toys/pending/sh.c
@@ -1921,7 +1921,7 @@ static int expand_arg_nobrace(struct sh_arg *arg, char *str, unsigned flags,
     else if (cc == '$') {
       cc = *(ss = str+ii++);
       if (cc=='\'') {
-        for (s = str+ii; *s != '\''; oo += wcrtomb(new+oo, unescape2(&s, 0),0));
+        for (s = str+ii; *s != '\''; oo += wctoutf8(new+oo, unescape2(&s, 0)));
         ii = s-str+1;
 
         continue;
diff --git a/toys/posix/echo.c b/toys/posix/echo.c
index 702efee6..eb483154 100644
--- a/toys/posix/echo.c
+++ b/toys/posix/echo.c
@@ -53,7 +53,7 @@ void echo_main(void)
 
       if (*c == '\\' && c[1] == 'c') return;
       if ((u = unescape2(&c, 1))<128) putchar(u);
-      else printf("%.*s", (int)wcrtomb(out, u, 0), out);
+      else printf("%.*s", wctoutf8(out, u), out);
     }
   }
 
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 0fde9c9e..a713e068 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -607,7 +607,7 @@ static int do_find(struct dirtree *new)
 
             if (fmt[1] == 'c') break;
             if ((u = unescape2(&fmt, 0))<128) putchar(u);
-            else printf("%.*s", (int)wcrtomb(buf, u, 0), buf);
+            else printf("%.*s", wctoutf8(buf, u), buf);
             fmt--;
           } else if (*fmt != '%') putchar(*fmt);
           else if (*++fmt == '%') putchar('%');
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 3addd451..784a39de 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -81,7 +81,7 @@ static int crunch_qb(FILE *out, int cols, int wc)
   else {
     if (wc<256) *buf = wc;
     // scrute the inscrutable, eff the ineffable, print the unprintable
-    else if ((len = wcrtomb(buf, wc, 0) ) == -1) len = 1;
+    else if ((len = wctoutf8(buf, wc)) == -1) len = 1;
     if (FLAG(b)) {
       char *to = buf, *from = buf+24;
       int i, j;
-- 
2.44.0

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to