printf not working, if buffering disabled

Marc Donner marc.donner@mdet.de
Thu Sep 22 10:34:00 GMT 2011


On 09/20/2011 01:11 AM, Jeff Johnston wrote:
> On 09/18/2011 08:13 AM, Marc Donner wrote:
>> Hi,
>>
>> i've wrote a small program using newlib's printf funktion. All works
>> well as long as buffering is enabled. If I disable buffering using the
>> following code
>>
>> setvbuf(stdout, NULL, _IONBF, 0);
>>
>> the printf functions are not working anymore, while a call to putchar
>> produces the expected result.
>>
>> The code looks like this
>>
>> #include<stdio.h>
>>
>> void
>> main (void) {
>> /* init USB CDC device */
>> usbcdc_init();
>>
>> //setvbuf(stdout, NULL, _IONBF, 0);
>> putchar('y');
>> putchar('\n');
>> iprintf("test\n"); /*<- not working if buffering disabled */
>>
>> while (1);
>> }
>>
>> The _write syscall is implemented as
>>
>> int _write(int file, char *ptr, int len) {
>> while (len--) {
>> if (*ptr == '\n') {
>> usbcdc_putch('\r');
>> }
>> }
>> return len;
>> }
>>
>> Anybody an idea?
>>
>
> Marc,
>
> Your test case works fine on a mn10300 simulator using newlib so I would
> tend to suspect there is something wrong with your implementation.
>
> If that is actually the code for _write, it has multiple flaws. It
> doesn't move ptr forward and doesn't appear to write any characters to
> the system but '\r'. It also returns -1 instead of number of bytes
> written. I hope you are just summarizing your code, otherwise, there's
> no way this could work. I'm guessing you have probably mapped putchar to
> usbcdc_putch somewhere.
>
> Test out your write syscall by calling it directly instead of iprintf.
> If your _write works, try debugging and follow the iprintf down to
> __sfvwrite_r and then fp->_write, etc... If you can't use a debugger for
> your platform, you might need to add appropriate usbcdc_xxxx statements
> so you can track your progress.
>
> -- Jeff J.
>
>> Regards,
>>
>> Marc
>

Hi Jeff,

thx for the answer. I found the problem, it was in my _write 
implementation. My _write returned len, which is always 0.
But I'm wondering why this has worked if stdout line buffering was enabled.

Regards,
Marc



More information about the Newlib mailing list