;
; int open (const char* name, int flags, ...); /* May take a mode argument */
;
+; Be sure to keep the value priority of closeallfiles lower than that of
+; closeallstreams (which is the high level C file I/O counterpart and must be
+; called before closeallfiles).
+
.export _open
+ .destructor closeallfiles, 17
.import SETLFS, OPEN, CLOSE
.import addysp, popax
- .import scratch, fnparse, fncomplete, fnset
+ .import scratch, fnparse, fnaddmode, fncomplete, fnset
.import opencmdchannel, closecmdchannel, readdiskerror
.import __oserror
.import fnunit
+ .import _close
.importzp sp, tmp2, tmp3
.include "errno.inc"
;--------------------------------------------------------------------------
-; initstdout: Open the stdout and stderr file descriptors for the screen.
+; closeallfiles: Close all open files.
+
+.proc closeallfiles
+
+ ldx #MAX_FDS-1
+loop: lda fdtab,x
+ beq next ; Skip unused entries
+
+; Close this file
+
+ txa
+ pha ; Save current value of X
+ ldx #0
+ jsr _close
+ pla
+ tax
+
+; Next file
+
+next: dex
+ bpl loop
+
+ rts
+
+.endproc
+
+;--------------------------------------------------------------------------
+; _open
.proc _open
- cpy #4 ; correct # of arguments (bytes)?
- beq parmok ; parameter count ok
- tya ; parm count < 4 shouldn't be needed to be...
- sec ; ...checked (it generates a c compiler warning)
- sbc #4
- tay
- jsr addysp ; fix stack, throw away unused parameters
+; Throw away any additional parameters passed through the ellipsis
+
+ dey ; Parm count < 4 shouldn't be needed to be...
+ dey ; ...checked (it generates a c compiler warning)
+ dey
+ dey
+ beq parmok ; Branch if parameter count ok
+ jsr addysp ; Fix stack, throw away unused parameters
; Parameters ok. Pop the flags and save them into tmp3
bne append ; Branch if yes
ldx #'w'
append: txa
- jsr fncomplete
+ jsr fncomplete ; Add type and mode to the name
; Setup the real open flags
; Read bit is set. Add an 'r' to the name
doread: lda #'r'
- jsr fncomplete
+ jsr fnaddmode ; Add the mode to the name
lda #LFN_READ
; Common read/write code. Flags in A, handle in tmp2