commit 7033385eda09474ab9a8e21ae811d916e3856ce6
parent b454c47b530863e732c6100728d35e8e663d3a5d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 10 Mar 2025 13:57:08 +0100
libc: Add get/set for mbstate_t
The definition of mbstate_t depends of the system, and for that reason
we cannot access directly the content of the data type. To solve this
problem different system functions were added making the generic code
portable between the different implementations.
Diffstat:
14 files changed, 73 insertions(+), 2 deletions(-)
diff --git a/src/libc/arch/bsd/Makefile b/src/libc/arch/bsd/Makefile
@@ -4,6 +4,8 @@ include $(PROJECTDIR)/scripts/rules.mk
include ../../rules.mk
OBJS=\
- _waitpid.$O\
+ _mbsget.$O\
+ _mbsset.$O\
+ _waitpid.$O\
all: $(OBJS)
diff --git a/src/libc/arch/bsd/_mbsget.c b/src/libc/arch/bsd/_mbsget.c
@@ -0,0 +1,9 @@
+#include <wchar.h>
+
+#include "../../libc.h"
+
+int
+_mbsget(mbstate_t *ps)
+{
+ return ps->__mbstate8[0];
+}
diff --git a/src/libc/arch/bsd/_mbsset.c b/src/libc/arch/bsd/_mbsset.c
@@ -0,0 +1,9 @@
+#include <wchar.h>
+
+#include "../../libc.h"
+
+int
+_mbsset(mbstate_t *ps, int ch)
+{
+ return ps->__mbstate8[0] = ch;
+}
diff --git a/src/libc/arch/darwin/Makefile b/src/libc/arch/darwin/Makefile
@@ -5,5 +5,7 @@ include ../../rules.mk
OBJS=\
_getheap.$O\
+ _mbsget.$O\
+ _mbsset.$O\
all: $(OBJS)
diff --git a/src/libc/arch/darwin/_mbsget.c b/src/libc/arch/darwin/_mbsget.c
@@ -0,0 +1,9 @@
+#include <wchar.h>
+
+#include "../../libc.h"
+
+int
+_mbsget(mbstate_t *ps)
+{
+ return ps->state[0];
+}
diff --git a/src/libc/arch/darwin/_mbsset.c b/src/libc/arch/darwin/_mbsset.c
@@ -0,0 +1,10 @@
+#include <wchar.h>
+
+#include "../../libc.h"
+
+int
+_mbsset(mbstate_t *ps, int ch)
+{
+ ps-count = 1;
+ return ps->state[0] = ch;
+}
diff --git a/src/libc/arch/linux/Makefile b/src/libc/arch/linux/Makefile
@@ -6,6 +6,8 @@ include ../../rules.mk
OBJS=\
_brk.$O\
_getheap.$O\
+ _mbsget.$O\
+ _mbsset.$O\
_sigaction.$O\
_waitpid.$O\
diff --git a/src/libc/arch/linux/_mbsget.c b/src/libc/arch/linux/_mbsget.c
@@ -0,0 +1,9 @@
+#include <wchar.h>
+
+#include "../../libc.h"
+
+int
+_mbsget(mbstate_t *ps)
+{
+ return ps->__opaque1;
+}
diff --git a/src/libc/arch/linux/_mbsset.c b/src/libc/arch/linux/_mbsset.c
@@ -0,0 +1,9 @@
+#include <wchar.h>
+
+#include "../../libc.h"
+
+int
+_mbsset(mbstate_t *ps, int ch)
+{
+ return ps->__opaque1 = ch;
+}
diff --git a/src/libc/libc.h b/src/libc/libc.h
@@ -61,6 +61,8 @@ extern void (*_atexithdl)(void);
#ifdef _WCHAR_H
extern int _validutf8(wchar_t, int *);
+extern int _mbsset(mbstate_t *, int);
+extern int _mbsget(mbstate_t *);
#ifdef _STDIO_H
extern wint_t _fputwc(wchar_t, FILE *, int *);
#endif
diff --git a/src/libc/objs/amd64-linux.mk b/src/libc/objs/amd64-linux.mk
@@ -36,6 +36,8 @@ OBJS =\
arch/amd64/strcpy.$O\
arch/linux/_brk.$O\
arch/linux/_getheap.$O\
+ arch/linux/_mbsget.$O\
+ arch/linux/_mbsset.$O\
arch/linux/_sigaction.$O\
arch/linux/_waitpid.$O\
arch/posix/_open.$O\
diff --git a/src/libc/objs/amd64-netbsd.mk b/src/libc/objs/amd64-netbsd.mk
@@ -28,6 +28,8 @@ OBJS =\
arch/amd64/strcmp.$O\
arch/amd64/strcpy.$O\
arch/bsd/_waitpid.$O\
+ arch/bsd/_mbsget.$O\
+ arch/bsd/_mbsset.$O\
arch/netbsd/_sigaction.$O\
arch/posix/_getheap.$O\
arch/posix/_open.$O\
diff --git a/src/libc/objs/amd64-openbsd.mk b/src/libc/objs/amd64-openbsd.mk
@@ -33,6 +33,8 @@ OBJS =\
arch/amd64/strcmp.$O\
arch/amd64/strcpy.$O\
arch/bsd/_waitpid.$O\
+ arch/bsd/_mbsget.$O\
+ arch/bsd/_mbsset.$O\
arch/posix/_getheap.$O\
arch/posix/_open.$O\
arch/posix/_systime.$O\
diff --git a/src/libc/wchar/mbsinit.c b/src/libc/wchar/mbsinit.c
@@ -1,5 +1,7 @@
#include <wchar.h>
+#include "../libc.h"
+
#undef mbsinit
int
@@ -7,5 +9,5 @@ mbsinit(const mbstate_t *ps)
{
if (!ps)
return 1;
- return *ps == 0;
+ return _mbsget(ps) == 0;
}