@@ -59,14 +59,22 @@ RT_EXT_API_GROUP_BEGIN
59
59
void RTDEF (Rename)(const Descriptor &path1, const Descriptor &path2,
60
60
const Descriptor *status, const char *sourceFile, int line) {
61
61
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)
62
67
#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)};
67
75
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 () )};
70
78
if (status) {
71
79
// When an error has happened,
72
80
int errorCode{0 }; // Assume success
@@ -76,14 +84,6 @@ void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2,
76
84
}
77
85
StoreIntToDescriptor (status, errorCode, terminator);
78
86
}
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
- }
87
87
#else // !defined(RT_DEVICE_COMPILATION)
88
88
terminator.Crash (" RENAME intrinsic is only supported on host devices" );
89
89
#endif // !defined(RT_DEVICE_COMPILATION)
0 commit comments