wcrtomb.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Convert wide character to multibyte character.
  2. Copyright (C) 2008-2017 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 <http://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. {
  22. /* This implementation of wcrtomb on top of wctomb() supports only
  23. stateless encodings. ps must be in the initial state. */
  24. if (ps != NULL && !mbsinit (ps))
  25. {
  26. errno = EINVAL;
  27. return (size_t)(-1);
  28. }
  29. if (s == NULL)
  30. /* We know the NUL wide character corresponds to the NUL character. */
  31. return 1;
  32. else
  33. {
  34. int ret = wctomb (s, wc);
  35. if (ret >= 0)
  36. return ret;
  37. else
  38. {
  39. errno = EILSEQ;
  40. return (size_t)(-1);
  41. }
  42. }
  43. }