aboutsummaryrefslogtreecommitdiff
path: root/src/lib/libkogata/stdio.c
blob: 1c298adce2892276f9b10880f2fb671811ad7591 (plain) (blame)
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
#include <syscall.h>

#include <printf.h>
#include <stdio.h>


fd_t stdio = 1;

int getc() {
	sel_fd_t fd;
	fd.fd = stdio;
	fd.req_flags = SEL_READ;
	ASSERT(select(&fd, 1, -1));
	ASSERT(fd.got_flags & SEL_READ);

	char chr;
	size_t sz = read(stdio, 0, 1, &chr);
	ASSERT(sz == 1);
	return chr;
}


void putc(int c) {
	char chr = c;
	write(stdio, 0, 1, &chr);
}

void puts(char* s) {
	while (*s) putc(*(s++));
}

void getline(char* buf, size_t l) {
	size_t i = 0;
	while (true) {
		int c = getc();
		if (c == '\n') {
			putc('\n');
			buf[i] = 0;
			break;
		} else if (c == '\b') {
			if (i > 0) {
				 i--;
				putc('\b');
			}
		} else if (c >= ' ') {
			buf[i] = c;
			if (i < l-1) {
				i++;
				putc(c);
			}
		}
	}
}

void printf(char* fmt, ...) {
	va_list ap;
	char buffer[256];

	va_start(ap, fmt);
	vsnprintf(buffer, 256, fmt, ap);
	va_end(ap);

	puts(buffer);
}

/* vim: set ts=4 sw=4 tw=0 noet :*/