3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/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>
30 #include <internal/debug.h>
33 #define ZEROPAD 1 /* pad with zero */
34 #define SIGN 2 /* unsigned/signed long */
35 #define PLUS 4 /* show plus */
36 #define SPACE 8 /* space if plus */
37 #define LEFT 16 /* left justified */
38 #define SPECIAL 32 /* 0x */
39 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
42 #define do_div(n,base) ({ \
44 __res = ((unsigned long long) n) % (unsigned) base; \
45 n = ((unsigned long long) n) / (unsigned) base; \
49 static int skip_atoi(const char **s)
54 i = i*10 + *((*s)++) - '0';
60 number(char *buf, char *end, long long num, int base, int size, int precision, int type)
64 const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
65 const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
68 digits = (type & LARGE) ? large_digits : small_digits;
71 if (base < 2 || base > 36)
73 c = (type & ZEROPAD) ? '0' : ' ';
80 } else if (type & PLUS) {
83 } else if (type & SPACE) {
98 tmp[i++] = digits[do_div(num,base)];
102 if (!(type&(ZEROPAD+LEFT))) {
114 if (type & SPECIAL) {
119 } else if (base==16) {
128 if (!(type & LEFT)) {
135 while (i < precision--) {
154 string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)
167 while (s[len] && (unsigned int)len < (unsigned int)precision)
172 if ((unsigned int)len > (unsigned int)precision)
177 while (len < field_width--)
183 for (i = 0; i < len; ++i)
189 while (len < field_width--)
199 stringw(char* buf, char* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
212 while (sw[len] && (unsigned int)len < (unsigned int)precision)
217 if ((unsigned int)len > (unsigned int)precision)
222 while (len < field_width--)
228 for (i = 0; i < len; ++i)
231 *buf = (unsigned char)(*sw++);
234 while (len < field_width--)
246 int _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
249 unsigned long long num;
255 int flags; /* flags to number() */
257 int field_width; /* width of output field */
258 int precision; /* min. # of digits for integers; max
259 number of chars for from string */
260 int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
269 for ( ; *fmt ; ++fmt) {
280 ++fmt; /* this also skips first '%' */
282 case '-': flags |= LEFT; goto repeat;
283 case '+': flags |= PLUS; goto repeat;
284 case ' ': flags |= SPACE; goto repeat;
285 case '#': flags |= SPECIAL; goto repeat;
286 case '0': flags |= ZEROPAD; goto repeat;
289 /* get field width */
292 field_width = skip_atoi(&fmt);
293 else if (*fmt == '*') {
295 /* it's the next argument */
296 field_width = va_arg(args, int);
297 if (field_width < 0) {
298 field_width = -field_width;
303 /* get the precision */
308 precision = skip_atoi(&fmt);
309 else if (*fmt == '*') {
311 /* it's the next argument */
312 precision = va_arg(args, int);
318 /* get the conversion qualifier */
320 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
323 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
332 case 'c': /* finished */
334 while (--field_width > 0)
340 if (qualifier == 'l' || qualifier == 'w')
343 *str = (unsigned char)(wchar_t) va_arg(args, int);
349 *str = (unsigned char) va_arg(args, int);
352 while (--field_width > 0)
360 case 'C': /* finished */
362 while (--field_width > 0)
368 if (qualifier == 'h')
371 *str = (unsigned char) va_arg(args, int);
377 *str = (unsigned char)(wchar_t) va_arg(args, int);
380 while (--field_width > 0)
388 case 's': /* finished */
389 if (qualifier == 'l' || qualifier == 'w') {
390 /* print unicode string */
391 sw = va_arg(args, wchar_t *);
392 str = stringw(str, end, sw, -1, field_width, precision, flags);
394 /* print ascii string */
395 s = va_arg(args, char *);
396 str = string(str, end, s, -1, field_width, precision, flags);
401 if (qualifier == 'h') {
402 /* print ascii string */
403 s = va_arg(args, char *);
404 str = string(str, end, s, -1, field_width, precision, flags);
406 /* print unicode string */
407 sw = va_arg(args, wchar_t *);
408 str = stringw(str, end, sw, -1, field_width, precision, flags);
413 if (qualifier == 'w') {
414 /* print counted unicode string */
415 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
416 if ((pus == NULL) || (pus->Buffer == NULL)) {
421 len = pus->Length / sizeof(WCHAR);
423 str = stringw(str, end, sw, len, field_width, precision, flags);
425 /* print counted ascii string */
426 PANSI_STRING pus = va_arg(args, PANSI_STRING);
427 if ((pus == NULL) || (pus->Buffer == NULL)) {
434 str = string(str, end, s, len, field_width, precision, flags);
439 if (field_width == -1) {
440 field_width = 2 * sizeof(void *);
443 str = number(str, end,
444 (unsigned long) va_arg(args, void *),
445 16, field_width, precision, flags);
449 /* FIXME: What does C99 say about the overflow case here? */
450 if (qualifier == 'l') {
451 long * ip = va_arg(args, long *);
454 int * ip = va_arg(args, int *);
459 /* integer number formats - set up the flags and "break" */
498 if (qualifier == 'I')
499 num = va_arg(args, unsigned long long);
500 else if (qualifier == 'l')
501 num = va_arg(args, unsigned long);
502 else if (qualifier == 'h') {
504 num = va_arg(args, int);
506 num = va_arg(args, unsigned int);
510 num = va_arg(args, int);
512 num = va_arg(args, unsigned int);
514 str = number(str, end, num, base, field_width, precision, flags);
520 /* don't write out a null byte if the buf size is zero */
530 int sprintf(char * buf, const char *fmt, ...)
536 i=_vsnprintf(buf,INT_MAX,fmt,args);
545 int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
551 i=_vsnprintf(buf,cnt,fmt,args);
560 int vsprintf(char *buf, const char *fmt, va_list args)
562 return _vsnprintf(buf,INT_MAX,fmt,args);