open.c 808 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. #include "system.h"
  7. extern File *_get_entry();
  8. int sys_open(char *path, int flag, File **filep)
  9. {
  10. int fd;
  11. File *fp;
  12. if ((fp = _get_entry()) == (File *)0)
  13. return 0;
  14. switch (flag) {
  15. case OP_READ:
  16. if ((fd = open(path, 0)) < 0)
  17. return 0;
  18. break;
  19. case OP_APPEND:
  20. if ((fd = open(path, 1)) < 0) {
  21. if (access(path, 0) == 0)
  22. return 0;
  23. }
  24. else {
  25. if (lseek(fd, 0L, 2) < 0L) {
  26. close(fd);
  27. return 0;
  28. }
  29. break;
  30. }
  31. /* Fall through */
  32. case OP_WRITE:
  33. if ((fd = creat(path, 0666)) < 0)
  34. return 0;
  35. break;
  36. default:
  37. return 0;
  38. }
  39. fp->o_flags = flag;
  40. fp->o_fd = fd;
  41. *filep = fp;
  42. return 1;
  43. }