On 05/30/2012 10:26 PM, Pedro Alves wrote: > On 05/30/2012 05:44 PM, Joerg Wunsch wrote: > >> As Richard Henderson wrote: >> >>> Instead of writing to stdout, open the file to write, and open >>> it in binary mode. Seems much easier than fighting with conversion >>> after the fact. >> >> (Disclaimer: I'm not the author.) >> >> There has been an argument that (some) older implementations might not >> be able to handle the "b" for binary mode. It's probably questionable >> whether such ancient (Unix) implementations bear any relevance anymore >> when it comes to the AVR port of GCC though. (IIRC, ISO-C90 did >> standardize the "b" mode letter to fopen().) > > > Not 'fopen' with "b", but 'open' with O_BINARY. There's precedent for that > already in gcc and other parts of the toolchain (binutils, gdb), as a grep > will tell. O_BINARY is defaulted to 0 in system.h (so that it's a nop), and > is usually defined in fcntl.h (to non-zero) on platforms that actually > differentiate text and binary modes, such as Windows. >
Oh, and BTW, include/ in the src tree has these fopen-bin.h, fopen-same.h and fopen-vms.h headers (*), that you could import into the gcc tree, and use if you want to stick with fopen and friends. They provide a series of defines like FOPEN_RB, FOPEN_WB, etc., to map to "r", "w", or "rb", "wb", etc. depending on host. This is used all over binutils whenever it wants to fopen files in binary mode. If it's fine for binutils, it should be fine for gcc. You'd might also want something like this <http://sourceware.org/ml/binutils/2012-05/msg00227.html> to reuse bfd's configury to pick the one to use (and perhaps add a wrapping fopen-foo.h header). (*) - http://sourceware.org/cgi-bin/cvsweb.cgi/~checkout~/src/include/fopen-bin.h?rev=1.1.1.1&content-type=text/plain&cvsroot=src http://sourceware.org/cgi-bin/cvsweb.cgi/~checkout~/src/include/fopen-same.h?rev=1.1&content-type=text/plain&cvsroot=src http://sourceware.org/cgi-bin/cvsweb.cgi/~checkout~/src/include/fopen-vms.h?rev=1.3&content-type=text/plain&cvsroot=src -- Pedro Alves