[libc-commits] [libc] [libc] Change __llvm_libc_stdio_seek to match cookie_io_functions_t (PR #225632)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Wed Sep 23 01:07:37 PDT 2026


https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/225632

This allows using __llvm_libc_stdio_seek directly without a need for an adapter, matching the rest of the stdio embedding interface.

>From 8cfa99720af787abe678085697825db9e0e0e86b Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 23 Sep 2026 08:03:30 +0000
Subject: [PATCH] [libc] Change __llvm_libc_stdio_seek to match
 cookie_io_functions_t

This allows using __llvm_libc_stdio_seek directly without a need for
an adapter, matching the rest of the stdio embedding interface.
---
 libc/src/__support/OSUtil/baremetal/io.h | 6 +++---
 libc/src/stdio/baremetal/fseek.cpp       | 4 ++--
 libc/src/stdio/baremetal/fseeko.cpp      | 2 +-
 libc/src/stdio/baremetal/ftell.cpp       | 5 +++--
 libc/src/stdio/baremetal/ftello.cpp      | 5 +++--
 5 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/libc/src/__support/OSUtil/baremetal/io.h b/libc/src/__support/OSUtil/baremetal/io.h
index 3a61fa8f4535ee..a24965500c7b71 100644
--- a/libc/src/__support/OSUtil/baremetal/io.h
+++ b/libc/src/__support/OSUtil/baremetal/io.h
@@ -57,11 +57,11 @@ extern "C" ssize_t __llvm_libc_stdio_read(void *cookie, char *buf, size_t size);
 extern "C" ssize_t __llvm_libc_stdio_write(void *cookie, const char *buf,
                                            size_t size);
 
-// Return the resulting absolute file position on success. On failure, return a
+// Update the `offset` to indicate the new stream offset. On failure, return a
 // negative errno value.
-extern "C" off_t __llvm_libc_stdio_seek(void *cookie, off_t offset, int whence);
+extern "C" int __llvm_libc_stdio_seek(void *cookie, off_t *offset, int whence);
 
-// Return 0 on success or EOF on failure, matching fclose.
+// Return 0 on success or EOF on failure.
 extern "C" int __llvm_libc_stdio_close(void *cookie);
 
 void write_to_stderr(cpp::string_view msg);
diff --git a/libc/src/stdio/baremetal/fseek.cpp b/libc/src/stdio/baremetal/fseek.cpp
index 540c3cb70fbe55..feb1cd581ded07 100644
--- a/libc/src/stdio/baremetal/fseek.cpp
+++ b/libc/src/stdio/baremetal/fseek.cpp
@@ -26,8 +26,8 @@ LLVM_LIBC_FUNCTION(int, fseek, (::FILE * stream, long offset, int whence)) {
     libc_errno = EINVAL;
     return -1;
   }
-  off_t result =
-      __llvm_libc_stdio_seek(stream, static_cast<off_t>(offset), whence);
+  int result = __llvm_libc_stdio_seek(
+      stream, reinterpret_cast<off_t *>(&offset), whence);
   if (result < 0) {
     libc_errno = static_cast<int>(-result);
     return -1;
diff --git a/libc/src/stdio/baremetal/fseeko.cpp b/libc/src/stdio/baremetal/fseeko.cpp
index 42c87aebe6911c..eb6c0358c37648 100644
--- a/libc/src/stdio/baremetal/fseeko.cpp
+++ b/libc/src/stdio/baremetal/fseeko.cpp
@@ -26,7 +26,7 @@ LLVM_LIBC_FUNCTION(int, fseeko, (::FILE * stream, off_t offset, int whence)) {
     libc_errno = EINVAL;
     return -1;
   }
-  off_t result = __llvm_libc_stdio_seek(stream, offset, whence);
+  int result = __llvm_libc_stdio_seek(stream, &offset, whence);
   if (result < 0) {
     libc_errno = static_cast<int>(-result);
     return -1;
diff --git a/libc/src/stdio/baremetal/ftell.cpp b/libc/src/stdio/baremetal/ftell.cpp
index 8b062d955a4f30..29db8148851a2c 100644
--- a/libc/src/stdio/baremetal/ftell.cpp
+++ b/libc/src/stdio/baremetal/ftell.cpp
@@ -27,12 +27,13 @@ LLVM_LIBC_FUNCTION(long, ftell, (::FILE * stream)) {
     libc_errno = EINVAL;
     return -1;
   }
-  off_t result = __llvm_libc_stdio_seek(stream, 0, SEEK_CUR);
+  off_t offset;
+  int result = __llvm_libc_stdio_seek(stream, &offset, SEEK_CUR);
   if (result < 0) {
     libc_errno = static_cast<int>(-result);
     return -1;
   }
-  return static_cast<long>(result);
+  return static_cast<long>(offset);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/ftello.cpp b/libc/src/stdio/baremetal/ftello.cpp
index 0f5db4872a1128..5b47e2b8d88092 100644
--- a/libc/src/stdio/baremetal/ftello.cpp
+++ b/libc/src/stdio/baremetal/ftello.cpp
@@ -27,12 +27,13 @@ LLVM_LIBC_FUNCTION(off_t, ftello, (::FILE * stream)) {
     libc_errno = EINVAL;
     return static_cast<off_t>(-1);
   }
-  off_t result = __llvm_libc_stdio_seek(stream, 0, SEEK_CUR);
+  off_t offset;
+  int result = __llvm_libc_stdio_seek(stream, &offset, SEEK_CUR);
   if (result < 0) {
     libc_errno = static_cast<int>(-result);
     return static_cast<off_t>(-1);
   }
-  return result;
+  return offset;
 }
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list