tiny-printf.c 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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;

static void out(char c)
{
	*bf++ = c;
}

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

30
static void div_out(unsigned int *num, unsigned int div)
31 32 33
{
	unsigned char dgt = 0;

34 35
	while (*num >= div) {
		*num -= div;
36 37 38 39 40 41 42
		dgt++;
	}

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

43
int vprintf(const char *fmt, va_list va)
44 45 46
{
	char ch;
	char *p;
47 48 49
	unsigned int num;
	char buf[12];
	unsigned int div;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

	while ((ch = *(fmt++))) {
		if (ch != '%') {
			putc(ch);
		} else {
			char lz = 0;
			char w = 0;

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

			if (ch >= '0' && ch <= '9') {
				w = 0;
				while (ch >= '0' && ch <= '9') {
67
					w = (w * 10) + ch - '0';
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
					ch = *fmt++;
				}
			}
			bf = buf;
			p = bf;
			zs = 0;

			switch (ch) {
			case 0:
				goto abort;
			case 'u':
			case 'd':
				num = va_arg(va, unsigned int);
				if (ch == 'd' && (int)num < 0) {
					num = -(int)num;
					out('-');
				}
85 86
				for (div = 1000000000; div; div /= 10)
					div_out(&num, div);
87 88 89
				break;
			case 'x':
				num = va_arg(va, unsigned int);
90 91
				for (div = 0x10000000; div; div /= 0x10)
					div_out(&num, div);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
				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;
			while (*bf++ && w > 0)
				w--;
			while (w-- > 0)
				putc(lz ? '0' : ' ');
			while ((ch = *p++))
				putc(ch);
		}
	}

abort:
	return 0;
}
119 120 121 122 123 124 125 126 127 128 129 130

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

	va_start(va, fmt);
	ret = vprintf(fmt, va);
	va_end(va);

	return ret;
}