12 changes: 12 additions & 0 deletions libc/src/stdio/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ 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.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)
25 changes: 25 additions & 0 deletions libc/src/stdio/linux/fdopen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Implementation of fdopen --------------------------------*- 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 "src/__support/File/linux/file.h"
#include "src/errno/libc_errno.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(::FILE *, fdopen, (int fd, const char *mode)) {
auto result = LIBC_NAMESPACE::create_file_from_fd(fd, mode);
if (!result.has_value()) {
libc_errno = result.error();
return nullptr;
}
return reinterpret_cast<::FILE *>(result.value());
}

} // namespace LIBC_NAMESPACE
10 changes: 10 additions & 0 deletions libc/test/src/fcntl/fcntl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,13 @@ TEST(LlvmLibcFcntlTest, FcntlGetLkWrite) {

ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
}

TEST(LlvmLibcFcntlTest, UseAfterClose) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_use_after_close.test";
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
ASSERT_EQ(-1, LIBC_NAMESPACE::fcntl(fd, F_GETFL));
ASSERT_ERRNO_EQ(EBADF);
}
16 changes: 16 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,22 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
libc.src.unistd.close
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_test(
fdopen_test
SUITE
libc_stdio_unittests
SRCS
fdopen_test.cpp
DEPENDS
libc.src.fcntl.open
libc.src.stdio.fclose
libc.src.stdio.fdopen
libc.src.stdio.fgets
libc.src.stdio.fputs
libc.src.unistd.close
libc.test.UnitTest.ErrnoSetterMatcher
)
endif()

add_libc_test(
Expand Down
89 changes: 89 additions & 0 deletions libc/test/src/stdio/fdopen_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//===-- Unittest for fdopen -----------------------------------------------===//
//
// 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 "hdr/fcntl_macros.h"
#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/stdio/fclose.h"
#include "src/stdio/fgets.h"
#include "src/stdio/fputs.h"
#include "src/unistd/close.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <sys/stat.h> // For S_IRWXU

TEST(LlvmLibcStdioFdopenTest, WriteAppendRead) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
LIBC_NAMESPACE::libc_errno = 0;
constexpr const char *TEST_FILE_NAME = "testdata/write_read_append.test";
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
auto *fp = LIBC_NAMESPACE::fdopen(fd, "w");
ASSERT_ERRNO_SUCCESS();
ASSERT_TRUE(nullptr != fp);
constexpr const char HELLO[] = "Hello";
LIBC_NAMESPACE::fputs(HELLO, fp);
LIBC_NAMESPACE::fclose(fp);
ASSERT_ERRNO_SUCCESS();

constexpr const char LLVM[] = "LLVM";
int fd2 = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_RDWR);
auto *fp2 = LIBC_NAMESPACE::fdopen(fd2, "a");
ASSERT_ERRNO_SUCCESS();
ASSERT_TRUE(nullptr != fp2);
LIBC_NAMESPACE::fputs(LLVM, fp2);
LIBC_NAMESPACE::fclose(fp2);
ASSERT_ERRNO_SUCCESS();

int fd3 = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_RDWR);
auto *fp3 = LIBC_NAMESPACE::fdopen(fd3, "r");
char buffer[10];
LIBC_NAMESPACE::fgets(buffer, sizeof(buffer), fp3);
ASSERT_STREQ("HelloLLVM", buffer);
LIBC_NAMESPACE::fclose(fp3);
ASSERT_ERRNO_SUCCESS();
}

TEST(LlvmLibcStdioFdopenTest, InvalidFd) {
LIBC_NAMESPACE::libc_errno = 0;
constexpr const char *TEST_FILE_NAME = "testdata/invalid_fd.test";
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC);
LIBC_NAMESPACE::close(fd);
// With `fd` already closed, `fdopen` should fail and set the `errno` to EBADF
auto *fp = LIBC_NAMESPACE::fdopen(fd, "r");
ASSERT_ERRNO_EQ(EBADF);
ASSERT_TRUE(nullptr == fp);
}

TEST(LlvmLibcStdioFdopenTest, InvalidMode) {
LIBC_NAMESPACE::libc_errno = 0;
constexpr const char *TEST_FILE_NAME = "testdata/invalid_mode.test";
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_RDONLY, S_IRWXU);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(fd, 0);

// `Mode` must be one of "r", "w" or "a"
auto *fp = LIBC_NAMESPACE::fdopen(fd, "m+");
ASSERT_ERRNO_EQ(EINVAL);
ASSERT_TRUE(nullptr == fp);

// If the mode argument is invalid, then `fdopen` returns a nullptr and sets
// the `errno` to EINVAL. In this case the `mode` param can only be "r" or
// "r+"
auto *fp2 = LIBC_NAMESPACE::fdopen(fd, "w");
ASSERT_ERRNO_EQ(EINVAL);
ASSERT_TRUE(nullptr == fp2);
LIBC_NAMESPACE::libc_errno = 0;
LIBC_NAMESPACE::close(fd);
ASSERT_ERRNO_SUCCESS();
}