mirror of
https://git.do.srb2.org/STJr/SRB2.git
synced 2024-11-15 09:11:48 +00:00
Add fopenfile, alternative to fopen that does not ever open directories
This commit is contained in:
parent
e08ebac5c8
commit
3bd3369fdc
2 changed files with 36 additions and 0 deletions
|
@ -104,6 +104,8 @@
|
|||
#include <io.h>
|
||||
#endif
|
||||
|
||||
FILE *fopenfile(const char*, const char*);
|
||||
|
||||
//#define NOMD5
|
||||
|
||||
// Uncheck this to compile debugging code
|
||||
|
|
|
@ -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"},
|
||||
|
|
Loading…
Reference in a new issue