2003-12-03 10:15:33 +00:00
|
|
|
;
|
2002-11-23 17:52:38 +00:00
|
|
|
; Ullrich von Bassewitz, 22.11.2002
|
|
|
|
;
|
|
|
|
; FILE* __fastcall__ _fopen (const char* name, const char* mode, FILE* f);
|
|
|
|
; /* Open the specified file and fill the descriptor values into f */
|
|
|
|
;
|
|
|
|
|
|
|
|
.export __fopen
|
|
|
|
|
|
|
|
.import _open
|
|
|
|
.import pushax, incsp4, return0
|
|
|
|
.importzp sp, ptr1
|
|
|
|
|
|
|
|
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "fcntl.inc"
|
|
|
|
.include "_file.inc"
|
|
|
|
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Code
|
|
|
|
|
|
|
|
.proc __fopen
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
sta file
|
2003-08-27 09:48:10 +00:00
|
|
|
stx file+1 ; Save f
|
2002-11-23 17:52:38 +00:00
|
|
|
|
|
|
|
; Get a pointer to the mode string
|
|
|
|
|
|
|
|
ldy #1
|
|
|
|
lda (sp),y
|
|
|
|
sta ptr1+1
|
|
|
|
dey
|
|
|
|
lda (sp),y
|
|
|
|
sta ptr1
|
|
|
|
|
|
|
|
; Look at the first character in mode
|
|
|
|
|
|
|
|
ldx #$00 ; Mode will be in X
|
|
|
|
lda (ptr1),y ; Get first char from mode
|
|
|
|
cmp #'w'
|
|
|
|
bne @L1
|
|
|
|
ldx #(O_WRONLY | O_CREAT | O_TRUNC)
|
|
|
|
bne @L3
|
|
|
|
@L1: cmp #'r'
|
|
|
|
bne @L2
|
|
|
|
ldx #O_RDONLY
|
|
|
|
bne @L3
|
|
|
|
@L2: cmp #'a'
|
|
|
|
bne invmode
|
|
|
|
ldx #(O_WRONLY | O_CREAT | O_APPEND)
|
|
|
|
|
|
|
|
; Look at more chars from the mode string
|
|
|
|
|
|
|
|
@L3: iny ; Next char
|
|
|
|
beq invmode
|
|
|
|
lda (ptr1),y
|
|
|
|
beq modeok ; End of mode string reached
|
|
|
|
cmp #'+'
|
|
|
|
bne @L4
|
|
|
|
txa
|
|
|
|
ora #O_RDWR ; Always do r/w in addition to anything else
|
|
|
|
tax
|
|
|
|
bne @L3
|
|
|
|
@L4: cmp #'b'
|
|
|
|
beq @L3 ; Binary mode is ignored
|
|
|
|
|
|
|
|
; Invalid mode
|
|
|
|
|
|
|
|
invmode:
|
|
|
|
lda #EINVAL
|
2022-08-28 20:37:33 +00:00
|
|
|
jsr ___seterrno ; Set __errno, returns zero in A
|
2004-05-13 21:09:08 +00:00
|
|
|
tax ; a/x = 0
|
2002-11-23 17:52:38 +00:00
|
|
|
jmp incsp4
|
|
|
|
|
|
|
|
; Mode string successfully parsed. Store the binary mode onto the stack in
|
2003-08-27 09:48:10 +00:00
|
|
|
; the same place where the mode string pointer was before. Then call open()
|
2002-11-23 17:52:38 +00:00
|
|
|
|
|
|
|
modeok: ldy #$00
|
|
|
|
txa ; Mode -> A
|
|
|
|
sta (sp),y
|
|
|
|
tya
|
|
|
|
iny
|
|
|
|
sta (sp),y
|
2018-08-17 20:24:08 +00:00
|
|
|
ldy #4 ; Size of arguments in bytes
|
2002-11-23 17:52:38 +00:00
|
|
|
jsr _open ; Will cleanup the stack
|
|
|
|
|
|
|
|
; Check the result of the open() call
|
|
|
|
|
|
|
|
cpx #$FF
|
|
|
|
bne openok
|
|
|
|
cmp #$FF
|
|
|
|
bne openok
|
2022-08-28 19:52:53 +00:00
|
|
|
jmp return0 ; Failure, errno/__oserror already set
|
2002-11-23 17:52:38 +00:00
|
|
|
|
|
|
|
; Open call succeeded
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
openok: ldy file
|
2002-11-23 17:52:38 +00:00
|
|
|
sty ptr1
|
2003-06-17 20:52:33 +00:00
|
|
|
ldy file+1
|
2002-11-23 17:52:38 +00:00
|
|
|
sty ptr1+1
|
2003-12-03 10:15:33 +00:00
|
|
|
ldy #_FILE::f_fd
|
2003-06-17 20:52:33 +00:00
|
|
|
sta (ptr1),y ; file->f_fd = fd;
|
2003-12-03 10:15:33 +00:00
|
|
|
ldy #_FILE::f_flags
|
2002-11-23 17:52:38 +00:00
|
|
|
lda #_FOPEN
|
2003-06-17 20:52:33 +00:00
|
|
|
sta (ptr1),y ; file->f_flags = _FOPEN;
|
2002-11-23 17:52:38 +00:00
|
|
|
|
|
|
|
; Return the pointer to the file structure
|
|
|
|
|
|
|
|
lda ptr1
|
|
|
|
ldx ptr1+1
|
|
|
|
rts
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data
|
|
|
|
|
|
|
|
.bss
|
2003-06-17 20:52:33 +00:00
|
|
|
file: .res 2
|
2002-11-23 17:52:38 +00:00
|
|
|
|
|
|
|
|