This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On 25/11/09 03:52 AM, Corinna Vinschen wrote:
On Nov 24 20:48, Anthony DeRosa wrote:
Hi,

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");
     fflush(stdout);

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 don't know whom to blame in your case, but the above testcase works fine on Cygwin, which is using newlib as well. I think you will have to put more debugging effort into this, like, say, running your testcase in a debugger.


Corinna is correct. You should be calling the __sfvwrite_r routine (libc/stdio/fvwrite.c) for each printf call with the input string (i.e. there isn't any formatting done by the main _vfprintf_r routine). In __sfvwrite_r, you should fall into the line-buffered code which searches the string for a newline and after finding one, calls _fflush_r. You should make two calls to _fflush_r when the string has the two newlines in it and each of these calls should end up calling your write syscall. That should give you a place to start.


FWIW: testing the above code using newlib and running on an mn10300-elf simulator also works fine.

-- Jeff J.


Corinna




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]