Skip to content

[libc] Implement fileno on Linux #85628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ add_entrypoint_object(
libc.src.stdio.printf_core.vfprintf_internal
)

add_stdio_entrypoint_object(
fileno
SRCS
fileno.cpp
HDRS
fileno.h
DEPENDS
libc.src.__support.arg_list
libc.src.stdio.fileno
)

add_subdirectory(printf_core)
add_subdirectory(scanf_core)

Expand Down
21 changes: 21 additions & 0 deletions libc/src/stdio/fileno.h
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

namespace LIBC_NAMESPACE {

int fileno(::FILE *f);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDIO_FILENO_H
12 changes: 12 additions & 0 deletions libc/src/stdio/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)

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
Expand Down
22 changes: 22 additions & 0 deletions libc/src/stdio/generic/fileno.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- 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 "src/__support/File/file.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
int result = get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
return result;
}

} // namespace LIBC_NAMESPACE
1 change: 1 addition & 0 deletions libc/test/src/stdio/fileop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, we also need to include "src/stdio/fileno.h" in order to have this declaration

constexpr char CONTENT[] = "1234567890987654321";
ASSERT_EQ(sizeof(CONTENT) - 1,
LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file));
Expand Down