diff --git a/src/util/vfs.c b/src/util/vfs.c index 36e59f617..d761d06a2 100644 --- a/src/util/vfs.c +++ b/src/util/vfs.c @@ -5,6 +5,36 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "vfs.h" +struct VFile* VFileOpen(const char* path, int flags) { +#ifdef USE_VFS_FILE + const char* chflags; + switch (flags & O_ACCMODE) { + case O_WRONLY: + if (flags & O_APPEND) { + chflags = "ab"; + } else { + chflags = "wb"; + } + break; + case O_RDWR: + if (flags & O_APPEND) { + chflags = "a+b"; + } else if (flags & O_TRUNC) { + chflags = "w+b"; + } else { + chflags = "r+b"; + } + break; + case O_RDONLY: + chflags = "rb"; + break; + } + return VFileFOpen(path, chflags); +#else + return VFileOpenFD(path, flags); +#endif +} + ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) { ssize_t bytesRead = 0; while (bytesRead < size - 1) { diff --git a/src/util/vfs.h b/src/util/vfs.h index c614d7a57..6b43d29d0 100644 --- a/src/util/vfs.h +++ b/src/util/vfs.h @@ -53,6 +53,8 @@ struct VDir { }; struct VFile* VFileOpen(const char* path, int flags); + +struct VFile* VFileOpenFD(const char* path, int flags); struct VFile* VFileFOpen(const char* path, const char* mode); struct VFile* VFileFromFD(int fd); struct VFile* VFileFromMemory(void* mem, size_t size); diff --git a/src/util/vfs/vfs-fd.c b/src/util/vfs/vfs-fd.c index 9487f501f..8e7196028 100644 --- a/src/util/vfs/vfs-fd.c +++ b/src/util/vfs/vfs-fd.c @@ -29,7 +29,7 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size); static void _vfdTruncate(struct VFile* vf, size_t size); static ssize_t _vfdSize(struct VFile* vf); -struct VFile* VFileOpen(const char* path, int flags) { +struct VFile* VFileOpenFD(const char* path, int flags) { if (!path) { return 0; } diff --git a/src/util/vfs/vfs-file.c b/src/util/vfs/vfs-file.c index 4b794dcfb..a995aeccf 100644 --- a/src/util/vfs/vfs-file.c +++ b/src/util/vfs/vfs-file.c @@ -7,6 +7,7 @@ #include "util/memory.h" +#include #include struct VFileFILE { @@ -28,6 +29,9 @@ struct VFile* VFileFOpen(const char* path, const char* mode) { return 0; } FILE* file = fopen(path, mode); + if (!file && errno == ENOENT && strcmp(mode, "r+b") == 0) { + file = fopen(path, "w+b"); + } return VFileFromFILE(file); }