xref: /llvm-project/libc/src/stdio/generic/putchar.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
160c0d303SJoseph Huber //===-- Implementation of putchar -----------------------------------------===//
260c0d303SJoseph Huber //
360c0d303SJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
460c0d303SJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
560c0d303SJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
660c0d303SJoseph Huber //
760c0d303SJoseph Huber //===----------------------------------------------------------------------===//
860c0d303SJoseph Huber 
960c0d303SJoseph Huber #include "src/stdio/putchar.h"
1060c0d303SJoseph Huber #include "src/__support/File/file.h"
1160c0d303SJoseph Huber 
125aed6d67SMichael Jones #include "hdr/types/FILE.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1460c0d303SJoseph Huber #include "src/errno/libc_errno.h"
155aed6d67SMichael Jones #include <stddef.h>
1660c0d303SJoseph Huber 
17*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1860c0d303SJoseph Huber 
1960c0d303SJoseph Huber LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
2060c0d303SJoseph Huber   unsigned char uc = static_cast<unsigned char>(c);
2160c0d303SJoseph Huber 
22b6bc9d72SGuillaume Chatelet   auto result = LIBC_NAMESPACE::stdout->write(&uc, 1);
2360c0d303SJoseph Huber   if (result.has_error())
2460c0d303SJoseph Huber     libc_errno = result.error;
2560c0d303SJoseph Huber   size_t written = result.value;
2660c0d303SJoseph Huber 
2760c0d303SJoseph Huber   if (1 != written) {
2860c0d303SJoseph Huber     // The stream should be in an error state in this case.
2960c0d303SJoseph Huber     return EOF;
3060c0d303SJoseph Huber   }
3160c0d303SJoseph Huber   return 0;
3260c0d303SJoseph Huber }
3360c0d303SJoseph Huber 
34*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
35