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