> 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
 09:31:07 up 21:55,  3 users,  load average: 0.09, 0.04, 0.05
This commit is contained in:
tracer-ics2023
2024-09-06 09:31:07 +08:00
committed by zzy
parent 7aa39f7e49
commit ef87034da7

View File

@ -45,36 +45,60 @@ int vsnprintf(char *out, size_t n, const char *fmt, va_list ap) {
return rvsnprintf(out, n, fmt, ap);
}
static inline 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;
}
for (int j = i; j >= 0; j --) {
*out = buf[j];
}
return i;
}
static inline 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;
}
static inline int fmt_match(char* out, size_t n, const char* fmt, va_list ap) {
int res;
switch(*fmt) {
case 'd':
res = fmt_d(out, n, fmt, ap);
break;
case 's':
res = fmt_s(out, n, fmt, ap);
break;
default:
res = 0;
break;
}
return res;
}
static int rvsnprintf(char* out, size_t n, const char* fmt, va_list ap) {
bool is_fmt;
for (is_fmt = false; n; fmt ++, out ++, n --) {
for (; n; fmt ++, out ++, n --) {
switch (*fmt)
{
case '\0':
*out = *fmt;
goto END;
case '%':
is_fmt = true;
break;
case 'd':
if (is_fmt) {
int va = va_arg(ap, int);
for (; va; va /= 10) {
*out = va % 10 + '0';
n --;
out ++;
}
is_fmt = false;
}
break;
case 's':
if (is_fmt) {
const char* va = va_arg(ap, const char*);
size_t len = strlen(va);
strncpy(out, va, len);
n += len;
is_fmt = false;
}
int len = fmt_match(out, n, fmt, ap);
out += len;
n -= len;
default:
*out = *fmt;
break;