Files
omoikane aff78be8f9 Adjusted alternate version to preserve formatting
The first alt version added some whitespaces that broke the outline, so
I simply filled those up.  I also expanded tabs to spaces since there is
no size limit restriction for the alternate version.

The second alt version attempted to force binary mode with _setmode()
and _fileno() calls, plus some additional headers.  Rather than doing it
this way, I hack in the binary mode adjustments via preprocessor.

Actually I did a similar trick for IOCCC 2011, but I guess I didn't
submit that version:
https://uguu.org/archive/ioccc2011/akari/source/binmode_main.h

The file above uses _setmode() and _fileno().  The leading underscore
indicates that they were nonstandard extensions.  For the binmode_main.h
in this change, I have opted for setmode() and STDIN_FILENO instead,
which are more widely available.
2025-05-15 17:51:12 -07:00

40 lines
2.0 KiB
C

/* Preprocessor hack to set binary mode for stdin and stdout.
This replaces main() with an alternative function forces binary
modes. This is intended to included via compiler arguments, so we
would be able to enable binary modes without modifying the original
program source. Conceptually, it's similar to LD_PRELOAD, except
we hack it in via preprocessor.
It only works for programs where main() occurs just once.
*/
#if defined(_WIN32)
#include<stdio.h>
#include<fcntl.h>
#include<io.h>
#include<unistd.h>
#define main \
/* Forward declaration for the updated main(), now renamed */ \
/* binmode_main(). Return type is omitted since this macro */ \
/* is expanded just after the return type. */ \
binmode_main(int argc, char **argv); \
\
/* Updated main(). */ \
int main(int argc, char **argv) \
{ \
/* Set binary modes. */ \
setmode(STDIN_FILENO, O_BINARY); \
setmode(STDOUT_FILENO, O_BINARY); \
\
/* Call the original main(). */ \
return binmode_main(argc, argv); \
} \
\
/* Definition for the original main. Function arguments are */ \
/* omitted since this macro is expanded just before the arguments. */ \
int binmode_main
#endif