Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set stdin and stdout in binary mode under Windows #6986

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/support/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@
#include <iostream>
#include <limits>

#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
#endif

#define DEBUG_TYPE "file"

std::vector<char> wasm::read_stdin() {
BYN_TRACE("Loading stdin...\n");
std::vector<char> input;
char c;
#if defined(_WIN32)
// Windows opens stdin in text mode. Set the file mode to binary so
// that we can read Wasm binary files correctly.
assert(_setmode(fileno(stdin), _O_BINARY));
#endif
while (std::cin.get(c) && !std::cin.eof()) {
input.push_back(c);
}
Expand Down Expand Up @@ -106,6 +116,11 @@ wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary)
// about the types of different returns here.
std::streambuf* buffer;
if (filename == "-" || filename.empty()) {
#if defined(_WIN32)
// Windows opens stdout in text mode. Set the file mode to binary
// so that we can output Wasm binary files correctly.
assert(_setmode(fileno(stdout), _O_BINARY));
#endif
buffer = std::cout.rdbuf();
} else {
BYN_TRACE("Opening '" << filename << "'\n");
Expand Down
Loading