aboutsummaryrefslogtreecommitdiff
path: root/src/lib/libc/stdio.c
blob: c8496830a597c0c6cba1d120052c58be3d81894d (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <string.h>
#include <stdio.h>

#include <proto/launch.h>

#include <kogata/syscall.h>
#include <kogata/printf.h>

FILE *stdin = 0;
FILE *stdout = 0;
FILE *stderr = 0;

FILE libc_tty_stdio, libc_stdin, libc_stdout, libc_stderr;

void setup_libc_stdio() {
	if (sc_stat_open(STD_FD_TTY_STDIO, &libc_tty_stdio.st)) {
		libc_tty_stdio.fd = STD_FD_TTY_STDIO;
		sc_fctl(libc_tty_stdio.fd, FC_SET_BLOCKING, 0);
		// TODO: initialize libc_tty_stdio as a TTY
		stdin = &libc_tty_stdio;
		stdout = &libc_tty_stdio;
		stderr = &libc_tty_stdio;
	}
	if (sc_stat_open(STD_FD_STDIN, &libc_stdin.st)) {
		libc_stdin.fd = STD_FD_STDIN;
		// TODO: initialize
		stdin = &libc_stdin;
	}
	if (sc_stat_open(STD_FD_STDOUT, &libc_stdout.st)) {
		libc_stdout.fd = STD_FD_STDOUT;
		// TODO: initialize
		stdout = &libc_stdout;
	}
	if (sc_stat_open(STD_FD_STDERR, &libc_stderr.st)) {
		libc_stderr.fd = STD_FD_STDERR;
		// TODO: initialize
		stderr = &libc_stderr;
	}
}


int getchar() {
	return fgetc(stdin);
}

int putchar(int c) {
	return fputc(c, stdout);
}

int puts(const char* s) {
	return fputs(s, stdout);
}

int printf(const char* fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	int ret = vfprintf(stdout, fmt, ap);
	va_end(ap);

	return ret;
}

// ==================
// BELOW IS TODO
// ==================


FILE *fopen(const char *path, const char *mode) {
	// TODO
	return 0;
}
FILE *freopen(const char *path, const char *mode, FILE *stream) {
	// TODO
	return 0;
}
int fclose(FILE* f) {
	// TODO
	return 0;
}


// ---------------
// INPUT FUNCTIONS
// ---------------

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
	if (stream == NULL || stream->fd == 0) return 0;

	// TODO buffering
	// TODO position
	return sc_read(stream->fd, 0, size * nmemb, ptr);
}

int fgetc(FILE *stream) {
	// TODO buffering && ungetc
	char x;
	if (fread(&x, 1, 1, stream)) {
		return x;
	} else {
		return EOF;
	}
}
char *fgets(char *s, int size, FILE *stream) {
	int l = 0;
	while (l < size - 1) {
		int c = fgetc(stream);
		if (c == EOF) {
			break;
		} else if (c == '\b') {
			if (l > 0) l--;
			// TODO if terminal write back space or something
		} else {
			s[l] = c;
			l++;
			if (c == '\n') break;
		}
	}
	s[l] = 0;
	return s;
}
int getc(FILE *stream) {
	// TODO
	return 0;
}
int ungetc(int c, FILE *stream) {
	// TODO
	return 0;
}

// ----------------
// OUTPUT FUNCTIONS
// ----------------

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
	if (stream == NULL || stream->fd == 0) return 0;

	// TODO buffering
	// TODO position
	return sc_write(stream->fd, 0, size * nmemb, ptr);
}

int fputc(int c, FILE *stream) {
	unsigned char x = c;
	return fwrite(&x, 1, 1, stream);
}
int fputs(const char *s, FILE *stream) {
	return fwrite(s, strlen(s), 1, stream);
}
int putc(int c, FILE *stream) {
	return fputc(c, stream);
}

int fprintf(FILE *stream, const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	int ret = vfprintf(stream, fmt, ap);
	va_end(ap);

	return ret;
}
int vfprintf(FILE *stream, const char *format, va_list ap) {
	char buf[1024];
	vsnprintf(buf, 1024, format, ap);

	return fputs(buf, stream);
}

// buffering

void setbuf(FILE *stream, char *buf) {
	setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
}
void setbuffer(FILE *stream, char *buf, size_t size) {
	// TODO
}
void setlinebuf(FILE *stream) {
	setvbuf(stream, NULL, _IOLBF, 0);
}
int setvbuf(FILE *stream, char *buf, int mode, size_t size) {
	// TODO
	return 0;
}

int fflush(FILE* f) {
	// TODO
	return 0;
}

// ---------------------
// COMPLICATED FUNCTIONS
// ---------------------

void clearerr(FILE *stream) {
	// TODO
}
int feof(FILE *stream) {
	// TODO
	return 0;
}
int ferror(FILE *stream) {
	// TODO
	return 0;
}
int fileno(FILE *stream) {
	// TODO
	return 0;
}


// ------------------
// POSITION FUNCTIONS
// ------------------


int fseek(FILE *stream, long offset, int whence) {
	// TODO
	return 0;
}
long ftell(FILE *stream) {
	// TODO
	return 0;
}
void rewind(FILE *stream) {
	// TODO
}
int fgetpos(FILE *stream, fpos_t *pos) {
	// TODO
	return 0;
}
int fsetpos(FILE *stream, const fpos_t *pos) {
	// TODO
	return 0;
}

// ---------------------
// PATH & FILE FUNCTIONS
// ---------------------

FILE *tmpfile(void) {
	// TODO
	return 0;
}
char *tmpnam(char *s) {
	// TODO
	return 0;
}

int rename(const char *old, const char *new) {
	// TODO
	return 0;
}
int remove(const char *pathname) {
	// TODO
	return 0;
}



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