1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
5 #include <internal/file.h>
8 extern int __mb_cur_max
;
9 int __vfprintf(FILE*, const char*, va_list);
14 int vfprintf(FILE* f
, const char* fmt
, va_list ap
)
17 char localbuf
[BUFSIZ
];
20 __fileno_lock(_fileno(f
));
22 if (f
->_flag
& _IONBF
) {
24 f
->_ptr
= f
->_base
= localbuf
;
25 f
->_bufsiz
= f
->_cnt
= BUFSIZ
;
26 len
= __vfprintf(f
, fmt
, ap
);
33 len
= __vfprintf(f
,fmt
, ap
);
36 __fileno_unlock(_fileno(f
));
38 return (ferror(f
) ? EOF
: len
);
43 * linux/lib/vsprintf.c
45 * Copyright (C) 1991, 1992 Linus Torvalds
48 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
50 * Wirzenius wrote this portably, Torvalds fucked it up :-)
54 * Appropiated for the reactos kernel, March 1998 -- David Welch
63 #include <internal/ieee.h>
66 #define ZEROPAD 1 /* pad with zero */
67 #define SIGN 2 /* unsigned/signed long */
68 #define PLUS 4 /* show plus */
69 #define SPACE 8 /* space if plus */
70 #define LEFT 16 /* left justified */
71 #define SPECIAL 32 /* 0x */
72 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
73 #define ZEROTRUNC 128 /* truncate zero 's */
76 static int skip_atoi(const char **s
)
81 i
= i
*10 + *((*s
)++) - '0';
86 static int do_div(LONGLONG
*n
,int base
)
88 int __res
= ((ULONGLONG
) *n
) % (unsigned) base
;
89 *n
= ((ULONGLONG
) *n
) / (unsigned) base
;
94 static int number(FILE * f
, LONGLONG num
, int base
, int size
, int precision
,int type
)
97 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
101 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
104 if (base
< 2 || base
> 36)
106 c
= (type
& ZEROPAD
) ? '0' : ' ';
113 } else if (type
& PLUS
) {
116 } else if (type
& SPACE
) {
121 if (type
& SPECIAL
) {
130 else while (num
!= 0)
131 tmp
[i
++] = digits
[do_div(&num
,base
)];
135 if (!(type
&(ZEROPAD
+LEFT
)))
138 if (putc(' ',f
) == EOF
)
144 if (putc(sign
,f
) == EOF
)
148 if (type
& SPECIAL
) {
150 if (putc('0',f
) == EOF
)
155 if (putc('0', f
) == EOF
)
158 if (putc(digits
[33],f
) == EOF
)
166 if (putc(c
,f
) == EOF
)
170 while (i
< precision
--)
172 if (putc('0', f
) == EOF
)
178 if (putc(tmp
[i
],f
) == EOF
)
184 if (putc(' ', f
) == EOF
)
192 static int numberf(FILE * f
, double __n
, char exp_sign
, int size
, int precision
, int type
)
194 double exponent
= 0.0;
209 int result
, done
= 0;
219 if ( exp_sign
== 'g' || exp_sign
== 'G' || exp_sign
== 'e' || exp_sign
== 'E' ) {
220 ie
= ((unsigned int)n
.n
->exponent
- (unsigned int)0x3ff);
221 exponent
= ie
/3.321928;
224 if ( exp_sign
== 'g' || exp_sign
== 'G' ) {
226 if ( exponent
< -4 || fabs(exponent
) >= precision
)
227 exp_sign
-= 2; // g -> e and G -> E
230 if ( exp_sign
== 'e' || exp_sign
== 'E' ) {
231 frac
= modf(exponent
,&e
);
234 else if ( frac
< -0.5 )
237 result
= numberf(f
,__n
/pow(10.0L,e
),'f',size
-4, precision
, type
);
241 if (putc( exp_sign
,f
) == EOF
)
250 result
= number(f
,ie
, 10,2, 2,type
);
257 if ( exp_sign
== 'f' ) {
263 c
= (type
& ZEROPAD
) ? '0' : ' ';
270 } else if (type
& PLUS
) {
273 } else if (type
& SPACE
) {
279 frac
= modf(__n
,&intr
);
281 // # flags forces a . and prevents trucation of trailing zero's
283 if ( precision
> 0 ) {
284 //frac = modfl(__n,&intr);
288 frac
= modf(frac
, &p
);
289 buf
[i
] = (int)p
+ '0';
300 if ( precision
>= 1 || type
& SPECIAL
) {
311 while ( intr
> 0.0 ) {
318 buf
[i
++] = (int)p
+ '0';
324 while ( j
< i
&& ro
== 1) {
325 if ( buf
[j
] >= '0' && buf
[j
] <= '8' ) {
329 else if ( buf
[j
] == '9' ) {
340 if (!(type
&(ZEROPAD
+LEFT
)))
343 if (putc(' ',f
) == EOF
)
349 if (putc( sign
,f
) == EOF
)
354 if (!(type
&(ZEROPAD
+LEFT
)))
357 if (putc(' ',f
) == EOF
)
361 if (type
& SPECIAL
) {
367 if (putc(c
,f
) == EOF
)
373 if ( type
& ZEROTRUNC
&& ((type
& SPECIAL
) != SPECIAL
) ) {
375 while ( j
< i
&& ( *tmp
== '0' || *tmp
== '.' )) {
381 // while (i < precision--)
385 if (putc(tmp
[i
],f
) == EOF
)
391 if (putc(' ', f
) == EOF
)
402 static int string(FILE *f
, const char* s
, int len
, int field_width
, int precision
, int flags
)
415 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
420 if ((unsigned int)len
> (unsigned int)precision
)
425 while (len
< field_width
--)
427 if (putc(' ', f
) == EOF
)
431 for (i
= 0; i
< len
; ++i
)
433 if (putc(*s
++, f
) == EOF
)
437 while (len
< field_width
--)
439 if (putc(' ', f
) == EOF
)
446 static int stringw(FILE *f
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
459 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
464 if ((unsigned int)len
> (unsigned int)precision
)
469 while (len
< field_width
--)
471 if (putc(' ', f
) == EOF
)
475 for (i
= 0; i
< len
; ++i
)
477 //#define MB_CUR_MAX 1
480 mbcount
= wctomb(mb
, *sw
++);
485 for (j
= 0; j
< mbcount
; j
++)
487 if (putc(mb
[j
], f
) == EOF
)
492 while (len
< field_width
--)
494 if (putc(' ', f
) == EOF
)
501 int __vfprintf(FILE *f
, const char *fmt
, va_list args
)
509 int result
, done
= 0;
511 int flags
; /* flags to number() */
513 int field_width
; /* width of output field */
514 int precision
; /* min. # of digits for integers; max
515 number of chars for from string */
516 int qualifier
= 0; /* 'h', 'l', 'L' or 'I64' for integer fields */
518 for (; *fmt
; ++fmt
) {
520 if (putc(*fmt
,f
) == EOF
)
529 ++fmt
; /* this also skips first '%' */
531 case '-': flags
|= LEFT
; goto repeat
;
532 case '+': flags
|= PLUS
; goto repeat
;
533 case ' ': flags
|= SPACE
; goto repeat
;
534 case '#': flags
|= SPECIAL
; goto repeat
;
535 case '0': flags
|= ZEROPAD
; goto repeat
;
538 /* get field width */
541 field_width
= skip_atoi(&fmt
);
542 else if (*fmt
== '*') {
544 /* it's the next argument */
545 field_width
= va_arg(args
, int);
546 if (field_width
< 0) {
547 field_width
= -field_width
;
552 /* get the precision */
557 precision
= skip_atoi(&fmt
);
558 else if (*fmt
== '*') {
560 /* it's the next argument */
561 precision
= va_arg(args
, int);
567 /* get the conversion qualifier */
569 // %Z can be just stand alone or as size_t qualifier
585 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
588 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
593 // go fine with ll instead of L
605 while (--field_width
> 0)
607 if (putc(' ', f
) == EOF
)
611 if (qualifier
== 'l' || qualifier
== 'w')
613 if (putc((unsigned char)(wchar_t) va_arg(args
, int), f
) == EOF
)
619 if (putc((unsigned char) va_arg(args
, int), f
) == EOF
)
623 while (--field_width
> 0)
625 if (putc(' ', f
) == EOF
)
633 while (--field_width
> 0)
635 if (putc(' ', f
) == EOF
)
639 if (qualifier
== 'h')
641 if (putc((unsigned char) va_arg(args
, int), f
) == EOF
)
647 if (putc((unsigned char)(wchar_t) va_arg(args
, int), f
) == EOF
)
651 while (--field_width
> 0)
653 if (putc(' ', f
) == EOF
)
660 if (qualifier
== 'l' || qualifier
== 'w') {
661 /* print unicode string */
662 sw
= va_arg(args
, wchar_t *);
663 result
= stringw(f
, sw
, -1, field_width
, precision
, flags
);
665 /* print ascii string */
666 s
= va_arg(args
, char *);
667 result
= string(f
, s
, -1, field_width
, precision
, flags
);
675 if (qualifier
== 'h') {
676 /* print ascii string */
677 s
= va_arg(args
, char *);
678 result
= string(f
, s
, -1, field_width
, precision
, flags
);
680 /* print unicode string */
681 sw
= va_arg(args
, wchar_t *);
682 result
= stringw(f
, sw
, -1, field_width
, precision
, flags
);
690 if (qualifier
== 'w') {
691 /* print counted unicode string */
692 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
693 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
698 len
= pus
->Length
/ sizeof(WCHAR
);
700 result
= stringw(f
, sw
, len
, field_width
, precision
, flags
);
702 /* print counted ascii string */
703 PANSI_STRING pas
= va_arg(args
, PANSI_STRING
);
704 if ((pas
== NULL
) || (pas
->Buffer
== NULL
)) {
711 result
= string(f
, s
, -1, field_width
, precision
, flags
);
723 _double
= (double)va_arg(args
, double);
725 if ( _isnan(_double
) ) {
729 if (putc(*s
++,f
) == EOF
)
734 } else if ( _isinf(_double
) < 0 ) {
738 if (putc(*s
++,f
) == EOF
)
743 } else if ( _isinf(_double
) > 0 ) {
747 if (putc(*s
++,f
) == EOF
)
753 if ( precision
== -1 )
755 result
= numberf(f
,_double
,*fmt
,field_width
,precision
,flags
);
763 if (field_width
== -1) {
764 field_width
= 2*sizeof(void *);
768 (unsigned long) va_arg(args
, void *), 16,
769 field_width
, precision
, flags
);
776 if (qualifier
== 'l') {
777 long * ip
= va_arg(args
, long *);
780 int * ip
= va_arg(args
, int *);
785 /* integer number formats - set up the flags and "break" */
809 if (putc('%', f
) == EOF
)
815 if (putc(*fmt
, f
) == EOF
)
824 if (qualifier
== 'I')
825 num
= va_arg(args
, ULONGLONG
);
826 else if (qualifier
== 'l') {
828 num
= va_arg(args
, long);
830 num
= va_arg(args
, unsigned long);
832 else if (qualifier
== 'h') {
834 num
= va_arg(args
, int);
836 num
= va_arg(args
, unsigned int);
838 else if (flags
& SIGN
)
839 num
= va_arg(args
, int);
841 num
= va_arg(args
, unsigned int);
842 result
= number(f
, num
, base
, field_width
, precision
, flags
);