casaroli opened a new pull request, #20633:
URL: https://github.com/apache/nuttx/pull/20633

   ## Summary
   
   A line buffered stream is flushed at a newline only by `fputc()`, `fputs()` 
and `puts()`. `printf()` writes through `fwrite()`, so a line that `printf()` 
writes to a line buffered stream (stdout or stderr on a terminal) stays in the 
buffer until the buffer is full or the program exits. The output of stderr 
comes after later lines of stdout, and complete lines are lost when the program 
ends with `_exit()` or `abort()`.
   
   This PR flushes the stream in `lib_fwrite_unlocked()` when the stream is 
line buffered and the data holds a newline.
   
   The first commit fixes an old nxstyle error in `lib_libfwrite.c` (a missing 
blank line), so that checkpatch passes on the file.
   
   ## Impact
   
   `fwrite()`, `printf()`, `fprintf()` and `vfprintf()` on line buffered 
streams (`CONFIG_STDIO_BUFFER_SIZE` > 0): a line is now written when the 
newline is written, as C and POSIX require. Fully buffered streams do not 
change.
   
   ## Testing
   
   `qemu-armv8a:knsh`, with these commits on a larger local branch. A test 
program writes four lines, alternating `fprintf(stderr, ...)` and `printf()`, 
with an `fflush(stdout)` after the second line:
   
   ```
   fprintf(stderr, "stdbuf: 1 stderr\n");
   printf("stdbuf: 2 stdout\n");
   fflush(stdout);
   fprintf(stderr, "stdbuf: 3 stderr\n");
   printf("stdbuf: 4 stdout\n");
   ```
   
   Before: "stdbuf: 1 stderr" came out after "stdbuf: 2 stdout", because stderr 
was flushed only at exit.
   
   After: the four lines come out in program order.
   
   Build: `qemu-armv8a:nsh`, `qemu-armv8a:knsh` and `lm3s6965-ek:qemu-flat`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to