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
)];
234 if (!(type
&(ZEROPAD
+LEFT
))) {
246 if (type
& SPECIAL
) {
251 } else if (base
==16) {
260 if (!(type
& LEFT
)) {
267 while (i
< precision
--) {
286 string(char* buf
, char* end
, const char* s
, int len
, int field_width
, int precision
, int flags
)
291 c
= (flags
& ZEROPAD
) ? '0' : ' ';
303 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
308 if ((unsigned int)len
> (unsigned int)precision
)
313 while (len
< field_width
--)
319 for (i
= 0; i
< len
; ++i
)
325 while (len
< field_width
--)
335 stringw(char* buf
, char* end
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
340 c
= (flags
& ZEROPAD
) ? '0' : ' ';
352 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
357 if ((unsigned int)len
> (unsigned int)precision
)
362 while (len
< field_width
--)
368 for (i
= 0; i
< len
; ++i
)
371 *buf
= (unsigned char)(*sw
++);
374 while (len
< field_width
--)
386 int __cdecl
_vsnprintf(char *buf
, size_t cnt
, const char *fmt
, va_list args
)
389 unsigned long long num
;
397 int flags
; /* flags to number() */
399 int field_width
; /* width of output field */
400 int precision
; /* min. # of digits for integers; max
401 number of chars for from string */
402 int qualifier
; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
404 /* clear the string buffer with zero so we do not need NULL terment it at end */
413 for ( ; *fmt
; ++fmt
) {
424 ++fmt
; /* this also skips first '%' */
426 case '-': flags
|= LEFT
; goto repeat
;
427 case '+': flags
|= PLUS
; goto repeat
;
428 case ' ': flags
|= SPACE
; goto repeat
;
429 case '#': flags
|= SPECIAL
; goto repeat
;
430 case '0': flags
|= ZEROPAD
; goto repeat
;
433 /* get field width */
436 field_width
= skip_atoi(&fmt
);
437 else if (*fmt
== '*') {
439 /* it's the next argument */
440 field_width
= va_arg(args
, int);
441 if (field_width
< 0) {
442 field_width
= -field_width
;
447 /* get the precision */
452 precision
= skip_atoi(&fmt
);
453 else if (*fmt
== '*') {
455 /* it's the next argument */
456 precision
= va_arg(args
, int);
462 /* get the conversion qualifier */
464 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
467 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
470 } else if (*fmt
== 'I' && *(fmt
+1) == '3' && *(fmt
+2) == '2') {
473 } else if (*fmt
== 'F' && *(fmt
+1) == 'p') {
482 case 'c': /* finished */
483 if (qualifier
== 'l' || qualifier
== 'w') {
485 /* print unicode string */
486 sw1
[0] = (wchar_t) va_arg(args
, int);
488 str
= stringw(str
, end
, (wchar_t *)&sw1
, -1, field_width
, precision
, flags
);
491 /* print ascii string */
492 s1
[0] = ( unsigned char) va_arg(args
, int);
494 str
= string(str
, end
, (char *)&s1
, -1, field_width
, precision
, flags
);
498 case 'C': /* finished */
500 while (--field_width
> 0) {
505 if (qualifier
== 'h') {
507 *str
= (unsigned char) va_arg(args
, int);
511 *str
= (unsigned char)(wchar_t) va_arg(args
, int);
514 while (--field_width
> 0) {
521 case 's': /* finished */
522 if (qualifier
== 'l' || qualifier
== 'w') {
523 /* print unicode string */
524 sw
= va_arg(args
, wchar_t *);
525 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
527 /* print ascii string */
528 s
= va_arg(args
, char *);
529 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
534 if (qualifier
== 'h') {
535 /* print ascii string */
536 s
= va_arg(args
, char *);
537 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
539 /* print unicode string */
540 sw
= va_arg(args
, wchar_t *);
541 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
546 if (qualifier
== 'w') {
547 /* print counted unicode string */
548 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
549 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
554 len
= pus
->Length
/ sizeof(WCHAR
);
556 str
= stringw(str
, end
, sw
, len
, field_width
, precision
, flags
);
558 /* print counted ascii string */
559 PANSI_STRING pus
= va_arg(args
, PANSI_STRING
);
560 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
567 str
= string(str
, end
, s
, len
, field_width
, precision
, flags
);
572 if ((flags
& LARGE
) == 0)
575 if (field_width
== -1) {
576 field_width
= 2 * sizeof(void *);
579 str
= number(str
, end
,
580 (ULONG_PTR
) va_arg(args
, void *), 16,
581 field_width
, precision
, flags
);
585 /* FIXME: What does C99 say about the overflow case here? */
586 if (qualifier
== 'l') {
587 long * ip
= va_arg(args
, long *);
590 int * ip
= va_arg(args
, int *);
595 /* float number formats - set up the flags and "break" */
601 _double
= (double)va_arg(args
, double);
602 if ( _isnan(_double
) ) {
611 } else if ( _isinf(_double
) < 0 ) {
620 } else if ( _isinf(_double
) > 0 ) {
630 if ( precision
== -1 )
632 str
= numberf(str
, end
, (int)_double
, base
, field_width
, precision
, flags
);
638 /* integer number formats - set up the flags and "break" */
669 if (qualifier
== 'I')
670 num
= va_arg(args
, unsigned long long);
671 else if (qualifier
== 'l') {
673 num
= va_arg(args
, long);
675 num
= va_arg(args
, unsigned long);
677 else if (qualifier
== 'h') {
679 num
= va_arg(args
, int);
681 num
= va_arg(args
, unsigned int);
685 num
= va_arg(args
, int);
687 num
= va_arg(args
, unsigned int);
689 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
694 /* don't write out a null byte if the buf size is zero */
703 int sprintf(char * buf
, const char *fmt
, ...)
709 i
=_vsnprintf(buf
,MAXLONG
,fmt
,args
);
718 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...)
724 i
=_vsnprintf(buf
,cnt
,fmt
,args
);
733 int __cdecl
vsprintf(char *buf
, const char *fmt
, va_list args
)
735 return _vsnprintf(buf
,MAXLONG
,fmt
,args
);