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 :-)
22 #define ZEROPAD 1 /* pad with zero */
23 #define SIGN 2 /* unsigned/signed long */
24 #define PLUS 4 /* show plus */
25 #define SPACE 8 /* space if plus */
26 #define LEFT 16 /* left justified */
27 #define SPECIAL 32 /* 0x */
28 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
29 #define REMOVEHEX 256 /* use 256 as remve 0x frim BASE 16 */
31 unsigned int mantissal
:32;
32 unsigned int mantissah
:20;
33 unsigned int exponent
:11;
49 return ( x
.x
->exponent
== 0x7ff && ( x
.x
->mantissah
== 0 && x
.x
->mantissal
== 0 ));
63 return ( x
.x
->exponent
== 0x7ff && ( x
.x
->mantissah
!= 0 || x
.x
->mantissal
!= 0 ));
70 do_div(long long *n
, int base
)
73 a
= ((unsigned long long) *n
) % (unsigned) base
;
74 *n
= ((unsigned long long) *n
) / (unsigned) base
;
79 static int skip_atoi(const char **s
)
84 i
= i
*10 + *((*s
)++) - '0';
90 number(char * buf
, char * end
, long long num
, int base
, int size
, int precision
, int type
)
94 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
95 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
98 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
101 if (base
< 2 || base
> 36)
103 c
= (type
& ZEROPAD
) ? '0' : ' ';
110 } else if (type
& PLUS
) {
113 } else if (type
& SPACE
) {
119 if ((type
& SPECIAL
) && ((type
& REMOVEHEX
) == 0)) {
125 if ((num
== 0) && (precision
!=0))
127 else while (num
!= 0)
128 tmp
[i
++] = digits
[do_div(&num
,base
)];
132 if (!(type
&(ZEROPAD
+LEFT
))) {
145 if ((type
& SPECIAL
) && ((type
& REMOVEHEX
) == 0)) {
156 if (!(type
& LEFT
)) {
163 while (i
< precision
--) {
183 numberf(char * buf
, char * end
, double num
, int base
, int size
, int precision
, int type
)
187 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
188 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
193 the float version of number is direcly copy of number
196 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
199 if (base
< 2 || base
> 36)
201 c
= (type
& ZEROPAD
) ? '0' : ' ';
208 } else if (type
& PLUS
) {
211 } else if (type
& SPACE
) {
216 if (type
& SPECIAL
) {
225 else while (num
!= 0)
228 tmp
[i
++] = digits
[do_div(&x
,base
)];
229 #ifndef _M_ARM // Missing __floatdidf in CeGCC 0.55 -- GCC 4.4
236 if (!(type
&(ZEROPAD
+LEFT
))) {
248 if (type
& SPECIAL
) {
253 } else if (base
==16) {
262 if (!(type
& LEFT
)) {
269 while (i
< precision
--) {
288 string(char* buf
, char* end
, const char* s
, int len
, int field_width
, int precision
, int flags
)
293 c
= (flags
& ZEROPAD
) ? '0' : ' ';
305 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
310 if ((unsigned int)len
> (unsigned int)precision
)
315 while (len
< field_width
--)
321 for (i
= 0; i
< len
; ++i
)
327 while (len
< field_width
--)
337 stringw(char* buf
, char* end
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
342 c
= (flags
& ZEROPAD
) ? '0' : ' ';
354 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
359 if ((unsigned int)len
> (unsigned int)precision
)
364 while (len
< field_width
--)
370 for (i
= 0; i
< len
; ++i
)
373 *buf
= (unsigned char)(*sw
++);
376 while (len
< field_width
--)
388 int __cdecl
_vsnprintf(char *buf
, size_t cnt
, const char *fmt
, va_list args
)
391 unsigned long long num
;
399 int flags
; /* flags to number() */
401 int field_width
; /* width of output field */
402 int precision
; /* min. # of digits for integers; max
403 number of chars for from string */
404 int qualifier
; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
406 /* clear the string buffer with zero so we do not need NULL terment it at end */
415 for ( ; *fmt
; ++fmt
) {
426 ++fmt
; /* this also skips first '%' */
428 case '-': flags
|= LEFT
; goto repeat
;
429 case '+': flags
|= PLUS
; goto repeat
;
430 case ' ': flags
|= SPACE
; goto repeat
;
431 case '#': flags
|= SPECIAL
; goto repeat
;
432 case '0': flags
|= ZEROPAD
; goto repeat
;
435 /* get field width */
438 field_width
= skip_atoi(&fmt
);
439 else if (*fmt
== '*') {
441 /* it's the next argument */
442 field_width
= va_arg(args
, int);
443 if (field_width
< 0) {
444 field_width
= -field_width
;
449 /* get the precision */
454 precision
= skip_atoi(&fmt
);
455 else if (*fmt
== '*') {
457 /* it's the next argument */
458 precision
= va_arg(args
, int);
464 /* get the conversion qualifier */
466 if (*fmt
== 'l' && *(fmt
+1) == 'l') {
469 } else if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
472 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
475 } else if (*fmt
== 'I' && *(fmt
+1) == '3' && *(fmt
+2) == '2') {
478 } else if (*fmt
== 'F' && *(fmt
+1) == 'p') {
487 case 'c': /* finished */
488 if (qualifier
== 'l' || qualifier
== 'w') {
490 /* print unicode string */
491 sw1
[0] = (wchar_t) va_arg(args
, int);
493 str
= stringw(str
, end
, (wchar_t *)&sw1
, -1, field_width
, precision
, flags
);
496 /* print ascii string */
497 s1
[0] = ( unsigned char) va_arg(args
, int);
499 str
= string(str
, end
, (char *)&s1
, -1, field_width
, precision
, flags
);
503 case 'C': /* finished */
505 while (--field_width
> 0) {
510 if (qualifier
== 'h') {
512 *str
= (unsigned char) va_arg(args
, int);
516 *str
= (unsigned char)(wchar_t) va_arg(args
, int);
519 while (--field_width
> 0) {
526 case 's': /* finished */
527 if (qualifier
== 'l' || qualifier
== 'w') {
528 /* print unicode string */
529 sw
= va_arg(args
, wchar_t *);
530 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
532 /* print ascii string */
533 s
= va_arg(args
, char *);
534 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
539 if (qualifier
== 'h') {
540 /* print ascii string */
541 s
= va_arg(args
, char *);
542 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
544 /* print unicode string */
545 sw
= va_arg(args
, wchar_t *);
546 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
551 if (qualifier
== 'w') {
552 /* print counted unicode string */
553 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
554 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
559 len
= pus
->Length
/ sizeof(WCHAR
);
561 str
= stringw(str
, end
, sw
, len
, field_width
, precision
, flags
);
563 /* print counted ascii string */
564 PANSI_STRING pus
= va_arg(args
, PANSI_STRING
);
565 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
572 str
= string(str
, end
, s
, len
, field_width
, precision
, flags
);
577 if ((flags
& LARGE
) == 0)
580 if (field_width
== -1) {
581 field_width
= 2 * sizeof(void *);
584 str
= number(str
, end
,
585 (ULONG_PTR
) va_arg(args
, void *), 16,
586 field_width
, precision
, flags
);
590 /* FIXME: What does C99 say about the overflow case here? */
591 if (qualifier
== 'l') {
592 long * ip
= va_arg(args
, long *);
595 int * ip
= va_arg(args
, int *);
600 /* float number formats - set up the flags and "break" */
606 _double
= (double)va_arg(args
, double);
607 if ( _isnan(_double
) ) {
616 } else if ( _isinf(_double
) < 0 ) {
625 } else if ( _isinf(_double
) > 0 ) {
635 if ( precision
== -1 )
637 str
= numberf(str
, end
, (int)_double
, base
, field_width
, precision
, flags
);
643 /* integer number formats - set up the flags and "break" */
674 if (qualifier
== 'I')
675 num
= va_arg(args
, unsigned long long);
676 else if (qualifier
== 'l') {
678 num
= va_arg(args
, long);
680 num
= va_arg(args
, unsigned long);
682 else if (qualifier
== 'h') {
684 num
= va_arg(args
, int);
686 num
= va_arg(args
, unsigned int);
690 num
= va_arg(args
, int);
692 num
= va_arg(args
, unsigned int);
694 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
699 /* don't write out a null byte if the buf size is zero */
708 int sprintf(char * buf
, const char *fmt
, ...)
714 i
=_vsnprintf(buf
,MAXLONG
,fmt
,args
);
723 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...)
729 i
=_vsnprintf(buf
,cnt
,fmt
,args
);
738 int __cdecl
vsprintf(char *buf
, const char *fmt
, va_list args
)
740 return _vsnprintf(buf
,MAXLONG
,fmt
,args
);