Repository navigation
Expand file tree
/
Copy pathStandardStreams.cpp
More file actions
179 lines (175 loc) · 6.67 KB
/
Copy pathStandardStreams.cpp
File metadata and controls
179 lines (175 loc) · 6.67 KB
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
#include "prx/libc/include/FileStream.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#ifdef _WIN32
#include <io.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
namespace {
#ifndef _WIN32
const bool standardDescriptorsOpen = [] {
for (int descriptor = 0; descriptor <= 2; ++descriptor) {
if (::fcntl(descriptor, F_GETFD) != -1 || errno != EBADF) continue;
const int opened = ::open("/dev/null", descriptor == 0 ? O_RDONLY : O_WRONLY);
if (opened > descriptor) {
::dup2(opened, descriptor);
::close(opened);
}
}
return true;
}();
#endif
FileStream input{stdin};
}
extern "C" {
FileStream* __stdinp_nid_postfix = &input;
FileStream* __stdoutp_nid_postfix = &_Stdout_nid_postfix;
FileStream* __stderrp_nid_postfix = &_Stderr_nid_postfix;
int __isthreaded_nid_postfix = 1;
int APS5_VABI fgetc_nid_postfix(FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
const int result = std::fgetc(GetNativeStream(stream));
stream->SyncStatus();
return result;
}
std::int32_t APS5_VABI fgetwc_nid_postfix(FileStream* stream) {
if (stream == nullptr) {
errno = 22;
return -1;
}
const int first = fgetc_nid_postfix(stream);
if (first == -1) return -1;
const unsigned char lead = static_cast<unsigned char>(first);
if (lead < 0x80) return lead;
const std::size_t extra = lead >= 0xf0 ? 3 : lead >= 0xe0 ? 2 : lead >= 0xc0 ? 1 : 0;
if (extra == 0) {
errno = 86;
stream->SetError();
return -1;
}
std::uint32_t code = extra == 3 ? (lead & 0x07) : extra == 2 ? (lead & 0x0f) : (lead & 0x1f);
for (std::size_t index = 0; index < extra; ++index) {
const int next = fgetc_nid_postfix(stream);
if (next == -1 || (static_cast<unsigned char>(next) & 0xc0) != 0x80) {
errno = 86;
stream->SetError();
return -1;
}
code = (code << 6) | (static_cast<unsigned char>(next) & 0x3f);
}
return static_cast<std::int32_t>(code);
}
std::int32_t APS5_VABI ungetwc_nid_postfix(std::int32_t value, FileStream* stream) {
if (value == -1) return -1;
if (value < 0 || value > 255) {
errno = 86;
stream->SetError();
return -1;
}
const int result = std::ungetc(value, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
int APS5_VABI getc_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); }
int APS5_VABI getc_unlocked_nid_postfix(FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
#ifdef _WIN32
const int result = _fgetc_nolock(GetNativeStream(stream));
#else
const int result = ::getc_unlocked(GetNativeStream(stream));
#endif
stream->SyncUnlockedStatus();
return result;
}
int APS5_VABI __srget_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); }
int APS5_VABI getchar_nid_postfix() { return fgetc_nid_postfix(__stdinp_nid_postfix); }
std::int32_t APS5_VABI getwc_nid_postfix(FileStream* stream) { return fgetwc_nid_postfix(stream); }
std::int32_t APS5_VABI getwchar_nid_postfix() { return fgetwc_nid_postfix(__stdinp_nid_postfix); }
int APS5_VABI getchar_unlocked_nid_postfix() { return getc_unlocked_nid_postfix(__stdinp_nid_postfix); }
int APS5_VABI fputc_nid_postfix(int value, FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
const int result = std::fputc(value, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
int APS5_VABI putc_nid_postfix(int value, FileStream* stream) { return fputc_nid_postfix(value, stream); }
int APS5_VABI putc_unlocked_nid_postfix(int value, FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
#ifdef _WIN32
const int result = _fputc_nolock(value, GetNativeStream(stream));
#else
const int result = ::putc_unlocked(value, GetNativeStream(stream));
#endif
stream->SyncUnlockedStatus();
return result;
}
int APS5_VABI __swbuf_nid_postfix(int value, FileStream* stream) { return fputc_nid_postfix(value, stream); }
int APS5_VABI putchar_nid_postfix(int value) { return fputc_nid_postfix(value, __stdoutp_nid_postfix); }
int APS5_VABI putchar_unlocked_nid_postfix(int value) { return putc_unlocked_nid_postfix(value, __stdoutp_nid_postfix); }
int APS5_VABI ungetc_nid_postfix(int value, FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
const int result = std::ungetc(value, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
char* APS5_VABI fgets_nid_postfix(char* buffer, int size, FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return nullptr; }
if (size <= 0) {
stream->SetError();
errno = 22;
return nullptr;
}
auto* result = std::fgets(buffer, size, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
int APS5_VABI feof_nid_postfix(FileStream* stream) {
stream->SyncStatus();
return (stream->GuestState().flags & 0x20) != 0;
}
int APS5_VABI ferror_nid_postfix(FileStream* stream) {
stream->SyncStatus();
return (stream->GuestState().flags & 0x40) != 0;
}
void APS5_VABI clearerr_nid_postfix(FileStream* stream) {
stream->ClearError();
}
int APS5_VABI fileno_nid_postfix(FileStream* stream) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
#ifdef _WIN32
const int descriptor = _fileno(GetNativeStream(stream));
#else
const int descriptor = ::fileno(GetNativeStream(stream));
#endif
stream->GuestState().descriptor = static_cast<std::int16_t>(descriptor);
return descriptor;
}
void APS5_VABI clearerr_unlocked_nid_postfix(FileStream* stream) {
stream->ClearError();
}
int APS5_VABI fileno_unlocked_nid_postfix(FileStream* stream) {
if (stream == nullptr || !stream->IsOpen()) return -1;
return fileno_nid_postfix(stream);
}
[[noreturn]] char* APS5_VABI fgetln_nid_postfix(FileStream*, std::size_t*) {
throw std::runtime_error("fgetln: the console's export is a hlt instruction, which ends the process");
}
int APS5_VABI setvbuf_nid_postfix(FileStream* stream, char* buffer, int mode, std::size_t size) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
if (mode < 0 || mode > 2) return -1;
const int native = mode == 0 ? _IOFBF : mode == 1 ? _IOLBF : _IONBF;
if (native != _IONBF && size == 0) {
buffer = nullptr;
size = BUFSIZ;
}
return std::setvbuf(GetStreamHandle(stream), buffer, native, size);
}
void APS5_VABI setbuf_nid_postfix(FileStream* stream, char* buffer) {
setvbuf_nid_postfix(stream, buffer, buffer ? 0 : 2, buffer ? 1024 : 0);
}
}