You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/10/30 08:42:03 UTC

[incubator-nuttx] 01/02: libc: Skip close stdin/stdout/stderr in fclose

This is an automated email from the ASF dual-hosted git repository.

aguettouche pushed a commit to branch releases/10.0
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 44e44953eb2b80a25832a9ecec96e2868889fd7e
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Thu Oct 15 21:08:01 2020 +0800

    libc: Skip close stdin/stdout/stderr in fclose
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 libs/libc/stdio/lib_fclose.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/libs/libc/stdio/lib_fclose.c b/libs/libc/stdio/lib_fclose.c
index 1842d5f..a2302a2 100644
--- a/libs/libc/stdio/lib_fclose.c
+++ b/libs/libc/stdio/lib_fclose.c
@@ -81,6 +81,23 @@ int fclose(FAR FILE *stream)
 
   if (stream)
     {
+      ret = OK;
+
+      /* If the stream was opened for writing, then flush the stream */
+
+      if ((stream->fs_oflags & O_WROK) != 0)
+        {
+          ret = lib_fflush(stream, true);
+          errcode = get_errno();
+        }
+
+      /* Skip close the builtin streams(stdin, stdout and stderr) */
+
+      if (stream == stdin || stream == stdout || stream == stderr)
+        {
+          goto done;
+        }
+
       /* Remove FILE structure from the stream list */
 
       slist = nxsched_get_streams();
@@ -114,17 +131,8 @@ int fclose(FAR FILE *stream)
        * file.
        */
 
-      ret = OK;
       if (stream->fs_fd >= 0)
         {
-          /* If the stream was opened for writing, then flush the stream */
-
-          if ((stream->fs_oflags & O_WROK) != 0)
-            {
-              ret = lib_fflush(stream, true);
-              errcode = get_errno();
-            }
-
           /* Close the file descriptor and save the return status */
 
           status = close(stream->fs_fd);
@@ -157,6 +165,7 @@ int fclose(FAR FILE *stream)
       lib_free(stream);
     }
 
+done:
   /* On an error, reset the errno to the first error encountered and return
    * EOF.
    */