2 * Tiny printf version for SPL
5 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
7 * Copyright (C) 2004,2008 Kustaa Nyholm
9 * SPDX-License-Identifier: LGPL-2.1+
19 /* Current position in sprintf() output string */
22 static void out(char c)
27 static void out_dgt(char dgt)
29 out(dgt + (dgt < 10 ? '0' : 'a' - 10));
33 static void div_out(unsigned int *num, unsigned int div)
35 unsigned char dgt = 0;
46 int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
54 while ((ch = *(fmt++))) {
67 if (ch >= '0' && ch <= '9') {
69 while (ch >= '0' && ch <= '9') {
70 width = (width * 10) + ch - '0';
83 num = va_arg(va, unsigned int);
84 if (ch == 'd' && (int)num < 0) {
91 for (div = 1000000000; div; div /= 10)
96 num = va_arg(va, unsigned int);
100 for (div = 0x10000000; div; div /= 0x10)
105 out((char)(va_arg(va, int)));
108 p = va_arg(va, char*);
118 while (*bf++ && width > 0)
121 putc(lz ? '0' : ' ');
133 int printf(const char *fmt, ...)
139 ret = _vprintf(fmt, va, putc);
145 static void putc_outstr(char ch)
150 int sprintf(char *buf, const char *fmt, ...)
157 ret = _vprintf(fmt, va, putc_outstr);
164 /* Note that size is ignored */
165 int snprintf(char *buf, size_t size, const char *fmt, ...)
171 ret = sprintf(buf, fmt, va);