open.c 860 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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
  9. sys_open(path, flag, filep)
  10. char *path;
  11. int flag;
  12. File **filep;
  13. {
  14. register int fd;
  15. register File *fp;
  16. long lseek();
  17. if ((fp = _get_entry()) == (File *)0)
  18. return 0;
  19. switch (flag) {
  20. case OP_READ:
  21. if ((fd = open(path, 0)) < 0)
  22. return 0;
  23. break;
  24. case OP_APPEND:
  25. if ((fd = open(path, 1)) < 0) {
  26. if (access(path, 0) == 0)
  27. return 0;
  28. }
  29. else {
  30. if (lseek(fd, 0L, 2) < 0L) {
  31. close(fd);
  32. return 0;
  33. }
  34. break;
  35. }
  36. /* Fall through */
  37. case OP_WRITE:
  38. if ((fd = creat(path, 0666)) < 0)
  39. return 0;
  40. break;
  41. default:
  42. return 0;
  43. }
  44. fp->o_flags = flag;
  45. fp->o_fd = fd;
  46. *filep = fp;
  47. return 1;
  48. }