diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-05-14 18:14:51 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-05-14 18:14:51 +0000 |
commit | 27b2633b7c50bb76bfe1333b628e1548ac6ca1d8 (patch) | |
tree | 6991dc5532357efe302086cabf47e65c8ca5a18f /nuttx/lib/stdio | |
parent | 18615da8957c23faa4924b2d1b3d222a28db9db8 (diff) | |
download | px4-nuttx-27b2633b7c50bb76bfe1333b628e1548ac6ca1d8.tar.gz px4-nuttx-27b2633b7c50bb76bfe1333b628e1548ac6ca1d8.tar.bz2 px4-nuttx-27b2633b7c50bb76bfe1333b628e1548ac6ca1d8.zip |
Extend line buffering logic to puts, fputs, putc, fputc, and putchar()
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3608 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib/stdio')
-rw-r--r-- | nuttx/lib/stdio/lib_fputc.c | 7 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_fputs.c | 53 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_puts.c | 16 |
3 files changed, 53 insertions, 23 deletions
diff --git a/nuttx/lib/stdio/lib_fputc.c b/nuttx/lib/stdio/lib_fputc.c index 34ef7aa25..917fcc10a 100644 --- a/nuttx/lib/stdio/lib_fputc.c +++ b/nuttx/lib/stdio/lib_fputc.c @@ -87,14 +87,17 @@ int fputc(int c, FAR FILE *stream) { unsigned char buf = (unsigned char)c; - if (lib_fwrite(&buf, 1, stream) > 0) + int ret; + + ret = lib_fwrite(&buf, 1, stream); + if (ret > 0) { /* Flush the buffer if a newline is output */ #ifdef CONFIG_STDIO_LINEBUFFER if (c == '\n') { - int ret = lib_fflush(stream, true); + ret = lib_fflush(stream, true); if (ret < 0) { return EOF; diff --git a/nuttx/lib/stdio/lib_fputs.c b/nuttx/lib/stdio/lib_fputs.c index a956c83b2..ddb548925 100644 --- a/nuttx/lib/stdio/lib_fputs.c +++ b/nuttx/lib/stdio/lib_fputs.c @@ -93,35 +93,62 @@ int fputs(FAR const char *s, FAR FILE *stream) { int ntowrite; - int nwritten; - int nput = EOF; + int nput; + int ret; /* Make sure that a string was provided. */ +#ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */ if (!s) { set_errno(EINVAL); + return EOF; } - else +#endif + + /* Get the length of the string. */ + + ntowrite = strlen(s); + if (ntowrite == 0) { - /* Get the length of the string. */ + return 0; + } + + /* Write the string */ - ntowrite = strlen(s); - if (ntowrite == 0) +#ifdef CONFIG_STDIO_LINEBUFFER + nput = ntowrite; + while (ntowrite-- > 0) + { + ret = lib_fwrite(s, 1, stream); + if (ret <= 0) { - nput = 0; + return EOF; } - else - { - /* Write the string */ - nwritten = lib_fwrite(s, ntowrite, stream); - if (nwritten > 0) + /* Flush the buffer if a newline was put to the buffer */ + + if (*s == '\n') + { + ret = lib_fflush(stream, true); + if (ret < 0) { - nput = nwritten; + return EOF; } } + + /* Set up for the next lib_fwrite() */ + + s++; } return nput; +#else + nput = lib_fwrite(s, ntowrite, stream); + if (nput < 0) + { + return EOF + } + return nput; +#endif } diff --git a/nuttx/lib/stdio/lib_puts.c b/nuttx/lib/stdio/lib_puts.c index e8cb186f6..088d0f043 100644 --- a/nuttx/lib/stdio/lib_puts.c +++ b/nuttx/lib/stdio/lib_puts.c @@ -93,6 +93,7 @@ int puts(FAR const char *s) FILE *stream = stdout; int nwritten; int nput = EOF; + int ret; /* Write the string (the next two steps must be atomic) */ @@ -106,20 +107,19 @@ int puts(FAR const char *s) /* Followed by a newline */ char newline = '\n'; - if (lib_fwrite(&newline, 1, stream) > 0) + ret = lib_fwrite(&newline, 1, stream); + if (ret > 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; - } - } + ret = lib_fflush(stream, true); + if (ret < 0) + { + nput = EOF; + } #endif } } |