[libc-commits] [libc] [libc] Implement freopen (PR #207837)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Wed Jul 8 07:50:47 PDT 2026
================
@@ -176,4 +183,103 @@ int get_fileno(File *f) {
return lf->get_fd();
}
+int reopenfile(File *f, const char *path, const char *mode) {
+ auto modeflags = File::mode_flags(mode);
+ if (modeflags == 0)
+ return EINVAL;
+
+ auto *lf = reinterpret_cast<LinuxFile *>(f);
+
+ if (path != nullptr) {
+ int open_flags = mode_flags_to_open_flags(modeflags);
+
+ constexpr mode_t OPEN_MODE =
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+
+ ErrorOr<int> new_fd = linux_syscalls::open(path, open_flags, OPEN_MODE);
+ int old_fd = lf->get_fd();
+
+ // If the new file fails to open, POSIX says we still have to close the old
+ // file.
+ if (!new_fd) {
+ if (old_fd >= 0) {
+ auto close_result = linux_syscalls::close(old_fd);
+ if (!close_result) {
+ f->reset_stream_state(modeflags);
+ return close_result.error();
----------------
kaladron wrote:
"Failure to close the file descriptor successfully shall be ignored" - I think this shouldn't return the error here.
https://github.com/llvm/llvm-project/pull/207837
More information about the libc-commits
mailing list