exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / wcrtomb.c
blob197b020e2801601c54b51e9ae4eed93340ee6f95
1 /* Convert wide character to multibyte character.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <wchar.h>
23 #include <errno.h>
24 #include <stdlib.h>
27 size_t
28 wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
29 #undef wcrtomb
31 /* This implementation of wcrtomb supports only stateless encodings.
32 ps must be in an initial state. */
33 if (ps != NULL && !mbsinit (ps))
35 errno = EINVAL;
36 return (size_t)(-1);
39 #if !HAVE_WCRTOMB /* IRIX 6.5 */ \
40 || WCRTOMB_RETVAL_BUG /* Solaris 11.3, MSVC */ \
41 || WCRTOMB_C_LOCALE_BUG /* Android */
42 if (s == NULL)
43 /* We know the NUL wide character corresponds to the NUL character. */
44 return 1;
45 else
46 #endif
48 #if HAVE_WCRTOMB
49 # if WCRTOMB_C_LOCALE_BUG /* Android */
50 /* Implement consistently with mbrtowc(): through a 1:1 correspondence,
51 as in ISO-8859-1. */
52 if (wc >= 0 && wc <= 0xff)
54 *s = (unsigned char) wc;
55 return 1;
57 else
59 errno = EILSEQ;
60 return (size_t)(-1);
62 # else
63 return wcrtomb (s, wc, ps);
64 # endif
65 #else /* IRIX 6.5 */
66 /* Fallback for platforms that don't have wcrtomb().
67 Implement on top of wctomb().
68 This code is not multithread-safe. */
69 int ret = wctomb (s, wc);
71 if (ret >= 0)
72 return ret;
73 else
75 errno = EILSEQ;
76 return (size_t)(-1);
78 #endif