diff --git a/src/rcore.c b/src/rcore.c index dd60ab731..9aadce1e4 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2583,19 +2583,27 @@ const char *GetPrevDirectoryPath(const char *dirPath) { static char prevDirPath[MAX_FILEPATH_LENGTH] = { 0 }; memset(prevDirPath, 0, MAX_FILEPATH_LENGTH); - int dirPathLength = (int)strlen(dirPath); - if (dirPathLength <= 3) snprintf(prevDirPath, MAX_FILEPATH_LENGTH, "%s", dirPath); - - for (int i = (dirPathLength - 1); (i >= 0) && (dirPathLength > 3); i--) + const int lastIndex = (int)strlen(dirPath) - 1; + bool isFile = IsPathFile(dirPath); + for (int i = lastIndex; i >= 0; i--) { + // If the character is a path separator. if ((dirPath[i] == '\\') || (dirPath[i] == '/')) { - // Check for root: "C:\" or "/" - if (((i == 2) && (dirPath[1] ==':')) || (i == 0)) i++; + // If this character is a leading '/' (e.g. the '/' in "/usr") or + // part of a drive root (e.g. "C:\"), include it with the result. + if ((i == 0) || ((i == 2) && (dirPath[1] == ':'))) i += 1; + // If this character is a trailing path separator (e.g. the last + // '/' in "/usr/bin/" or the last '\' in "C:\raylib\"), continue. + else if (i == lastIndex) continue; - memcpy(prevDirPath, dirPath, i); - break; + if (!isFile) + { + memcpy(prevDirPath, dirPath, i); + break; + } + else isFile = false; } }