diff --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp index fa040b417c31c..3b495c37d0828 100644 --- a/flang/runtime/file.cpp +++ b/flang/runtime/file.cpp @@ -132,7 +132,7 @@ void OpenFile::Open(OpenStatus status, std::optional action, RUNTIME_CHECK(handler, action.has_value()); pending_.reset(); if (position == Position::Append && !RawSeekToEnd()) { - handler.SignalErrno(); + handler.SignalError(IostatOpenBadAppend); } isTerminal_ = ::isatty(fd_) == 1; mayRead_ = *action != Action::Write; diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp index c2f9d37bb1224..73a96b8bf3dc7 100644 --- a/flang/runtime/unit.cpp +++ b/flang/runtime/unit.cpp @@ -137,7 +137,7 @@ void ExternalFileUnit::OpenUnit(std::optional status, "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is invalid", unitNumber(), static_cast(*openRecl)); } else if (totalBytes && (*totalBytes % *openRecl != 0)) { - handler.SignalError(IostatOpenBadAppend, + handler.SignalError(IostatOpenBadRecl, "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is not an " "even divisor of the file size %jd", unitNumber(), static_cast(*openRecl), @@ -150,12 +150,17 @@ void ExternalFileUnit::OpenUnit(std::optional status, if (totalBytes && access == Access::Direct && openRecl.value_or(0) > 0) { endfileRecordNumber = 1 + (*totalBytes / *openRecl); } - if (position == Position::Append && access != Access::Stream) { - if (!endfileRecordNumber) { - // Fake it so that we can backspace relative from the end - endfileRecordNumber = std::numeric_limits::max() - 2; + if (position == Position::Append) { + if (totalBytes) { + frameOffsetInFile_ = *totalBytes; + } + if (access != Access::Stream) { + if (!endfileRecordNumber) { + // Fake it so that we can backspace relative from the end + endfileRecordNumber = std::numeric_limits::max() - 2; + } + currentRecordNumber = *endfileRecordNumber; } - currentRecordNumber = *endfileRecordNumber; } }