2 * PROGRAMMERS: David Welch
6 * - Verify the implementation of '%Z'.
10 * linux/lib/vsprintf.c
12 * Copyright (C) 1991, 1992 Linus Torvalds
15 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
17 * Wirzenius wrote this portably, Torvalds fucked it up :-)
20 #define WIN32_NO_STATUS
22 #include <ndk/ntndk.h>
26 #define ZEROPAD 1 /* pad with zero */
27 #define SIGN 2 /* unsigned/signed long */
28 #define PLUS 4 /* show plus */
29 #define SPACE 8 /* space if plus */
30 #define LEFT 16 /* left justified */
31 #define SPECIAL 32 /* 0x */
32 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
33 #define REMOVEHEX 256 /* use 256 as remve 0x frim BASE 16 */
35 unsigned int mantissal
:32;
36 unsigned int mantissah
:20;
37 unsigned int exponent
:11;
53 return ( x
.x
->exponent
== 0x7ff && ( x
.x
->mantissah
== 0 && x
.x
->mantissal
== 0 ));
67 return ( x
.x
->exponent
== 0x7ff && ( x
.x
->mantissah
!= 0 || x
.x
->mantissal
!= 0 ));
74 do_div(long long *n
, int base
)
77 a
= ((unsigned long long) *n
) % (unsigned) base
;
78 *n
= ((unsigned long long) *n
) / (unsigned) base
;
83 static int skip_atoi(const char **s
)
88 i
= i
*10 + *((*s
)++) - '0';
94 number(char * buf
, char * end
, long long num
, int base
, int size
, int precision
, int type
)
98 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
99 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
102 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
105 if (base
< 2 || base
> 36)
107 c
= (type
& ZEROPAD
) ? '0' : ' ';
114 } else if (type
& PLUS
) {
117 } else if (type
& SPACE
) {
123 if ((type
& SPECIAL
) && ((type
& REMOVEHEX
) == 0)) {
129 if ((num
== 0) && (precision
!=0))
131 else while (num
!= 0)
132 tmp
[i
++] = digits
[do_div(&num
,base
)];
136 if (!(type
&(ZEROPAD
+LEFT
))) {
149 if ((type
& SPECIAL
) && ((type
& REMOVEHEX
) == 0)) {
160 if (!(type
& LEFT
)) {
167 while (i
< precision
--) {
187 numberf(char * buf
, char * end
, double num
, int base
, int size
, int precision
, int type
)
191 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
192 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
197 the float version of number is direcly copy of number
200 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
203 if (base
< 2 || base
> 36)
205 c
= (type
& ZEROPAD
) ? '0' : ' ';
212 } else if (type
& PLUS
) {
215 } else if (type
& SPACE
) {
220 if (type
& SPECIAL
) {
229 else while (num
!= 0)
232 tmp
[i
++] = digits
[do_div(&x
,base
)];
238 if (!(type
&(ZEROPAD
+LEFT
))) {
250 if (type
& SPECIAL
) {
255 } else if (base
==16) {
264 if (!(type
& LEFT
)) {
271 while (i
< precision
--) {
290 string(char* buf
, char* end
, const char* s
, int len
, int field_width
, int precision
, int flags
)
295 c
= (flags
& ZEROPAD
) ? '0' : ' ';
307 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
312 if ((unsigned int)len
> (unsigned int)precision
)
317 while (len
< field_width
--)
323 for (i
= 0; i
< len
; ++i
)
329 while (len
< field_width
--)
339 stringw(char* buf
, char* end
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
344 c
= (flags
& ZEROPAD
) ? '0' : ' ';
356 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
361 if ((unsigned int)len
> (unsigned int)precision
)
366 while (len
< field_width
--)
372 for (i
= 0; i
< len
; ++i
)
375 *buf
= (unsigned char)(*sw
++);
378 while (len
< field_width
--)
390 int __cdecl
_vsnprintf(char *buf
, size_t cnt
, const char *fmt
, va_list args
)
393 unsigned long long num
;
401 int flags
; /* flags to number() */
403 int field_width
; /* width of output field */
404 int precision
; /* min. # of digits for integers; max
405 number of chars for from string */
406 int qualifier
; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
408 /* clear the string buffer with zero so we do not need NULL terment it at end */
417 for ( ; *fmt
; ++fmt
) {
428 ++fmt
; /* this also skips first '%' */
430 case '-': flags
|= LEFT
; goto repeat
;
431 case '+': flags
|= PLUS
; goto repeat
;
432 case ' ': flags
|= SPACE
; goto repeat
;
433 case '#': flags
|= SPECIAL
; goto repeat
;
434 case '0': flags
|= ZEROPAD
; goto repeat
;
437 /* get field width */
440 field_width
= skip_atoi(&fmt
);
441 else if (*fmt
== '*') {
443 /* it's the next argument */
444 field_width
= va_arg(args
, int);
445 if (field_width
< 0) {
446 field_width
= -field_width
;
451 /* get the precision */
456 precision
= skip_atoi(&fmt
);
457 else if (*fmt
== '*') {
459 /* it's the next argument */
460 precision
= va_arg(args
, int);
466 /* get the conversion qualifier */
468 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
471 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
474 } else if (*fmt
== 'I' && *(fmt
+1) == '3' && *(fmt
+2) == '2') {
477 } else if (*fmt
== 'F' && *(fmt
+1) == 'p') {
486 case 'c': /* finished */
487 if (qualifier
== 'l' || qualifier
== 'w') {
489 /* print unicode string */
490 sw1
[0] = (wchar_t) va_arg(args
, int);
492 str
= stringw(str
, end
, (wchar_t *)&sw1
, -1, field_width
, precision
, flags
);
495 /* print ascii string */
496 s1
[0] = ( unsigned char) va_arg(args
, int);
498 str
= string(str
, end
, (char *)&s1
, -1, field_width
, precision
, flags
);
502 case 'C': /* finished */
504 while (--field_width
> 0) {
509 if (qualifier
== 'h') {
511 *str
= (unsigned char) va_arg(args
, int);
515 *str
= (unsigned char)(wchar_t) va_arg(args
, int);
518 while (--field_width
> 0) {
525 case 's': /* finished */
526 if (qualifier
== 'l' || qualifier
== 'w') {
527 /* print unicode string */
528 sw
= va_arg(args
, wchar_t *);
529 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
531 /* print ascii string */
532 s
= va_arg(args
, char *);
533 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
538 if (qualifier
== 'h') {
539 /* print ascii string */
540 s
= va_arg(args
, char *);
541 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
543 /* print unicode string */
544 sw
= va_arg(args
, wchar_t *);
545 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
550 if (qualifier
== 'w') {
551 /* print counted unicode string */
552 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
553 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
558 len
= pus
->Length
/ sizeof(WCHAR
);
560 str
= stringw(str
, end
, sw
, len
, field_width
, precision
, flags
);
562 /* print counted ascii string */
563 PANSI_STRING pus
= va_arg(args
, PANSI_STRING
);
564 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
571 str
= string(str
, end
, s
, len
, field_width
, precision
, flags
);
576 if ((flags
& LARGE
) == 0)
579 if (field_width
== -1) {
580 field_width
= 2 * sizeof(void *);
583 str
= number(str
, end
,
584 (ULONG_PTR
) va_arg(args
, void *), 16,
585 field_width
, precision
, flags
);
589 /* FIXME: What does C99 say about the overflow case here? */
590 if (qualifier
== 'l') {
591 long * ip
= va_arg(args
, long *);
594 int * ip
= va_arg(args
, int *);
599 /* float number formats - set up the flags and "break" */
605 _double
= (double)va_arg(args
, double);
606 if ( _isnan(_double
) ) {
615 } else if ( _isinf(_double
) < 0 ) {
624 } else if ( _isinf(_double
) > 0 ) {
634 if ( precision
== -1 )
636 str
= numberf(str
, end
, (int)_double
, base
, field_width
, precision
, flags
);
642 /* integer number formats - set up the flags and "break" */
673 if (qualifier
== 'I')
674 num
= va_arg(args
, unsigned long long);
675 else if (qualifier
== 'l') {
677 num
= va_arg(args
, long);
679 num
= va_arg(args
, unsigned long);
681 else if (qualifier
== 'h') {
683 num
= va_arg(args
, int);
685 num
= va_arg(args
, unsigned int);
689 num
= va_arg(args
, int);
691 num
= va_arg(args
, unsigned int);
693 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
698 /* don't write out a null byte if the buf size is zero */
707 int sprintf(char * buf
, const char *fmt
, ...)
713 i
=_vsnprintf(buf
,MAXLONG
,fmt
,args
);
722 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...)
728 i
=_vsnprintf(buf
,cnt
,fmt
,args
);
737 int __cdecl
vsprintf(char *buf
, const char *fmt
, va_list args
)
739 return _vsnprintf(buf
,MAXLONG
,fmt
,args
);