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