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)
64 lda (ptr1),y ; Load D->ptr
66 sta ptr2 ; Save function result
69 ; Pop the last parameter from stack and store it in ptr1. This means that
70 ; the stack is clean now.
76 ; Load the offset of ccount in struct outdesc
80 ; Check the return code. Checking the hig byte against $FF is ok here.
86 ; We had an error. Store -1 into d->ccount
93 ; Result was ok, count bytes written
105 ; ----------------------------------------------------------------------------
106 ; vfprintf - formatted output
108 ; int vfprintf (FILE* f, const char* format, va_list ap)
112 ; /* Setup descriptor */
116 ; /* Do formatting and output */
117 ; _printf (&d, format, ap);
119 ; /* Return bytes written */
124 pha ; Save low byte of ap
126 ; Setup the outdesc structure
130 sta outdesc+1 ; Clear ccount
132 ; Reorder the stack. Replace f on the stack by &d, so the stack frame is
133 ; exactly as _printf expects it. Parameters will get dropped by _printf.
136 lda (sp),y ; Low byte of f
141 lda (sp),y ; High byte of f
146 ; Restore low byte of ap and call _printf
151 ; Return the number of bytes written