3 Copyright (C) 2001, 2002 Hans-Peter Nilsson
5 Permission to use, copy, modify, and distribute this software is
6 freely granted, provided that the above copyright notice, this notice
7 and the following disclaimer are preserved with no changes.
9 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
10 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 #include <sys/types.h>
19 #include "sys/syscall.h"
22 /* Let's keep the filehandle array here, since this is a primary
24 unsigned char _MMIX_allocated_filehandle
[32] =
30 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0
36 _open (const char *path
,
41 long append_contents
= 0;
42 unsigned long prev_contents_size
= 0;
43 char *prev_contents
= NULL
;
47 fileno
< (sizeof (_MMIX_allocated_filehandle
) /
48 sizeof (_MMIX_allocated_filehandle
[0]));
50 if (_MMIX_allocated_filehandle
[fileno
] == 0)
53 if (fileno
== (sizeof (_MMIX_allocated_filehandle
) /
54 sizeof (_MMIX_allocated_filehandle
[0])))
60 /* We map this to a fopen call. The flags parameter is stymied because
61 we don't support other than these flags. */
62 if (flags
& ~(O_RDONLY
| O_WRONLY
| O_RDWR
| O_CREAT
| O_APPEND
| O_TRUNC
))
64 UNIMPLEMENTED (("path: %s, flags: %d", path
, flags
));
69 if ((flags
& O_ACCMODE
) == O_RDONLY
)
71 else if ((flags
& (O_WRONLY
| O_APPEND
)) == (O_WRONLY
| O_APPEND
))
73 mode
= BinaryReadWrite
;
76 else if ((flags
& (O_RDWR
| O_APPEND
)) == (O_RDWR
| O_APPEND
))
78 mode
= BinaryReadWrite
;
81 else if ((flags
& (O_WRONLY
| O_CREAT
)) == (O_WRONLY
| O_CREAT
)
82 || (flags
& (O_WRONLY
| O_TRUNC
)) == (O_WRONLY
| O_TRUNC
))
84 else if ((flags
& (O_RDWR
| O_CREAT
)) == (O_RDWR
| O_CREAT
))
85 mode
= BinaryReadWrite
;
86 else if (flags
& O_RDWR
)
87 mode
= BinaryReadWrite
;
96 /* BinaryReadWrite is equal to "w+", so it truncates the file rather
97 than keeping the contents, as can be imagined if you're looking
98 for append functionality. The only way we can keep the contents
99 so we can append to it, is by first reading in and saving the
100 contents, then re-opening the file as BinaryReadWrite and write
101 the previous contents. This seems to work for the needs of
102 simple test-programs. */
103 long openexist
= TRAP3f (SYS_Fopen
, fileno
, path
, BinaryRead
);
106 /* Yes, this file exists, now opened, so let's read it and keep
107 the contents. Better have the memory around for this to
109 long seekval
= TRAP2f (SYS_Fseek
, fileno
, -1);
113 prev_contents_size
= TRAP1f (SYS_Ftell
, fileno
);
115 /* If the file has non-zero size, we have something to
117 if (prev_contents_size
!= 0)
119 /* Start reading from the beginning. Ignore the return
120 value from this call: we'll notice if we can't read
121 as much as we want. */
122 TRAP2f (SYS_Fseek
, fileno
, 0);
124 prev_contents
= malloc (prev_contents_size
);
125 if (prev_contents
!= 0)
127 /* I don't like the thought of trying to read the
128 whole file all at once, disregarding the size,
129 because the host system might not support that
130 and we'd get funky errors. Read in 32k at a
132 char *ptr
= prev_contents
;
133 unsigned long read_more
= prev_contents_size
;
134 unsigned long chunk_size
= 1 << 15;
136 while (read_more
>= chunk_size
)
139 = TRAP3f (SYS_Fread
, fileno
, ptr
, chunk_size
);
143 free (prev_contents
);
144 TRAP1f (SYS_Fclose
, fileno
);
148 read_more
-= chunk_size
;
155 = TRAP3f (SYS_Fread
, fileno
, ptr
, read_more
);
158 free (prev_contents
);
159 TRAP1f (SYS_Fclose
, fileno
);
167 /* Malloc of area to copy to failed. The glibc
168 manpage says its open can return ENOMEM due to
169 kernel memory failures, so let's do that too
178 /* Seek failed. Gotta be some I/O error. */
183 TRAP1f (SYS_Fclose
, fileno
);
187 ret
= TRAP3f (SYS_Fopen
, fileno
, path
, mode
);
190 /* It's totally unknown what the error was. We'll just take our
191 chances and assume ENOENT. */
196 if (prev_contents_size
!= 0)
198 /* Write out the previous contents, a chunk at a time. Leave the
199 file pointer at the end of the file. */
200 unsigned long write_more
= prev_contents_size
;
201 unsigned long chunk_size
= 1 << 15;
202 char *ptr
= prev_contents
;
204 while (write_more
>= chunk_size
)
207 = TRAP3f (SYS_Fwrite
, fileno
, ptr
, chunk_size
);
210 free (prev_contents
);
211 TRAP1f (SYS_Fclose
, fileno
);
215 write_more
-= chunk_size
;
221 = TRAP3f (SYS_Fwrite
, fileno
, ptr
, write_more
);
224 free (prev_contents
);
225 TRAP1f (SYS_Fclose
, fileno
);
231 free (prev_contents
);
234 _MMIX_allocated_filehandle
[fileno
] = 1;