diff --git a/flang/runtime/file.h b/flang/runtime/file.h --- a/flang/runtime/file.h +++ b/flang/runtime/file.h @@ -109,5 +109,6 @@ bool MayRead(const char *path); bool MayWrite(const char *path); bool MayReadAndWrite(const char *path); +std::int64_t SizeInBytes(const char *path); } // namespace Fortran::runtime::io #endif // FORTRAN_RUNTIME_FILE_H_ diff --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp --- a/flang/runtime/file.cpp +++ b/flang/runtime/file.cpp @@ -434,4 +434,17 @@ bool MayReadAndWrite(const char *path) { return ::access(path, R_OK | W_OK) == 0; } + +std::int64_t SizeInBytes(const char *path) { +#ifndef _WIN32 + struct stat buf; + if (::stat(path, &buf) == 0) { + return buf.st_size; + } +#else // TODO: _WIN32 +#endif + // No Fortran compiler signals an error + return -1; +} + } // namespace Fortran::runtime::io diff --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp --- a/flang/runtime/io-stmt.cpp +++ b/flang/runtime/io-stmt.cpp @@ -1483,9 +1483,11 @@ case HashInquiryKeyword("NUMBER"): case HashInquiryKeyword("POS"): case HashInquiryKeyword("RECL"): - case HashInquiryKeyword("SIZE"): result = -1; return true; + case HashInquiryKeyword("SIZE"): + result = SizeInBytes(path_.get()); + return true; default: BadInquiryKeywordHashCrash(inquiry); return false;