3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/stdio/sprintf.c
6 * PURPOSE: Single byte sprintf functions
7 * PROGRAMMERS: David Welch
13 * linux/lib/vsprintf.c
15 * Copyright (C) 1991, 1992 Linus Torvalds
18 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
20 * Wirzenius wrote this portably, Torvalds fucked it up :-)
23 #include <ddk/ntddk.h>
26 #define __NO_CTYPE_INLINES
32 #include <ntdll/ntdll.h>
35 #define ZEROPAD 1 /* pad with zero */
36 #define SIGN 2 /* unsigned/signed long */
37 #define PLUS 4 /* show plus */
38 #define SPACE 8 /* space if plus */
39 #define LEFT 16 /* left justified */
40 #define SPECIAL 32 /* 0x */
41 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
44 #define do_div(n,base) ({ \
46 __res = ((unsigned long long) n) % (unsigned) base; \
47 n = ((unsigned long long) n) / (unsigned) base; \
51 static int skip_atoi(const char **s
)
56 i
= i
*10 + *((*s
)++) - '0';
62 number(char * buf
, char * end
, long long num
, int base
, int size
, int precision
, int type
)
66 const char *small_digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
67 const char *large_digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
70 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
73 if (base
< 2 || base
> 36)
75 c
= (type
& ZEROPAD
) ? '0' : ' ';
82 } else if (type
& PLUS
) {
85 } else if (type
& SPACE
) {
100 tmp
[i
++] = digits
[do_div(num
,base
)];
104 if (!(type
&(ZEROPAD
+LEFT
))) {
116 if (type
& SPECIAL
) {
121 } else if (base
==16) {
130 if (!(type
& LEFT
)) {
137 while (i
< precision
--) {
156 string(char* buf
, char* end
, const char* s
, int len
, int field_width
, int precision
, int flags
)
169 while ((unsigned int)len
< (unsigned int)precision
&& s
[len
])
174 if ((unsigned int)len
> (unsigned int)precision
)
179 while (len
< field_width
--)
185 for (i
= 0; i
< len
; ++i
)
191 while (len
< field_width
--)
201 stringw(char* buf
, char* end
, const wchar_t* sw
, int len
, int field_width
, int precision
, int flags
)
214 while ((unsigned int)len
< (unsigned int)precision
&& sw
[len
])
219 if ((unsigned int)len
> (unsigned int)precision
)
224 while (len
< field_width
--)
230 for (i
= 0; i
< len
; ++i
)
233 *buf
= (unsigned char)(*sw
++);
236 while (len
< field_width
--)
248 int _vsnprintf(char *buf
, size_t cnt
, const char *fmt
, va_list args
)
251 unsigned long long num
;
257 int flags
; /* flags to number() */
259 int field_width
; /* width of output field */
260 int precision
; /* min. # of digits for integers; max
261 number of chars for from string */
262 int qualifier
; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
271 for ( ; *fmt
; ++fmt
) {
282 ++fmt
; /* this also skips first '%' */
284 case '-': flags
|= LEFT
; goto repeat
;
285 case '+': flags
|= PLUS
; goto repeat
;
286 case ' ': flags
|= SPACE
; goto repeat
;
287 case '#': flags
|= SPECIAL
; goto repeat
;
288 case '0': flags
|= ZEROPAD
; goto repeat
;
291 /* get field width */
294 field_width
= skip_atoi(&fmt
);
295 else if (*fmt
== '*') {
297 /* it's the next argument */
298 field_width
= va_arg(args
, int);
299 if (field_width
< 0) {
300 field_width
= -field_width
;
305 /* get the precision */
310 precision
= skip_atoi(&fmt
);
311 else if (*fmt
== '*') {
313 /* it's the next argument */
314 precision
= va_arg(args
, int);
320 /* get the conversion qualifier */
322 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'w') {
325 } else if (*fmt
== 'I' && *(fmt
+1) == '6' && *(fmt
+2) == '4') {
334 case 'c': /* finished */
336 while (--field_width
> 0) {
341 if (qualifier
== 'l' || qualifier
== 'w') {
343 *str
= (unsigned char)(wchar_t) va_arg(args
, int);
347 *str
= (unsigned char) va_arg(args
, int);
350 while (--field_width
> 0) {
357 case 'C': /* finished */
359 while (--field_width
> 0) {
364 if (qualifier
== 'h') {
366 *str
= (unsigned char) va_arg(args
, int);
370 *str
= (unsigned char)(wchar_t) va_arg(args
, int);
373 while (--field_width
> 0) {
380 case 's': /* finished */
381 if (qualifier
== 'l' || qualifier
== 'w') {
382 /* print unicode string */
383 sw
= va_arg(args
, wchar_t *);
384 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
386 /* print ascii string */
387 s
= va_arg(args
, char *);
388 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
393 if (qualifier
== 'h') {
394 /* print ascii string */
395 s
= va_arg(args
, char *);
396 str
= string(str
, end
, s
, -1, field_width
, precision
, flags
);
398 /* print unicode string */
399 sw
= va_arg(args
, wchar_t *);
400 str
= stringw(str
, end
, sw
, -1, field_width
, precision
, flags
);
405 if (qualifier
== 'w') {
406 /* print counted unicode string */
407 PUNICODE_STRING pus
= va_arg(args
, PUNICODE_STRING
);
408 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
413 len
= pus
->Length
/ sizeof(WCHAR
);
415 str
= stringw(str
, end
, sw
, len
, field_width
, precision
, flags
);
417 /* print counted ascii string */
418 PANSI_STRING pus
= va_arg(args
, PANSI_STRING
);
419 if ((pus
== NULL
) || (pus
->Buffer
== NULL
)) {
426 str
= string(str
, end
, s
, len
, field_width
, precision
, flags
);
431 if (field_width
== -1) {
432 field_width
= 2 * sizeof(void *);
435 str
= number(str
, end
,
436 (unsigned long) va_arg(args
, void *), 16,
437 field_width
, precision
, flags
);
441 /* FIXME: What does C99 say about the overflow case here? */
442 if (qualifier
== 'l') {
443 long * ip
= va_arg(args
, long *);
446 int * ip
= va_arg(args
, int *);
451 /* integer number formats - set up the flags and "break" */
487 if (qualifier
== 'I')
488 num
= va_arg(args
, unsigned long long);
489 else if (qualifier
== 'l') {
491 num
= va_arg(args
, long);
493 num
= va_arg(args
, unsigned long);
495 else if (qualifier
== 'h') {
497 num
= va_arg(args
, int);
499 num
= va_arg(args
, unsigned int);
503 num
= va_arg(args
, int);
505 num
= va_arg(args
, unsigned int);
507 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
512 /* don't write out a null byte if the buf size is zero */
521 int sprintf(char * buf
, const char *fmt
, ...)
527 i
=_vsnprintf(buf
,INT_MAX
,fmt
,args
);
536 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...)
542 i
=_vsnprintf(buf
,cnt
,fmt
,args
);
551 int vsprintf(char *buf
, const char *fmt
, va_list args
)
553 return _vsnprintf(buf
,INT_MAX
,fmt
,args
);