wcrtomb.c 2.3 KB

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