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--)
243 int _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
246 unsigned long long num;
252 int flags; /* flags to number() */
254 int field_width; /* width of output field */
255 int precision; /* min. # of digits for integers; max
256 number of chars for from string */
257 int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
266 for ( ; *fmt ; ++fmt) {
277 ++fmt; /* this also skips first '%' */
279 case '-': flags |= LEFT; goto repeat;
280 case '+': flags |= PLUS; goto repeat;
281 case ' ': flags |= SPACE; goto repeat;
282 case '#': flags |= SPECIAL; goto repeat;
283 case '0': flags |= ZEROPAD; goto repeat;
286 /* get field width */
289 field_width = skip_atoi(&fmt);
290 else if (*fmt == '*') {
292 /* it's the next argument */
293 field_width = va_arg(args, int);
294 if (field_width < 0) {
295 field_width = -field_width;
300 /* get the precision */
305 precision = skip_atoi(&fmt);
306 else if (*fmt == '*') {
308 /* it's the next argument */
309 precision = va_arg(args, int);
315 /* get the conversion qualifier */
317 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
320 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
329 case 'c': /* finished */
331 while (--field_width > 0)
337 if (qualifier == 'l' || qualifier == 'w')
340 *str = (unsigned char)(wchar_t) va_arg(args, int);
346 *str = (unsigned char) va_arg(args, int);
349 while (--field_width > 0)
357 case 'C': /* finished */
359 while (--field_width > 0)
365 if (qualifier == 'h')
368 *str = (unsigned char) va_arg(args, int);
374 *str = (unsigned char)(wchar_t) va_arg(args, int);
377 while (--field_width > 0)
385 case 's': /* finished */
386 if (qualifier == 'l' || qualifier == 'w') {
387 /* print unicode string */
388 sw = va_arg(args, wchar_t *);
389 str = stringw(str, end, sw, -1, field_width, precision, flags);
391 /* print ascii string */
392 s = va_arg(args, char *);
393 str = string(str, end, s, -1, field_width, precision, flags);
398 if (qualifier == 'h') {
399 /* print ascii string */
400 s = va_arg(args, char *);
401 str = string(str, end, s, -1, field_width, precision, flags);
403 /* print unicode string */
404 sw = va_arg(args, wchar_t *);
405 str = stringw(str, end, sw, -1, field_width, precision, flags);
410 if (qualifier == 'w') {
411 /* print counted unicode string */
412 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
413 if ((pus == NULL) || (pus->Buffer == NULL)) {
418 len = pus->Length / sizeof(WCHAR);
420 str = stringw(str, end, sw, len, field_width, precision, flags);
422 /* print counted ascii string */
423 PANSI_STRING pus = va_arg(args, PANSI_STRING);
424 if ((pus == NULL) || (pus->Buffer == NULL)) {
431 str = string(str, end, s, len, field_width, precision, flags);
436 if (field_width == -1) {
437 field_width = 2 * sizeof(void *);
440 str = number(str, end,
441 (unsigned long) va_arg(args, void *),
442 16, field_width, precision, flags);
446 /* FIXME: What does C99 say about the overflow case here? */
447 if (qualifier == 'l') {
448 long * ip = va_arg(args, long *);
451 int * ip = va_arg(args, int *);
456 /* integer number formats - set up the flags and "break" */
495 if (qualifier == 'I')
496 num = va_arg(args, unsigned long long);
497 else if (qualifier == 'l')
498 num = va_arg(args, unsigned long);
499 else if (qualifier == 'h') {
501 num = va_arg(args, int);
503 num = va_arg(args, unsigned int);
507 num = va_arg(args, int);
509 num = va_arg(args, unsigned int);
511 str = number(str, end, num, base, field_width, precision, flags);
517 /* don't write out a null byte if the buf size is zero */
524 int sprintf(char * buf, const char *fmt, ...)
530 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);
548 int vsprintf(char *buf, const char *fmt, va_list args)
550 return _vsnprintf(buf,INT_MAX,fmt,args);