I'm writing binary data to a file. The data may contain 0xa ( = \n, NL, ^J
).
When I open the output file within the program with ` fopen("file", "wb");'
the data is
written correctly. Of course using "w" without the "b" causes hex 0xa to be
pre-pended
with 0xd ( = \r, CR, ^M) corrupting the data, as expected, so must use "wb".
That's fine.
However, if within the program I write the data to stdout, and then on
command line
redirect stdout to my file, the file seems to always contain the undesired
0xd (^M) characters.
e.g.: .\example > file
I've tried a few things from standard Unix like freopen() fdopen() but
nothing seemed to help.
Is there some way to redirect purely binary (raw) data to stdout without
having extra bytes
inserted into the output stream?
What is __imp__fmode hook in tinycc? Maybe that would help?
John Refling
PS: Using cygwin bash shell but also same output under cmd.exe
EXAMPLE.C FOLLOWS:
// includes and prototypes
#include <stdio.h>
#include <stdlib.h>
#include <sys/unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <ctype.h>
#include <fcntl.h>
// is there a way to use this to change output mode???
extern unsigned int * __imp__fmode;
int main(int argc, char **argv) {
// *__imp__fmode = 3; // ????
char *buffer_nl = "test\n";
char *buffer_xx = "test";
// // ^M tacked onto lines in file `file' (as expected)
// FILE *F = fopen("file", "w");
// fprintf(F, buffer_nl);
// // no ^M tacked onto lines in file `file' (as expected)
// FILE *F = fopen("file", "wb");
// fprintf(F, buffer_nl);
// i want output to redirect stdout from here on...
// this is where ^M seems to be ALWAYS added
// // .\example.exe > redir, `redir' has ^M tacked onto lines
// fprintf(stdout, buffer_nl);
// // .\example.exe > redir, `redir' no newline in buffer, no ^M tacked
// fprintf(stdout, buffer_xx);
// some segfault under tcc, not under gcc
// FILE *F = freopen("nul:", "wb", stdout); // nothing
in file
// FILE *F = freopen("tty:", "wb", stdout); // segfault
// FILE *F = freopen(NULL, "wb", stdout); // segfault
// FILE *F = freopen("", "wb", stdout); // segfault
// FILE *F = freopen("con:", "wb", stdout); // can't
redirect this
// fprintf(F, buffer_nl);
// // .\example.exe > redir, `redir' has ^M tacked onto lines
// FILE *F = fdopen(1, "wb");
FILE *F = fdopen(1, "wt");
fprintf(F, buffer_nl);
return 0;
}
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel