2002-11-16 23:45:15 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 16.11.2002
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
; int open (const char* name, int flags, ...); /* May take a mode argument */
|
2002-12-03 21:44:58 +00:00
|
|
|
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.export _open
|
2011-01-28 10:55:03 +00:00
|
|
|
.destructor closeallfiles, 5
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.import addysp, popax
|
2003-03-13 23:09:53 +00:00
|
|
|
.import scratch, fnparse, fnaddmode, fncomplete, fnset
|
2002-11-17 22:45:55 +00:00
|
|
|
.import opencmdchannel, closecmdchannel, readdiskerror
|
2012-05-29 20:52:18 +00:00
|
|
|
.import fnunit, fnisfile
|
2002-12-03 21:44:58 +00:00
|
|
|
.import _close
|
2002-11-16 23:45:15 +00:00
|
|
|
.importzp sp, tmp2, tmp3
|
|
|
|
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "fcntl.inc"
|
|
|
|
.include "filedes.inc"
|
2018-07-03 19:47:42 +00:00
|
|
|
.include "cbm.inc"
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
2002-12-03 21:44:58 +00:00
|
|
|
; closeallfiles: Close all open files.
|
|
|
|
|
|
|
|
.proc closeallfiles
|
|
|
|
|
2004-08-09 10:38:12 +00:00
|
|
|
ldx #MAX_FDS-1
|
|
|
|
loop: lda fdtab,x
|
2002-12-03 21:44:58 +00:00
|
|
|
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
|
2004-08-09 10:38:12 +00:00
|
|
|
bpl loop
|
2002-12-03 21:44:58 +00:00
|
|
|
|
|
|
|
rts
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
; _open
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.proc _open
|
|
|
|
|
2005-04-03 18:04:53 +00:00
|
|
|
; Throw away any additional parameters passed through the ellipsis
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
dey ; Parm count < 4 shouldn't be needed to be...
|
|
|
|
dey ; ...checked (it generates a c compiler warning)
|
2005-04-03 18:04:53 +00:00
|
|
|
dey
|
2013-05-09 11:56:54 +00:00
|
|
|
dey
|
|
|
|
beq parmok ; Branch if parameter count ok
|
|
|
|
jsr addysp ; Fix stack, throw away unused parameters
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Parameters ok. Pop the flags and save them into tmp3
|
|
|
|
|
|
|
|
parmok: jsr popax ; Get flags
|
|
|
|
sta tmp3
|
|
|
|
|
|
|
|
; Get the filename from stack and parse it. Bail out if is not ok
|
|
|
|
|
|
|
|
jsr popax ; Get name
|
|
|
|
jsr fnparse ; Parse it
|
2012-07-15 12:30:43 +00:00
|
|
|
tax
|
2010-06-10 18:10:53 +00:00
|
|
|
bne oserror ; Bail out if problem with name
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Get a free file handle and remember it in tmp2
|
|
|
|
|
|
|
|
jsr freefd
|
2010-06-10 18:10:53 +00:00
|
|
|
lda #EMFILE ; Load error code
|
|
|
|
bcs seterrno ; Jump in case of errors
|
2002-11-16 23:45:15 +00:00
|
|
|
stx tmp2
|
|
|
|
|
2002-11-19 14:27:06 +00:00
|
|
|
; Check the flags. We cannot have both, read and write flags set, and we cannot
|
|
|
|
; open a file for writing without creating it.
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
lda tmp3
|
2002-11-19 14:27:06 +00:00
|
|
|
and #(O_RDWR | O_CREAT)
|
|
|
|
cmp #O_RDONLY ; Open for reading?
|
|
|
|
beq doread ; Yes: Branch
|
|
|
|
cmp #(O_WRONLY | O_CREAT) ; Open for writing?
|
2012-07-15 12:30:43 +00:00
|
|
|
beq dowrite
|
2010-06-10 18:10:53 +00:00
|
|
|
|
|
|
|
; Invalid open mode
|
|
|
|
|
2012-08-06 18:45:26 +00:00
|
|
|
lda #EINVAL
|
2010-06-10 18:10:53 +00:00
|
|
|
|
2022-08-28 19:52:53 +00:00
|
|
|
; Error entry. Sets _errno, clears __oserror, returns -1
|
2010-06-10 18:10:53 +00:00
|
|
|
|
2011-01-28 10:55:03 +00:00
|
|
|
seterrno:
|
2022-08-28 19:52:53 +00:00
|
|
|
jmp ___directerrno
|
2010-06-10 18:10:53 +00:00
|
|
|
|
|
|
|
; Error entry: Close the file and exit. OS error code is in A on entry
|
|
|
|
|
|
|
|
closeandexit:
|
|
|
|
pha
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #LFN_OFFS
|
|
|
|
jsr CLOSE
|
|
|
|
ldx fnunit
|
|
|
|
jsr closecmdchannel
|
|
|
|
pla
|
|
|
|
|
|
|
|
; Error entry: Set oserror and errno using error code in A and return -1
|
|
|
|
|
2022-08-28 19:52:53 +00:00
|
|
|
oserror:jmp ___mappederrno
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-07-15 12:30:43 +00:00
|
|
|
; Read bit is set. Add an 'r' to the name
|
|
|
|
|
2012-08-06 18:45:26 +00:00
|
|
|
doread: lda #'r'
|
2012-07-15 12:30:43 +00:00
|
|
|
jsr fnaddmode ; Add the mode to the name
|
2012-08-06 18:45:26 +00:00
|
|
|
lda #LFN_READ
|
2012-07-15 12:30:43 +00:00
|
|
|
bne common ; Branch always
|
|
|
|
|
2002-11-16 23:45:15 +00:00
|
|
|
; If O_TRUNC is set, scratch the file, but ignore any errors
|
|
|
|
|
2012-07-15 12:30:43 +00:00
|
|
|
dowrite:
|
2002-11-16 23:45:15 +00:00
|
|
|
lda tmp3
|
|
|
|
and #O_TRUNC
|
|
|
|
beq notrunc
|
|
|
|
jsr scratch
|
|
|
|
|
2022-02-21 20:44:31 +00:00
|
|
|
; Complete the file name. Check for append mode here.
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
notrunc:
|
2002-11-19 14:27:06 +00:00
|
|
|
lda tmp3 ; Get the mode again
|
|
|
|
and #O_APPEND ; Append mode?
|
|
|
|
bne append ; Branch if yes
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-07-15 12:30:43 +00:00
|
|
|
; Setup the name for create mode
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-07-15 12:30:43 +00:00
|
|
|
lda #'w'
|
|
|
|
jsr fncomplete ; Add type and mode to the name
|
|
|
|
jmp appendcreate
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-07-15 12:30:43 +00:00
|
|
|
; Append bit is set. Add an 'a' to the name
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-07-15 12:30:43 +00:00
|
|
|
append: lda #'a'
|
|
|
|
jsr fnaddmode ; Add open mode to file name
|
|
|
|
appendcreate:
|
|
|
|
lda #LFN_WRITE
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Common read/write code. Flags in A, handle in tmp2
|
|
|
|
|
|
|
|
common: sta tmp3
|
|
|
|
jsr fnset ; Set the file name
|
|
|
|
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #LFN_OFFS
|
|
|
|
ldx fnunit
|
2012-05-29 20:52:18 +00:00
|
|
|
ldy fnisfile ; Is this a standard file on disk?
|
|
|
|
beq nofile ; Branch if not
|
|
|
|
tay ; Use the LFN also as SA for files
|
|
|
|
nofile: ; ... else use SA=0 (read)
|
2002-11-16 23:45:15 +00:00
|
|
|
jsr SETLFS ; Set the file params
|
|
|
|
|
|
|
|
jsr OPEN
|
2010-06-10 18:10:53 +00:00
|
|
|
bcs oserror
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2022-02-21 20:44:31 +00:00
|
|
|
; Open the drive command channel and read it
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
ldx fnunit
|
2002-11-17 22:45:55 +00:00
|
|
|
jsr opencmdchannel
|
|
|
|
bne closeandexit
|
|
|
|
ldx fnunit
|
|
|
|
jsr readdiskerror
|
|
|
|
bne closeandexit ; Branch on error
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; File is open. Mark it as open in the table
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
ldx tmp2
|
2002-11-16 23:45:15 +00:00
|
|
|
lda tmp3
|
|
|
|
sta fdtab,x
|
|
|
|
lda fnunit
|
|
|
|
sta unittab,x ; Remember
|
|
|
|
|
|
|
|
; Done. Return the handle in a/x
|
|
|
|
|
|
|
|
txa ; Handle
|
|
|
|
ldx #0
|
2022-08-28 20:37:33 +00:00
|
|
|
stx ___oserror ; Clear __oserror
|
2002-11-16 23:45:15 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|