Skip to content

Commit

Permalink
do not mistake WAD-named directories for actual WAD files (#2118)
Browse files Browse the repository at this point in the history
* do not mistake WAD-named directories for actual WAD files

* explicitly check for files not dirs
  • Loading branch information
fabiangreffrath authored Jan 5, 2025
1 parent 22a1ee9 commit 7e39b8d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
14 changes: 2 additions & 12 deletions src/d_iwad.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,6 @@ static void CheckDOSDefaults(void)

#endif

// Returns true if the specified path is a path to a file
// of the specified name.

static boolean DirIsFile(const char *path, const char *filename)
{
return strchr(path, DIR_SEPARATOR) != NULL
&& !strcasecmp(M_BaseName(path), filename);
}

// Add IWAD directories parsed from splitting a path string containing
// paths separated by PATH_SEPARATOR. 'suffix' is a string to concatenate
// to the end of the paths before adding them.
Expand Down Expand Up @@ -620,7 +611,6 @@ void BuildIWADDirList(void)

char *D_FindWADByName(const char *name)
{
char *path;
char *probe;

// Absolute path?
Expand All @@ -643,15 +633,15 @@ char *D_FindWADByName(const char *name)
// file.

probe = M_FileCaseExists(*dir);
if (DirIsFile(*dir, name) && probe != NULL)
if (probe != NULL)
{
return probe;
}
free(probe);

// Construct a string for the full path

path = M_StringJoin(*dir, DIR_SEPARATOR_S, name);
char *path = M_StringJoin(*dir, DIR_SEPARATOR_S, name);

probe = M_FileCaseExists(path);
if (probe != NULL)
Expand Down
19 changes: 8 additions & 11 deletions src/m_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

// Check if a file exists

boolean M_FileExists(const char *filename)
static boolean M_FileExistsNotDir(const char *filename)
{
FILE *fstream;

Expand All @@ -45,14 +45,11 @@ boolean M_FileExists(const char *filename)
if (fstream != NULL)
{
fclose(fstream);
return true;
return M_DirExists(filename) == false;
}
else
{
// If we can't open because the file is a directory, the
// "file" exists at least!

return errno == EISDIR;
return false;
}
}

Expand Down Expand Up @@ -118,7 +115,7 @@ char *M_FileCaseExists(const char *path)
path_dup = M_StringDuplicate(path);

// 0: actual path
if (M_FileExists(path_dup))
if (M_FileExistsNotDir(path_dup))
{
return path_dup;
}
Expand All @@ -130,15 +127,15 @@ char *M_FileCaseExists(const char *path)
// 1: lowercase filename, e.g. doom2.wad
M_StringToLower(filename);

if (M_FileExists(path_dup))
if (M_FileExistsNotDir(path_dup))
{
return path_dup;
}

// 2: uppercase filename, e.g. DOOM2.WAD
M_StringToUpper(filename);

if (M_FileExists(path_dup))
if (M_FileExistsNotDir(path_dup))
{
return path_dup;
}
Expand All @@ -149,7 +146,7 @@ char *M_FileCaseExists(const char *path)
{
M_StringToLower(ext + 1);

if (M_FileExists(path_dup))
if (M_FileExistsNotDir(path_dup))
{
return path_dup;
}
Expand All @@ -160,7 +157,7 @@ char *M_FileCaseExists(const char *path)
{
M_StringToLower(filename + 1);

if (M_FileExists(path_dup))
if (M_FileExistsNotDir(path_dup))
{
return path_dup;
}
Expand Down
1 change: 0 additions & 1 deletion src/m_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "doomtype.h"

boolean M_FileExists(const char *file);
boolean M_DirExists(const char *path);
int M_FileLength(const char *path);
char *M_TempFile(const char *s);
Expand Down

0 comments on commit 7e39b8d

Please sign in to comment.