1
0
mirror of https://github.com/cc65/cc65.git synced 2024-07-10 07:29:05 +00:00

Fixed a bug (report and patch by Greg King)

git-svn-id: svn://svn.cc65.org/cc65/trunk@3384 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2005-02-14 09:59:54 +00:00
parent 9afcff6020
commit 100fbfc0d1

View File

@ -1,8 +1,9 @@
; vfprintf.s
; ;
; int vfprintf (FILE* f, const char* Format, va_list ap); ; int fastcall vfprintf(FILE* f, const char* Format, va_list ap);
;
; Ullrich von Bassewitz, 1.12.2000
; ;
; 2005-02-08, Ullrich von Bassewitz
; 2005-02-11, Greg King
.export _vfprintf .export _vfprintf
.import pushax, popax, push1, pushwysp, ldaxysp, ldaxidx, incsp6 .import pushax, popax, push1, pushwysp, ldaxysp, ldaxidx, incsp6
@ -15,15 +16,13 @@
.data .data
; ---------------------------------------------------------------------------- ; ----------------------------------------------------------------------------
;
; Static data for the _vfprintf routine ; Static data for the _vfprintf routine
; ;
outdesc: ; Static outdesc structure outdesc: ; Static outdesc structure
.word 0 ; ccount ccount: .res 2
.word out ; Output function pointer .word out ; Output function pointer
.word 0 ; ptr ptr: .res 2 ; Points to output file
.word 0 ; uns .res 2 ; (Not used by this function)
.code .code
@ -33,24 +32,20 @@ outdesc: ; Static outdesc structure
; static void out (struct outdesc* d, const char* buf, unsigned count) ; static void out (struct outdesc* d, const char* buf, unsigned count)
; /* Routine used for writing */ ; /* Routine used for writing */
; { ; {
; register size_t cnt;
; /* Write to the file */ ; /* Write to the file */
; if (fwrite (buf, count, 1, (FILE*) d->ptr) == -1) { ; if ((cnt = fwrite(buf, 1, count, (FILE *)d->ptr)) == 0) {
; d->ccount = -1; ; d->ccount = -1;
; } else { ; } else {
; d->ccount += count; ; d->ccount += cnt;
; } ; }
; } ; }
out:
; About to call ; About to call
; ;
; fwrite (buf, 1, count, (FILE*) d->ptr); ; fwrite (buf, 1, count, (FILE*) d->ptr);
; ;
; Since Buf and Count are already in place, we will just push the last out: ldy #5
; two parameters. The fwrite function will remove Buf and Count on exit.
ldy #5
jsr pushwysp ; Push buf jsr pushwysp ; Push buf
jsr push1 ; Push #1 jsr push1 ; Push #1
ldy #7 ldy #7
@ -74,14 +69,19 @@ out:
ldy #$00 ldy #$00
; Check the return code. Checking the high byte against $FF is ok here. ; Check the return value.
lda ptr2+1 lda ptr2+1
cmp #$FF ora ptr2
bne @Ok bne @Ok
; We had an error. Store -1 into d->ccount ; We had an error. Store -1 into d->ccount
.ifp02
lda #<-1
.else
dec a
.endif
sta (ptr1),y sta (ptr1),y
iny iny
bne @Done ; Branch always bne @Done ; Branch always
@ -101,12 +101,15 @@ out:
; ---------------------------------------------------------------------------- ; ----------------------------------------------------------------------------
; vfprintf - formatted output ; vfprintf - formatted output
; ;
; int vfprintf (FILE* f, const char* format, va_list ap) ; int fastcall vfprintf(FILE* f, const char* format, va_list ap)
; { ; {
; struct outdesc d; ; static struct outdesc d = {
; 0,
; out
; };
; ;
; /* Setup descriptor */ ; /* Setup descriptor */
; d.fout = out; ; d.ccount = 0;
; d.ptr = f; ; d.ptr = f;
; ;
; /* Do formatting and output */ ; /* Do formatting and output */
@ -115,27 +118,27 @@ out:
; /* Return bytes written */ ; /* Return bytes written */
; return d.ccount; ; return d.ccount;
; } ; }
;
_vfprintf: _vfprintf:
pha ; Save low byte of ap pha ; Save low byte of ap
; Setup the outdesc structure ; Setup the outdesc structure
lda #0 lda #0
sta outdesc sta ccount
sta outdesc+1 ; Clear ccount sta ccount+1 ; Clear character-count
; Reorder the stack. Replace f on the stack by &d, so the stack frame is ; Reorder the stack. Replace f on the stack by &d, so the stack frame is
; exactly as _printf expects it. Parameters will get dropped by _printf. ; exactly as _printf expects it. Parameters will get dropped by _printf.
ldy #2 ; ldy #2
lda (sp),y ; Low byte of f lda (sp),y ; Low byte of f
sta outdesc+4 ; sta ptr
lda #<outdesc lda #<outdesc
sta (sp),y sta (sp),y
iny iny
lda (sp),y ; High byte of f lda (sp),y ; High byte of f
sta outdesc+5 sta ptr+1
lda #>outdesc lda #>outdesc
sta (sp),y sta (sp),y
@ -146,8 +149,8 @@ _vfprintf:
; Return the number of bytes written ; Return the number of bytes written
lda outdesc lda ccount
ldx outdesc+1 ldx ccount+1
rts rts