[flang-commits] [flang] 9878fac - [flang][runtime] INQUIRE(FILE="...", SIZE=nbytes)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Jun 3 18:05:38 PDT 2022


Author: Peter Klausler
Date: 2022-06-03T18:05:27-07:00
New Revision: 9878facfd01cdfaab171b26850e609e78ba68b0e

URL: https://github.com/llvm/llvm-project/commit/9878facfd01cdfaab171b26850e609e78ba68b0e
DIFF: https://github.com/llvm/llvm-project/commit/9878facfd01cdfaab171b26850e609e78ba68b0e.diff

LOG: [flang][runtime] INQUIRE(FILE="...",SIZE=nbytes)

Implement inquire-by-file SIZE= specifier.

Differential Revision: https://reviews.llvm.org/D127014

Added: 
    

Modified: 
    flang/runtime/file.cpp
    flang/runtime/file.h
    flang/runtime/io-stmt.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp
index d1744c053e8d..6a86c9ddfc5d 100644
--- a/flang/runtime/file.cpp
+++ b/flang/runtime/file.cpp
@@ -434,4 +434,17 @@ bool MayWrite(const char *path) { return ::access(path, W_OK) == 0; }
 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/file.h b/flang/runtime/file.h
index 712b2045532b..3d43d57c1b96 100644
--- a/flang/runtime/file.h
+++ b/flang/runtime/file.h
@@ -109,5 +109,6 @@ bool IsExtant(const char *path);
 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/io-stmt.cpp b/flang/runtime/io-stmt.cpp
index 78fae20a173c..c3dab0506825 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang/runtime/io-stmt.cpp
@@ -1483,9 +1483,11 @@ bool InquireUnconnectedFileState::Inquire(
   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;


        


More information about the flang-commits mailing list