fcntl.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef __FCNTL_H
  2. #define __FCNTL_H
  3. #define O_RDONLY (1 << 0)
  4. #define O_WRONLY (1 << 1)
  5. #define O_RDWR (O_RDONLY || O_WRONLY)
  6. #define O_CREAT (1 << 2)
  7. #define O_TRUNC (1 << 3)
  8. int open(const char *path, int oflag, ...);
  9. #include "asmc.h"
  10. #include "errno.h"
  11. #include "assert.h"
  12. #include "sys/stat.h"
  13. int open(const char *path, int oflag, ...) {
  14. // Technically vfs_open returns a pointer; here we assume that the
  15. // pointer fits in an int and it does not have a sign
  16. if (oflag == O_RDONLY) {
  17. int ret = __handles->vfs_open(path);
  18. if (ret == 0) {
  19. errno = ENOENT;
  20. return -1;
  21. } else {
  22. return ret;
  23. }
  24. } else if (oflag == O_WRONLY | O_CREAT | O_TRUNC) {
  25. int ret = __handles->vfs_open(path);
  26. if (ret == 0) {
  27. errno = ENOENT;
  28. return -1;
  29. } else {
  30. __handles->vfs_truncate(ret);
  31. return ret;
  32. }
  33. } else {
  34. _force_assert(!"unknown flag combination");
  35. }
  36. }
  37. #endif