Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e552845)
raw | patch | inline | side by side (parent: e552845)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Mon, 26 Jan 2026 09:38:31 +0000 (17:38 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Mon, 26 Jan 2026 09:38:31 +0000 (17:38 +0800) |
| include/stdio.h | patch | blob | history | |
| include/types.h | patch | blob | history | |
| kernel/printk.c | patch | blob | history | |
| lib/vsprintf.c | patch | blob | history |
diff --git a/include/stdio.h b/include/stdio.h
--- a/include/stdio.h
+++ b/include/stdio.h
#define _STDIO_H
#include <string.h>
#include <syscall.h>
+#include <types.h>
extern int write(int fd, const char* buf, unsigned long size);
-extern int vsprintf(char* buf, const char* fmt, char* args);
+extern int vsprintf(char* buf, const char* fmt, va_list args);
static inline int printf(const char* fmt, ...) {
char ptfbuf[512];
char* args = (char*)(((char*)&fmt) + 4);
diff --git a/include/types.h b/include/types.h
--- a/include/types.h
+++ b/include/types.h
typedef void (*pf_intr)();
//
+#define INTSIZEOF(n) ((sizeof(n) + sizeof(uint32_t) - 1) & (~(sizeof(uint32_t) - 1)))
typedef char* va_list;
-#define va_start(ap, last) ((ap) = (va_list) & (last) + sizeof(last))
-#define va_arg(ap, type) (*(type*)(((ap) += sizeof(type)) - sizeof(type)))
+#define va_start(ap, last) ((ap) = (va_list) & (last) + INTSIZEOF(last))
+#define va_arg(ap, type) (*(type*)(((ap) += INTSIZEOF(type)) - INTSIZEOF(type)))
#define va_end(ap) ((ap) = (va_list)0)
#endif //_TYPES_H
diff --git a/kernel/printk.c b/kernel/printk.c
--- a/kernel/printk.c
+++ b/kernel/printk.c
#include <system.h>
#include <tty.h>
-int vsprintf(char* buf, const char* fmt, char* args);
+int vsprintf(char* buf, const char* fmt, va_list args);
void serial_write(const char* buf, size_t size);
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
return charcnt > len ? charcnt : len;
}
-int vsprintf(char* buf, const char* fmt, char* args) {
+int vsprintf(char* buf, const char* fmt, va_list args) {
char* p = buf;
int char_cnt;
char tmp[64];
switch (*fmt) {
case 'c':
- *p++ = *args;
+ *p++ = va_arg(args, char);
break;
case 'd':
- itoa(tmp, *((int*)args));
+ itoa(tmp, va_arg(args, int));
p += write_buf(p, tmp, char_fill, char_cnt, align);
break;
case 'l':
fmt++;
if (*fmt == 'u' || *fmt == 'd') { // d u都当成u来处理
- i64tou(tmp, *((int64_t*)args));
+ i64tou(tmp, va_arg(args, uint64_t));
p += write_buf(p, tmp, char_fill, char_cnt, align);
- args += 4;
} else if (*fmt == 'o') {
- i64too(tmp, *((uint64_t*)args));
+ i64too(tmp, va_arg(args, uint64_t));
p += write_buf(p, tmp, char_fill, char_cnt, align);
- args += 4;
} else if (*fmt == 'x' || *fmt == 'X') {
- // i64tox(tmp, *((uint64_t *)args), *fmt == 'X' ? 1 : 0);
- i64tox(tmp, *((uint64_t*)args), 1); // x X都强制为大写
+ i64tox(tmp, va_arg(args, uint64_t), 1); // x X都强制为大写
p += write_buf(p, tmp, char_fill, char_cnt, align);
- args += 4;
}
break;
case 's':
- p += write_buf(p, (const char*)*((unsigned int*)args), char_fill, char_cnt, align);
+ p += write_buf(p, (const char*)va_arg(args, char*), char_fill, char_cnt, align);
break;
case 'u':
- itou(tmp, *((unsigned int*)args));
+ itou(tmp, va_arg(args, uint32_t));
p += write_buf(p, tmp, char_fill, char_cnt, align);
break;
case 'x':
case 'X':
- // itox(tmp, *((unsigned int *)args), *fmt == 'X' ? 1 : 0);
- itox(tmp, *((unsigned int*)args), 1); // x X都强制为大写
+ itox(tmp, va_arg(args, uint32_t), 1); // x X都强制为大写
p += write_buf(p, tmp, char_fill, char_cnt, align);
break;
case 'o':
- itoo(tmp, *((unsigned*)args));
+ itoo(tmp, va_arg(args, uint32_t));
p += write_buf(p, tmp, char_fill, char_cnt, align);
break;
default:
break;
}
- args += 4;
fmt++;
}
*p = 0;