-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdio.c
455 lines (357 loc) · 7.47 KB
/
stdio.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright 2022 Mark Seminatore. All rights reserved.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "unistd.h"
// TODO - update for non ARM64 arch
#if defined(__aarch64__)
typedef long INT;
#else
typedef int INT;
#endif
#if defined(_WIN32)
#pragma warning (disable : 4996)
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
// TODO - do we need to AllocConsole() or AttachConsole() on startup?
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#define RWX (S_IWRITE | S_IREAD)
#else
#include <sys/syscall.h>
#include "unistd.h"
#define RWX S_IRWXU
#endif
#include "stdarg.h"
#define PRINTF_MAX 256
#if defined(INC_FLOAT)
typedef struct _FLOAT
{
unsigned long long sign : 1;
unsigned long long exp : 11;
unsigned long long mant : 52;
} _FLOAT;
union FLOAT
{
_FLOAT f;
double d;
};
#endif
static char __cwd[FILENAME_MAX] = "." DIR_MARKER;
// TODO - replace with static array alloc
FILE _stdin = { 0, 0, 0}, *stdin = &_stdin;
FILE _stdout = { 0, 1 ,0 }, *stdout = &_stdout;
FILE _stderr = { 0, 2, 0 }, *stderr = &_stderr;
//
int putchar(int c)
{
char chr[2];
chr[0] = c;
chr[1] = 0;
puts(chr);
return 1;
}
//
int putc(int chr, FILE *stream)
{
assert(stream);
if (!stream || 1 != write(stream->fildes, &chr, 1))
return EOF;
return 1;
}
//
int fputc(int chr, FILE *stream)
{
return putc(chr, stream);
}
//
int fputs(const char *str, FILE *stream)
{
assert(str && stream);
if (!str || !stream)
return EOF;
return write(stream->fildes, str, strlen(str));
}
//
int puts(const char *str)
{
assert(str);
if (!str)
return EOF;
int len = strlen(str);
#if defined(_WIN32)
HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdOut != NULL && stdOut != INVALID_HANDLE_VALUE)
{
DWORD written = 0;
// If GetStdHandle or WriteConsole return false, then you may need to use AllocConsole().
WriteConsoleA(stdOut, str, len, &written, NULL);
}
#endif
#if defined(__APPLE_CC__)
int result = write(STDOUT_FILENO, str, len);
if (result != len)
return EOF;
#endif
return 1;
}
// Opens the filename pointed to by filename using the given mode.
FILE *fopen(const char *filename, const char *mode)
{
assert(filename && mode);
if (!filename || ! mode)
return NULL;
// process mode argument
int flags = 0;
if (strchr(mode, 'r'))
flags |= (O_RDONLY | O_APPEND);
if (strchr(mode, 'w'))
flags |= (O_WRONLY | O_CREAT);
if (strchr(mode, 'a'))
flags |= O_APPEND;
mode_t omode = 0;
if (flags & O_CREAT)
omode = RWX;
int fd = open(filename, flags, omode);
if (fd < 2)
return NULL;
// alloc FILE structure
// TODO - replace with static array alloc
FILE *stream = (FILE*)malloc(sizeof(FILE));
assert(stream);
if (!stream)
return NULL;
stream->fildes = fd;
stream->pos = 0;
stream->status = 0;
return stream;
}
//
int fclose(FILE *stream)
{
assert(stream);
if (!stream)
return EOF;
int result = close(stream->fildes);
stream->fildes = -1;
stream->status = -1;
stream->pos = -1;
// free FILE structure
free(stream);
return result;
}
// Reads data from the given stream into the array pointed to by ptr.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
assert(ptr && stream /*&& stream->status != -1 && stream->fildes != -1*/);
if (!ptr || !stream)
return 0;
int count = read(stream->fildes, ptr, nmemb * size);
if (count == nmemb * size)
return nmemb;
return 0;
}
// Writes data from the array pointed to by ptr to the given stream.
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
assert(ptr && stream);
if (!ptr || !stream)
return 0;
int count = write(stream->fildes, ptr, nmemb * size);
if (count == nmemb * size)
return nmemb;
return 0;
}
// Sends formatted output to a string using an argument list.
int vsprintf(char *str, const char *format, va_list argp)
{
int count = 0, chr;
char *pbuf = str;
while ((chr = *format))
{
if (chr == '%')
{
// get format specififer
format++;
switch (*format)
{
case '%':
*pbuf++ = '%';
count++;
break;
case 'i':
case 'd':
{
int ival = va_arg(argp, INT);
char int_string[32];
_itoa(ival, int_string, 10);
strcpy(pbuf, int_string);
int len = strlen(int_string);
pbuf += len;
count += len;
}
break;
case 'f':
{
assert(("not yet implemented", 0));
//union FLOAT f;
//double val = va_arg(argp, double);
//f.d = val;
//printf("%c", f.f.sign);
}
break;
case 'c':
{
*pbuf++ = va_arg(argp, INT);
count++;
}
break;
case 's':
{
char *pstr = va_arg(argp, char*);
strcpy(pbuf, pstr);
int len = strlen(pstr);
pbuf += len;
count += len;
}
break;
default:
assert(0);
return -1;
}
}
else
{
*pbuf++ = chr;
count++;
}
format++;
}
*pbuf = 0; // make sure we are asciiz
return count;
}
// Sends formatted output to a string.
int sprintf(char *str, const char *format, ...)
{
int count;
va_list argp;
assert(str && format);
if (!str || !format)
return 0;
va_start(argp, format);
count = vsprintf(str, format, argp);
va_end(argp);
return count;
}
// Sends formatted output to a stream using an argument list.
int vfprintf(FILE *stream, const char *format, va_list argp)
{
int count;
char buf[PRINTF_MAX];
assert(stream && format);
if (!stream || !format)
return 0;
count = vsprintf(buf, format, argp);
fputs(buf, stream);
return count;
}
// Sends formatted output to a stream.
int fprintf(FILE *stream, const char *format, ...)
{
int count;
va_list argp;
assert(stream && format);
va_start(argp, format);
count = vfprintf(stream, format, argp);
va_end(argp);
return count;
}
// Sends formatted output to stdout using an argument list.
int vprintf(const char *format, va_list argp)
{
return vfprintf(stdout, format, argp);
}
// Sends formatted output to stdout.
int printf(const char *format, ...)
{
int count;
va_list argp;
assert(format);
va_start(argp, format);
count = vprintf(format, argp);
va_end(argp);
return count;
}
//int remove(const char *filename);
//
int fgetc(FILE *stream)
{
int chr;
assert(stream);
if (!stream)
return EOF;
int count = read(stream->fildes, &chr, 1);
if (count != 1)
return EOF;
return chr;
}
// Reads a line from the specified stream and stores it into the string
// pointed to by str. It stops when either (n-1) characters are read,
// the newline character is read, or the end-of-file is reached, whichever
// comes first.
char *fgets(char *str, int n, FILE *stream)
{
assert(str && stream);
if (!str || !stream)
return NULL;
// BUG - not correctly implemented!
int count = read(stream->fildes, str, n);
if (count != n)
return NULL;
return str;
}
//
int getc(FILE *stream)
{
assert(stream);
return fgetc(stream);
}
//
int getchar(void)
{
return fgetc(stdin);
}
// Reads a line from stdin and stores it into the string pointed to by, str.
// It stops when either the newline character is read or when the end-of-file
// is reached, whichever comes first.
char *gets(char *str)
{
assert(str);
// BUG - not correctly implemented!
return NULL;
}
//
////
//int ungetc(int chr, FILE *stream)
//{
// assert(stream);
//}
//
char *_itoa(int value, char *str, int base)
{
char *pchr = str;
int place_value;
// check for negative numbers
if (value < 0 && base == 10)
*pchr++ = '-';
while (value)
{
place_value = value % base;
*pchr++ = place_value + '0';
value /= base;
}
*pchr = 0; // make sure we are asciiz!
return _strrev(str);
}