-
Notifications
You must be signed in to change notification settings - Fork 57
/
u-lib.cc
78 lines (65 loc) · 1.99 KB
/
u-lib.cc
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
#include "u-lib.hh"
// dprintf
// Construct a string from `format` and pass it to `sys_write(fd)`.
// Returns the number of characters printed, or E_2BIG if the string
// could not be constructed.
int dprintf(int fd, const char* format, ...) {
char buf[513];
va_list val;
va_start(val, format);
size_t n = vsnprintf(buf, sizeof(buf), format, val);
if (n < sizeof(buf)) {
return sys_write(fd, buf, n);
} else {
return E_2BIG;
}
}
// printf
// Like `dprintf(1, ...)`.
int printf(const char* format, ...) {
char buf[513];
va_list val;
va_start(val, format);
size_t n = vsnprintf(buf, sizeof(buf), format, val);
if (n < sizeof(buf)) {
return sys_write(1, buf, n);
} else {
return E_2BIG;
}
}
// panic, assert_fail
// Call the SYSCALL_PANIC system call so the kernel loops until Control-C.
void panic(const char* format, ...) {
va_list val;
va_start(val, format);
char buf[160];
memcpy(buf, "PANIC: ", 7);
int len = vsnprintf(&buf[7], sizeof(buf) - 7, format, val) + 7;
va_end(val);
if (len > 0 && buf[len - 1] != '\n') {
strcpy(buf + len - (len == (int) sizeof(buf) - 1), "\n");
}
int cpos = consoletype == CONSOLE_NORMAL ? -1 : CPOS(23, 0);
(void) console_printf(cpos, 0xC000, "%s", buf);
sys_panic(nullptr);
}
int error_vprintf(int cpos, int color, const char* format, va_list val) {
return console_vprintf(cpos, color, format, val);
}
void assert_fail(const char* file, int line, const char* msg,
const char* description) {
if (consoletype != CONSOLE_NORMAL) {
cursorpos = CPOS(23, 0);
}
if (description) {
error_printf("%s:%d: %s\n", file, line, description);
}
error_printf("%s:%d: user assertion '%s' failed\n", file, line, msg);
sys_panic(nullptr);
}
// sys_clone
// Create a new thread.
pid_t sys_clone(int (*function)(void*), void* arg, char* stack_top) {
// Your code here
return E_NOSYS;
}