xref: /aosp_15_r20/external/llvm-libc/src/stdio/gpu/fgets.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 //===----------------------------------------------------------------------===//
8 
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"
14 
15 #include "hdr/stdio_macros.h" // for EOF.
16 #include "hdr/types/FILE.h"
17 #include <stddef.h>
18 
19 namespace LIBC_NAMESPACE_DECL {
20 
21 LLVM_LIBC_FUNCTION(char *, fgets,
22                    (char *__restrict str, int count,
23                     ::FILE *__restrict stream)) {
24   if (count < 1)
25     return nullptr;
26 
27   uint64_t recv_size;
28   void *buf = nullptr;
29   rpc::Client::Port port = rpc::client.open<RPC_READ_FGETS>();
__anon752e04c60102(rpc::Buffer *buffer, uint32_t) 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,
__anon752e04c60202(uint64_t) 35               [&](uint64_t) { return reinterpret_cast<void *>(str); });
36   port.close();
37 
38   if (recv_size == 0)
39     return nullptr;
40 
41   return str;
42 }
43 
44 } // namespace LIBC_NAMESPACE_DECL
45