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"
16 namespace __llvm_libc
{
18 LLVM_LIBC_FUNCTION(char *, fgets
,
19 (char *__restrict str
, int count
,
20 ::FILE *__restrict raw_stream
)) {
24 unsigned char c
= '\0';
25 auto stream
= reinterpret_cast<__llvm_libc::File
*__restrict
>(raw_stream
);
28 // i is an int because it's frequently compared to count, which is also int.
31 for (; i
< (count
- 1) && c
!= '\n'; ++i
) {
32 auto result
= stream
->read_unlocked(&c
, 1);
33 size_t r
= result
.value
;
34 if (result
.has_error())
42 bool has_error
= stream
->error_unlocked();
43 bool has_eof
= stream
->iseof_unlocked();
46 // If the requested read size makes no sense, an error occured, or no bytes
47 // were read due to an EOF, then return nullptr and don't write the null byte.
48 if (has_error
|| (i
== 0 && has_eof
))
55 } // namespace __llvm_libc