From 5dda105a79c5d258a3c1ae48bb35ce817540b632 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 25 Aug 2018 09:20:36 +0200 Subject: [PATCH 1/2] core: Support slashes as well in GetFileName & GetDirectoryPath Fixes #634. --- src/core.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core.c b/src/core.c index 7363a0c1..81ca2f58 100644 --- a/src/core.c +++ b/src/core.c @@ -1346,11 +1346,20 @@ const char *GetExtension(const char *fileName) return (dot + 1); } +/* "string pointer reverse break": return right-most occurrence of charset in s */ +static const char *strprbrk(const char *s, const char *charset) +{ + const char *latest_match = NULL; + for(; s = strpbrk(s, charset), s != NULL; latest_match=s++) + ; + return latest_match; +} + // Get pointer to filename for a path string const char *GetFileName(const char *filePath) { - const char *fileName = strrchr(filePath, '\\'); - + const char *fileName = strprbrk(filePath, "\\/"); + if (!fileName || fileName == filePath) return filePath; return fileName + 1; @@ -1360,14 +1369,14 @@ const char *GetFileName(const char *filePath) // Get directory for a given fileName (with path) const char *GetDirectoryPath(const char *fileName) { - char *lastSlash = NULL; + const char *lastSlash = NULL; static char filePath[256]; // MAX_DIRECTORY_PATH_SIZE = 256 memset(filePath, 0, 256); - - lastSlash = strrchr(fileName, '\\'); + + lastSlash = strprbrk(fileName, "\\/"); strncpy(filePath, fileName, strlen(fileName) - (strlen(lastSlash) - 1)); filePath[strlen(fileName) - strlen(lastSlash)] = '\0'; - + return filePath; } From 85213795d162ff03e48523afb854d6d6d17f27d4 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 25 Aug 2018 09:27:41 +0200 Subject: [PATCH 2/2] GetDirectoryPath: return NULL, don't crash when no slash Noted in #634. --- src/core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core.c b/src/core.c index 81ca2f58..ce510406 100644 --- a/src/core.c +++ b/src/core.c @@ -1374,6 +1374,9 @@ const char *GetDirectoryPath(const char *fileName) memset(filePath, 0, 256); lastSlash = strprbrk(fileName, "\\/"); + if (!lastSlash) + return NULL; + strncpy(filePath, fileName, strlen(fileName) - (strlen(lastSlash) - 1)); filePath[strlen(fileName) - strlen(lastSlash)] = '\0';