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, 5
14 .import SETLFS, OPEN, CLOSE
16 .import scratch, fnparse, fnaddmode, fncomplete, fnset
17 .import opencmdchannel, closecmdchannel, readdiskerror
18 .import fnunit, fnisfile
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 ; Read bit is set. Add an 'r' to the name
125 jsr fnaddmode ; Add the mode to the name
127 bne common ; Branch always
129 ; If O_TRUNC is set, scratch the file, but ignore any errors
137 ; Complete the the file name. Check for append mode here.
140 lda tmp3 ; Get the mode again
141 and #O_APPEND ; Append mode?
142 bne append ; Branch if yes
144 ; Setup the name for create mode
147 jsr fncomplete ; Add type and mode to the name
150 ; Append bit is set. Add an 'a' to the name
153 jsr fnaddmode ; Add open mode to file name
157 ; Common read/write code. Flags in A, handle in tmp2
160 jsr fnset ; Set the file name
166 ldy fnisfile ; Is this a standard file on disk?
167 beq nofile ; Branch if not
168 tay ; Use the LFN also as SA for files
169 nofile: ; ... else use SA=0 (read)
170 jsr SETLFS ; Set the file params
175 ; Open the the drive command channel and read it
182 bne closeandexit ; Branch on error
184 ; File is open. Mark it as open in the table
190 sta unittab,x ; Remember
192 ; Done. Return the handle in a/x
196 stx __oserror ; Clear _oserror