1 //===-- Implementation of fgets -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/stdio/fgets.h"
10 #include "src/__support/File/file.h"
12 #include "hdr/types/FILE.h"
13 #include "src/__support/macros/config.h"
14 #include "src/errno/libc_errno.h"
17 namespace LIBC_NAMESPACE_DECL
{
19 LLVM_LIBC_FUNCTION(char *, fgets
,
20 (char *__restrict str
, int count
,
21 ::FILE *__restrict raw_stream
)) {
25 unsigned char c
= '\0';
26 auto stream
= reinterpret_cast<LIBC_NAMESPACE::File
*__restrict
>(raw_stream
);
29 // i is an int because it's frequently compared to count, which is also int.
32 for (; i
< (count
- 1) && c
!= '\n'; ++i
) {
33 auto result
= stream
->read_unlocked(&c
, 1);
34 size_t r
= result
.value
;
35 if (result
.has_error())
36 libc_errno
= result
.error
;
43 bool has_error
= stream
->error_unlocked();
44 bool has_eof
= stream
->iseof_unlocked();
47 // If the requested read size makes no sense, an error occured, or no bytes
48 // were read due to an EOF, then return nullptr and don't write the null byte.
49 if (has_error
|| (i
== 0 && has_eof
))
56 } // namespace LIBC_NAMESPACE_DECL