2002-11-16 23:45:15 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 16.11.2002
|
|
|
|
;
|
|
|
|
; int open (const char* name, int flags, ...); /* May take a mode argument */
|
|
|
|
;
|
|
|
|
|
|
|
|
.export _open
|
|
|
|
|
2002-11-19 23:02:47 +00:00
|
|
|
.import SETLFS, OPEN, CLOSE
|
2002-11-16 23:45:15 +00:00
|
|
|
.import addysp, popax
|
|
|
|
.import scratch, fnparse, fncomplete, fnset
|
2002-11-17 22:45:55 +00:00
|
|
|
.import opencmdchannel, closecmdchannel, readdiskerror
|
2002-11-16 23:45:15 +00:00
|
|
|
.import __errno, __oserror
|
|
|
|
.import fnunit
|
|
|
|
.importzp sp, tmp2, tmp3
|
|
|
|
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "fcntl.inc"
|
|
|
|
.include "filedes.inc"
|
|
|
|
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
; initstdout: Open the stdout and stderr file descriptors for the screen.
|
|
|
|
|
|
|
|
.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
|
|
|
|
|
|
|
|
; 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
|
|
|
|
cmp #0
|
|
|
|
bne error ; Bail out if problem with name
|
|
|
|
|
|
|
|
; Get a free file handle and remember it in tmp2
|
|
|
|
|
|
|
|
jsr freefd
|
|
|
|
bcs nofile
|
|
|
|
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?
|
|
|
|
bne invflags ; No: Invalid open mode
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; If O_TRUNC is set, scratch the file, but ignore any errors
|
|
|
|
|
|
|
|
lda tmp3
|
|
|
|
and #O_TRUNC
|
|
|
|
beq notrunc
|
|
|
|
jsr scratch
|
|
|
|
|
2002-11-19 14:27:06 +00:00
|
|
|
; Complete the 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
|
|
|
|
ldx #'a'
|
|
|
|
and #O_APPEND ; Append mode?
|
|
|
|
bne append ; Branch if yes
|
|
|
|
ldx #'w'
|
|
|
|
append: txa
|
2002-11-16 23:45:15 +00:00
|
|
|
jsr fncomplete
|
|
|
|
|
|
|
|
; Setup the real open flags
|
|
|
|
|
|
|
|
lda #LFN_WRITE
|
|
|
|
bne common
|
|
|
|
|
|
|
|
; Read bit is set. Add an 'r' to the name
|
|
|
|
|
|
|
|
doread: lda #'r'
|
|
|
|
jsr fncomplete
|
|
|
|
lda #LFN_READ
|
|
|
|
|
|
|
|
; 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
|
|
|
|
tay ; Use the LFN also as SA
|
|
|
|
jsr SETLFS ; Set the file params
|
|
|
|
|
|
|
|
jsr OPEN
|
|
|
|
bcs error
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
; Open the 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
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Error entry: No more file handles
|
|
|
|
|
|
|
|
nofile: lda #1 ; Too many open files
|
|
|
|
|
|
|
|
; Error entry. Error code is in A.
|
|
|
|
|
|
|
|
error: sta __oserror
|
|
|
|
errout: lda #$FF
|
|
|
|
tax ; Return -1
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Error entry: Invalid flag parameter
|
|
|
|
|
|
|
|
invflags:
|
|
|
|
lda #EINVAL
|
|
|
|
sta __errno
|
|
|
|
lda #0
|
|
|
|
sta __errno+1
|
|
|
|
beq errout
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
; Error entry: Close the file and exit
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
closeandexit:
|
|
|
|
pha
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #LFN_OFFS
|
|
|
|
jsr CLOSE
|
|
|
|
ldx fnunit
|
|
|
|
jsr closecmdchannel
|
|
|
|
pla
|
|
|
|
bne error ; Branch always
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|