split dirname and basename at both types of slashes on Windows (#439)

User-supplied path names may still contain both types of slashes, i.e. forward and backward slashes.

Fixes #437
This commit is contained in:
Fabian Greffrath 2022-02-02 08:38:44 +01:00 committed by GitHub
parent 9b138df643
commit 2eb9a183be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -189,15 +189,21 @@ boolean M_StrToInt(const char *str, int *result)
char *M_DirName(const char *path)
{
char *p, *result;
char *pf, *pb, *result;
p = strrchr(path, DIR_SEPARATOR);
if (p == NULL)
pf = strrchr(path, '/');
#ifdef _WIN32
pb = strrchr(path, '\\');
#else
pb = NULL;
#endif
if (pf == NULL && pb == NULL)
{
return M_StringDuplicate(".");
}
else
{
char *p = MAX(pf, pb);
result = M_StringDuplicate(path);
result[p - path] = '\0';
return result;
@ -210,15 +216,21 @@ char *M_DirName(const char *path)
const char *M_BaseName(const char *path)
{
const char *p;
const char *pf, *pb;
p = strrchr(path, DIR_SEPARATOR);
if (p == NULL)
pf = strrchr(path, '/');
#ifdef _WIN32
pb = strrchr(path, '\\');
#else
pb = NULL;
#endif
if (pf == NULL && pb == NULL)
{
return path;
}
else
{
const char *p = MAX(pf, pb);
return p + 1;
}
}