Merge tag 'pull-ppc-20221117' of https://gitlab.com/danielhb/qemu into staging
[qemu/ar7.git] / tests / multiboot / libc.c
blob6df9bda96d884eceb160c1c659b7248524a3bbbe
1 /*
2 * Copyright (c) 2013 Kevin Wolf <kwolf@redhat.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
23 #include "libc.h"
25 void* memcpy(void *dest, const void *src, int n)
27 char *d = dest;
28 const char *s = src;
30 while (n--) {
31 *d++ = *s++;
34 return dest;
37 static void print_char(char c)
39 outb(0xe9, c);
42 static void print_str(char *s)
44 while (*s) {
45 print_char(*s++);
49 static void print_num(uint64_t value, int base)
51 char digits[] = "0123456789abcdef";
52 char buf[32] = { 0 };
53 int i = sizeof(buf) - 2;
55 do {
56 buf[i--] = digits[value % base];
57 value /= base;
58 } while (value);
60 print_str(&buf[i + 1]);
63 void printf(const char *fmt, ...)
65 va_list ap;
66 uint64_t val;
67 char *str;
68 int base;
69 int has_long;
70 int alt_form;
72 va_start(ap, fmt);
74 for (; *fmt; fmt++) {
75 if (*fmt != '%') {
76 print_char(*fmt);
77 continue;
79 fmt++;
81 if (*fmt == '#') {
82 fmt++;
83 alt_form = 1;
84 } else {
85 alt_form = 0;
88 if (*fmt == 'l') {
89 fmt++;
90 if (*fmt == 'l') {
91 fmt++;
92 has_long = 2;
93 } else {
94 has_long = 1;
96 } else {
97 has_long = 0;
100 switch (*fmt) {
101 case 'x':
102 case 'p':
103 base = 16;
104 goto convert_number;
105 case 'd':
106 case 'i':
107 case 'u':
108 base = 10;
109 goto convert_number;
110 case 'o':
111 base = 8;
112 goto convert_number;
114 convert_number:
115 switch (has_long) {
116 case 0:
117 val = va_arg(ap, unsigned int);
118 break;
119 case 1:
120 val = va_arg(ap, unsigned long);
121 break;
122 case 2:
123 val = va_arg(ap, unsigned long long);
124 break;
127 if (alt_form && base == 16) {
128 print_str("0x");
131 print_num(val, base);
132 break;
134 case 's':
135 str = va_arg(ap, char*);
136 print_str(str);
137 break;
138 case '%':
139 print_char(*fmt);
140 break;
141 default:
142 print_char('%');
143 print_char(*fmt);
144 break;
148 va_end(ap);