From fe55ffee7bdbbd5f4291969cb390a8f795d81aa3 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Thu, 26 Sep 2024 18:52:30 -0400 Subject: [PATCH] Use `setmode` instead of `fdopen` --- src/asm/output.cpp | 7 +++++-- src/fix/main.cpp | 5 +++-- src/link/object.cpp | 3 ++- src/link/output.cpp | 12 ++++++++---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/asm/output.cpp b/src/asm/output.cpp index f73a0f2a2..c98731750 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -12,6 +12,7 @@ #include "error.hpp" #include "helpers.hpp" // assume, Defer +#include "platform.hpp" #include "asm/charmap.hpp" #include "asm/fstack.hpp" @@ -311,7 +312,8 @@ void out_WriteObject() { file = fopen(objectFileName.c_str(), "wb"); } else { objectFileName = ""; - file = fdopen(STDOUT_FILENO, "wb"); + (void)setmode(STDOUT_FILENO, O_BINARY); + file = stdout; } if (!file) err("Failed to open object file '%s'", objectFileName.c_str()); @@ -503,7 +505,8 @@ void out_WriteState(std::string name, std::vector const &features) file = fopen(name.c_str(), "wb"); } else { name = ""; - file = fdopen(STDOUT_FILENO, "wb"); + (void)setmode(STDOUT_FILENO, O_BINARY); + file = stdout; } if (!file) err("Failed to open state file '%s'", name.c_str()); diff --git a/src/fix/main.cpp b/src/fix/main.cpp index eec8b8969..46b9c28ce 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -1160,9 +1160,9 @@ static bool processFilename(char const *name) { nbErrors = 0; if (!strcmp(name, "-")) { + name = ""; (void)setmode(STDIN_FILENO, O_BINARY); (void)setmode(STDOUT_FILENO, O_BINARY); - name = ""; processFile(STDIN_FILENO, STDOUT_FILENO, name, 0); } else { // POSIX specifies that the results of O_RDWR on a FIFO are undefined. @@ -1435,7 +1435,8 @@ int main(int argc, char *argv[]) { logoFile = fopen(logoFilename, "rb"); } else { logoFilename = ""; - logoFile = fdopen(STDIN_FILENO, "rb"); + (void)setmode(STDIN_FILENO, O_BINARY); + logoFile = stdin; } if (!logoFile) { fprintf( diff --git a/src/link/object.cpp b/src/link/object.cpp index 227bd974e..4115786fa 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -490,7 +490,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { file = fopen(fileName, "rb"); } else { fileName = ""; - file = fdopen(STDIN_FILENO, "rb"); // `stdin` is in text mode by default + (void)setmode(STDIN_FILENO, O_BINARY); + file = stdin; } if (!file) err("Failed to open file \"%s\"", fileName); diff --git a/src/link/output.cpp b/src/link/output.cpp index 9ce02b968..e584ce916 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -207,7 +207,8 @@ static void writeROM() { outputFile = fopen(outputFileName, "wb"); } else { outputFileName = ""; - outputFile = fdopen(STDOUT_FILENO, "wb"); + (void)setmode(STDOUT_FILENO, O_BINARY); + outputFile = stdout; } if (!outputFile) err("Failed to open output file \"%s\"", outputFileName); @@ -222,7 +223,8 @@ static void writeROM() { overlayFile = fopen(overlayFileName, "rb"); } else { overlayFileName = ""; - overlayFile = fdopen(STDIN_FILENO, "rb"); + (void)setmode(STDIN_FILENO, O_BINARY); + overlayFile = stdin; } if (!overlayFile) err("Failed to open overlay file \"%s\"", overlayFileName); @@ -545,7 +547,8 @@ static void writeSym() { symFile = fopen(symFileName, "w"); } else { symFileName = ""; - symFile = fdopen(STDOUT_FILENO, "w"); + (void)setmode(STDOUT_FILENO, O_TEXT); // May have been set to O_BINARY previously + symFile = stdout; } if (!symFile) err("Failed to open sym file \"%s\"", symFileName); @@ -589,7 +592,8 @@ static void writeMap() { mapFile = fopen(mapFileName, "w"); } else { mapFileName = ""; - mapFile = fdopen(STDOUT_FILENO, "w"); + (void)setmode(STDOUT_FILENO, O_TEXT); // May have been set to O_BINARY previously + mapFile = stdout; } if (!mapFile) err("Failed to open map file \"%s\"", mapFileName);