Pristine Ack-5.5
[Ack-5.5.git] / modules / src / system / open.c
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: open.c,v 1.6 1994/06/24 11:24:37 ceriel Exp $ */
6
7 #include <system.h>
8
9 extern File *_get_entry();
10
11 int
12 sys_open(path, flag, filep)
13         char *path;
14         int flag;
15         File **filep;
16 {
17         register int fd;
18         register File *fp;
19         long lseek();
20
21         if ((fp = _get_entry()) == (File *)0)
22                 return 0;
23         switch (flag) {
24         case OP_READ:
25                 if ((fd = open(path, 0)) < 0)
26                         return 0;
27                 break;
28         case OP_APPEND:
29                 if ((fd = open(path, 1)) < 0) {
30                         if (access(path, 0) == 0)
31                                 return 0;
32                 }
33                 else {
34                         if (lseek(fd, 0L, 2) < 0L) {
35                                 close(fd);
36                                 return 0;
37                         }
38                         break;
39                 }
40                 /* Fall through */
41         case OP_WRITE:
42                 if ((fd = creat(path, 0666)) < 0)
43                         return 0;
44                 break;
45         default:
46                 return 0;
47         }
48         fp->o_flags = flag;
49         fp->o_fd = fd;
50         *filep = fp;
51         return 1;
52 }