Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / libc / src / stdio / gpu / fgets.cpp
blobfbc1b0cf7d1a87802f69f5ae7b66426c8e657c6c
1 //===-- GPU 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 "file.h"
11 #include "src/__support/macros/config.h"
12 #include "src/stdio/feof.h"
13 #include "src/stdio/ferror.h"
15 #include "hdr/stdio_macros.h" // for EOF.
16 #include "hdr/types/FILE.h"
17 #include <stddef.h>
19 namespace LIBC_NAMESPACE_DECL {
21 LLVM_LIBC_FUNCTION(char *, fgets,
22 (char *__restrict str, int count,
23 ::FILE *__restrict stream)) {
24 if (count < 1)
25 return nullptr;
27 uint64_t recv_size;
28 void *buf = nullptr;
29 rpc::Client::Port port = rpc::client.open<RPC_READ_FGETS>();
30 port.send([=](rpc::Buffer *buffer, uint32_t) {
31 buffer->data[0] = count;
32 buffer->data[1] = file::from_stream(stream);
33 });
34 port.recv_n(&buf, &recv_size,
35 [&](uint64_t) { return reinterpret_cast<void *>(str); });
36 port.close();
38 if (recv_size == 0)
39 return nullptr;
41 return str;
44 } // namespace LIBC_NAMESPACE_DECL