1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
4 extern int __mb_cur_max
;
5 int __vfprintf(FILE*, const char*, va_list);
10 int vfprintf(FILE* f
, const char* fmt
, va_list ap
)
13 char localbuf
[BUFSIZ
];
16 __fileno_lock(_fileno(f
));
18 if (f
->_flag
& _IONBF
) {
20 f
->_ptr
= f
->_base
= localbuf
;
21 f
->_bufsiz
= f
->_cnt
= BUFSIZ
;
22 len
= __vfprintf(f
, fmt
, ap
);
29 len
= __vfprintf(f
,fmt
, ap
);
32 __fileno_unlock(_fileno(f
));
34 return (ferror(f
) ? EOF
: len
);
39 * linux/lib/vsprintf.c
41 * Copyright (C) 1991, 1992 Linus Torvalds
44 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
46 * Wirzenius wrote this portably, Torvalds fucked it up :-)
50 * Appropiated for the reactos kernel, March 1998 -- David Welch
58 #include <internal/ieee.h>
61 #define ZEROPAD 1 /* pad with zero */
62 #define SIGN 2 /* unsigned/signed long */
63 #define PLUS 4 /* show plus */
64 #define SPACE 8 /* space if plus */
65 #define LEFT 16 /* left justified */
66 #define SPECIAL 32 /* 0x */
67 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
68 #define ZEROTRUNC 128 /* truncate zero 's */
71 static int skip_atoi(const char **s
)
76 i
= i
*10 + *((*s
)++) - '0';
81 static int do_div(LONGLONG
*n
,int base
)
83 int __res
= ((ULONGLONG
) *n
) % (unsigned) base
;
84 *n
= ((ULONGLONG
) *n
) / (unsigned) base
;
89 static int number(FILE * f
, LONGLONG num
, int base
, int size
, int precision
,int type
)
92 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
96 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
99 if (base
< 2 || base
> 36)
101 c
= (type
& ZEROPAD
) ? '0' : ' ';
108 } else if (type
& PLUS
) {
111 } else if (type
& SPACE
) {
116 if (type
& SPECIAL
) {
125 else while (num
!= 0)
126 tmp
[i
++] = digits
[do_div(&num
,base
)];
130 if (!(type
&(ZEROPAD
+LEFT
)))
133 if (putc(' ',f
) == EOF
)
139 if (putc(sign
,f
) == EOF
)
143 if (type
& SPECIAL
) {
145 if (putc('0',f
) == EOF
)
150 if (putc('0', f
) == EOF
)
153 if (putc(digits
[33],f
) == EOF
)
161 if (putc(c
,f
) == EOF
)
165 while (i
< precision
--)
167 if (putc('0', f
) == EOF
)
173 if (putc(tmp
[i
],f
) == EOF
)
179 if (putc(' ', f
) == EOF
)
187 static int numberf(FILE * f
, double __n
, char exp_sign
, int size
, int precision
, int type
)
189 double exponent
= 0.0;
204 int result
, done
= 0;
214 if ( exp_sign
== 'g' || exp_sign
== 'G' || exp_sign
== 'e' || exp_sign
== 'E' ) {
215 ie
= ((unsigned int)n
.n
->exponent
- (unsigned int)0x3ff);
216 exponent
= ie
/3.321928;
219 if ( exp_sign
== 'g' || exp_sign
== 'G' ) {
221 if ( exponent
< -4 || fabs(exponent
) >= precision
)
222 exp_sign
-= 2; // g -> e and G -> E
227 if ( exp_sign
== 'e' || exp_sign
== 'E' ) {
228 frac
= modf(exponent
,&e
);
231 else if ( frac
< -0.5 )
234 result
= numberf(f
,__n
/pow(10.0L,e
),'f',size
-4, precision
, type
);
238 if (putc( exp_sign
,f
) == EOF
)
247 result
= number(f
,ie
, 10,2, 2,type
);
254 if ( exp_sign
== 'f' ) {
260 c
= (type
& ZEROPAD
) ? '0' : ' ';
267 } else if (type
& PLUS
) {
270 } else if (type
& SPACE
) {
276 frac
= modf(__n
,&intr
);
278 // # flags forces a . and prevents trucation of trailing zero's
280 if ( precision
> 0 ) {
281 //frac = modfl(__n,&intr);
285 frac
= modf(frac
, &p
);
286 buf
[i
] = (int)p
+ '0';
297 if ( precision
>= 1 || type
& SPECIAL
) {
308 while ( intr
> 0.0 ) {
315 buf
[i
++] = (int)p
+ '0';
321 while ( j
< i
&& ro
== 1) {
322 if ( buf
[j
] >= '0' && buf
[j
] <= '8' ) {
326 else if ( buf
[j
] == '9' ) {
337 if (!(type
&(ZEROPAD
+LEFT
)))
340 if (putc(' ',f
) == EOF
)
346 if (putc( sign
,f
) == EOF
)
351 if (!(type
&(ZEROPAD
+LEFT
)))
354 if (putc(' ',f
) == EOF
)
358 if (type
& SPECIAL
) {
364 if (putc(c
,f
) == EOF
)
370 if ( type
& ZEROTRUNC
&& ((type
& SPECIAL
) != SPECIAL
) ) {
372 while ( j
< i
&& ( *tmp
== '0' || *tmp
== '.' )) {
378 // while (i < precision--)
382 if (putc(tmp
[i
],f
) == EOF
)
388 if (putc(' ', f
) == EOF
)
399 static int string(FILE *f
, const char* s
, int len
, int field_width
, int precision
, int flags
)
412 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
417 if ((unsigned int)len
> (unsigned int)precision
)
422 while (len
< field_width
--)
424 if (putc(' ', f
) == EOF
)
428 for (i
= 0; i
< len
; ++i
)
430 if (putc(*s
++, f
) == EOF
)
434 while (len
< field_width
--)
436 if (putc(' ', f
) == EOF
)
443 static int stringw(FILE *f
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
457 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
462 if ((unsigned int)len
> (unsigned int)precision
)
467 while (len
< field_width
--)
469 if (putc(' ', f
) == EOF
)
473 mb
= malloc(MB_CUR_MAX
* sizeof(char));
476 for (i
= 0; i
< len
; ++i
)
479 mbcount
= wctomb(mb
, *sw
++);
484 for (j
= 0; j
< mbcount
; j
++)
486 if (putc(mb
[j
], f
) == EOF
)
494 while (len
< field_width
--)
496 if (putc(' ', f
) == EOF
)
507 int __vfprintf(FILE *f
, const char *fmt
, va_list args
)
515 int result
, done
= 0;
517 int flags
; /* flags to number() */
519 int field_width
; /* width of output field */
520 int precision
; /* min. # of digits for integers; max
521 number of chars for from string */
522 int qualifier
= 0; /* 'h', 'l', 'L' or 'I64' for integer fields */
524 for (; *fmt
; ++fmt
) {
526 if (putc(*fmt
,f
) == EOF
)
535 ++fmt
; /* this also skips first '%' */
537 case '-': flags
|= LEFT
; goto repeat
;
538 case '+': flags
|= PLUS
; goto repeat
;
539 case ' ': flags
|= SPACE
; goto repeat
;
540 case '#': flags
|= SPECIAL
; goto repeat
;
541 case '0': flags
|= ZEROPAD
; goto repeat
;
544 /* get field width */
547 field_width
= skip_atoi(&fmt
);
548 else if (*fmt
== '*') {
550 /* it's the next argument */
551 field_width
= va_arg(args
, int);
552 if (field_width
< 0) {
553 field_width
= -field_width
;
558 /* get the precision */
563 precision
= skip_atoi(&fmt
);
564 else if (*fmt
== '*') {
566 /* it's the next argument */
567 precision
= va_arg(args
, int);
573 /* get the conversion qualifier */
575 // %Z can be just stand alone or as size_t qualifier
591 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
594 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
599 // go fine with ll instead of L
611 while (--field_width
> 0)
613 if (putc(' ', f
) == EOF
)
617 if (qualifier
== 'l' || qualifier
== 'w')
619 if (putc((unsigned char)(wchar_t) va_arg(args
, int), f
) == EOF
)
625 if (putc((unsigned char) va_arg(args
, int), f
) == EOF
)
629 while (--field_width
> 0)
631 if (putc(' ', f
) == EOF
)
639 while (--field_width
> 0)
641 if (putc(' ', f
) == EOF
)
645 if (qualifier
== 'h')
647 if (putc((unsigned char) va_arg(args
, int), f
) == EOF
)
653 if (putc((unsigned char)(wchar_t) va_arg(args
, int), f
) == EOF
)
657 while (--field_width
> 0)
659 if (putc(' ', f
) == EOF
)
666 if (qualifier
== 'l' || qualifier
== 'w') {
667 /* print unicode string */
668 sw
= va_arg(args
, wchar_t *);
669 result
= stringw(f
, sw
, -1, field_width
, precision
, flags
);
671 /* print ascii string */
672 s
= va_arg(args
, char *);
673 result
= string(f
, s
, -1, field_width
, precision
, flags
);
681 if (qualifier
== 'h') {
682 /* print ascii string */
683 s
= va_arg(args
, char *);
684 result
= string(f
, s
, -1, field_width
, precision
, flags
);
686 /* print unicode string */
687 sw
= va_arg(args
, wchar_t *);
688 result
= stringw(f
, sw
, -1, field_width
, precision
, flags
);
696 if (qualifier
== 'w') {
697 /* print counted unicode string */
698 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
699 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
704 len
= pus
->Length
/ sizeof(WCHAR
);
706 result
= stringw(f
, sw
, len
, field_width
, precision
, flags
);
708 /* print counted ascii string */
709 PANSI_STRING pas
= va_arg(args
, PANSI_STRING
);
710 if ((pas
== NULL
) || (pas
->Buffer
== NULL
)) {
717 result
= string(f
, s
, -1, field_width
, precision
, flags
);
729 _double
= (double)va_arg(args
, double);
731 if ( _isnan(_double
) ) {
735 if (putc(*s
++,f
) == EOF
)
740 } else if ( _isinf(_double
) < 0 ) {
744 if (putc(*s
++,f
) == EOF
)
749 } else if ( _isinf(_double
) > 0 ) {
753 if (putc(*s
++,f
) == EOF
)
759 if ( precision
== -1 )
761 result
= numberf(f
,_double
,*fmt
,field_width
,precision
,flags
);
769 if (field_width
== -1) {
770 field_width
= 2*sizeof(void *);
774 (unsigned long) va_arg(args
, void *), 16,
775 field_width
, precision
, flags
);
782 if (qualifier
== 'l') {
783 long * ip
= va_arg(args
, long *);
786 int * ip
= va_arg(args
, int *);
791 /* integer number formats - set up the flags and "break" */
815 if (putc('%', f
) == EOF
)
821 if (putc(*fmt
, f
) == EOF
)
830 if (qualifier
== 'I')
831 num
= va_arg(args
, ULONGLONG
);
832 else if (qualifier
== 'l') {
834 num
= va_arg(args
, long);
836 num
= va_arg(args
, unsigned long);
838 else if (qualifier
== 'h') {
840 num
= va_arg(args
, int);
842 num
= va_arg(args
, unsigned int);
844 else if (flags
& SIGN
)
845 num
= va_arg(args
, int);
847 num
= va_arg(args
, unsigned int);
848 result
= number(f
, num
, base
, field_width
, precision
, flags
);