newlib/libc/ctype: Bypass _jp2uc/_uc2jp if _MB_CAPABLE is not set

Message ID 20260119170323.83493-1-andrew@andrewoates.com
State New
Headers
Series newlib/libc/ctype: Bypass _jp2uc/_uc2jp if _MB_CAPABLE is not set |

Commit Message

Andrew Oates Jan. 19, 2026, 5:03 p.m. UTC
  From: Andrew Oates <andrew@andrewoates.com>

Currently, if an application uses towctrans or towctrans_l with a newlib
compiled without _MB_CAPABLE, it will fail to link due to _jp2uc/_uc2jp
being undefined.  This extends the cygwin behavior to all
non-_MB_CAPABLE builds.
---
 newlib/libc/ctype/local.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Corinna Vinschen Jan. 20, 2026, 9:40 a.m. UTC | #1
Hi Andrew,

On Jan 19 12:03, andrew@andrewoates.com wrote:
> From: Andrew Oates <andrew@andrewoates.com>
> 
> Currently, if an application uses towctrans or towctrans_l with a newlib
> compiled without _MB_CAPABLE, it will fail to link due to _jp2uc/_uc2jp
> being undefined.  This extends the cygwin behavior to all
> non-_MB_CAPABLE builds.

I don't think this is the right thing to do.  Check with other wide-char
functions calling _jp2uc_l, for instance, iswprint_l().  The functions
fall back to the equivalent functions for singlebyte charsets.

Since there's no `toctrans' function, we have to call toupper/tolower
from here.  We can also exlude building the touupper/toulower functions
in the same file, which reduces the footprint of the file for not
_MB_CAPABLE systems.

So I'd propose the following patch instead:


From cb7f1ad1061c8cc55d2ff4b659bbff3395809d90 Mon Sep 17 00:00:00 2001
From: Corinna Vinschen <corinna@vinschen.de>
Date: Tue, 20 Jan 2026 10:35:20 +0100
Subject: [PATCH] towctrans_l: handle not _MB_CAPABLE targets

towctrans_l neglected to special case systems for which newlib has
been built without _MB_CAPABLE.  Add that code equivalent to other
wide-char functions, calling the singlebyte functions toupper/tolower.
---
 newlib/libc/ctype/towctrans_l.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/newlib/libc/ctype/towctrans_l.c b/newlib/libc/ctype/towctrans_l.c
index b829266a49cd..e94d6f492c8c 100644
--- a/newlib/libc/ctype/towctrans_l.c
+++ b/newlib/libc/ctype/towctrans_l.c
@@ -1,10 +1,13 @@
 /* Modified (m) 2017 Thomas Wolff: revise Unicode and locale/wchar handling */
 #include <_ansi.h>
+#include <ctype.h>
 #include <wctype.h>
 #include <stdint.h>
 //#include <errno.h>
 #include "local.h"
 
+#ifdef _MB_CAPABLE
+
 /*
    struct caseconv_entry describes the case conversion behaviour
    of a range of Unicode characters.
@@ -139,9 +142,12 @@ touupper (wint_t c)
   return c;
 }
 
+#endif /* _MB_CAPABLE */
+
 wint_t
 towctrans_l (wint_t c, wctrans_t w, struct __locale_t *locale)
 {
+#ifdef _MB_CAPABLE
   wint_t u = _jp2uc_l (c, locale);
   wint_t res;
   if (w == WCT_TOLOWER)
@@ -159,4 +165,14 @@ towctrans_l (wint_t c, wctrans_t w, struct __locale_t *locale)
     return _uc2jp_l (res, locale);
   else
     return c;
+#else
+  if (c < (wint_t)0x100)
+    {
+      if (w == WCT_TOLOWER)
+	return tolower (c);
+      if (w == WCT_TOUPPER)
+	return toupper (c);
+    }
+  return c;
+#endif /* _MB_CAPABLE */
 }
  
Corinna Vinschen Jan. 23, 2026, 9:49 a.m. UTC | #2
On Jan 20 10:40, Corinna Vinschen wrote:
> Hi Andrew,
> 
> On Jan 19 12:03, andrew@andrewoates.com wrote:
> > From: Andrew Oates <andrew@andrewoates.com>
> > 
> > Currently, if an application uses towctrans or towctrans_l with a newlib
> > compiled without _MB_CAPABLE, it will fail to link due to _jp2uc/_uc2jp
> > being undefined.  This extends the cygwin behavior to all
> > non-_MB_CAPABLE builds.
> 
> I don't think this is the right thing to do.  Check with other wide-char
> functions calling _jp2uc_l, for instance, iswprint_l().  The functions
> fall back to the equivalent functions for singlebyte charsets.
> 
> Since there's no `toctrans' function, we have to call toupper/tolower
> from here.  We can also exlude building the touupper/toulower functions
> in the same file, which reduces the footprint of the file for not
> _MB_CAPABLE systems.
> 
> So I'd propose the following patch instead:

Pushed.


Corinna
  

Patch

diff --git a/newlib/libc/ctype/local.h b/newlib/libc/ctype/local.h
index 5c293c83d..1acdfd277 100644
--- a/newlib/libc/ctype/local.h
+++ b/newlib/libc/ctype/local.h
@@ -32,7 +32,7 @@ 
    * for towupper and towlower, the result must be back-transformed
      into the respective locale encoding; currently NOT IMPLEMENTED
 */
-#ifdef __CYGWIN__
+#if defined(__CYGWIN__) || !defined(_MB_CAPABLE)
 /* Under Cygwin, wchar_t (or its extension wint_t) is Unicode */
 #define _jp2uc(c) (c)
 #define _jp2uc_l(c, l) (c)