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 ; Throw away any additional parameters passed through the ellipsis
62 dey ; Parm count < 4 shouldn't be needed to be...
63 dey ; ...checked (it generates a c compiler warning)
66 beq parmok ; Branch if parameter count ok
67 jsr addysp ; Fix stack, throw away unused parameters
69 ; Parameters ok. Pop the flags and save them into tmp3
71 parmok: jsr popax ; Get flags
74 ; Get the filename from stack and parse it. Bail out if is not ok
77 jsr fnparse ; Parse it
79 bne error ; Bail out if problem with name
81 ; Get a free file handle and remember it in tmp2
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?
95 bne invflags ; No: Invalid open mode
97 ; If O_TRUNC is set, scratch the file, but ignore any errors
104 ; Complete the the file name. Check for append mode here.
107 lda tmp3 ; Get the mode again
109 and #O_APPEND ; Append mode?
110 bne append ; Branch if yes
113 jsr fncomplete ; Add type and mode to the name
115 ; Setup the real open flags
120 ; Read bit is set. Add an 'r' to the name
123 jsr fnaddmode ; Add the mode to the name
126 ; Common read/write code. Flags in A, handle in tmp2
129 jsr fnset ; Set the file name
135 tay ; Use the LFN also as SA
136 jsr SETLFS ; Set the file params
141 ; Open the the drive command channel and read it
148 bne closeandexit ; Branch on error
150 ; File is open. Mark it as open in the table
156 sta unittab,x ; Remember
158 ; Done. Return the handle in a/x
164 ; Error entry: No more file handles
166 nofile: lda #1 ; Too many open files
168 ; Error entry. Error code is in A.
175 ; Error entry: Invalid flag parameter
184 ; Error entry: Close the file and exit
195 bne error ; Branch always