summaryrefslogtreecommitdiff
path: root/src/Util.cpp
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@centrum.cz>2021-11-01 12:37:11 +0000
committerR. Voggenauer <rvogg@users.noreply.github.com>2021-11-23 22:09:13 +0100
commit00681940d9c03f68a457b3369b31a7d588104555 (patch)
tree35a06e51db49b4d765c98c8785bdf3774064752c /src/Util.cpp
parentcc96e876ed034d992ecae0f0e8f04caf178c371b (diff)
downloadccache-00681940d9c03f68a457b3369b31a7d588104555.tar.gz
fix: handle newlines on Windows properly
Diffstat (limited to 'src/Util.cpp')
-rw-r--r--src/Util.cpp28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/Util.cpp b/src/Util.cpp
index 24ce1bbd..1f812fe3 100644
--- a/src/Util.cpp
+++ b/src/Util.cpp
@@ -1174,12 +1174,36 @@ same_program_name(nonstd::string_view program_name,
#endif
}
+#ifdef _WIN32
+// Stdout/stderr are normally opened in text mode, which would convert newlines
+// a second time, since we treat outputs as binary data.
+// Make sure to switch to binary mode.
+namespace {
+struct BinaryModeHelper
+{
+ BinaryModeHelper(int fd_) : fd(fd_), oldmode(_setmode(fd, _O_BINARY))
+ {
+ }
+ ~BinaryModeHelper()
+ {
+ _setmode(fd, oldmode);
+ }
+ int fd;
+ int oldmode;
+};
+} // namespace
+#endif
+
void
-send_to_stderr(const Context& ctx, const std::string& text)
+send_to_fd(const Context& ctx, const std::string& text, int fd)
{
const std::string* text_to_send = &text;
std::string modified_text;
+#ifdef _WIN32
+ BinaryModeHelper helper(fd);
+#endif
+
if (ctx.args_info.strip_diagnostics_colors) {
try {
modified_text = strip_ansi_csi_seqs(text);
@@ -1195,7 +1219,7 @@ send_to_stderr(const Context& ctx, const std::string& text)
}
try {
- write_fd(STDERR_FILENO, text_to_send->data(), text_to_send->length());
+ write_fd(fd, text_to_send->data(), text_to_send->length());
} catch (core::Error& e) {
throw core::Error("Failed to write to stderr: {}", e.what());
}