| 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 | |
| 11 | #include "file.h" |
| 12 | #include "hdr/stdio_macros.h" // for EOF. |
| 13 | #include "hdr/types/FILE.h" |
| 14 | #include "src/__support/common.h" |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | |
| 18 | namespace LIBC_NAMESPACE_DECL { |
| 19 | |
| 20 | LLVM_LIBC_FUNCTION(char *, fgets, |
| 21 | (char *__restrict str, int count, |
| 22 | ::FILE *__restrict stream)) { |
| 23 | if (count < 1) |
| 24 | return nullptr; |
| 25 | |
| 26 | uint64_t recv_size; |
| 27 | void *buf = nullptr; |
| 28 | rpc::Client::Port port = rpc::client.open<LIBC_READ_FGETS>(); |
| 29 | port.send([=](rpc::Buffer *buffer, uint32_t) { |
| 30 | buffer->data[0] = count; |
| 31 | buffer->data[1] = file::from_stream(stream); |
| 32 | }); |
| 33 | port.recv_n(&buf, &recv_size, |
| 34 | [&](uint64_t) { return reinterpret_cast<void *>(str); }); |
| 35 | port.close(); |
| 36 | |
| 37 | if (recv_size == 0) |
| 38 | return nullptr; |
| 39 | |
| 40 | return str; |
| 41 | } |
| 42 | |
| 43 | } // namespace LIBC_NAMESPACE_DECL |
| 44 | |