[libc-commits] [libc] [libc] Add the implementation of the fdopen function (PR #94186)
Xu Zhang via libc-commits
libc-commits at lists.llvm.org
Sun Jun 2 22:37:02 PDT 2024
https://github.com/simonzgx created https://github.com/llvm/llvm-project/pull/94186
None
>From d43458bf555bafe3a456e8ad32380f0909ff0c18 Mon Sep 17 00:00:00 2001
From: Xu Zhang <simonzgx at gmail.com>
Date: Mon, 3 Jun 2024 13:36:03 +0800
Subject: [PATCH] [libc] Add the implementation of the fdopen function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/spec/posix.td | 5 ++
libc/src/stdio/CMakeLists.txt | 7 ++
libc/src/stdio/fdopen.h | 20 ++++++
libc/src/stdio/linux/CMakeLists.txt | 14 ++++
libc/src/stdio/linux/fdopen.cpp | 79 +++++++++++++++++++++++
libc/test/src/stdio/fdopen_test.cpp | 0
9 files changed, 128 insertions(+)
create mode 100644 libc/src/stdio/fdopen.h
create mode 100644 libc/src/stdio/linux/fdopen.cpp
create mode 100644 libc/test/src/stdio/fdopen_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ca0418c3618ae..81d14271547dc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -201,6 +201,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.snprintf
libc.src.stdio.vsprintf
libc.src.stdio.vsnprintf
+ libc.src.stdio.fdopen
#libc.src.stdio.sscanf
#libc.src.stdio.scanf
#libc.src.stdio.fscanf
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 479af40b5b26b..fc5300f9be2d0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -209,6 +209,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.sscanf
libc.src.stdio.scanf
libc.src.stdio.fscanf
+ libc.src.stdio.fdopen
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 699bb9dcdf3c9..082dfe7ac5f73 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -214,6 +214,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.scanf
libc.src.stdio.fscanf
libc.src.stdio.fileno
+ libc.src.stdio.fdopen
# sys/epoll.h entrypoints
libc.src.sys.epoll.epoll_create
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index e16353b8142de..78d9d727ac7b4 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -1293,6 +1293,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
+ FunctionSpec<
+ "fdopen",
+ RetValSpec<FILEPtr>,
+ [ArgSpec<IntType>, ArgSpec<ConstCharPtr>]
+ >,
]
>;
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 1056f38fc7513..deb7e5ad942bf 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -263,6 +263,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.rename
)
+add_entrypoint_object(
+ fdopen
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.fdopen
+)
+
# These entrypoints have multiple potential implementations.
add_stdio_entrypoint_object(feof)
add_stdio_entrypoint_object(feof_unlocked)
diff --git a/libc/src/stdio/fdopen.h b/libc/src/stdio/fdopen.h
new file mode 100644
index 0000000000000..158a133e7e131
--- /dev/null
+++ b/libc/src/stdio/fdopen.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of open ---------------------------*- 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_FDOPEN_H
+#define LLVM_LIBC_SRC_STDIO_FDOPEN_H
+
+#include <stdio.h>
+
+namespace LIBC_NAMESPACE {
+
+FILE *fdopen(int fd, const char *mode);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDIO_FDOPEN_H
diff --git a/libc/src/stdio/linux/CMakeLists.txt b/libc/src/stdio/linux/CMakeLists.txt
index a08ff0ba4832f..133740fcb41f4 100644
--- a/libc/src/stdio/linux/CMakeLists.txt
+++ b/libc/src/stdio/linux/CMakeLists.txt
@@ -24,3 +24,17 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
+
+add_entrypoint_object(
+ fdopen
+ SRCS
+ fdopen.cpp
+ HDRS
+ ../fdopen.h
+ DEPENDS
+ libc.include.fcntl
+ libc.include.stdio
+ libc.src.errno.errno
+ libc.src.fcntl.fcntl
+ libc.src.stdio.fseek
+)
\ No newline at end of file
diff --git a/libc/src/stdio/linux/fdopen.cpp b/libc/src/stdio/linux/fdopen.cpp
new file mode 100644
index 0000000000000..91b27a568b0e1
--- /dev/null
+++ b/libc/src/stdio/linux/fdopen.cpp
@@ -0,0 +1,79 @@
+//===-- Implementation of fprintf -------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/fdopen.h"
+
+#include "include/llvm-libc-macros/generic-error-number-macros.h"
+#include "include/llvm-libc-macros/linux/fcntl-macros.h"
+#include "src/__support/File/linux/file.h"
+#include "src/errno/libc_errno.h"
+#include "src/fcntl/fcntl.h"
+#include "src/stdio/fseek.h"
+
+#include <stdio.h>
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(::FILE *, fdopen, (int fd, const char *mode)) {
+ auto modeflags = LIBC_NAMESPACE::File::mode_flags(mode);
+ if (modeflags == 0) {
+ libc_errno = EINVAL;
+ return nullptr;
+ }
+
+ int fd_flags = LIBC_NAMESPACE::fcntl(fd, F_GETFL);
+ if (fd_flags == -1) {
+ libc_errno = EBADF;
+ return nullptr;
+ }
+
+ using OpenMode = File::OpenMode;
+ if (((fd_flags & O_ACCMODE) == O_RDONLY &&
+ !(modeflags & static_cast<ModeFlags>(OpenMode::READ))) ||
+ ((fd_flags & O_ACCMODE) == O_WRONLY &&
+ !(modeflags & static_cast<ModeFlags>(OpenMode::WRITE)))) {
+ libc_errno = EINVAL;
+ return nullptr;
+ }
+
+ bool do_seek = false;
+ if ((modeflags & static_cast<File::ModeFlags>(File::OpenMode::APPEND)) &&
+ !(fd_flags & O_APPEND)) {
+ do_seek = true;
+ if (LIBC_NAMESPACE::fcntl(fd, F_SETFL, fd_flags | O_APPEND) == -1) {
+ libc_errno = EBADF;
+ return nullptr;
+ }
+ }
+
+ uint8_t *buffer;
+ {
+ AllocChecker ac;
+ buffer = new (ac) uint8_t[File::DEFAULT_BUFFER_SIZE];
+ if (!ac) {
+ libc_errno = ENOMEM;
+ return nullptr;
+ }
+ }
+ AllocChecker ac;
+ auto *linux_file = new (ac)
+ LinuxFile(fd, buffer, File::DEFAULT_BUFFER_SIZE, _IOFBF, true, modeflags);
+ if (!ac) {
+ libc_errno = ENOMEM;
+ return nullptr;
+ }
+ auto *file = reinterpret_cast<::FILE *>(linux_file);
+ if (do_seek && LIBC_NAMESPACE::fseek(file, 0, SEEK_END) != 0) {
+ free(linux_file);
+ return nullptr;
+ }
+
+ return file;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/stdio/fdopen_test.cpp b/libc/test/src/stdio/fdopen_test.cpp
new file mode 100644
index 0000000000000..e69de29bb2d1d
More information about the libc-commits
mailing list