2 ; Ullrich von Bassewitz, 16.11.2002
4 ; int open (const char* name, int flags, ...); /* May take a mode argument */
6 ; Be sure to keep the value priority of closeallfiles lower than that of
7 ; closeallstreams (which is the high level C file I/O counterpart and must be
8 ; called before closeallfiles).
12 .destructor closeallfiles, 17
14 .import SETLFS, OPEN, CLOSE
16 .import scratch, fnparse, fnaddmode, fncomplete, fnset
17 .import opencmdchannel, closecmdchannel, readdiskerror
20 .importzp sp, tmp2, tmp3
24 .include "filedes.inc"
27 ;--------------------------------------------------------------------------
28 ; closeallfiles: Close all open files.
34 beq next ; Skip unused entries
39 pha ; Save current value of X
54 ;--------------------------------------------------------------------------
59 ; Throw away any additional parameters passed through the ellipsis
61 dey ; Parm count < 4 shouldn't be needed to be...
62 dey ; ...checked (it generates a c compiler warning)
65 beq parmok ; Branch if parameter count ok
66 jsr addysp ; Fix stack, throw away unused parameters
68 ; Parameters ok. Pop the flags and save them into tmp3
70 parmok: jsr popax ; Get flags
73 ; Get the filename from stack and parse it. Bail out if is not ok
76 jsr fnparse ; Parse it
78 bne oserror ; Bail out if problem with name
80 ; Get a free file handle and remember it in tmp2
83 lda #EMFILE ; Load error code
84 bcs seterrno ; Jump in case of errors
87 ; Check the flags. We cannot have both, read and write flags set, and we cannot
88 ; open a file for writing without creating it.
91 and #(O_RDWR | O_CREAT)
92 cmp #O_RDONLY ; Open for reading?
93 beq doread ; Yes: Branch
94 cmp #(O_WRONLY | O_CREAT) ; Open for writing?
101 ; Error entry. Sets _errno, clears _oserror, returns -1
106 ; Error entry: Close the file and exit. OS error code is in A on entry
118 ; Error entry: Set oserror and errno using error code in A and return -1
120 oserror:jmp __mappederrno
122 ; If O_TRUNC is set, scratch the file, but ignore any errors
130 ; Complete the the file name. Check for append mode here.
133 lda tmp3 ; Get the mode again
135 and #O_APPEND ; Append mode?
136 bne append ; Branch if yes
139 jsr fncomplete ; Add type and mode to the name
141 ; Setup the real open flags
146 ; Read bit is set. Add an 'r' to the name
149 jsr fnaddmode ; Add the mode to the name
152 ; Common read/write code. Flags in A, handle in tmp2
155 jsr fnset ; Set the file name
161 tay ; Use the LFN also as SA
162 jsr SETLFS ; Set the file params
167 ; Open the the drive command channel and read it
174 bne closeandexit ; Branch on error
176 ; File is open. Mark it as open in the table
182 sta unittab,x ; Remember
184 ; Done. Return the handle in a/x
188 stx __oserror ; Clear _oserror