Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions flang-rt/lib/runtime/misc-intrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,22 @@ RT_EXT_API_GROUP_BEGIN
void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2,
const Descriptor *status, const char *sourceFile, int line) {
Terminator terminator{sourceFile, line};

// Semantics for character strings: A null character (CHAR(0)) can be used to
// mark the end of the names in PATH1 and PATH2; otherwise, trailing blanks in
// the file names are ignored.
// (https://gcc.gnu.org/onlinedocs/gfortran/RENAME.html)
#if !defined(RT_DEVICE_COMPILATION)
char *pathSrc{EnsureNullTerminated(
path1.OffsetElement(), path1.ElementBytes(), terminator)};
char *pathDst{EnsureNullTerminated(
path2.OffsetElement(), path2.ElementBytes(), terminator)};
// Trim tailing spaces, respect presences of null character when doing so.
auto pathSrc{SaveDefaultCharacter(path1.OffsetElement(),
TrimTrailingSpaces(path1.OffsetElement(), path1.ElementBytes()),
terminator)};
auto pathDst{SaveDefaultCharacter(path2.OffsetElement(),
TrimTrailingSpaces(path2.OffsetElement(), path2.ElementBytes()),
terminator)};

// We simply call rename(2) from POSIX
int result{rename(pathSrc, pathDst)};
// We can now simply call rename(2) from POSIX.
int result{rename(pathSrc.get(), pathDst.get())};
if (status) {
// When an error has happened,
int errorCode{0}; // Assume success
Expand All @@ -76,14 +84,6 @@ void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2,
}
StoreIntToDescriptor(status, errorCode, terminator);
}

// Deallocate memory if EnsureNullTerminated dynamically allocated memory
if (pathSrc != path1.OffsetElement()) {
FreeMemory(pathSrc);
}
if (pathDst != path2.OffsetElement()) {
FreeMemory(pathDst);
}
#else // !defined(RT_DEVICE_COMPILATION)
terminator.Crash("RENAME intrinsic is only supported on host devices");
#endif // !defined(RT_DEVICE_COMPILATION)
Expand Down
Loading