From 2dfddf3fa8fce4172559858f38c77d577118bfaa Mon Sep 17 00:00:00 2001 From: Tee-Kiah Chia Date: Sat, 27 Mar 2021 06:59:07 +0000 Subject: [PATCH] libcc.ansi: add support for O_TEXT, O_BINARY file status flags --- lang/cem/libcc.ansi/headers/ack/config.h | 4 ++++ lang/cem/libcc.ansi/headers/ack/emufile.h | 1 + lang/cem/libcc.ansi/headers/unistd.h | 4 ++++ lang/cem/libcc.ansi/sys/stdio/fdopen.c | 18 ++++++++++++++++++ lang/cem/libcc.ansi/sys/stdio/fopen.c | 11 +++++++++-- lang/cem/libcc.ansi/sys/stdio/freopen.c | 10 +++++++++- 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lang/cem/libcc.ansi/headers/ack/config.h b/lang/cem/libcc.ansi/headers/ack/config.h index 41e9bc3b6..cf8f5d835 100644 --- a/lang/cem/libcc.ansi/headers/ack/config.h +++ b/lang/cem/libcc.ansi/headers/ack/config.h @@ -15,6 +15,10 @@ #define ACKCONF_WANT_STANDARD_O 1 #endif +#ifndef ACKCONF_WANT_O_TEXT_O_BINARY +#define ACKCONF_WANT_O_TEXT_O_BINARY 0 +#endif + #ifndef ACKCONF_WANT_STANDARD_SIGNALS #define ACKCONF_WANT_STANDARD_SIGNALS 1 #endif diff --git a/lang/cem/libcc.ansi/headers/ack/emufile.h b/lang/cem/libcc.ansi/headers/ack/emufile.h index 06a1ffcec..158ec58a8 100644 --- a/lang/cem/libcc.ansi/headers/ack/emufile.h +++ b/lang/cem/libcc.ansi/headers/ack/emufile.h @@ -24,6 +24,7 @@ struct FILE { #define _IOREADING 0x080 #define _IOWRITING 0x100 #define _IOAPPEND 0x200 +#define _IOBINARY 0x400 #if !defined BUFSIZ #define BUFSIZ 1024 diff --git a/lang/cem/libcc.ansi/headers/unistd.h b/lang/cem/libcc.ansi/headers/unistd.h index bab5ada71..a316132d2 100644 --- a/lang/cem/libcc.ansi/headers/unistd.h +++ b/lang/cem/libcc.ansi/headers/unistd.h @@ -24,6 +24,10 @@ O_WRONLY = 1, O_RDWR = 2, + #if ACKCONF_WANT_O_TEXT_O_BINARY + O_TEXT = 010000, + O_BINARY = 020000, + #endif O_CREAT = 0100, O_TRUNC = 01000, O_APPEND = 02000, diff --git a/lang/cem/libcc.ansi/sys/stdio/fdopen.c b/lang/cem/libcc.ansi/sys/stdio/fdopen.c index aff7d248e..58fcecfcd 100644 --- a/lang/cem/libcc.ansi/sys/stdio/fdopen.c +++ b/lang/cem/libcc.ansi/sys/stdio/fdopen.c @@ -5,6 +5,10 @@ #include #include +#if ACKCONF_WANT_O_TEXT_O_BINARY + #include + #include +#endif #if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE @@ -38,6 +42,9 @@ FILE* fdopen(int fd, const char* mode) switch (*mode++) { case 'b': +#if ACKCONF_WANT_O_TEXT_O_BINARY + flags |= _IOBINARY; +#endif continue; case '+': flags |= _IOREAD | _IOWRITE; @@ -57,6 +64,17 @@ FILE* fdopen(int fd, const char* mode) if ((flags & _IOREAD) && (flags & _IOWRITE)) flags &= ~(_IOREADING | _IOWRITING); +#if ACKCONF_WANT_O_TEXT_O_BINARY + { + /* + * FIXME: this assumes that any platform that has O_TEXT and + * O_BINARY will also have a _setmode() function. + */ + extern int _setmode(int, int); + _setmode(fd, (flags & _IOBINARY) ? O_BINARY : O_TEXT); + } +#endif + stream->_count = 0; stream->_fd = fd; stream->_flags = flags; diff --git a/lang/cem/libcc.ansi/sys/stdio/fopen.c b/lang/cem/libcc.ansi/sys/stdio/fopen.c index f2189ee8f..cfea44154 100644 --- a/lang/cem/libcc.ansi/sys/stdio/fopen.c +++ b/lang/cem/libcc.ansi/sys/stdio/fopen.c @@ -7,7 +7,6 @@ #include #include #include -#include #if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE @@ -68,6 +67,9 @@ FILE* fopen(const char* name, const char* mode) switch (*mode++) { case 'b': +#if ACKCONF_WANT_O_TEXT_O_BINARY + flags |= _IOBINARY; +#endif continue; case '+': rwmode = O_RDWR; @@ -80,6 +82,7 @@ FILE* fopen(const char* name, const char* mode) break; } +#if !ACKCONF_WANT_O_TEXT_O_BINARY /* Perform a creat() when the file should be truncated or when * the file is opened for writing and the open() failed. */ @@ -87,12 +90,16 @@ FILE* fopen(const char* name, const char* mode) || (((fd = open(name, rwmode)) < 0) && (rwflags & O_CREAT))) { - if (((fd = creat(name, PMODE)) > 0) && flags | _IOREAD) + if (((fd = creat(name, PMODE)) >= 0) && (flags & _IOREAD)) { (void)close(fd); fd = open(name, rwmode); } } +#else + rwflags |= (flags & _IOBINARY) ? O_BINARY : O_TEXT; + fd = open(name, rwmode | rwflags, PMODE); +#endif if (fd < 0) return (FILE*)NULL; diff --git a/lang/cem/libcc.ansi/sys/stdio/freopen.c b/lang/cem/libcc.ansi/sys/stdio/freopen.c index 846208420..1ae5befe0 100644 --- a/lang/cem/libcc.ansi/sys/stdio/freopen.c +++ b/lang/cem/libcc.ansi/sys/stdio/freopen.c @@ -50,6 +50,9 @@ FILE* freopen(const char* name, const char* mode, FILE* stream) switch (*mode++) { case 'b': +#if ACKCONF_WANT_O_TEXT_O_BINARY + flags |= _IOBINARY; +#endif continue; case '+': rwmode = O_RDWR; @@ -62,16 +65,21 @@ FILE* freopen(const char* name, const char* mode, FILE* stream) break; } +#if !ACKCONF_WANT_O_TEXT_O_BINARY if ((rwflags & O_TRUNC) || (((fd = open(name, rwmode)) < 0) && (rwflags & O_CREAT))) { - if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD) + if (((fd = creat(name, PMODE)) >= 0) && (flags & _IOREAD)) { (void)close(fd); fd = open(name, rwmode); } } +#else + rwflags |= (flags & _IOBINARY) ? O_BINARY : O_TEXT; + fd = open(name, rwmode | rwflags, PMODE); +#endif if (fd < 0) {