aboutsummaryrefslogtreecommitdiff
path: root/util/extensions/legacybios/kernel/lib.c
blob: b0a3fbb0e7f7abc4c3eec57730cfc30c5e7c5068 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* lib.c
 * tag: simple function library
 *
 * Copyright (C) 2003 Stefan Reinauer
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */

#include <stdarg.h>
#include <string.h>
#include "types.h"

int putchar(int c);

/* ****************************************************************** */

/* Convert the integer D to a string and save the string in BUF. If
 * BASE is equal to 'd', interpret that D is decimal, and if BASE is
 * equal to 'x', interpret that D is hexadecimal.  
 */

static void itoa(char *buf, int base, int d)
{
	char *p = buf;
	char *p1, *p2;
	unsigned long ud = d;
	int divisor = 10;

	/* If %d is specified and D is minus, put `-' in the head.  */
	if (base == 'd' && d < 0) {
		*p++ = '-';
		buf++;
		ud = -d;
	} else if (base == 'x')
		divisor = 16;

	/* Divide UD by DIVISOR until UD == 0.  */
	do {
		int remainder = ud % divisor;

		*p++ =
		    (remainder <
		     10) ? remainder + '0' : remainder + 'a' - 10;
	}
	while (ud /= divisor);

	/* Terminate BUF.  */
	*p = 0;

	/* Reverse BUF.  */
	p1 = buf;
	p2 = p - 1;
	while (p1 < p2) {
		char tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2--;
	}
}

/* Format a string and print it on the screen, just like the libc
 * function printf. 
 */
void printk(const char *format, ...)
{
	va_list ap;
	int c, d;
	char buf[20];

	va_start(ap, format);
	while ((c = *format++) != 0) {
		char *p;
		if (c != '%') {
			putchar(c);
			continue;
		}

		c = *format++;
		switch (c) {
		case 'd':
		case 'u':
		case 'x':
			d = va_arg(ap, int);
			itoa(buf, c, d);
			p = buf;
			goto string;
			break;

		case 's':
			p = va_arg(ap, char *);
			if (!p)
				p = "(null)";

		      string:
			while (*p)
				putchar(*p++);
			break;

		default:
			putchar(va_arg(ap, int));
			break;
		}
	}
	va_end(ap);
}

/* memset might be a macro or alike. Disable it to be sure */
#undef memset

void *memset(void *s, int c, size_t count)
{
	char *xs = (char *) s;
	while (count--)
		*xs++ = c;
	return s;
}

void *memmove(void *dest, const void *src, size_t count)
{
	char *tmp, *s;
	if (dest <= src) {
		tmp = (char *) dest;
		s = (char *) src;
		while (count--)
			*tmp++ = *s++;
	} else {
		tmp = (char *) dest + count;
		s = (char *) src + count;
		while (count--)
			*--tmp = *--s;
	}
	return dest;
}

#ifdef DEBUG_GDB
void *memcpy(void *dest, const void *src, size_t count)
{
	char *tmp = (char *) dest, *s = (char *) src;

	while (count--)
		*tmp++ = *s++;

	return dest;
}

#undef strncmp
int strncmp(const char *cs, const char *ct, size_t count)
{
	register signed char __res = 0;

	while (count) {
		if ((__res = *cs - *ct++) != 0 || !*cs++)
			break;
		count--;
	}

	return __res;
}

size_t strlen(const char *s)
{
	const char *sc;

	for (sc = s; *sc != '\0'; ++sc)
		/* nothing */ ;
	return sc - s;
}
#endif