[flang-commits] [PATCH] D127014: [flang][runtime] INQUIRE(FILE="...", SIZE=nbytes)

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jun 3 15:44:39 PDT 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

Implement inquire-by-file SIZE= specifier.


https://reviews.llvm.org/D127014

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


Index: flang/runtime/io-stmt.cpp
===================================================================
--- flang/runtime/io-stmt.cpp
+++ 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;
Index: flang/runtime/file.h
===================================================================
--- flang/runtime/file.h
+++ 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_
Index: flang/runtime/file.cpp
===================================================================
--- flang/runtime/file.cpp
+++ 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127014.434173.patch
Type: text/x-patch
Size: 1463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220603/eac06302/attachment-0001.bin>


More information about the flang-commits mailing list