[libc-commits] [libc] e4b93b7 - [libc] implement rewind (#191302)

via libc-commits libc-commits at lists.llvm.org
Fri Apr 10 11:04:56 PDT 2026


Author: Michael Jones
Date: 2026-04-10T11:04:52-07:00
New Revision: e4b93b70c92ffc581a16578acc2ef58e02824076

URL: https://github.com/llvm/llvm-project/commit/e4b93b70c92ffc581a16578acc2ef58e02824076
DIFF: https://github.com/llvm/llvm-project/commit/e4b93b70c92ffc581a16578acc2ef58e02824076.diff

LOG: [libc] implement rewind (#191302)

Add the "rewind" function defined in the C standard, and adds tests.

Added: 
    libc/src/stdio/generic/rewind.cpp
    libc/src/stdio/rewind.h
    libc/test/src/stdio/rewind_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/stdio.yaml
    libc/src/stdio/CMakeLists.txt
    libc/src/stdio/generic/CMakeLists.txt
    libc/test/src/stdio/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 26892ba21cff6..72836d031c0e8 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1098,6 +1098,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putc
     libc.src.stdio.putchar
     libc.src.stdio.puts
+    libc.src.stdio.rewind
     libc.src.stdio.setbuf
     libc.src.stdio.setvbuf
     libc.src.stdio.stderr

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 6cf0bed25cce2..2038b776e539b 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1227,6 +1227,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putc
     libc.src.stdio.putchar
     libc.src.stdio.puts
+    libc.src.stdio.rewind
     libc.src.stdio.setbuf
     libc.src.stdio.setvbuf
     libc.src.stdio.stderr

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 8a85f1ad0a65e..b43ec51d1ece8 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1285,6 +1285,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putc
     libc.src.stdio.putchar
     libc.src.stdio.puts
+    libc.src.stdio.rewind
     libc.src.stdio.setbuf
     libc.src.stdio.setvbuf
     libc.src.stdio.stderr

diff  --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index edb915085722a..24890b4b25670 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -218,6 +218,12 @@ functions:
       - type: FILE *
       - type: long
       - type: int
+  - name: rewind
+    standards:
+      - stdc
+    return_type: void
+    arguments:
+      - type: FILE *
   - name: fseeko
     standards:
       - POSIX

diff  --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 1079a6ac51dfc..feee8d60d1c60 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -263,6 +263,7 @@ add_stdio_entrypoint_object(feof_unlocked)
 add_stdio_entrypoint_object(ferror)
 add_stdio_entrypoint_object(ferror_unlocked)
 add_stdio_entrypoint_object(fseek)
+add_stdio_entrypoint_object(rewind)
 add_stdio_entrypoint_object(ftell)
 add_stdio_entrypoint_object(fseeko)
 add_stdio_entrypoint_object(fileno)

diff  --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 9cde0980d6f32..a9c5ae698c3bb 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -123,6 +123,19 @@ add_generic_entrypoint_object(
     libc.src.__support.File.platform_file
 )
 
+add_generic_entrypoint_object(
+  rewind
+  SRCS
+    rewind.cpp
+  HDRS
+    ../rewind.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+    libc.src.__support.macros.null_check
+)
+
 add_generic_entrypoint_object(
   ftell
   SRCS

diff  --git a/libc/src/stdio/generic/rewind.cpp b/libc/src/stdio/generic/rewind.cpp
new file mode 100644
index 0000000000000..d2d22c68eb7c0
--- /dev/null
+++ b/libc/src/stdio/generic/rewind.cpp
@@ -0,0 +1,28 @@
+//===-- Implementation of rewind ------------------------------------------===//
+//
+// 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/rewind.h"
+#include "src/__support/File/file.h"
+
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, rewind, (::FILE * stream)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+  File *FilePtr = reinterpret_cast<File *>(stream);
+  auto Result = FilePtr->seek(0, SEEK_SET);
+  FilePtr->clearerr();
+
+  if (!Result.has_value())
+    libc_errno = Result.error();
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/stdio/rewind.h b/libc/src/stdio/rewind.h
new file mode 100644
index 0000000000000..1c8b154842cdf
--- /dev/null
+++ b/libc/src/stdio/rewind.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of rewind -------------------------*- 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_REWIND_H
+#define LLVM_LIBC_SRC_STDIO_REWIND_H
+
+#include "hdr/types/FILE.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void rewind(FILE *stream);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDIO_REWIND_H

diff  --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 689845ea231ef..038ac9766b80b 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -23,6 +23,24 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
+add_libc_test(
+  rewind_test
+  SUITE
+    libc_stdio_unittests
+  SRCS
+    rewind_test.cpp
+  DEPENDS
+    libc.include.stdio
+    libc.src.stdio.fclose
+    libc.src.stdio.ferror
+    libc.src.stdio.fopen
+    libc.src.stdio.fwrite
+    libc.src.stdio.fread
+    libc.src.stdio.rewind
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   ungetc_test
   SUITE

diff  --git a/libc/test/src/stdio/rewind_test.cpp b/libc/test/src/stdio/rewind_test.cpp
new file mode 100644
index 0000000000000..41dcbbdb8dd80
--- /dev/null
+++ b/libc/test/src/stdio/rewind_test.cpp
@@ -0,0 +1,76 @@
+//===-- Unittests for file operations like fopen, flcose etc --------------===//
+//
+// 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/fclose.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+#include "src/stdio/fwrite.h"
+#include "src/stdio/rewind.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcRewindTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
+  constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/rewind.test");
+  auto FILEPATH = libc_make_test_file_path(FILENAME);
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILEPATH, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  constexpr char FIRST_DATA[] = "123456789";
+  ASSERT_THAT(LIBC_NAMESPACE::fwrite(FIRST_DATA, 1, sizeof(FIRST_DATA), file),
+              Succeeds(sizeof(FIRST_DATA)));
+
+  // File state is "123456789"
+
+  LIBC_NAMESPACE::rewind(file);
+
+  // Cursor is back to the start
+
+  constexpr char SECOND_DATA[] = "abc";
+  ASSERT_THAT(
+      LIBC_NAMESPACE::fwrite(SECOND_DATA, 1, sizeof(SECOND_DATA) - 1, file),
+      Succeeds(sizeof(SECOND_DATA) - 1));
+
+  // File state is "abc456789"
+
+  // attempt to read from write-only file causing error state
+  char read_data[sizeof(FIRST_DATA)];
+  ASSERT_EQ(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
+            size_t(0));
+  ASSERT_ERRNO_FAILURE();
+  ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
+
+  // rewind to start and check that that clears the error.
+  LIBC_NAMESPACE::rewind(file);
+  ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
+
+  // Close out the file and reopen in read mode.
+  LIBC_NAMESPACE::fclose(file);
+  file = LIBC_NAMESPACE::fopen(FILEPATH, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  // Read the file to check that it was written correctly.
+  ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, 3, file),
+              Succeeds(size_t(3)));
+  read_data[3] = '\0';
+  ASSERT_STREQ(read_data, "abc");
+  ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
+
+  // check that rewind also works on read files.
+  LIBC_NAMESPACE::rewind(file);
+  ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
+              Succeeds(sizeof(FIRST_DATA)));
+  read_data[sizeof(FIRST_DATA) - 1] = '\0';
+  ASSERT_STREQ(read_data, "abc456789");
+
+  LIBC_NAMESPACE::fclose(file);
+}


        


More information about the libc-commits mailing list