xref: /llvm-project/libc/src/stdio/generic/fgets.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
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 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdio/fgets.h"
10 #include "src/__support/File/file.h"
11 
12 #include "src/errno/libc_errno.h"
13 #include <stddef.h>
14 #include <stdio.h>
15 
16 namespace LIBC_NAMESPACE {
17 
18 LLVM_LIBC_FUNCTION(char *, fgets,
19                    (char *__restrict str, int count,
20                     ::FILE *__restrict raw_stream)) {
21   if (count < 1)
22     return nullptr;
23 
24   unsigned char c = '\0';
25   auto stream = reinterpret_cast<LIBC_NAMESPACE::File *__restrict>(raw_stream);
26   stream->lock();
27 
28   // i is an int because it's frequently compared to count, which is also int.
29   int i = 0;
30 
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       libc_errno = result.error;
36 
37     if (r != 1)
38       break;
39     str[i] = c;
40   }
41 
42   bool has_error = stream->error_unlocked();
43   bool has_eof = stream->iseof_unlocked();
44   stream->unlock();
45 
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;
50 
51   str[i] = '\0';
52   return str;
53 }
54 
55 } // namespace LIBC_NAMESPACE
56