Fwd: Printf stops calling write() syscall after encountering a line feed

Anthony DeRosa anthony.derosa@gmail.com
Wed Nov 25 05:13:00 GMT 2009


I am having trouble with printf, and I am positive that its not my
terminal's fault or a buffering problem.  First I'll explain the
problem, and second why I think it's not my terminal's fault.

The problem is that printf stops calling the write() syscall after
encountering a line feed character ('\n').  Below is some code and
corresponding output to demonstrate.

The code:

    printf("line 0\n");
    printf("line 1\nline 2\n");
    printf("line 3");
    printf("\nline 4\n");
    printf("line 5\n");

The expected output:

line 0
line 1
line 2
line 3
line 4
line 5

(my broken) Newlib's output:

line 0
line 1  // stops printing line 2 after line feed
line 3  // stops printing line 4 after line feed
line 5

The reason I don't blame my terminal is because if I replace all calls
to printf with calls to write(1, text, textlen), then everything works
as expected with regards to line feeds.  Things also work as expected
if I use sprintf() followed by a call to write().

I did not use any special build options for reentrancy when I built
newlib, and this is a single-threaded application on an i686-elf
platform.  I have tried overriding _write_r() as well as write(), but
neither of them are getting called after the line feed found in any
given string.  One other thing to mention is that I am not doing
anything to initialize newlib in any way, except for having the grub
bootloader load my elf (and initialize the data section and clear
bss).  Don't know if I'm missing something there.

Thanks for your help,

Anthony DeRosa

More information about the Newlib mailing list