aboutsummaryrefslogtreecommitdiff
path: root/src/core/io_win.c
blob: e197353268cf0b2467e6ef91741dc95f861d1fcc (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
/* 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