diff --git a/flang/runtime/file.h b/flang/runtime/file.h --- a/flang/runtime/file.h +++ b/flang/runtime/file.h @@ -85,6 +85,7 @@ position_ = pos; openPosition_.reset(); } + void CloseFd(IoErrorHandler &); int fd_{-1}; OwningPtr path_; diff --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp --- a/flang/runtime/file.cpp +++ b/flang/runtime/file.cpp @@ -66,16 +66,7 @@ (status == OpenStatus::Old || status == OpenStatus::Unknown)) { return; } - if (fd_ >= 0) { - if (fd_ <= 2) { - // don't actually close a standard file descriptor, we might need it - } else { - if (::close(fd_) != 0) { - handler.SignalErrno(); - } - } - fd_ = -1; - } + CloseFd(handler); if (status == OpenStatus::Scratch) { if (path_.get()) { handler.SignalError("FILE= must not appear with STATUS='SCRATCH'"); @@ -179,12 +170,7 @@ break; } path_.reset(); - if (fd_ >= 0) { - if (::close(fd_) != 0) { - handler.SignalErrno(); - } - fd_ = -1; - } + CloseFd(handler); } std::size_t OpenFile::Read(FileOffset at, char *buffer, std::size_t minBytes, @@ -410,6 +396,19 @@ return id; } +void OpenFile::CloseFd(IoErrorHandler &handler) { + if (fd_ >= 0) { + if (fd_ <= 2) { + // don't actually close a standard file descriptor, we might need it + } else { + if (::close(fd_) != 0) { + handler.SignalErrno(); + } + } + fd_ = -1; + } +} + bool IsATerminal(int fd) { return ::isatty(fd); } #ifdef WIN32