2 ; Ullrich von Bassewitz, 16.11.2002
4 ; int open (const char* name, int flags, ...); /* May take a mode argument */
8 .destructor closeallfiles, 5
11 .import scratch, fnparse, fnaddmode, fncomplete, fnset
12 .import opencmdchannel, closecmdchannel, readdiskerror
13 .import fnunit, fnisfile
15 .importzp sp, tmp2, tmp3
19 .include "filedes.inc"
23 ;--------------------------------------------------------------------------
24 ; closeallfiles: Close all open files.
30 beq next ; Skip unused entries
35 pha ; Save current value of X
50 ;--------------------------------------------------------------------------
55 ; Throw away any additional parameters passed through the ellipsis
57 dey ; Parm count < 4 shouldn't be needed to be...
58 dey ; ...checked (it generates a c compiler warning)
61 beq parmok ; Branch if parameter count ok
62 jsr addysp ; Fix stack, throw away unused parameters
64 ; Parameters ok. Pop the flags and save them into tmp3
66 parmok: jsr popax ; Get flags
69 ; Get the filename from stack and parse it. Bail out if is not ok
72 jsr fnparse ; Parse it
74 bne oserror ; Bail out if problem with name
76 ; Get a free file handle and remember it in tmp2
79 lda #EMFILE ; Load error code
80 bcs seterrno ; Jump in case of errors
83 ; Check the flags. We cannot have both, read and write flags set, and we cannot
84 ; open a file for writing without creating it.
87 and #(O_RDWR | O_CREAT)
88 cmp #O_RDONLY ; Open for reading?
89 beq doread ; Yes: Branch
90 cmp #(O_WRONLY | O_CREAT) ; Open for writing?
97 ; Error entry. Sets _errno, clears _oserror, returns -1
102 ; Error entry: Close the file and exit. OS error code is in A on entry
114 ; Error entry: Set oserror and errno using error code in A and return -1
116 oserror:jmp __mappederrno
118 ; Read bit is set. Add an 'r' to the name
121 jsr fnaddmode ; Add the mode to the name
123 bne common ; Branch always
125 ; If O_TRUNC is set, scratch the file, but ignore any errors
133 ; Complete the the file name. Check for append mode here.
136 lda tmp3 ; Get the mode again
137 and #O_APPEND ; Append mode?
138 bne append ; Branch if yes
140 ; Setup the name for create mode
143 jsr fncomplete ; Add type and mode to the name
146 ; Append bit is set. Add an 'a' to the name
149 jsr fnaddmode ; Add open mode to file name
153 ; Common read/write code. Flags in A, handle in tmp2
156 jsr fnset ; Set the file name
162 ldy fnisfile ; Is this a standard file on disk?
163 beq nofile ; Branch if not
164 tay ; Use the LFN also as SA for files
165 nofile: ; ... else use SA=0 (read)
166 jsr SETLFS ; Set the file params
171 ; Open the the drive command channel and read it
178 bne closeandexit ; Branch on error
180 ; File is open. Mark it as open in the table
186 sta unittab,x ; Remember
188 ; Done. Return the handle in a/x
192 stx __oserror ; Clear _oserror