> compile NEMU

221220000 张三
Linux zzy 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
 16:04:02 up  6:43,  1 user,  load average: 0.22, 0.22, 0.21
This commit is contained in:
tracer-ics2023
2024-09-11 16:04:03 +08:00
committed by zzy
parent 541fe44507
commit eecbabeaaa
2 changed files with 48 additions and 62 deletions

View File

@ -9,7 +9,7 @@
extern "C" {
#endif
//#define __NATIVE_USE_KLIB__
#define __NATIVE_USE_KLIB__
// string.h
void *memset (void *s, int c, size_t n);

View File

@ -19,7 +19,8 @@ int printf(const char *fmt, ...) {
}
int vsprintf(char *out, const char *fmt, va_list ap) {
return vsnprintf(out, SIZE_MAX, fmt, ap);
// FIXME
return vsnprintf(out, 999, fmt, ap);
}
int sprintf(char *out, const char *fmt, ...) {
@ -46,79 +47,64 @@ int vsnprintf(char *out, size_t n, const char *fmt, va_list ap) {
}
__attribute__((noinline))
static int fmt_d(char* out, size_t n, const char* fmt, va_list ap) {
char buf[16];
int in = va_arg(ap, int);
if (in == 0) {
*out = '0';
out ++;
return 1;
}
int i;
for (i = 0; in; in /= 10, i ++) {
buf[i] = in % 10;
buf[i] += '0';
}
for (int j = i - 1; j >= 0; j --) {
*out = buf[j];
}
return i;
}
__attribute__((noinline))
static int fmt_s(char* out, size_t n, const char* fmt, va_list ap) {
const char* ch = va_arg(ap, const char*);
size_t len = strlen(ch);
strncpy(out, ch, len);
return len;
}
__attribute__((noinline))
static int fmt_match(char* out, size_t n, const char* fmt, va_list ap) {
int res;
assert(*(fmt - 1) == '%');
switch(*fmt) {
static int print_fmt(char **out, int n, const char** fmt, va_list ap) {
int ret = 0;
switch (**fmt) {
case 'd':
res = fmt_d(out, n, fmt, ap);
//
(*fmt) ++;
int num = va_arg(ap, int), i = 0;
char buf[sizeof(num) * 3];
if (num == 0) {
buf[i++] = '0';
}
for (i = 0; num; i ++) {
buf[i] = '0' + num % 10;
num /= 10;
}
if (i > n) {
break; // error buffer overflow
}
ret = i;
// reverse buf
for (i --; i >= 0; i --, (*out) ++ ) {
**out = buf[i];
}
break;
case 's':
res = fmt_s(out, n, fmt, ap);
(*fmt) ++;
const char* str = va_arg(ap, const char*);
int n = 999; // FIXME
if (ret > n) break; // error buffer overflow
for (; *str; str ++, (*out) ++ ) {
**out = *str;
}
break;
default:
res = 0;
break;
}
return res;
return ret;
}
__attribute__((noinline))
static int rvsnprintf(char* out, size_t n, const char* fmt, va_list ap) {
int ret = 0;
char* _out = out;
size_t _n = n;
const char* _fmt = fmt;
while (_n) {
switch (*_fmt) {
case '\0':
*_out = *_fmt;
_n = 0;
break;
case '%':
int len = fmt_match(_out, _n, _fmt, ap);
_out += len;
_n -= len;
_fmt += 2;
break;
default:
*_out = *_fmt;
_out ++;
_fmt ++;
_n ++;
break;
while(*fmt) {
if (*fmt == '%') {
// FIXME
fmt++;
print_fmt(&out, 123, &fmt, ap);
continue;
}
*out = *fmt;
out ++;
fmt ++;
}
assert(*_out == '\0');
return 1;
*out = '\0';
putstr(_out);
return ret;
}
#endif