Skip to content

Commit 46774a8

Browse files
committed
REVIEWED: GetDirectoryPath()
1 parent f28c1ef commit 46774a8

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

src/core.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,16 +1941,31 @@ const char *GetFileNameWithoutExt(const char *filePath)
19411941
// Get directory for a given filePath
19421942
const char *GetDirectoryPath(const char *filePath)
19431943
{
1944+
/*
1945+
// NOTE: Directory separator is different in Windows and other platforms,
1946+
// fortunately, Windows also support the '/' separator, that's the one should be used
1947+
#if defined(_WIN32)
1948+
char separator = '\\';
1949+
#else
1950+
char separator = '/';
1951+
#endif
1952+
*/
19441953
const char *lastSlash = NULL;
19451954
static char dirPath[MAX_FILEPATH_LENGTH];
19461955
memset(dirPath, 0, MAX_FILEPATH_LENGTH);
1956+
1957+
// For security, we set starting path to current directory,
1958+
// obtained path will be concated to this
1959+
dirPath[0] = '.';
1960+
dirPath[1] = '/';
19471961

19481962
lastSlash = strprbrk(filePath, "\\/");
1949-
if (!lastSlash) return NULL;
1950-
1951-
// NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
1952-
strncpy(dirPath, filePath, strlen(filePath) - (strlen(lastSlash) - 1));
1953-
dirPath[strlen(filePath) - strlen(lastSlash)] = '\0'; // Add '\0' manually
1963+
if (lastSlash)
1964+
{
1965+
// NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
1966+
strncpy(dirPath + 2, filePath, strlen(filePath) - (strlen(lastSlash) - 1));
1967+
dirPath[2 + strlen(filePath) - strlen(lastSlash)] = '\0'; // Add '\0' manually
1968+
}
19541969

19551970
return dirPath;
19561971
}

0 commit comments

Comments
 (0)