summaryrefslogtreecommitdiff
path: root/flang-rt
diff options
context:
space:
mode:
authorPeter Klausler <pklausler@nvidia.com>2025-09-19 08:09:39 -0700
committerGitHub <noreply@github.com>2025-09-19 08:09:39 -0700
commit80fa3bddd0dfd5c1ed4301033e5647a3c7f226ee (patch)
tree44015c41d7f393a4c6514c0a1a746970cb56bec3 /flang-rt
parentb21dd44dbc00a60b787dcfa90ca0e8b677c5211f (diff)
[flang] Implement FNUM() (#159433)
The GNU Fortran library function FNUM(u) returns the UNIX file descriptor that corresponds to an open Fortran unit number, if any; otherwise -1. This implementation is a library extension only, not an intrinsic.
Diffstat (limited to 'flang-rt')
-rw-r--r--flang-rt/include/flang-rt/runtime/file.h1
-rw-r--r--flang-rt/lib/runtime/extensions.cpp9
-rw-r--r--flang-rt/lib/runtime/unit.h1
3 files changed, 11 insertions, 0 deletions
diff --git a/flang-rt/include/flang-rt/runtime/file.h b/flang-rt/include/flang-rt/runtime/file.h
index 468a759214b8..25942c053bbe 100644
--- a/flang-rt/include/flang-rt/runtime/file.h
+++ b/flang-rt/include/flang-rt/runtime/file.h
@@ -27,6 +27,7 @@ class OpenFile {
public:
using FileOffset = std::int64_t;
+ int fd() const { return fd_; }
const char *path() const { return path_.get(); }
std::size_t pathLength() const { return pathLength_; }
void set_path(OwningPtr<char> &&, std::size_t bytes);
diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index 2c42597a5654..19e75143705a 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -424,6 +424,15 @@ std::int64_t RTNAME(Ftell)(int unitNumber) {
return -1;
}
}
+
+std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber) {
+ if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) {
+ return unit->fd();
+ } else {
+ return -1;
+ }
+}
+
} // namespace io
} // extern "C"
diff --git a/flang-rt/lib/runtime/unit.h b/flang-rt/lib/runtime/unit.h
index 5ea52d1907f6..7aeea0931e01 100644
--- a/flang-rt/lib/runtime/unit.h
+++ b/flang-rt/lib/runtime/unit.h
@@ -59,6 +59,7 @@ class PseudoOpenFile {
public:
using FileOffset = std::int64_t;
+ RT_API_ATTRS int fd() const { return 1 /*stdout*/; }
RT_API_ATTRS const char *path() const { return nullptr; }
RT_API_ATTRS std::size_t pathLength() const { return 0; }
RT_API_ATTRS void set_path(OwningPtr<char> &&, std::size_t bytes) {}