-.proc _open
-
- cpy #4 ; correct # of arguments (bytes)?
- beq parmok ; parameter count ok
- tya ; parm count < 4 shouldn't be needed to be checked
- sec ; (it generates a c compiler warning)
- sbc #4
- tay
- jsr addysp ; fix stack, throw away unused parameters
-
-parmok: jsr findfreeiocb
- beq iocbok ; we found one
-
- lda #<EMFILE ; "too many open files"
- ldx #>EMFILE
-seterr: jsr __seterrno
- jsr incsp4 ; clean up stack
- lda #$FF
- tax
- rts ; return -1
-
- ; process the mode argument
- ; @@@TODO: append not handled yet!
-
-iocbok: stx tmp4
- jsr clriocb ; init with zero
- ldy #1
- jsr ldaxysp ; get mode
- ldx tmp4
- cmp #O_RDONLY
- bne l1
- lda #OPNIN
-set: sta ICAX1,x
- bne cont
-
-l1: cmp #O_WRONLY
- bne l2
- lda #OPNOT
- bne set
-
-l2: ; O_RDWR
- lda #OPNOT|OPNIN
- bne set
-
- ; process the filename argument
-
-cont: ldy #3
- jsr ldaxysp
-
-.ifdef UCASE_FILENAME
-
- jsr ucase_fn
- bcc ucok1
- lda #<EINVAL ; file name is too long
- ldx #>EINVAL
- jmp seterr
+.proc _open
+
+ dey ; parm count < 4 shouldn't be needed to be checked
+ dey ; (it generates a c compiler warning)
+ dey
+ dey
+ beq parmok ; parameter count ok
+ jsr addysp ; fix stack, throw away unused parameters
+
+parmok: jsr findfreeiocb
+ beq iocbok ; we found one
+
+ lda #<EMFILE ; "too many open files"
+seterr: jsr __directerrno
+ jsr incsp4 ; clean up stack
+ lda #$FF
+ tax
+ rts ; return -1
+
+ ; process the mode argument
+
+iocbok: stx tmp4
+ jsr clriocb ; init with zero
+ ldy #1
+ jsr ldaxysp ; get mode
+ ldx tmp4
+ pha
+ and #O_APPEND
+ beq no_app
+ pla
+ and #15
+ cmp #O_RDONLY ; DOS supports append with write-only only
+ beq invret
+ cmp #O_RDWR
+ beq invret
+ lda #OPNOT|APPEND
+ bne set
+
+.ifndef UCASE_FILENAME
+invret: lda #<EINVAL ; file name is too long
+ jmp seterr
+.endif
+
+no_app: pla
+ and #15
+ cmp #O_RDONLY
+ bne l1
+ lda #OPNIN
+set: sta ICAX1,x
+ bne cont
+
+l1: cmp #O_WRONLY
+ bne l2
+ lda #OPNOT
+ bne set
+
+l2: ; O_RDWR
+ lda #OPNOT|OPNIN
+ bne set
+
+ ; process the filename argument
+
+cont: ldy #3
+ jsr ldaxysp
+
+.ifdef UCASE_FILENAME
+.ifdef DEFAULT_DEVICE
+ ldy #$80
+ sty tmp2 ; set flag for ucase_fn
+.endif
+ jsr ucase_fn
+ bcc ucok1
+invret: lda #<EINVAL ; file name is too long
+ jmp seterr