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 "hdr/types/FILE.h"
13#include "src/__support/libc_errno.h"
14#include "src/__support/macros/config.h"
15#include <stddef.h>
16
17namespace LIBC_NAMESPACE_DECL {
18
19LLVM_LIBC_FUNCTION(char *, fgets,
20 (char *__restrict str, int count,
21 ::FILE *__restrict raw_stream)) {
22 if (count < 1)
23 return nullptr;
24
25 unsigned char c = '\0';
26 auto stream = reinterpret_cast<LIBC_NAMESPACE::File *__restrict>(raw_stream);
27 stream->lock();
28
29 // i is an int because it's frequently compared to count, which is also int.
30 int i = 0;
31
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;
37
38 if (r != 1)
39 break;
40 str[i] = c;
41 }
42
43 bool has_error = stream->error_unlocked();
44 bool has_eof = stream->iseof_unlocked();
45 stream->unlock();
46
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;
51
52 str[i] = '\0';
53 return str;
54}
55
56} // namespace LIBC_NAMESPACE_DECL
57

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of libc/src/stdio/generic/fgets.cpp