xref: /llvm-project/libc/src/stdio/generic/fputs.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1d2024bbcSJoseph Huber //===-- Implementation of fputs -------------------------------------------===//
2d2024bbcSJoseph Huber //
3d2024bbcSJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d2024bbcSJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
5d2024bbcSJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d2024bbcSJoseph Huber //
7d2024bbcSJoseph Huber //===----------------------------------------------------------------------===//
8d2024bbcSJoseph Huber 
9d2024bbcSJoseph Huber #include "src/stdio/fputs.h"
10d2024bbcSJoseph Huber #include "src/__support/CPP/string_view.h"
11d2024bbcSJoseph Huber #include "src/__support/File/file.h"
12d2024bbcSJoseph Huber 
135aed6d67SMichael Jones #include "hdr/types/FILE.h"
14*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
15d2024bbcSJoseph Huber #include "src/errno/libc_errno.h"
165aed6d67SMichael Jones #include <stddef.h>
17d2024bbcSJoseph Huber 
18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
19d2024bbcSJoseph Huber 
20d2024bbcSJoseph Huber LLVM_LIBC_FUNCTION(int, fputs,
21d2024bbcSJoseph Huber                    (const char *__restrict str, ::FILE *__restrict stream)) {
22d2024bbcSJoseph Huber   cpp::string_view str_view(str);
23d2024bbcSJoseph Huber 
24b6bc9d72SGuillaume Chatelet   auto result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->write(
25d2024bbcSJoseph Huber       str, str_view.size());
26d2024bbcSJoseph Huber   if (result.has_error())
27d2024bbcSJoseph Huber     libc_errno = result.error;
28d2024bbcSJoseph Huber   size_t written = result.value;
29d2024bbcSJoseph Huber 
30d2024bbcSJoseph Huber   if (str_view.size() != written) {
31d2024bbcSJoseph Huber     // The stream should be in an error state in this case.
32d2024bbcSJoseph Huber     return EOF;
33d2024bbcSJoseph Huber   }
34d2024bbcSJoseph Huber   return 0;
35d2024bbcSJoseph Huber }
36d2024bbcSJoseph Huber 
37*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
38