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
21 .importzp sp, tmp2, tmp3
25 .include "filedes.inc"
28 ;--------------------------------------------------------------------------
29 ; closeallfiles: Close all open files.
35 beq next ; Skip unused entries
40 pha ; Save current value of X
55 ;--------------------------------------------------------------------------
60 cpy #4 ; correct # of arguments (bytes)?
61 beq parmok ; parameter count ok
62 tya ; parm count < 4 shouldn't be needed to be...
63 sec ; ...checked (it generates a c compiler warning)
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 error ; Bail out if problem with name
80 ; Get a free file handle and remember it in tmp2
86 ; Check the flags. We cannot have both, read and write flags set, and we cannot
87 ; open a file for writing without creating it.
90 and #(O_RDWR | O_CREAT)
91 cmp #O_RDONLY ; Open for reading?
92 beq doread ; Yes: Branch
93 cmp #(O_WRONLY | O_CREAT) ; Open for writing?
94 bne invflags ; No: Invalid open mode
96 ; If O_TRUNC is set, scratch the file, but ignore any errors
103 ; Complete the the file name. Check for append mode here.
106 lda tmp3 ; Get the mode again
108 and #O_APPEND ; Append mode?
109 bne append ; Branch if yes
112 jsr fncomplete ; Add type and mode to the name
114 ; Setup the real open flags
119 ; Read bit is set. Add an 'r' to the name
122 jsr fnaddmode ; Add the mode to the name
125 ; Common read/write code. Flags in A, handle in tmp2
128 jsr fnset ; Set the file name
134 tay ; Use the LFN also as SA
135 jsr SETLFS ; Set the file params
140 ; Open the the drive command channel and read it
147 bne closeandexit ; Branch on error
149 ; File is open. Mark it as open in the table
155 sta unittab,x ; Remember
157 ; Done. Return the handle in a/x
163 ; Error entry: No more file handles
165 nofile: lda #1 ; Too many open files
167 ; Error entry. Error code is in A.
174 ; Error entry: Invalid flag parameter
183 ; Error entry: Close the file and exit
194 bne error ; Branch always