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
|
#include <camellia/syscalls.h>
#include <shared/printf.h>
#include <stdio.h>
#include <string.h>
static void backend_file(void *arg, const char *buf, size_t len) {
fwrite(buf, 1, len, arg);
}
int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) {
return __printf_internal(fmt, ap, backend_file, f);
}
static void backend_buf(void *arg, const char *buf, size_t len) {
char **ptrs = arg;
size_t space = ptrs[1] - ptrs[0];
if (len > space) len = space;
memcpy(ptrs[0], buf, len);
ptrs[0] += len;
/* ptrs[1] is the last byte of the buffer, it must be 0.
* on overflow:
* ptrs[0] + (ptrs[1] - ptrs[0]) = ptrs[1] */
*ptrs[0] = '\0';
}
int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap) {
char *ptrs[2] = {str, str + len - 1};
return __printf_internal(fmt, ap, backend_buf, &ptrs);
}
int printf(const char *restrict fmt, ...) {
int ret;
va_list argp;
va_start(argp, fmt);
ret = vprintf(fmt, argp);
va_end(argp);
return ret;
}
int fprintf(FILE *restrict f, const char *restrict fmt, ...) {
int ret;
va_list argp;
va_start(argp, fmt);
ret = vfprintf(f, fmt, argp);
va_end(argp);
return ret;
}
int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...) {
int ret;
va_list argp;
va_start(argp, fmt);
ret = vsnprintf(str, len, fmt, argp);
va_end(argp);
return ret;
}
int vprintf(const char *restrict fmt, va_list ap) {
return vfprintf(stdout, fmt, ap);
}
int _klogf(const char *fmt, ...) {
char buf[256];
int ret;
va_list argp;
va_start(argp, fmt);
ret = vsnprintf(buf, sizeof buf, fmt, argp);
va_end(argp);
_syscall_debug_klog(buf, ret);
return ret;
}
|