xref: /llvm-project/libc/src/stdio/gpu/fgets.cpp (revision a6ef0debb1d60966b5bcc69f7d58a2b75c9c621d)
1a1be5d69SJoseph Huber //===-- GPU implementation of fgets ---------------------------------------===//
2a1be5d69SJoseph Huber //
3a1be5d69SJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a1be5d69SJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
5a1be5d69SJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a1be5d69SJoseph Huber //
7a1be5d69SJoseph Huber //===----------------------------------------------------------------------===//
8a1be5d69SJoseph Huber 
9a1be5d69SJoseph Huber #include "src/stdio/fgets.h"
10a1be5d69SJoseph Huber #include "file.h"
115ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
12a1be5d69SJoseph Huber #include "src/stdio/feof.h"
13a1be5d69SJoseph Huber #include "src/stdio/ferror.h"
14a1be5d69SJoseph Huber 
155aed6d67SMichael Jones #include "hdr/stdio_macros.h" // for EOF.
165aed6d67SMichael Jones #include "hdr/types/FILE.h"
17a1be5d69SJoseph Huber #include <stddef.h>
18a1be5d69SJoseph Huber 
195ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
20a1be5d69SJoseph Huber 
21a1be5d69SJoseph Huber LLVM_LIBC_FUNCTION(char *, fgets,
22a1be5d69SJoseph Huber                    (char *__restrict str, int count,
23a1be5d69SJoseph Huber                     ::FILE *__restrict stream)) {
24a1be5d69SJoseph Huber   if (count < 1)
25a1be5d69SJoseph Huber     return nullptr;
26a1be5d69SJoseph Huber 
27a3921576SJoseph Huber   uint64_t recv_size;
28a3921576SJoseph Huber   void *buf = nullptr;
29*a6ef0debSJoseph Huber   rpc::Client::Port port = rpc::client.open<LIBC_READ_FGETS>();
30be0c67c9SJoseph Huber   port.send([=](rpc::Buffer *buffer, uint32_t) {
31a3921576SJoseph Huber     buffer->data[0] = count;
32a3921576SJoseph Huber     buffer->data[1] = file::from_stream(stream);
33a3921576SJoseph Huber   });
34a3921576SJoseph Huber   port.recv_n(&buf, &recv_size,
35a3921576SJoseph Huber               [&](uint64_t) { return reinterpret_cast<void *>(str); });
36a3921576SJoseph Huber   port.close();
37a1be5d69SJoseph Huber 
38a3921576SJoseph Huber   if (recv_size == 0)
39a1be5d69SJoseph Huber     return nullptr;
40a1be5d69SJoseph Huber 
41a1be5d69SJoseph Huber   return str;
42a1be5d69SJoseph Huber }
43a1be5d69SJoseph Huber 
445ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
45