[libc] Use best-fit binary trie to make malloc logarithmic (#106259)
[llvm-project.git] / libc / src / stdio / generic / fgets.cpp
blobde6474087a1402114a9b6e5d2b74521c17724ffb
1 //===-- Implementation of fgets -------------------------------------------===//
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 //===----------------------------------------------------------------------===//
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"
15 #include <stddef.h>
17 namespace LIBC_NAMESPACE_DECL {
19 LLVM_LIBC_FUNCTION(char *, fgets,
20 (char *__restrict str, int count,
21 ::FILE *__restrict raw_stream)) {
22 if (count < 1)
23 return nullptr;
25 unsigned char c = '\0';
26 auto stream = reinterpret_cast<LIBC_NAMESPACE::File *__restrict>(raw_stream);
27 stream->lock();
29 // i is an int because it's frequently compared to count, which is also int.
30 int i = 0;
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;
38 if (r != 1)
39 break;
40 str[i] = c;
43 bool has_error = stream->error_unlocked();
44 bool has_eof = stream->iseof_unlocked();
45 stream->unlock();
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))
50 return nullptr;
52 str[i] = '\0';
53 return str;
56 } // namespace LIBC_NAMESPACE_DECL