This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new fd660a0a379 libc/stdio: Set the file offset on fflush() of a seekable
read stream.
fd660a0a379 is described below
commit fd660a0a3796ec2012e2d9ed4de799084128a59a
Author: ganjing <[email protected]>
AuthorDate: Tue Nov 24 12:10:00 2026 +0800
libc/stdio: Set the file offset on fflush() of a seekable read stream.
fflush() of a stream that was only opened for reading returned EBADF
and left the file offset where the read-ahead had put it, so after
fread(buf, 1, 3, f);
fflush(f);
lseek(fileno(f), 0, SEEK_CUR) returned the size of the buffered block
instead of 3. An update stream holding read data was not changed
either.
POSIX says that fflush() of a seekable input stream sets the file offset
to the stream position and discards the characters pushed back with
ungetc(). Do that in fflush() and fflush_unlocked() only, and only when
the file can seek. The read data of a pipe or a terminal is kept,
because it could not be read again, and lib_fflush_unlocked(), which the
read path calls before every read, is not changed, so reading an "r+"
stream does not discard its buffer. fflush() of a read stream now
returns 0 instead of EBADF.
Signed-off-by: ganjing <[email protected]>
---
libs/libc/stdio/lib_fflush.c | 90 ++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 87 insertions(+), 3 deletions(-)
diff --git a/libs/libc/stdio/lib_fflush.c b/libs/libc/stdio/lib_fflush.c
index 2a7358c6d20..7dbcc51dfb7 100644
--- a/libs/libc/stdio/lib_fflush.c
+++ b/libs/libc/stdio/lib_fflush.c
@@ -30,11 +30,91 @@
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
+#include <unistd.h>
#include <nuttx/fs/fs.h>
#include "libc.h"
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifndef CONFIG_STDIO_DISABLE_BUFFERING
+/****************************************************************************
+ * Name: fflush_seekable
+ *
+ * Description:
+ * Return true if the file of the stream can seek. errno is preserved.
+ *
+ ****************************************************************************/
+
+static bool fflush_seekable(FAR FILE *stream)
+{
+ int errcode = get_errno();
+ off_t offset = 0;
+ bool seekable;
+
+ if (stream->fs_iofunc.seek != NULL)
+ {
+ seekable = stream->fs_iofunc.seek(stream->fs_cookie, &offset,
+ SEEK_CUR) >= 0;
+ }
+ else
+ {
+ seekable = lseek((int)(intptr_t)stream->fs_cookie, 0, SEEK_CUR) >= 0;
+ }
+
+ set_errno(errcode);
+ return seekable;
+}
+#endif
+
+/****************************************************************************
+ * Name: fflush_stream
+ *
+ * Description:
+ * Flush one stream. Buffered write data is written out. If the stream
+ * holds read-ahead data or pushed back characters and its file can seek,
+ * they are discarded and the file offset is set to the stream position,
+ * as POSIX requires. The read data of a stream that cannot seek (a pipe
+ * or a terminal) is kept, because it could not be read again.
+ *
+ ****************************************************************************/
+
+static ssize_t fflush_stream(FAR FILE *stream)
+{
+#ifndef CONFIG_STDIO_DISABLE_BUFFERING
+ bool rddata = stream->fs_bufstart != NULL &&
+ stream->fs_bufread != stream->fs_bufstart;
+
+# if CONFIG_NUNGET_CHARS > 0
+ rddata = rddata || stream->fs_nungotten > 0;
+# endif
+
+ if (rddata && fflush_seekable(stream))
+ {
+ if (lib_rdflush_unlocked(stream) < 0)
+ {
+ return -get_errno();
+ }
+
+# if CONFIG_NUNGET_CHARS > 0
+ stream->fs_nungotten = 0;
+# endif
+ }
+#endif
+
+ /* A stream that is not open for writing has no write data */
+
+ if ((stream->fs_oflags & O_ACCMODE) == O_RDONLY)
+ {
+ return 0;
+ }
+
+ return lib_fflush_unlocked(stream);
+}
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -45,7 +125,9 @@
* Description:
* The function fflush() forces a write of all user-space buffered data for
* the given output or update stream via the stream's underlying write
- * function. The open status of the stream is unaffected.
+ * function. The open status of the stream is unaffected. For a stream
+ * whose file can seek, read-ahead data and pushed back characters are
+ * discarded and the file offset is set to the stream position.
*
* If the stream argument is NULL, fflush() flushes all open output streams.
*
@@ -68,7 +150,7 @@ int fflush_unlocked(FAR FILE *stream)
}
else
{
- ret = lib_fflush_unlocked(stream);
+ ret = fflush_stream(stream);
}
/* Check the return value */
@@ -103,7 +185,9 @@ int fflush(FAR FILE *stream)
}
else
{
- ret = lib_fflush(stream);
+ flockfile(stream);
+ ret = fflush_stream(stream);
+ funlockfile(stream);
}
/* Check the return value */