[libc-commits] [libc] [libc] Add 'x' and 'e' mode support to fopen() (PR #224207)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Sep 22 15:52:22 PDT 2026


================
@@ -42,3 +57,78 @@ TEST(LlvmLibcFOpenTest, PrintToFile) {
     ASSERT_STREQ(data, STRING);
   }
 }
+
+#ifdef LIBC_TARGET_OS_IS_LINUX
+class LlvmLibcFOpenModeTest
+    : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
+protected:
+  void check_exclusive_create(const char *mode) {
+    const auto FILENAME = libc_make_test_file_path("fopen_exclusive.test");
+    constexpr char CONTENT[] = "Preserve this content";
+
+    // Remove a file left by an interrupted test run.
+    LIBC_NAMESPACE::remove(FILENAME);
+    libc_errno = 0;
+    FILE *file = LIBC_NAMESPACE::fopen(FILENAME, mode);
+    ASSERT_NE(file, nullptr);
+    scope_exit remove_file(
+        [&] { EXPECT_THAT(LIBC_NAMESPACE::remove(FILENAME), Succeeds(0)); });
+    {
+      scope_exit close_file(
+          [&] { EXPECT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds(0)); });
+      ASSERT_EQ(LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file),
+                sizeof(CONTENT) - 1);
+    }
----------------
michaelrj-google wrote:

ah, I see now that `close_file` is in an inner scope. Given that it seems like `close_file` could probably be removed since there's only one line after it.
```suggestion
    scope_exit remove_file(
        [&] { EXPECT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0)); });
    // Flush and close the writer before the following checks.
    {
      ASSERT_EQ(LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file),
                sizeof(CONTENT) - 1);
       EXPECT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds(0));
    }
```

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


More information about the libc-commits mailing list