@@ -59,14 +59,22 @@ RT_EXT_API_GROUP_BEGIN
5959void RTDEF (Rename)(const Descriptor &path1, const Descriptor &path2,
6060 const Descriptor *status, const char *sourceFile, int line) {
6161 Terminator terminator{sourceFile, line};
62+
63+ // Semantics for character strings: A null character (CHAR(0)) can be used to
64+ // mark the end of the names in PATH1 and PATH2; otherwise, trailing blanks in
65+ // the file names are ignored.
66+ // (https://gcc.gnu.org/onlinedocs/gfortran/RENAME.html)
6267#if !defined(RT_DEVICE_COMPILATION)
63- char *pathSrc{EnsureNullTerminated (
64- path1.OffsetElement (), path1.ElementBytes (), terminator)};
65- char *pathDst{EnsureNullTerminated (
66- path2.OffsetElement (), path2.ElementBytes (), terminator)};
68+ // Trim tailing spaces, respect presences of null character when doing so.
69+ auto pathSrc{SaveDefaultCharacter (path1.OffsetElement (),
70+ TrimTrailingSpaces (path1.OffsetElement (), path1.ElementBytes ()),
71+ terminator)};
72+ auto pathDst{SaveDefaultCharacter (path2.OffsetElement (),
73+ TrimTrailingSpaces (path2.OffsetElement (), path2.ElementBytes ()),
74+ terminator)};
6775
68- // We simply call rename(2) from POSIX
69- int result{rename (pathSrc, pathDst)};
76+ // We can now simply call rename(2) from POSIX.
77+ int result{rename (pathSrc. get () , pathDst. get () )};
7078 if (status) {
7179 // When an error has happened,
7280 int errorCode{0 }; // Assume success
@@ -76,14 +84,6 @@ void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2,
7684 }
7785 StoreIntToDescriptor (status, errorCode, terminator);
7886 }
79-
80- // Deallocate memory if EnsureNullTerminated dynamically allocated memory
81- if (pathSrc != path1.OffsetElement ()) {
82- FreeMemory (pathSrc);
83- }
84- if (pathDst != path2.OffsetElement ()) {
85- FreeMemory (pathDst);
86- }
8787#else // !defined(RT_DEVICE_COMPILATION)
8888 terminator.Crash (" RENAME intrinsic is only supported on host devices" );
8989#endif // !defined(RT_DEVICE_COMPILATION)
0 commit comments