open.c 587 B

1234567891011121314151617181920212223242526272829303132
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <va_list.h>
  4. #include <errno.h>
  5. #include <syscall.h>
  6. DEFN_SYSCALL3(open, 2, const char *, int, int);
  7. int open(const char *name, int flags, ...) {
  8. va_list argp;
  9. int mode = 0;
  10. int result;
  11. va_start(argp, flags);
  12. if (flags & O_CREAT) mode = va_arg(argp, int);
  13. va_end(argp);
  14. result = syscall_open(name, flags, mode);
  15. if (result == -1) {
  16. /* Not sure this is necessary */
  17. if (flags & O_CREAT) {
  18. errno = EACCES;
  19. } else {
  20. errno = ENOENT;
  21. }
  22. } else if (result < 0) {
  23. errno = -result;
  24. result = -1;
  25. }
  26. return result;
  27. }