1//===-- Implementation of wctomb ------------------------------------------===//
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/wctomb.h"
10
11#include "hdr/types/wchar_t.h"
12#include "src/__support/common.h"
13#include "src/__support/libc_errno.h"
14#include "src/__support/macros/config.h"
15#include "src/__support/wchar/mbstate.h"
16#include "src/__support/wchar/wcrtomb.h"
17
18namespace LIBC_NAMESPACE_DECL {
19
20LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) {
21 static internal::mbstate internal_mbstate;
22 if (s == nullptr)
23 return 0;
24
25 auto result = internal::wcrtomb(s, wc, &internal_mbstate);
26
27 if (!result.has_value()) { // invalid wide character
28 libc_errno = EILSEQ;
29 return -1;
30 }
31
32 return static_cast<int>(result.value());
33}
34
35} // namespace LIBC_NAMESPACE_DECL
36

source code of libc/src/wchar/wctomb.cpp