diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-05-14 17:37:47 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-05-14 17:37:47 +0000 |
commit | 18615da8957c23faa4924b2d1b3d222a28db9db8 (patch) | |
tree | fdd388cc6fa54c5a3cc7702b5128b7ca3cf5325a /nuttx/lib | |
parent | f62084035feaf8b9a22804821b22e2a3c15e6eda (diff) | |
download | px4-nuttx-18615da8957c23faa4924b2d1b3d222a28db9db8.tar.gz px4-nuttx-18615da8957c23faa4924b2d1b3d222a28db9db8.tar.bz2 px4-nuttx-18615da8957c23faa4924b2d1b3d222a28db9db8.zip |
Flush buffer after newline in putc, fputc, and puts (but not fputs)
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3607 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib')
-rw-r--r-- | nuttx/lib/stdio/lib_fputc.c | 12 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_fputs.c | 25 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_libvsprintf.c | 2 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_puts.c | 25 |
4 files changed, 47 insertions, 17 deletions
diff --git a/nuttx/lib/stdio/lib_fputc.c b/nuttx/lib/stdio/lib_fputc.c index f5eeecea7..34ef7aa25 100644 --- a/nuttx/lib/stdio/lib_fputc.c +++ b/nuttx/lib/stdio/lib_fputc.c @@ -89,6 +89,18 @@ int fputc(int c, FAR FILE *stream) unsigned char buf = (unsigned char)c; if (lib_fwrite(&buf, 1, stream) > 0) { + /* Flush the buffer if a newline is output */ + +#ifdef CONFIG_STDIO_LINEBUFFER + if (c == '\n') + { + int ret = lib_fflush(stream, true); + if (ret < 0) + { + return EOF; + } + } +#endif return c; } else diff --git a/nuttx/lib/stdio/lib_fputs.c b/nuttx/lib/stdio/lib_fputs.c index 54801ca50..a956c83b2 100644 --- a/nuttx/lib/stdio/lib_fputs.c +++ b/nuttx/lib/stdio/lib_fputs.c @@ -108,19 +108,20 @@ int fputs(FAR const char *s, FAR FILE *stream) ntowrite = strlen(s); if (ntowrite == 0) - { - nput = 0; - } + { + nput = 0; + } else - { - /* Write the string */ - - nwritten = lib_fwrite(s, ntowrite, stream); - if (nwritten > 0) - { - nput = nwritten; - } - } + { + /* Write the string */ + + nwritten = lib_fwrite(s, ntowrite, stream); + if (nwritten > 0) + { + nput = nwritten; + } + } } + return nput; } diff --git a/nuttx/lib/stdio/lib_libvsprintf.c b/nuttx/lib/stdio/lib_libvsprintf.c index 8e0709ec9..34a27ea4e 100644 --- a/nuttx/lib/stdio/lib_libvsprintf.c +++ b/nuttx/lib/stdio/lib_libvsprintf.c @@ -1136,6 +1136,8 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, const char *src, va_list ap) #ifdef CONFIG_STDIO_LINEBUFFER if (*src == '\n') { + /* Should return an error on a failure to flush */ + (void)obj->flush(obj); } #endif diff --git a/nuttx/lib/stdio/lib_puts.c b/nuttx/lib/stdio/lib_puts.c index 5e651c4de..e8cb186f6 100644 --- a/nuttx/lib/stdio/lib_puts.c +++ b/nuttx/lib/stdio/lib_puts.c @@ -84,31 +84,46 @@ * Name: puts * * Description: - * puts() writes the string s and a trailing newline to - * stdout. + * puts() writes the string s and a trailing newline to stdout. + * ****************************************************************************/ int puts(FAR const char *s) { + FILE *stream = stdout; int nwritten; int nput = EOF; /* Write the string (the next two steps must be atomic) */ - lib_take_semaphore(stdout); + lib_take_semaphore(stream); /* Write the string without its trailing '\0' */ - nwritten = fputs(s, stdout); + nwritten = fputs(s, stream); if (nwritten > 0) { /* Followed by a newline */ + char newline = '\n'; - if (lib_fwrite(&newline, 1, stdout) > 0) + if (lib_fwrite(&newline, 1, stream) > 0) { nput = nwritten + 1; + + /* Flush the buffer after the newline is output */ + +#ifdef CONFIG_STDIO_LINEBUFFER + { + int ret = lib_fflush(stream, true); + if (ret < 0) + { + nput = EOF; + } + } +#endif } } + lib_give_semaphore(stdout); return nput; } |