mirror of
https://github.com/apache/nuttx.git
synced 2026-06-07 01:05:54 +08:00
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
This commit is contained in:
@@ -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
|
||||
|
||||
+12
-11
@@ -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 */
|
||||
{
|
||||
/* Write the string */
|
||||
|
||||
nwritten = lib_fwrite(s, ntowrite, stream);
|
||||
if (nwritten > 0)
|
||||
{
|
||||
nput = nwritten;
|
||||
}
|
||||
}
|
||||
nwritten = lib_fwrite(s, ntowrite, stream);
|
||||
if (nwritten > 0)
|
||||
{
|
||||
nput = nwritten;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nput;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+20
-5
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user