Skip to content

Commit ca04b56

Browse files
authored
[libc] Implement fileno (llvm#85628)
fixes: llvm#85150
1 parent f804217 commit ca04b56

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ set(TARGET_LIBC_ENTRYPOINTS
208208
libc.src.stdio.sscanf
209209
libc.src.stdio.scanf
210210
libc.src.stdio.fscanf
211+
libc.src.stdio.fileno
211212

212213
# sys/epoll.h entrypoints
213214
libc.src.sys.epoll.epoll_wait

libc/src/stdio/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ add_entrypoint_object(
236236
libc.src.stdio.printf_core.vfprintf_internal
237237
)
238238

239+
add_stdio_entrypoint_object(
240+
fileno
241+
SRCS
242+
fileno.cpp
243+
HDRS
244+
fileno.h
245+
DEPENDS
246+
libc.src.stdio.fileno
247+
)
248+
239249
add_subdirectory(printf_core)
240250
add_subdirectory(scanf_core)
241251

libc/src/stdio/fileno.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header of fileno --------------------------*- C++
2+
//-*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_LIBC_SRC_STDIO_FILENO_H
11+
#define LLVM_LIBC_SRC_STDIO_FILENO_H
12+
13+
#include "include/llvm-libc-types/FILE.h"
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
int fileno(::FILE *f);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_STDIO_FILENO_H

libc/src/stdio/generic/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ add_entrypoint_object(
7070
libc.src.__support.File.platform_file
7171
)
7272

73+
add_entrypoint_object(
74+
fileno
75+
SRCS
76+
fileno.cpp
77+
HDRS
78+
../fileno.h
79+
DEPENDS
80+
libc.include.stdio
81+
libc.src.__support.File.file
82+
libc.src.__support.File.platform_file
83+
)
84+
7385
add_entrypoint_object(
7486
fflush
7587
SRCS

libc/src/stdio/generic/fileno.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of fileno
2+
//-------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "src/stdio/fileno.h"
11+
12+
#include "include/llvm-libc-types/FILE.h"
13+
#include "src/__support/File/file.h"
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
18+
return get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
19+
}
20+
21+
} // namespace LIBC_NAMESPACE

libc/test/src/stdio/fileop_test.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) {
3030
constexpr char FILENAME[] = "testdata/simple_operations.test";
3131
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
3232
ASSERT_FALSE(file == nullptr);
33+
ASSERT_EQ(LIBC_NAMESPACE::fileno(file), 3);
3334
constexpr char CONTENT[] = "1234567890987654321";
3435
ASSERT_EQ(sizeof(CONTENT) - 1,
3536
LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file));

0 commit comments

Comments
 (0)