xref: /llvm-project/libc/src/stdio/generic/getc_unlocked.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1a1be5d69SJoseph Huber //===-- Implementation of getc_unlocked ----------------------------------===//
2a1be5d69SJoseph Huber //
3a1be5d69SJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a1be5d69SJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
5a1be5d69SJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a1be5d69SJoseph Huber //
7a1be5d69SJoseph Huber //===----------------------------------------------------------------------===//
8a1be5d69SJoseph Huber 
9a1be5d69SJoseph Huber #include "src/stdio/getc_unlocked.h"
10a1be5d69SJoseph Huber #include "src/__support/File/file.h"
11a1be5d69SJoseph Huber 
125aed6d67SMichael Jones #include "hdr/types/FILE.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
14a1be5d69SJoseph Huber #include "src/errno/libc_errno.h"
155aed6d67SMichael Jones #include <stddef.h>
16a1be5d69SJoseph Huber 
17*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
18a1be5d69SJoseph Huber 
19a1be5d69SJoseph Huber LLVM_LIBC_FUNCTION(int, getc_unlocked, (::FILE * stream)) {
20a1be5d69SJoseph Huber   unsigned char c;
21a1be5d69SJoseph Huber   auto result =
22b6bc9d72SGuillaume Chatelet       reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->read_unlocked(&c, 1);
23a1be5d69SJoseph Huber   size_t r = result.value;
24a1be5d69SJoseph Huber   if (result.has_error())
25a1be5d69SJoseph Huber     libc_errno = result.error;
26a1be5d69SJoseph Huber 
27a1be5d69SJoseph Huber   if (r != 1)
28a1be5d69SJoseph Huber     return EOF;
29a1be5d69SJoseph Huber   return c;
30a1be5d69SJoseph Huber }
31a1be5d69SJoseph Huber 
32*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
33