diff options
author | Shourya Goel <shouryagoel10000@gmail.com> | 2024-03-18 21:56:30 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-18 09:26:30 -0700 |
commit | ca04b56a8b26cbe9327eaab113e1e4af096d902a (patch) | |
tree | 4bd93dfe8bd29c6b696847c5d9fa05e61c47cab9 | |
parent | f8042171552ca16e77a5e9367188e7f218474380 (diff) | |
download | llvm-ca04b56a8b26cbe9327eaab113e1e4af096d902a.zip llvm-ca04b56a8b26cbe9327eaab113e1e4af096d902a.tar.gz llvm-ca04b56a8b26cbe9327eaab113e1e4af096d902a.tar.bz2 |
[libc] Implement fileno (#85628)
fixes: #85150
-rw-r--r-- | libc/config/linux/x86_64/entrypoints.txt | 1 | ||||
-rw-r--r-- | libc/src/stdio/CMakeLists.txt | 10 | ||||
-rw-r--r-- | libc/src/stdio/fileno.h | 21 | ||||
-rw-r--r-- | libc/src/stdio/generic/CMakeLists.txt | 12 | ||||
-rw-r--r-- | libc/src/stdio/generic/fileno.cpp | 21 | ||||
-rw-r--r-- | libc/test/src/stdio/fileop_test.cpp | 1 |
6 files changed, 66 insertions, 0 deletions
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index ee02488..f81d334 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -208,6 +208,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.stdio.sscanf libc.src.stdio.scanf libc.src.stdio.fscanf + libc.src.stdio.fileno # sys/epoll.h entrypoints libc.src.sys.epoll.epoll_wait diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt index bb8e416..ece93fd 100644 --- a/libc/src/stdio/CMakeLists.txt +++ b/libc/src/stdio/CMakeLists.txt @@ -236,6 +236,16 @@ add_entrypoint_object( libc.src.stdio.printf_core.vfprintf_internal ) +add_stdio_entrypoint_object( + fileno + SRCS + fileno.cpp + HDRS + fileno.h + DEPENDS + libc.src.stdio.fileno +) + add_subdirectory(printf_core) add_subdirectory(scanf_core) diff --git a/libc/src/stdio/fileno.h b/libc/src/stdio/fileno.h new file mode 100644 index 0000000..d41f112 --- /dev/null +++ b/libc/src/stdio/fileno.h @@ -0,0 +1,21 @@ +//===-- Implementation header of fileno --------------------------*- C++ +//-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_STDIO_FILENO_H +#define LLVM_LIBC_SRC_STDIO_FILENO_H + +#include "include/llvm-libc-types/FILE.h" + +namespace LIBC_NAMESPACE { + +int fileno(::FILE *f); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_STDIO_FILENO_H diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt index 4e4a709..0aa213c 100644 --- a/libc/src/stdio/generic/CMakeLists.txt +++ b/libc/src/stdio/generic/CMakeLists.txt @@ -71,6 +71,18 @@ add_entrypoint_object( ) add_entrypoint_object( + fileno + SRCS + fileno.cpp + HDRS + ../fileno.h + DEPENDS + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + +add_entrypoint_object( fflush SRCS fflush.cpp diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp new file mode 100644 index 0000000..663ba92 --- /dev/null +++ b/libc/src/stdio/generic/fileno.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of fileno +//-------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/stdio/fileno.h" + +#include "include/llvm-libc-types/FILE.h" +#include "src/__support/File/file.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) { + return get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream)); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp index f5dbc49..2f2e63e 100644 --- a/libc/test/src/stdio/fileop_test.cpp +++ b/libc/test/src/stdio/fileop_test.cpp @@ -30,6 +30,7 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) { constexpr char FILENAME[] = "testdata/simple_operations.test"; ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w"); ASSERT_FALSE(file == nullptr); + ASSERT_EQ(LIBC_NAMESPACE::fileno(file), 3); constexpr char CONTENT[] = "1234567890987654321"; ASSERT_EQ(sizeof(CONTENT) - 1, LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file)); |