1
0

fopen.c 689 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "stdio_impl.h"
  2. FILE *fopen(const char *filename, const char *mode)
  3. {
  4. FILE *f;
  5. int fd;
  6. int flags;
  7. int plus = !!strchr(mode, '+');
  8. /* Check for valid initial mode character */
  9. if (!strchr("rwa", *mode)) {
  10. errno = EINVAL;
  11. return 0;
  12. }
  13. /* Compute the flags to pass to open() */
  14. if (plus) flags = O_RDWR;
  15. else if (*mode == 'r') flags = O_RDONLY;
  16. else flags = O_WRONLY;
  17. if (*mode != 'r') flags |= O_CREAT;
  18. if (*mode == 'w') flags |= O_TRUNC;
  19. if (*mode == 'a') flags |= O_APPEND;
  20. fd = syscall_cp(SYS_open, filename, flags|O_LARGEFILE, 0666);
  21. if (fd < 0) return 0;
  22. f = __fdopen(fd, mode);
  23. if (f) return f;
  24. __syscall(SYS_close, fd);
  25. return 0;
  26. }
  27. LFS64(fopen);