tiny-printf.c 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Tiny printf version for SPL
 *
 * Copied from:
 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
 *
 * Copyright (C) 2004,2008  Kustaa Nyholm
 *
 * SPDX-License-Identifier:	LGPL-2.1+
 */

#include <common.h>
#include <stdarg.h>
#include <serial.h>

static char *bf;
static char zs;

S
Simon Glass 已提交
19 20 21
/* Current position in sprintf() output string */
static char *outstr;

22 23 24 25 26 27 28
static void out(char c)
{
	*bf++ = c;
}

static void out_dgt(char dgt)
{
29
	out(dgt + (dgt < 10 ? '0' : 'a' - 10));
30 31 32
	zs = 1;
}

33
static void div_out(unsigned int *num, unsigned int div)
34 35 36
{
	unsigned char dgt = 0;

37 38
	while (*num >= div) {
		*num -= div;
39 40 41 42 43 44 45
		dgt++;
	}

	if (zs || dgt > 0)
		out_dgt(dgt);
}

S
Simon Glass 已提交
46
int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
47 48 49
{
	char ch;
	char *p;
50 51 52
	unsigned int num;
	char buf[12];
	unsigned int div;
53 54 55 56 57

	while ((ch = *(fmt++))) {
		if (ch != '%') {
			putc(ch);
		} else {
S
Simon Glass 已提交
58 59
			bool lz = false;
			int width = 0;
60 61 62 63 64 65 66 67

			ch = *(fmt++);
			if (ch == '0') {
				ch = *(fmt++);
				lz = 1;
			}

			if (ch >= '0' && ch <= '9') {
S
Simon Glass 已提交
68
				width = 0;
69
				while (ch >= '0' && ch <= '9') {
S
Simon Glass 已提交
70
					width = (width * 10) + ch - '0';
71 72 73 74 75 76 77 78
					ch = *fmt++;
				}
			}
			bf = buf;
			p = bf;
			zs = 0;

			switch (ch) {
S
Simon Glass 已提交
79
			case '\0':
80 81 82 83 84 85 86 87
				goto abort;
			case 'u':
			case 'd':
				num = va_arg(va, unsigned int);
				if (ch == 'd' && (int)num < 0) {
					num = -(int)num;
					out('-');
				}
S
Simon Glass 已提交
88 89 90 91 92 93
				if (!num) {
					out_dgt(0);
				} else {
					for (div = 1000000000; div; div /= 10)
						div_out(&num, div);
				}
94 95 96
				break;
			case 'x':
				num = va_arg(va, unsigned int);
S
Simon Glass 已提交
97 98 99 100 101 102
				if (!num) {
					out_dgt(0);
				} else {
					for (div = 0x10000000; div; div /= 0x10)
						div_out(&num, div);
				}
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
				break;
			case 'c':
				out((char)(va_arg(va, int)));
				break;
			case 's':
				p = va_arg(va, char*);
				break;
			case '%':
				out('%');
			default:
				break;
			}

			*bf = 0;
			bf = p;
S
Simon Glass 已提交
118 119 120
			while (*bf++ && width > 0)
				width--;
			while (width-- > 0)
121
				putc(lz ? '0' : ' ');
122 123 124 125
			if (p) {
				while ((ch = *p++))
					putc(ch);
			}
126 127 128 129 130 131
		}
	}

abort:
	return 0;
}
132 133 134 135 136 137 138

int printf(const char *fmt, ...)
{
	va_list va;
	int ret;

	va_start(va, fmt);
S
Simon Glass 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	ret = _vprintf(fmt, va, putc);
	va_end(va);

	return ret;
}

static void putc_outstr(char ch)
{
	*outstr++ = ch;
}

/* Note that size is ignored */
int snprintf(char *buf, size_t size, const char *fmt, ...)
{
	va_list va;
	int ret;

	va_start(va, fmt);
	outstr = buf;
	ret = _vprintf(fmt, va, putc_outstr);
159
	va_end(va);
S
Simon Glass 已提交
160
	*outstr = '\0';
161 162 163

	return ret;
}