wctomb.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <wchar.h>
  17. #include <gconv.h>
  18. #include <wcsmbs/wcsmbsload.h>
  19. /* Shared with __wctomb_chk. */
  20. mbstate_t __wctomb_state attribute_hidden;
  21. /* Convert WCHAR into its multibyte character representation,
  22. putting this in S and returning its length.
  23. Attention: this function should NEVER be intentionally used.
  24. The interface is completely stupid. The state is shared between
  25. all conversion functions. You should use instead the restartable
  26. version `wcrtomb'. */
  27. int
  28. wctomb (char *s, wchar_t wchar)
  29. {
  30. /* If S is NULL the function has to return null or not null
  31. depending on the encoding having a state depending encoding or
  32. not. */
  33. if (s == NULL)
  34. {
  35. const struct gconv_fcts *fcts;
  36. /* Get the conversion functions. */
  37. fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
  38. /* This is an extension in the Unix standard which does not directly
  39. violate ISO C. */
  40. memset (&__wctomb_state, '\0', sizeof __wctomb_state);
  41. return fcts->tomb->__stateful;
  42. }
  43. return __wcrtomb (s, wchar, &__wctomb_state);
  44. }
  45. libc_hidden_def (wctomb)