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+
17 char *bf; /* Digit buffer */
18 char zs; /* non-zero if a digit has been written */
19 char *outstr; /* Next output position for sprintf() */
21 /* Output a character */
22 void (*putc)(struct printf_info *info, char ch);
25 void putc_normal(struct printf_info *info, char ch)
30 static void out(struct printf_info *info, char c)
35 static void out_dgt(struct printf_info *info, char dgt)
37 out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
41 static void div_out(struct printf_info *info, unsigned int *num,
44 unsigned char dgt = 0;
51 if (info->zs || dgt > 0)
55 int _vprintf(struct printf_info *info, const char *fmt, va_list va)
63 while ((ch = *(fmt++))) {
76 if (ch >= '0' && ch <= '9') {
78 while (ch >= '0' && ch <= '9') {
79 width = (width * 10) + ch - '0';
92 num = va_arg(va, unsigned int);
93 if (ch == 'd' && (int)num < 0) {
100 for (div = 1000000000; div; div /= 10)
101 div_out(info, &num, div);
105 num = va_arg(va, unsigned int);
109 for (div = 0x10000000; div; div /= 0x10)
110 div_out(info, &num, div);
114 out(info, (char)(va_arg(va, int)));
117 p = va_arg(va, char*);
127 while (*info->bf++ && width > 0)
130 info->putc(info, lz ? '0' : ' ');
133 info->putc(info, ch);
142 int vprintf(const char *fmt, va_list va)
144 struct printf_info info;
146 info.putc = putc_normal;
147 return _vprintf(&info, fmt, va);
150 int printf(const char *fmt, ...)
152 struct printf_info info;
157 info.putc = putc_normal;
159 ret = _vprintf(&info, fmt, va);
165 static void putc_outstr(struct printf_info *info, char ch)
167 *info->outstr++ = ch;
170 int sprintf(char *buf, const char *fmt, ...)
172 struct printf_info info;
178 info.putc = putc_outstr;
179 ret = _vprintf(&info, fmt, va);
186 /* Note that size is ignored */
187 int snprintf(char *buf, size_t size, const char *fmt, ...)
189 struct printf_info info;
195 info.putc = putc_outstr;
196 ret = _vprintf(&info, fmt, va);
203 void __assert_fail(const char *assertion, const char *file, unsigned line,
204 const char *function)
206 /* This will not return */
207 printf("%s:%u: %s: Assertion `%s' failed.", file, line, function,