[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / src / stdio / fgets.cpp
blobbb7c794d8614922ad1b23eeaa3248af72c2737af
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 <errno.h>
13 #include <stddef.h>
14 #include <stdio.h>
16 namespace __llvm_libc {
18 LLVM_LIBC_FUNCTION(char *, fgets,
19 (char *__restrict str, int count,
20 ::FILE *__restrict raw_stream)) {
21 if (count < 1)
22 return nullptr;
24 unsigned char c = '\0';
25 auto stream = reinterpret_cast<__llvm_libc::File *__restrict>(raw_stream);
26 stream->lock();
28 // i is an int because it's frequently compared to count, which is also int.
29 int i = 0;
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())
35 errno = result.error;
37 if (r != 1)
38 break;
39 str[i] = c;
42 bool has_error = stream->error_unlocked();
43 bool has_eof = stream->iseof_unlocked();
44 stream->unlock();
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))
49 return nullptr;
51 str[i] = '\0';
52 return str;
55 } // namespace __llvm_libc