aboutsummaryrefslogtreecommitdiff
path: root/src/core/io_win.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/io_win.c')
-rw-r--r--src/core/io_win.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/core/io_win.c b/src/core/io_win.c
new file mode 100644
index 0000000..e197353
--- /dev/null
+++ b/src/core/io_win.c
@@ -0,0 +1,25 @@
+/* Windows I/O port for tinyvm: provides Q, G and J with binary stdio */
+#include <stdio.h>
+#include <fcntl.h>
+#include <io.h>
+
+int J;
+static int pk(void) { int c = _fgetc_nolock(stdin); if (c != EOF) _ungetc_nolock(c, stdin); return c == EOF; }
+char Q(void) { int c = _fgetc_nolock(stdin); J = pk(); return (char)c; }
+void G(char c) { _fputc_nolock(c, stdout); }
+
+static void init_io(void) {
+ _setmode(_fileno(stdin), _O_BINARY);
+ _setmode(_fileno(stdout), _O_BINARY);
+ J = pk();
+}
+
+#if defined(__GNUC__) || defined(__clang__)
+__attribute__((constructor)) static void init(void) { init_io(); }
+#elif defined(_MSC_VER)
+#pragma section(".CRT$XCU", read)
+static void __cdecl msc_init(void) { init_io(); }
+__declspec(allocate(".CRT$XCU")) static void (__cdecl *msc_init_fn)(void) = msc_init;
+#else
+static void init(void) { init_io(); }
+#endif