wcrtomb.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Convert wide character to multibyte character.
  2. Copyright (C) 2008-2013 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 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <wchar.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. // wcrtomb is defined for msvs 2017 15.7
  20. #if _MSC_VER < 1914
  21. size_t
  22. wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
  23. {
  24. /* This implementation of wcrtomb on top of wctomb() supports only
  25. stateless encodings. ps must be in the initial state. */
  26. if (ps != NULL && !mbsinit (ps))
  27. {
  28. errno = EINVAL;
  29. return (size_t)(-1);
  30. }
  31. if (s == NULL)
  32. /* We know the NUL wide character corresponds to the NUL character. */
  33. return 1;
  34. else
  35. {
  36. int ret = wctomb (s, wc);
  37. if (ret >= 0)
  38. return ret;
  39. else
  40. {
  41. errno = EILSEQ;
  42. return (size_t)(-1);
  43. }
  44. }
  45. }
  46. #endif