-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.h
More file actions
86 lines (80 loc) · 2.16 KB
/
stdio.h
File metadata and controls
86 lines (80 loc) · 2.16 KB
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
79
80
81
82
83
84
85
86
// stdio.h clone - SimpleC
#ifndef SIMPLEC_STDIO_H
#define SIMPLEC_STDIO_H
#include <stdarg.h>
// putchar using syscall (x86_64 + i386)
static inline void putchar(char c) {
#if defined(__i386__)
asm volatile (
"movl $4, %%eax\n"
"movl $1, %%ebx\n"
"movl %[buf], %%ecx\n"
"movl $1, %%edx\n"
"int $0x80\n"
:
: [buf] "r" (&c)
: "eax","ebx","ecx","edx"
);
#elif defined(__x86_64__)
asm volatile (
"movq $1, %%rax\n"
"movq $1, %%rdi\n"
"movq %[buf], %%rsi\n"
"movq $1, %%rdx\n"
"syscall\n"
:
: [buf] "r" (&c)
: "rax","rdi","rsi","rdx"
);
#else
#error "Unsupported architecture for putchar."
#endif
}
static inline void io_itoa(int n, char *buf, unsigned long bufsize) {
if (bufsize == 0) return;
char tmp[12]; unsigned long i=0, ti=0;
unsigned int x = (n < 0) ? -n : n;
if (n==0) tmp[ti++]='0';
else while(x && ti<sizeof(tmp)) { tmp[ti++]='0'+(x%10); x/=10; }
if(n<0) tmp[ti++]='-';
while(ti && i+1<bufsize) buf[i++] = tmp[--ti];
buf[i] = '\0';
}
static inline void printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
while(*fmt) {
if(*fmt=='%') {
fmt++;
switch(*fmt) {
case 'd': {
int val = va_arg(args, int);
char buf[12];
io_itoa(val, buf, sizeof(buf));
char *p = buf; while(*p) putchar(*p++);
break;
}
case 's': {
char *s = va_arg(args, char*);
while(*s) putchar(*s++);
break;
}
case 'c': {
int c = va_arg(args, int);
putchar((char)c);
break;
}
case '%': {
putchar('%');
break;
}
default: {
putchar('%'); putchar(*fmt);
}
}
} else putchar(*fmt);
fmt++;
}
va_end(args);
}
#endif // SIMPLEC_STDIO_H