| 1 | //===-- Implementation of wcrtomb -----------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/wchar/wcrtomb.h" |
| 10 | |
| 11 | #include "hdr/types/mbstate_t.h" |
| 12 | #include "hdr/types/wchar_t.h" |
| 13 | #include "src/__support/common.h" |
| 14 | #include "src/__support/libc_errno.h" |
| 15 | #include "src/__support/macros/config.h" |
| 16 | #include "src/__support/wchar/mbstate.h" |
| 17 | #include "src/__support/wchar/wcrtomb.h" |
| 18 | |
| 19 | namespace LIBC_NAMESPACE_DECL { |
| 20 | |
| 21 | LLVM_LIBC_FUNCTION(size_t, wcrtomb, |
| 22 | (char *__restrict s, wchar_t wc, mbstate_t *__restrict ps)) { |
| 23 | static internal::mbstate internal_mbstate; |
| 24 | |
| 25 | // when s is nullptr, this is equivalent to wcrtomb(buf, L'\0', ps) |
| 26 | char buf[sizeof(wchar_t) / sizeof(char)]; |
| 27 | if (s == nullptr) { |
| 28 | s = buf; |
| 29 | wc = L'\0'; |
| 30 | } |
| 31 | |
| 32 | auto result = internal::wcrtomb( |
| 33 | s, wc, |
| 34 | ps == nullptr ? &internal_mbstate |
| 35 | : reinterpret_cast<internal::mbstate *>(ps)); |
| 36 | |
| 37 | if (!result.has_value()) { |
| 38 | libc_errno = result.error(); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | return result.value(); |
| 43 | } |
| 44 | |
| 45 | } // namespace LIBC_NAMESPACE_DECL |
| 46 | |