]> pd.if.org Git - pdclib.old/blob - functions/stdio/vfprintf.c
Namespace cleanliness: Rename all ***_unlocked functions to _PDCLIB_***_unlocked.
[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 int _PDCLIB_vfprintf_unlocked( FILE * _PDCLIB_restrict stream, 
18                        const char * _PDCLIB_restrict format, 
19                        va_list arg )
20 {
21     /* TODO: This function should interpret format as multibyte characters.  */
22     struct _PDCLIB_status_t status;
23     status.base = 0;
24     status.flags = 0;
25     status.n = UINT_MAX;
26     status.i = 0;
27     status.current = 0;
28     status.s = NULL;
29     status.width = 0;
30     status.prec = 0;
31     status.stream = stream;
32     va_copy( status.arg, arg );
33
34     while ( *format != '\0' )
35     {
36         const char * rc;
37         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
38         {
39             /* No conversion specifier, print verbatim */
40             _PDCLIB_putc_unlocked( *(format++), stream );
41             status.i++;
42         }
43         else
44         {
45             /* Continue parsing after conversion specifier */
46             format = rc;
47         }
48     }
49     va_end( status.arg );
50     return status.i;
51 }
52
53 int vfprintf( FILE * _PDCLIB_restrict stream, 
54               const char * _PDCLIB_restrict format, 
55               va_list arg )
56 {
57     _PDCLIB_flockfile( stream );
58     int r = _PDCLIB_vfprintf_unlocked( stream, format, arg );
59     _PDCLIB_funlockfile( stream );
60     return r;
61 }
62
63 #endif
64
65 #ifdef TEST
66 #define _PDCLIB_FILEID "stdio/vfprintf.c"
67 #define _PDCLIB_FILEIO
68
69 #include <_PDCLIB_test.h>
70
71 static int testprintf( FILE * stream, const char * format, ... )
72 {
73     int i;
74     va_list arg;
75     va_start( arg, format );
76     i = vfprintf( stream, format, arg );
77     va_end( arg );
78     return i;
79 }
80
81 int main( void )
82 {
83     FILE * target;
84     TESTCASE( ( target = tmpfile() ) != NULL );
85 #include "printf_testcases.h"
86     TESTCASE( fclose( target ) == 0 );
87     return TEST_RESULTS;
88 }
89
90 #endif