[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] stdio write problem in mintlib



Alan Hourihane a écrit :
Yeah, it's o.k. turns out to be the shell.

Not to worry.

Unfortunately, you're wrong. The problem is in the MiNTLib.
The main idea is that the text mode (CR+LF) should only be used with regular files (if not disabled by UNIXMODE=b), and never used with pipes. The shell statement `command` does exactly what is expected : it runs the command to a pipe, then replaces spaces and LF by single spaces. The CR characters are treated as normal characters (not removed). It should not be a problem, because no end-of-line CR should be present in the pipe. I know very well the problem, because some time ago, I had a discussion about that with Cygwin people, where the situation is similar.

The current MiNTLib always configures stdout in textmode, regardless if it is connected to a device (console), a file, or a pipe. It really should check for that, like Cygwin.

A workaround is to configure the correct mode by hand at the beginning of the program.

#include <stdio.h>
#include <sys/stat.h>

main()
{
    /* Never use text mode to pipes. */
    struct stat st;
    fstat(1, &st);
    if (S_ISSOCK(st.st_mode))
        __set_binmode(stdout, 1);

    putchar('a');
    putchar('\n');
}

This will work as expected in all cases.

Note that during my tests, I got the same results with putchar() and printf(), maybe you made a mistake somewhere.

--
Vincent Rivière