]> pd.if.org Git - pdclib.old/blob - functions/stdio/vfprintf.c
Add _cbprintf/_vcbprintf (callback based printf formatters)
[pdclib.old] / functions / stdio / vfprintf.c
1 /* $Id$ */
2
3 /* vfprintf( FILE *, const char *, va_list )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdint.h>
12 #include <limits.h>
13
14 #ifndef REGTEST
15 #include <_PDCLIB_io.h>
16
17 static size_t filecb(void *p, const char *buf, size_t size)
18 {
19     return _PDCLIB_fwrite_unlocked( buf, 1, size, (FILE*) p );
20 }
21
22 int _PDCLIB_vfprintf_unlocked( FILE * _PDCLIB_restrict stream,
23                        const char * _PDCLIB_restrict format,
24                        va_list arg )
25 {
26     return _vcbprintf(stream, filecb, format, arg);
27 }
28
29 int vfprintf( FILE * _PDCLIB_restrict stream,
30               const char * _PDCLIB_restrict format,
31               va_list arg )
32 {
33     _PDCLIB_flockfile( stream );
34     int r = _PDCLIB_vfprintf_unlocked( stream, format, arg );
35     _PDCLIB_funlockfile( stream );
36     return r;
37 }
38
39 #endif
40
41 #ifdef TEST
42 #define _PDCLIB_FILEID "stdio/vfprintf.c"
43 #define _PDCLIB_FILEIO
44 #include <stddef.h>
45 #include <_PDCLIB_test.h>
46
47 static int testprintf( FILE * stream, const char * format, ... )
48 {
49     int i;
50     va_list arg;
51     va_start( arg, format );
52     i = vfprintf( stream, format, arg );
53     va_end( arg );
54     return i;
55 }
56
57 int main( void )
58 {
59     FILE * target;
60     TESTCASE( ( target = tmpfile() ) != NULL );
61 #include "printf_testcases.h"
62     TESTCASE( fclose( target ) == 0 );
63     return TEST_RESULTS;
64 }
65
66 #endif