summaryrefslogtreecommitdiffhomepage
path: root/src/core.c
diff options
context:
space:
mode:
authorGil Barbosa Reis <[email protected]>2021-01-16 06:33:13 -0300
committerGitHub <[email protected]>2021-01-16 10:33:13 +0100
commit1866be047578fbe38e3e857ae047ce84d92870ee (patch)
treef5c6ac14cc048006db035afd65a21e27a86bd820 /src/core.c
parenteb7820b2b0832b2e624709cfae1a61606833ef62 (diff)
downloadraylib-1866be047578fbe38e3e857ae047ce84d92870ee.tar.gz
raylib-1866be047578fbe38e3e857ae047ce84d92870ee.zip
Fix absolute path handling in GetFileName and GetDirectoryPath (#1534) (#1535)
Diffstat (limited to 'src/core.c')
-rw-r--r--src/core.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/core.c b/src/core.c
index 15aab363..3cac4796 100644
--- a/src/core.c
+++ b/src/core.c
@@ -2353,7 +2353,7 @@ const char *GetFileName(const char *filePath)
const char *fileName = NULL;
if (filePath != NULL) fileName = strprbrk(filePath, "\\/");
- if (!fileName || (fileName == filePath)) return filePath;
+ if (!fileName) return filePath;
return fileName + 1;
}
@@ -2399,9 +2399,9 @@ const char *GetDirectoryPath(const char *filePath)
static char dirPath[MAX_FILEPATH_LENGTH];
memset(dirPath, 0, MAX_FILEPATH_LENGTH);
- // In case provided path does not contains a root drive letter (C:\, D:\),
+ // In case provided path does not contain a root drive letter (C:\, D:\) nor leading path separator (\, /),
// we add the current directory path to dirPath
- if (filePath[1] != ':')
+ if (filePath[1] != ':' && filePath[0] != '\\' && filePath[0] != '/')
{
// For security, we set starting path to current directory,
// obtained path will be concated to this
@@ -2412,9 +2412,18 @@ const char *GetDirectoryPath(const char *filePath)
lastSlash = strprbrk(filePath, "\\/");
if (lastSlash)
{
- // NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
- memcpy(dirPath + ((filePath[1] != ':')? 2 : 0), filePath, strlen(filePath) - (strlen(lastSlash) - 1));
- dirPath[strlen(filePath) - strlen(lastSlash) + ((filePath[1] != ':')? 2 : 0)] = '\0'; // Add '\0' manually
+ if (lastSlash == filePath)
+ {
+ // The last and only slash is the leading one: path is in a root directory
+ dirPath[0] = filePath[0];
+ dirPath[1] = '\0';
+ }
+ else
+ {
+ // NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
+ memcpy(dirPath + (filePath[1] != ':' && filePath[0] != '\\' && filePath[0] != '/' ? 2 : 0), filePath, strlen(filePath) - (strlen(lastSlash) - 1));
+ dirPath[strlen(filePath) - strlen(lastSlash) + (filePath[1] != ':' && filePath[0] != '\\' && filePath[0] != '/' ? 2 : 0)] = '\0'; // Add '\0' manually
+ }
}
return dirPath;