2 ; int vfprintf (FILE* f, const char* Format, va_list ap);
4 ; Ullrich von Bassewitz, 1.12.2000
8 .import pushax, popax, push1
9 .import _fwrite, __printf
10 .importzp sp, ptr1, ptr2
17 ; ----------------------------------------------------------------------------
19 ; Static data for the _vfprintf routine
22 outdesc: ; Static outdesc structure
24 .word out ; Output function pointer
30 ; ----------------------------------------------------------------------------
31 ; Callback routine used for the actual output.
33 ; static void out (struct outdesc* d, const char* buf, unsigned count)
34 ; /* Routine used for writing */
36 ; /* Write to the file */
37 ; if (fwrite (buf, count, 1, (FILE*) d->ptr) == -1) {
48 ; fwrite (buf, count, 1, (FILE*) d->ptr);
50 ; Since Buf and Count are already in place, we will just push the last
51 ; two parameters. The fwrite function will remove Buf and Count on exit.
54 ldy #7 ; Offset of D+1 on stack
57 dey ; Offset of D on stack (6)
60 dey ; Offset of ptr+1 in struct outdesc (5)
65 jsr pushax ; Push D->ptr
67 sta ptr2 ; Save function result
70 ; Pop the last parameter from stack and store it in ptr1. This means that
71 ; the stack is clean now.
77 ; Load the offset of ccount in struct outdesc
81 ; Check the return code. Checking the hig byte against $FF is ok here.
87 ; We had an error. Store -1 into d->ccount
94 ; Result was ok, count bytes written
106 ; ----------------------------------------------------------------------------
107 ; vfprintf - formatted output
109 ; int vfprintf (FILE* f, const char* format, va_list ap)
113 ; /* Setup descriptor */
117 ; /* Do formatting and output */
118 ; _printf (&d, format, ap);
120 ; /* Return bytes written */
125 pha ; Save low byte of ap
127 ; Setup the outdesc structure
131 sta outdesc+1 ; Clear ccount
133 ; Reorder the stack. Replace f on the stack by &d, so the stack frame is
134 ; exactly as _printf expects it. Parameters will get dropped by _printf.
137 lda (sp),y ; Low byte of f
142 lda (sp),y ; High byte of f
147 ; Restore low byte of ap and call _printf
152 ; Return the number of bytes written