[libc-commits] [libc] [libc] Implement freopen (PR #207837)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Tue Aug 18 10:38:37 PDT 2026


================
@@ -0,0 +1,328 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for freopen.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/fcntl.h"
+#include "src/stdio/clearerr.h"
+#include "src/stdio/fclose.h"
+#include "src/stdio/feof.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fflush.h"
+#include "src/stdio/fileno.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+#include "src/stdio/freopen.h"
+#include "src/stdio/fwrite.h"
+#include "src/stdio/stdout.h"
+#include "src/unistd/close.h"
+#include "src/wchar/fwide.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/stdio_macros.h"
+#include "src/__support/macros/properties/os.h"
+
+using LlvmLibcFreopenTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST_F(LlvmLibcFreopenTest, ReopenFile) {
+  const auto FILENAME_A = libc_make_test_file_path("freopen_a.test");
+  const auto FILENAME_B = libc_make_test_file_path("freopen_b.test");
+
+  // Step 1: Open file A and write initial content.
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME_A, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  constexpr char CONTENT_A[] = "File A Content";
+  ASSERT_EQ(LIBC_NAMESPACE::fwrite(CONTENT_A, 1, sizeof(CONTENT_A) - 1, file),
+            sizeof(CONTENT_A) - 1);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+
+  // Step 2: Open file A for reading.
+  file = LIBC_NAMESPACE::fopen(FILENAME_A, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  // Step 3: Use freopen to redirect stream from file A to file B for writing.
+  ::FILE *reopened_file = LIBC_NAMESPACE::freopen(FILENAME_B, "w", file);
+  ASSERT_NE(reopened_file, static_cast<::FILE *>(nullptr));
+  ASSERT_EQ(reopened_file, file);
+
+  // Step 4: Write to reopened stream (file B).
+  constexpr char CONTENT_B[] = "File B Content Written via freopen";
+  ASSERT_EQ(LIBC_NAMESPACE::fwrite(CONTENT_B, 1, sizeof(CONTENT_B) - 1,
+                                   reopened_file),
+            sizeof(CONTENT_B) - 1);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(reopened_file), 0);
+
+  // Step 5: Verify file B content.
+  file = LIBC_NAMESPACE::fopen(FILENAME_B, "r");
+  ASSERT_FALSE(file == nullptr);
+  char read_buf[sizeof(CONTENT_B)] = {0};
+  ASSERT_EQ(LIBC_NAMESPACE::fread(read_buf, 1, sizeof(CONTENT_B) - 1, file),
+            sizeof(CONTENT_B) - 1);
+  ASSERT_STREQ(read_buf, CONTENT_B);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+TEST_F(LlvmLibcFreopenTest, NullFilenameModeChange) {
+  const auto FILENAME = libc_make_test_file_path("freopen_null_filename.test");
+
+  // Step 1: Open file with write-update mode.
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w+");
+  ASSERT_FALSE(file == nullptr);
+
+  constexpr char INITIAL_CONTENT[] = "Initial Data ";
+  ASSERT_EQ(LIBC_NAMESPACE::fwrite(INITIAL_CONTENT, 1,
+                                   sizeof(INITIAL_CONTENT) - 1, file),
+            sizeof(INITIAL_CONTENT) - 1);
+
+  // Step 2: Change mode with filename == nullptr to append.
+  ::FILE *reopened = LIBC_NAMESPACE::freopen(nullptr, "a", file);
+  ASSERT_NE(reopened, static_cast<::FILE *>(nullptr));
+  ASSERT_EQ(reopened, file);
+
+  // Step 3: Write appended content.
+  constexpr char APPENDED_CONTENT[] = "Appended Data";
+  ASSERT_EQ(LIBC_NAMESPACE::fwrite(APPENDED_CONTENT, 1,
+                                   sizeof(APPENDED_CONTENT) - 1, reopened),
+            sizeof(APPENDED_CONTENT) - 1);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(reopened), 0);
+
+  // Step 4: Verify combined content.
+  file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+  char read_buf[64] = {0};
+  size_t read_bytes =
+      LIBC_NAMESPACE::fread(read_buf, 1, sizeof(read_buf) - 1, file);
+  read_buf[read_bytes] = '\0';
+  ASSERT_STREQ(read_buf, "Initial Data Appended Data");
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+TEST_F(LlvmLibcFreopenTest, NullFilenameInvalidModeChange) {
+  const auto FILENAME =
+      libc_make_test_file_path("freopen_invalid_mode_change.test");
+
+  // Open file read-only.
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+
+  file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  // Attempt incompatible mode change (r to w with filename == nullptr).
+  ASSERT_THAT(LIBC_NAMESPACE::freopen(nullptr, "w", file),
+              Fails(EBADF, static_cast<void *>(nullptr)));
+
+  // Attempt incompatible mode change (r to w+ with filename == nullptr).
+  ASSERT_THAT(LIBC_NAMESPACE::freopen(nullptr, "w+", file),
+              Fails(EBADF, static_cast<void *>(nullptr)));
+
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+#if defined(LIBC_TARGET_OS_IS_POSIX) || defined(LIBC_TARGET_OS_IS_LINUX)
+TEST_F(LlvmLibcFreopenTest, InvalidModeFailure) {
+  const auto FILENAME = libc_make_test_file_path("freopen_invalid_mode.test");
+
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  int old_fd = LIBC_NAMESPACE::fileno(file);
+  ASSERT_GT(old_fd, 0);
+
+  ASSERT_THAT(LIBC_NAMESPACE::freopen(FILENAME, "invalid_mode_str", file),
+              Fails(EINVAL, static_cast<void *>(nullptr)));
+
+  // Per POSIX spec, original stream fd was closed on filename != nullptr
+  // freopen attempt.
+  ASSERT_EQ(LIBC_NAMESPACE::fcntl(old_fd, F_GETFL), -1);
+  ASSERT_ERRNO_EQ(EBADF);
----------------
vonosmas wrote:

ASSERT_THAT( ... Fails) here and below?

https://github.com/llvm/llvm-project/pull/207837


More information about the libc-commits mailing list