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 :-)
25 #define ZEROPAD 1 /* pad with zero */
26 #define SIGN 2 /* unsigned/signed long */
27 #define PLUS 4 /* show plus */
28 #define SPACE 8 /* space if plus */
29 #define LEFT 16 /* left justified */
30 #define SPECIAL 32 /* 0x */
31 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
32 #define REMOVEHEX 256 /* use 256 as remve 0x frim BASE 16 */
37 do_div(long long *n
, int base
)
40 a
= ((unsigned long long) *n
) % (unsigned) base
;
41 *n
= ((unsigned long long) *n
) / (unsigned) base
;
46 static int skip_atoi(const char **s
)
51 i
= i
*10 + *((*s
)++) - '0';
57 number(char * buf
, char * end
, long long num
, int base
, int size
, int precision
, int type
)
61 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
62 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
65 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
68 if (base
< 2 || base
> 36)
70 c
= (type
& ZEROPAD
) ? '0' : ' ';
77 } else if (type
& PLUS
) {
80 } else if (type
& SPACE
) {
86 if ((type
& SPECIAL
) && ((type
& REMOVEHEX
) == 0)) {
92 if ((num
== 0) && (precision
!=0))
95 tmp
[i
++] = digits
[do_div(&num
,base
)];
99 if (!(type
&(ZEROPAD
+LEFT
))) {
112 if ((type
& SPECIAL
) && ((type
& REMOVEHEX
) == 0)) {
123 if (!(type
& LEFT
)) {
130 while (i
< precision
--) {
150 numberf(char * buf
, char * end
, double num
, int base
, int size
, int precision
, int type
)
154 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
155 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
160 the float version of number is direcly copy of number
163 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
166 if (base
< 2 || base
> 36)
168 c
= (type
& ZEROPAD
) ? '0' : ' ';
174 } else if (type
& PLUS
) {
177 } else if (type
& SPACE
) {
181 if (type
& SPECIAL
) {
190 else while (num
!= 0)
193 tmp
[i
++] = digits
[do_div(&x
,base
)];
199 if (!(type
&(ZEROPAD
+LEFT
))) {
211 if (type
& SPECIAL
) {
216 } else if (base
==16) {
225 if (!(type
& LEFT
)) {
232 if (type
& ZEROPAD
) {
233 while (i
< precision
--) {
253 string(char* buf
, char* end
, const char* s
, int len
, int field_width
, int precision
, int flags
)
258 c
= (flags
& ZEROPAD
) ? '0' : ' ';
270 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
275 if ((unsigned int)len
> (unsigned int)precision
)
280 while (len
< field_width
--)
286 for (i
= 0; i
< len
; ++i
)
292 while (len
< field_width
--)
302 stringw(char* buf
, char* end
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
307 c
= (flags
& ZEROPAD
) ? '0' : ' ';
319 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
324 if ((unsigned int)len
> (unsigned int)precision
)
329 while (len
< field_width
--)
335 for (i
= 0; i
< len
; ++i
)
338 *buf
= (unsigned char)(*sw
++);
341 while (len
< field_width
--)
353 int __cdecl
lnx_vsnprintf(char *buf
, size_t cnt
, const char *fmt
, va_list args
)
356 unsigned long long num
;
364 int flags
; /* flags to number() */
366 int field_width
; /* width of output field */
367 int precision
; /* min. # of digits for integers; max
368 number of chars for from string */
369 int qualifier
; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
371 /* clear the string buffer with zero so we do not need NULL terment it at end */
380 for ( ; *fmt
; ++fmt
) {
391 ++fmt
; /* this also skips first '%' */
393 case '-': flags
|= LEFT
; goto repeat
;
394 case '+': flags
|= PLUS
; goto repeat
;
395 case ' ': flags
|= SPACE
; goto repeat
;
396 case '#': flags
|= SPECIAL
; goto repeat
;
397 case '0': flags
|= ZEROPAD
; goto repeat
;
400 /* get field width */
403 field_width
= skip_atoi(&fmt
);
404 else if (*fmt
== '*') {
406 /* it's the next argument */
407 field_width
= va_arg(args
, int);
408 if (field_width
< 0) {
409 field_width
= -field_width
;
414 /* get the precision */
419 precision
= skip_atoi(&fmt
);
420 else if (*fmt
== '*') {
422 /* it's the next argument */
423 precision
= va_arg(args
, int);
429 /* get the conversion qualifier */
431 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
434 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
437 } else if (*fmt
== 'I' && *(fmt
+1) == '3' && *(fmt
+2) == '2') {
440 } else if (*fmt
== 'F' && *(fmt
+1) == 'p') {
449 case 'c': /* finished */
450 if (qualifier
== 'l' || qualifier
== 'w') {
452 /* print unicode string */
453 sw1
[0] = (wchar_t) va_arg(args
, int);
455 str
= stringw(str
, end
, (wchar_t *)&sw1
, -1, field_width
, precision
, flags
);
458 /* print ascii string */
459 s1
[0] = ( unsigned char) va_arg(args
, int);
461 str
= string(str
, end
, (char *)&s1
, -1, field_width
, precision
, flags
);
465 case 'C': /* finished */
467 while (--field_width
> 0) {
472 if (qualifier
== 'h') {
474 *str
= (unsigned char) va_arg(args
, int);
478 *str
= (unsigned char)(wchar_t) va_arg(args
, int);
481 while (--field_width
> 0) {
488 case 's': /* finished */
489 if (qualifier
== 'l' || qualifier
== 'w') {
490 /* print unicode string */
491 sw
= va_arg(args
, wchar_t *);
492 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
494 /* print ascii string */
495 s
= va_arg(args
, char *);
496 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
501 if (qualifier
== 'h') {
502 /* print ascii string */
503 s
= va_arg(args
, char *);
504 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
506 /* print unicode string */
507 sw
= va_arg(args
, wchar_t *);
508 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
513 if (qualifier
== 'w') {
514 /* print counted unicode string */
515 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
516 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
521 len
= pus
->Length
/ sizeof(WCHAR
);
523 str
= stringw(str
, end
, sw
, len
, field_width
, precision
, flags
);
525 /* print counted ascii string */
526 PANSI_STRING pus
= va_arg(args
, PANSI_STRING
);
527 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
534 str
= string(str
, end
, s
, len
, field_width
, precision
, flags
);
539 if ((flags
& LARGE
) == 0)
542 if (field_width
== -1) {
543 field_width
= 2 * sizeof(void *);
546 str
= number(str
, end
,
547 (unsigned long) va_arg(args
, void *), 16,
548 field_width
, precision
, flags
);
552 /* FIXME: What does C99 say about the overflow case here? */
553 if (qualifier
== 'l') {
554 long * ip
= va_arg(args
, long *);
557 int * ip
= va_arg(args
, int *);
562 /* float number formats - set up the flags and "break" */
568 _double
= (double)va_arg(args
, double);
569 if ( _isnan(_double
) ) {
578 } else if ( _isinf(_double
) < 0 ) {
587 } else if ( _isinf(_double
) > 0 ) {
597 if ( precision
== -1 )
599 str
= numberf(str
, end
, (int)_double
, base
, field_width
, precision
, flags
);
605 /* integer number formats - set up the flags and "break" */
636 if (qualifier
== 'I')
637 num
= va_arg(args
, unsigned long long);
638 else if (qualifier
== 'l') {
640 num
= va_arg(args
, long);
642 num
= va_arg(args
, unsigned long);
644 else if (qualifier
== 'h') {
646 num
= va_arg(args
, int);
648 num
= va_arg(args
, unsigned int);
652 num
= va_arg(args
, int);
654 num
= va_arg(args
, unsigned int);
656 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
661 /* don't write out a null byte if the buf size is zero */
670 int lnx_sprintf(char * buf
, const char *fmt
, ...)
676 i
=lnx_vsnprintf(buf
,MAXLONG
,fmt
,args
);
685 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...)
691 i
=_vsnprintf(buf
,cnt
,fmt
,args
);
700 int __cdecl
vsprintf(char *buf
, const char *fmt
, va_list args
)
702 return _vsnprintf(buf
,MAXLONG
,fmt
,args
);