1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
5 #include <internal/file.h>
13 extern int __mb_cur_max
;
17 int __vfprintf(FILE*, const char*, va_list);
23 int vfprintf(FILE* f
, const char* fmt
, va_list ap
)
26 char localbuf
[BUFSIZ
];
29 __fileno_lock(_fileno(f
));
31 if (f
->_flag
& _IONBF
) {
33 f
->_ptr
= f
->_base
= localbuf
;
35 len
= __vfprintf(f
, fmt
, ap
);
42 len
= __vfprintf(f
,fmt
, ap
);
45 __fileno_unlock(_fileno(f
));
47 return (ferror(f
) ? EOF
: len
);
52 * linux/lib/vsprintf.c
54 * Copyright (C) 1991, 1992 Linus Torvalds
57 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
59 * Wirzenius wrote this portably, Torvalds fucked it up :-)
63 * Appropiated for the reactos kernel, March 1998 -- David Welch
72 #include <internal/ieee.h>
75 #define ZEROPAD 1 /* pad with zero */
76 #define SIGN 2 /* unsigned/signed long */
77 #define PLUS 4 /* show plus */
78 #define SPACE 8 /* space if plus */
79 #define LEFT 16 /* left justified */
80 #define SPECIAL 32 /* 0x */
81 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
82 #define ZEROTRUNC 128 /* truncate zero 's */
85 static int skip_atoi(const char **s
)
90 i
= i
*10 + *((*s
)++) - '0';
95 static int do_div(LONGLONG
*n
,int base
)
97 int __res
= ((ULONGLONG
) *n
) % (unsigned) base
;
98 *n
= ((ULONGLONG
) *n
) / (unsigned) base
;
103 static int number(FILE * f
, LONGLONG num
, int base
, int size
, int precision
,int type
)
106 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
110 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
113 if (base
< 2 || base
> 36)
115 c
= (type
& ZEROPAD
) ? '0' : ' ';
122 } else if (type
& PLUS
) {
125 } else if (type
& SPACE
) {
130 if (type
& SPECIAL
) {
139 else while (num
!= 0)
140 tmp
[i
++] = digits
[do_div(&num
,base
)];
144 if (!(type
&(ZEROPAD
+LEFT
)))
147 if (putc(' ',f
) == EOF
)
153 if (putc(sign
,f
) == EOF
)
157 if (type
& SPECIAL
) {
159 if (putc('0',f
) == EOF
)
164 if (putc('0', f
) == EOF
)
167 if (putc(digits
[33],f
) == EOF
)
175 if (putc(c
,f
) == EOF
)
179 while (i
< precision
--)
181 if (putc('0', f
) == EOF
)
187 if (putc(tmp
[i
],f
) == EOF
)
193 if (putc(' ', f
) == EOF
)
201 static int numberf(FILE * f
, double __n
, char exp_sign
, int size
, int precision
, int type
)
203 double exponent
= 0.0;
218 int result
, done
= 0;
228 if ( exp_sign
== 'g' || exp_sign
== 'G' || exp_sign
== 'e' || exp_sign
== 'E' ) {
229 ie
= ((unsigned int)n
.n
->exponent
- (unsigned int)0x3ff);
230 exponent
= ie
/3.321928;
233 if ( exp_sign
== 'g' || exp_sign
== 'G' ) {
235 if ( exponent
< -4 || fabs(exponent
) >= precision
)
236 exp_sign
-= 2; // g -> e and G -> E
239 if ( exp_sign
== 'e' || exp_sign
== 'E' ) {
240 frac
= modf(exponent
,&e
);
243 else if ( frac
< -0.5 )
246 result
= numberf(f
,__n
/pow(10.0L,e
),'f',size
-4, precision
, type
);
250 if (putc( exp_sign
,f
) == EOF
)
259 result
= number(f
,ie
, 10,2, 2,type
);
266 if ( exp_sign
== 'f' ) {
272 c
= (type
& ZEROPAD
) ? '0' : ' ';
279 } else if (type
& PLUS
) {
282 } else if (type
& SPACE
) {
288 frac
= modf(__n
,&intr
);
290 // # flags forces a . and prevents trucation of trailing zero's
292 if ( precision
> 0 ) {
293 //frac = modfl(__n,&intr);
297 frac
= modf(frac
, &p
);
298 buf
[i
] = (int)p
+ '0';
309 if ( precision
>= 1 || type
& SPECIAL
) {
320 while ( intr
> 0.0 ) {
327 buf
[i
++] = (int)p
+ '0';
333 while ( j
< i
&& ro
== 1) {
334 if ( buf
[j
] >= '0' && buf
[j
] <= '8' ) {
338 else if ( buf
[j
] == '9' ) {
349 if (!(type
&(ZEROPAD
+LEFT
)))
352 if (putc(' ',f
) == EOF
)
358 if (putc( sign
,f
) == EOF
)
363 if (!(type
&(ZEROPAD
+LEFT
)))
366 if (putc(' ',f
) == EOF
)
370 if (type
& SPECIAL
) {
376 if (putc(c
,f
) == EOF
)
382 if ( type
& ZEROTRUNC
&& ((type
& SPECIAL
) != SPECIAL
) ) {
384 while ( j
< i
&& ( *tmp
== '0' || *tmp
== '.' )) {
390 // while (i < precision--)
394 if (putc(tmp
[i
],f
) == EOF
)
400 if (putc(' ', f
) == EOF
)
411 static int string(FILE *f
, const char* s
, int len
, int field_width
, int precision
, int flags
)
424 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
429 if ((unsigned int)len
> (unsigned int)precision
)
434 while (len
< field_width
--)
436 if (putc(' ', f
) == EOF
)
440 for (i
= 0; i
< len
; ++i
)
442 if (putc(*s
++, f
) == EOF
)
446 while (len
< field_width
--)
448 if (putc(' ', f
) == EOF
)
455 static int stringw(FILE *f
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
468 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
473 if ((unsigned int)len
> (unsigned int)precision
)
478 while (len
< field_width
--)
480 if (putc(' ', f
) == EOF
)
484 for (i
= 0; i
< len
; ++i
)
486 //#define MB_CUR_MAX 1
489 mbcount
= wctomb(mb
, *sw
++);
494 for (j
= 0; j
< mbcount
; j
++)
496 if (putc(mb
[j
], f
) == EOF
)
501 while (len
< field_width
--)
503 if (putc(' ', f
) == EOF
)
510 int __vfprintf(FILE *f
, const char *fmt
, va_list args
)
518 int result
, done
= 0;
520 int flags
; /* flags to number() */
522 int field_width
; /* width of output field */
523 int precision
; /* min. # of digits for integers; max
524 number of chars for from string */
525 int qualifier
= 0; /* 'h', 'l', 'L' or 'I64' for integer fields */
527 for (; *fmt
; ++fmt
) {
529 if (putc(*fmt
,f
) == EOF
)
538 ++fmt
; /* this also skips first '%' */
540 case '-': flags
|= LEFT
; goto repeat
;
541 case '+': flags
|= PLUS
; goto repeat
;
542 case ' ': flags
|= SPACE
; goto repeat
;
543 case '#': flags
|= SPECIAL
; goto repeat
;
544 case '0': flags
|= ZEROPAD
; goto repeat
;
547 /* get field width */
550 field_width
= skip_atoi(&fmt
);
551 else if (*fmt
== '*') {
553 /* it's the next argument */
554 field_width
= va_arg(args
, int);
555 if (field_width
< 0) {
556 field_width
= -field_width
;
561 /* get the precision */
566 precision
= skip_atoi(&fmt
);
567 else if (*fmt
== '*') {
569 /* it's the next argument */
570 precision
= va_arg(args
, int);
576 /* get the conversion qualifier */
578 // %Z can be just stand alone or as size_t qualifier
594 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
597 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
602 // go fine with ll instead of L
614 while (--field_width
> 0)
616 if (putc(' ', f
) == EOF
)
620 if (qualifier
== 'l' || qualifier
== 'w')
622 if (putc((unsigned char)(wchar_t) va_arg(args
, int), f
) == EOF
)
628 if (putc((unsigned char) va_arg(args
, int), f
) == EOF
)
632 while (--field_width
> 0)
634 if (putc(' ', f
) == EOF
)
642 while (--field_width
> 0)
644 if (putc(' ', f
) == EOF
)
648 if (qualifier
== 'h')
650 if (putc((unsigned char) va_arg(args
, int), f
) == EOF
)
656 if (putc((unsigned char)(wchar_t) va_arg(args
, int), f
) == EOF
)
660 while (--field_width
> 0)
662 if (putc(' ', f
) == EOF
)
669 if (qualifier
== 'l' || qualifier
== 'w') {
670 /* print unicode string */
671 sw
= va_arg(args
, wchar_t *);
672 result
= stringw(f
, sw
, -1, field_width
, precision
, flags
);
674 /* print ascii string */
675 s
= va_arg(args
, char *);
676 result
= string(f
, s
, -1, field_width
, precision
, flags
);
684 if (qualifier
== 'h') {
685 /* print ascii string */
686 s
= va_arg(args
, char *);
687 result
= string(f
, s
, -1, field_width
, precision
, flags
);
689 /* print unicode string */
690 sw
= va_arg(args
, wchar_t *);
691 result
= stringw(f
, sw
, -1, field_width
, precision
, flags
);
699 if (qualifier
== 'w') {
700 /* print counted unicode string */
701 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
702 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
707 len
= pus
->Length
/ sizeof(WCHAR
);
709 result
= stringw(f
, sw
, len
, field_width
, precision
, flags
);
711 /* print counted ascii string */
712 PANSI_STRING pas
= va_arg(args
, PANSI_STRING
);
713 if ((pas
== NULL
) || (pas
->Buffer
== NULL
)) {
720 result
= string(f
, s
, -1, field_width
, precision
, flags
);
732 _double
= (double)va_arg(args
, double);
734 if ( _isnan(_double
) ) {
738 if (putc(*s
++,f
) == EOF
)
743 } else if ( _isinf(_double
) < 0 ) {
747 if (putc(*s
++,f
) == EOF
)
752 } else if ( _isinf(_double
) > 0 ) {
756 if (putc(*s
++,f
) == EOF
)
762 if ( precision
== -1 )
764 result
= numberf(f
,_double
,*fmt
,field_width
,precision
,flags
);
772 if (field_width
== -1) {
773 field_width
= 2*sizeof(void *);
777 (unsigned long) va_arg(args
, void *), 16,
778 field_width
, precision
, flags
);
785 if (qualifier
== 'l') {
786 long * ip
= va_arg(args
, long *);
789 int * ip
= va_arg(args
, int *);
794 /* integer number formats - set up the flags and "break" */
818 if (putc('%', f
) == EOF
)
824 if (putc(*fmt
, f
) == EOF
)
833 if (qualifier
== 'I')
834 num
= va_arg(args
, ULONGLONG
);
835 else if (qualifier
== 'l') {
837 num
= va_arg(args
, long);
839 num
= va_arg(args
, unsigned long);
841 else if (qualifier
== 'h') {
843 num
= va_arg(args
, int);
845 num
= va_arg(args
, unsigned int);
847 else if (flags
& SIGN
)
848 num
= va_arg(args
, int);
850 num
= va_arg(args
, unsigned int);
851 result
= number(f
, num
, base
, field_width
, precision
, flags
);