2 ; int __fastcall__ vsnprintf (char* Buf, size_t size, const char* Format, va_list ap);
4 ; 2009-09-26, Ullrich von Bassewitz
5 ; 2015-07-17, Greg King
8 .export _vsnprintf, vsnprintf
9 .import ldaxysp, popax, incsp2, incsp6
10 .import _memcpy, __printf
19 ; ----------------------------------------------------------------------------
21 ; Static data for the _vsnprintf routine
24 outdesc: ; Static outdesc structure
25 ccount: .word 0 ; ccount
26 func: .word out ; Output function pointer
28 bufsize:.word 0 ; Buffer size
32 ; ----------------------------------------------------------------------------
33 ; vsprintf - formatted output into a buffer
35 ; int __fastcall__ vsnprintf (char* buf, size_t size, const char* format, va_list ap);
43 ; Setup the outdesc structure. This is also an additional entry point for
44 ; vsprintf with ap on stack
49 sta ccount+1 ; Clear ccount
51 ; Get the size parameter and replace it by a pointer to outdesc. This is to
52 ; build a stack frame for the call to _printf. The size must not be greater
53 ; than INT_MAX because the return type is int. If the size is zero,
54 ; then nothing will be written into the buffer; but, the arguments still will
55 ; be formatted and counted.
66 bmi L9 ; More than $7FFF
72 ; Write size-1 to outdesc.uns. It will be -1 if there is no buffer.
82 ; Copy buf to the outdesc.ptr
89 ; There must be a buffer if its size is non-zero.
94 bze L0 ; The pointer shouldn't be NULL
96 ; Restore ap and call _printf
103 ; Terminate the string if there is a buffer. The last char. is at either
104 ; bufptr+bufsize or bufptr+ccount, whichever is smaller.
107 bmi L4 ; -1 -- No buffer
126 ; Return the number of bytes written and drop buf
132 ; Bail out if size is too high.
135 .byte $2C ;(bit $xxxx)
137 ; NULL buffer pointers usually are invalid.
143 jsr __directerrno ; Return -1
144 jmp incsp6 ; Drop parameters
147 ; ----------------------------------------------------------------------------
148 ; Callback routine used for the actual output.
150 ; static void __cdecl__ out (struct outdesc* d, const char* buf, unsigned count)
151 ; /* Routine used for writing */
153 ; Since we know, we're called with a pointer to our static outdesc structure,
154 ; we don't need the pointer passed on the stack.
158 ; Calculate the space left in the buffer. If no space is left, don't copy
161 lda bufsize+0 ; Low byte of buffer size
163 sbc ccount+0 ; Low byte of bytes already written
166 bmi @L9 ; -1 -- No buffer
169 bcs @L0 ; Branch if space left
172 sta ptr1+1 ; No space left
174 ; Replace the pointer to d by a pointer to the write position in the buffer
175 ; for the call to memcpy that follows.
188 ; Get Count from stack
192 ; outdesc.ccount += Count;
203 ; if (Count > Left) Count = Left;
212 ; Jump to memcpy, which will cleanup the stack and return to the caller