aboutsummaryrefslogtreecommitdiff
path: root/src/file_posix.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/file_posix.c')
-rw-r--r--src/file_posix.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/file_posix.c b/src/file_posix.c
index 0ffbf5e..e6915a5 100644
--- a/src/file_posix.c
+++ b/src/file_posix.c
@@ -3,6 +3,9 @@
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
static const bh_io_table_t bh_file_table = {
(int (*)(struct bh_io_s *, int)) bh_file_open_base,
@@ -88,23 +91,33 @@ int bh_file_open_base(bh_file_t *file,
return BH_INVALID;
/* Determine open mode */
- switch (mode & BH_IO_MASK)
+ switch (mode & (BH_IO_CREATE | BH_IO_OPEN))
{
- case BH_IO_OPEN: break;
- case BH_IO_CREATE: open_flags |= O_CREAT | O_EXCL; break;
- case BH_IO_APPEND: open_flags |= O_CREAT | O_APPEND; break;
- case BH_IO_TRUNCATE: open_flags |= O_CREAT | O_TRUNC; break;
-
default:
- return BH_NO_IMPL;
+ case 0: open_flags |= O_CREAT; break;
+ case BH_IO_CREATE: open_flags |= O_CREAT | O_EXCL; break;
+ case BH_IO_OPEN: /* Do nothing */ break;
}
+ if (mode & BH_IO_APPEND)
+ open_flags |= O_APPEND;
+
+ if (mode & BH_IO_TRUNCATE)
+ open_flags |= O_TRUNC;
+
/* Open file */
file->handle = open(file->path, open_flags, open_mode);
/* Check for errors */
if (file->handle == -1)
- return BH_ERROR;
+ {
+ switch (errno)
+ {
+ case EEXIST: return BH_FOUND;
+ case ENOENT: return BH_NOT_FOUND;
+ default: return BH_ERROR;
+ }
+ }
return BH_OK;
}
@@ -210,7 +223,7 @@ int bh_file_seek_base(bh_file_t *file,
return BH_ERROR;
/* Seek to desired location */
- if (lseek(file->handle, pos, dir) == -1);
+ if (lseek(file->handle, pos, dir) == -1)
return BH_ERROR;
return BH_OK;