1 line
94 KiB
NASM
1 line
94 KiB
NASM
|
keep stdio
mcopy stdio.macros
case on
****************************************************************
*
* StdIO - Standard I/O Library
*
* This code implements the tables and subroutines needed to
* support the standard C library STDIO.
*
* November 1988
* Mike Westerfield
*
* Copyright 1988
* Byte Works, Inc.
*
* Note: Portions of this library appear in SysFloat.
*
****************************************************************
*
StdIO start dummy segment
copy equates.asm
end
****************************************************************
*
* void clearerr(stream)
* FILE *stream;
*
* Clears the error flag for the givin stream.
*
* Inputs:
* stream - file to clear
*
****************************************************************
*
clearerr start
stream equ 4 input stream
tsc
phd
tcd
ph4 stream verify that stream exists
jsl ~VerifyStream
bcs lb1
ldy #FILE_flag clear the error flag
lda [stream],Y
and #$FFFF-_IOERR-_IOEOF
sta [stream],Y
lb1 pld
lda 2,S
sta 6,S
pla
sta 3,S
pla
rtl
end
****************************************************************
*
* int fclose(stream)
* FILE *stream;
*
* Inputs:
* stream - pointer to the file buffer to close
*
* Outputs:
* A - EOF for an error; 0 if there was no error
*
****************************************************************
*
fclose start
nameBuffSize equ 8*1024 pathname buffer size
err equ 1 return value
p equ 3 work pointer
stdfile equ 7 is this a standard file?
csubroutine (4:stream),8
phb
phk
plb
lda #EOF assume we will get an error
sta err
ph4 stream verify that stream exists
jsl ~VerifyStream
jcs rts
ph4 stream do any pending I/O
jsl fflush
tax
jne rts
stz stdfile not a standard file
lda stream+2 bypass file disposal if the file is
cmp #^stdin+4 one of the standard ones
bne cl0
lda stream
cmp #stdin+4
beq lb1
cmp #stdout+4
beq lb1
cmp #stderr+4
bne cl0
lb1 inc stdfile
bra cl3a
cl0 lla p,stderr+4 find the file record that points to this
ldy #2 one
cl1 lda [p]
ora [p],Y
jeq rts
lda [p],Y
tax
lda [p]
cmp stream
bne cl2
cpx stream+2
beq cl3
cl2 stx p+2
sta p
bra cl1
cl3 lda [stream] remove stream from the file list
sta [p]
lda [stream],Y
sta [p],Y
cl3a ldy #FILE_flag if the file was opened by tmpfile then
lda [stream],Y
and #_IOTEMPFILE
beq cl3d
ph4 #nameBuffSize p = malloc(nameBuffSize)
jsl malloc grPathname = p
sta p dsPathname = p+2
stx p+2
sta grPathname
stx grPathname+2
clc
adc #2
bcc cl3b
inx
cl3b sta dsPathname
stx dsPathname+2
lda #nameBuffSize p->size = nameBuffSize
sta [p]
ldy #FILE_file clRefnum = grRefnum = stream->_file
lda [stream],Y
beq cl3e
sta grRefnum
GetRefInfoGS gr GetRefInfoGS(gr)
bcs cl3c
lda grRefnum OSClose(cl)
sta clRefNum
OSClose cl
DestroyGS ds DestroyGS(ds)
cl3c ph4 p free(p)
jsl free
bra cl3e else
cl3d ldy #FILE_file close the file
lda [stream],Y
beq cl3e
sta clRefNum
OSClose cl
cl3e ldy #FILE_flag if the buffer was allocated by fopen then
lda [stream],Y
and #_IOMYBUF
beq cl4
ldy #FILE_base+2 dispose of the file buffer
lda [stream],Y
pha
dey
dey
lda [stream],Y
pha
jsl free
cl4 lda stdfile if this is not a standard file then
bne cl5
ph4 stream dispose of the file buffer
jsl free
bra cl7 else
cl5 add4 stream,#sizeofFILE-4,p reset the standard out stuff
ldy #sizeofFILE-2
cl6 lda [p],Y
sta [stream],Y
dey
dey
cpy #2
bne cl6
cl7 stz err no error found
rts plb
creturn 2:err
cl dc i'1' parameter block for OSclose
clRefNum ds 2
gr dc i'3' parameter block for GetRefInfoGS
grRefnum ds 2
ds 2
grPathname ds 4
ds dc i'1' parameter block for DestroyGS
dsPathname ds 4
end
****************************************************************
*
* int feof(stream)
* FILE *stream;
*
* Inputs:
* stream - file to check
*
* Outputs:
* Returns _IOEOF if an end of file has been reached; else
* 0.
*
****************************************************************
*
feof start
stream equ 4 input stream
tsc
phd
tcd
ph4 stream verify that stream exists
jsl ~VerifyStream
ldx #_IOEOF
bcs lb1
|