Add fopenfile, alternative to fopen that does not ever open directories

This commit is contained in:
James R 2022-10-14 21:56:01 -07:00
parent e08ebac5c8
commit 3bd3369fdc
2 changed files with 36 additions and 0 deletions

View file

@ -104,6 +104,8 @@
#include <io.h>
#endif
FILE *fopenfile(const char*, const char*);
//#define NOMD5
// Uncheck this to compile debugging code

View file

@ -39,6 +39,7 @@
#define SUFFIX "*"
#define SLASH "\\"
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#ifndef INVALID_FILE_ATTRIBUTES
@ -307,6 +308,39 @@ closedir (DIR * dirp)
}
#endif
// fopen but it REALLY only works on regular files
// Turns out, on linux, anyway, you can fopen directories
// in read mode. (It's supposed to fail in write mode
// though!!)
FILE *fopenfile(const char *path, const char *mode)
{
FILE *h = fopen(path, mode);
if (h != NULL)
{
struct stat st;
int eno;
if (fstat(fileno(h), &st) == -1)
{
eno = errno;
}
else if (!S_ISREG(st.st_mode))
{
eno = EACCES; // set some kinda error
}
else
{
return h; // ok
}
fclose(h);
errno = eno;
}
return NULL;
}
static CV_PossibleValue_t addons_cons_t[] = {{0, "Default"},
#if 1
{1, "HOME"}, {2, "SRB2"},