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>
31 #include <ntdll/ntdll.h>
34 #define ZEROPAD 1 /* pad with zero */
35 #define SIGN 2 /* unsigned/signed long */
36 #define PLUS 4 /* show plus */
37 #define SPACE 8 /* space if plus */
38 #define LEFT 16 /* left justified */
39 #define SPECIAL 32 /* 0x */
40 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
43 #define do_div(n,base) ({ \
45 __res = ((unsigned long long) n) % (unsigned) base; \
46 n = ((unsigned long long) n) / (unsigned) base; \
50 static int skip_atoi(const char **s)
55 i = i*10 + *((*s)++) - '0';
61 number(char * buf, char * end, long long num, int base, int size, int precision, int type)
65 const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
66 const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
69 digits = (type & LARGE) ? large_digits : small_digits;
72 if (base < 2 || base > 36)
74 c = (type & ZEROPAD) ? '0' : ' ';
81 } else if (type & PLUS) {
84 } else if (type & SPACE) {
99 tmp[i++] = digits[do_div(num,base)];
103 if (!(type&(ZEROPAD+LEFT))) {
115 if (type & SPECIAL) {
120 } else if (base==16) {
129 if (!(type & LEFT)) {
136 while (i < precision--) {
155 string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)
168 while (s[len] && (unsigned int)len < (unsigned int)precision)
173 if ((unsigned int)len > (unsigned int)precision)
178 while (len < field_width--)
184 for (i = 0; i < len; ++i)
190 while (len < field_width--)
200 stringw(char* buf, char* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
213 while (sw[len] && (unsigned int)len < (unsigned int)precision)
218 if ((unsigned int)len > (unsigned int)precision)
223 while (len < field_width--)
229 for (i = 0; i < len; ++i)
232 *buf = (unsigned char)(*sw++);
235 while (len < field_width--)
247 int _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
250 unsigned long long num;
256 int flags; /* flags to number() */
258 int field_width; /* width of output field */
259 int precision; /* min. # of digits for integers; max
260 number of chars for from string */
261 int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
270 for ( ; *fmt ; ++fmt) {
281 ++fmt; /* this also skips first '%' */
283 case '-': flags |= LEFT; goto repeat;
284 case '+': flags |= PLUS; goto repeat;
285 case ' ': flags |= SPACE; goto repeat;
286 case '#': flags |= SPECIAL; goto repeat;
287 case '0': flags |= ZEROPAD; goto repeat;
290 /* get field width */
293 field_width = skip_atoi(&fmt);
294 else if (*fmt == '*') {
296 /* it's the next argument */
297 field_width = va_arg(args, int);
298 if (field_width < 0) {
299 field_width = -field_width;
304 /* get the precision */
309 precision = skip_atoi(&fmt);
310 else if (*fmt == '*') {
312 /* it's the next argument */
313 precision = va_arg(args, int);
319 /* get the conversion qualifier */
321 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
324 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
333 case 'c': /* finished */
335 while (--field_width > 0) {
340 if (qualifier == 'l' || qualifier == 'w') {
342 *str = (unsigned char)(wchar_t) va_arg(args, int);
346 *str = (unsigned char) va_arg(args, int);
349 while (--field_width > 0) {
356 case 'C': /* finished */
358 while (--field_width > 0) {
363 if (qualifier == 'h') {
365 *str = (unsigned char) va_arg(args, int);
369 *str = (unsigned char)(wchar_t) va_arg(args, int);
372 while (--field_width > 0) {
379 case 's': /* finished */
380 if (qualifier == 'l' || qualifier == 'w') {
381 /* print unicode string */
382 sw = va_arg(args, wchar_t *);
383 str = stringw(str, end, sw, -1, field_width, precision, flags);
385 /* print ascii string */
386 s = va_arg(args, char *);
387 str = string(str, end, s, -1, field_width, precision, flags);
392 if (qualifier == 'h') {
393 /* print ascii string */
394 s = va_arg(args, char *);
395 str = string(str, end, s, -1, field_width, precision, flags);
397 /* print unicode string */
398 sw = va_arg(args, wchar_t *);
399 str = stringw(str, end, sw, -1, field_width, precision, flags);
404 if (qualifier == 'w') {
405 /* print counted unicode string */
406 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
407 if ((pus == NULL) || (pus->Buffer == NULL)) {
412 len = pus->Length / sizeof(WCHAR);
414 str = stringw(str, end, sw, len, field_width, precision, flags);
416 /* print counted ascii string */
417 PANSI_STRING pus = va_arg(args, PANSI_STRING);
418 if ((pus == NULL) || (pus->Buffer == NULL)) {
425 str = string(str, end, s, len, field_width, precision, flags);
430 if (field_width == -1) {
431 field_width = 2 * sizeof(void *);
434 str = number(str, end,
435 (unsigned long) va_arg(args, void *), 16,
436 field_width, precision, flags);
440 /* FIXME: What does C99 say about the overflow case here? */
441 if (qualifier == 'l') {
442 long * ip = va_arg(args, long *);
445 int * ip = va_arg(args, int *);
450 /* integer number formats - set up the flags and "break" */
486 if (qualifier == 'I')
487 num = va_arg(args, unsigned long long);
488 else if (qualifier == 'l')
489 num = va_arg(args, unsigned long);
490 else if (qualifier == 'h') {
492 num = va_arg(args, int);
494 num = va_arg(args, unsigned int);
498 num = va_arg(args, int);
500 num = va_arg(args, unsigned int);
502 str = number(str, end, num, base, field_width, precision, flags);
507 /* don't write out a null byte if the buf size is zero */
516 int sprintf(char * buf, const char *fmt, ...)
522 i=_vsnprintf(buf,INT_MAX,fmt,args);
531 int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
537 i=_vsnprintf(buf,cnt,fmt,args);
546 int vsprintf(char *buf, const char *fmt, va_list args)
548 return _vsnprintf(buf,INT_MAX,fmt,args);