summaryrefslogtreecommitdiff
path: root/nuttx/lib/stdio
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-10-10 19:40:56 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-10-10 19:40:56 +0000
commit95f5523197bf51b232a38993f3489346500819e7 (patch)
tree34680d933b0f7aa2fe3f63525a6bce83cb6a99c4 /nuttx/lib/stdio
parent825cd6c478c17dc00135182595c796fcd023854e (diff)
downloadpx4-nuttx-95f5523197bf51b232a38993f3489346500819e7.tar.gz
px4-nuttx-95f5523197bf51b232a38993f3489346500819e7.tar.bz2
px4-nuttx-95f5523197bf51b232a38993f3489346500819e7.zip
Fix fclose() return value when closing read-only file
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4036 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib/stdio')
-rw-r--r--nuttx/lib/stdio/lib_fclose.c34
-rw-r--r--nuttx/lib/stdio/lib_libfflush.c2
2 files changed, 22 insertions, 14 deletions
diff --git a/nuttx/lib/stdio/lib_fclose.c b/nuttx/lib/stdio/lib_fclose.c
index 06f970e11..8cecb8af3 100644
--- a/nuttx/lib/stdio/lib_fclose.c
+++ b/nuttx/lib/stdio/lib_fclose.c
@@ -2,7 +2,7 @@
* lib/stdio/lib_fclose.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ * Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -41,6 +41,7 @@
#include <unistd.h>
#include <stdlib.h>
+#include <fcntl.h>
#include <string.h>
#include <errno.h>
@@ -70,29 +71,36 @@ int fclose(FAR FILE *stream)
{
int err = EINVAL;
int ret = ERROR;
+ int status;
/* Verify that a stream was provided. */
if (stream)
{
- /* Flush the stream */
-
- ret = lib_fflush(stream, true);
- err = errno;
+ /* Check that the underlying file descriptor corresponds to an an open
+ * file.
+ */
+
+ ret = OK;
+ if (stream->fs_filedes >= 0)
+ {
+ /* If the stream was opened for writing, then flush the stream */
- /* Close the underlying file descriptor */
+ if ((stream->fs_oflags & O_WROK) != 0)
+ {
+ ret = lib_fflush(stream, true);
+ err = errno;
+ }
- if (stream->fs_filedes > 0)
- {
- /* Close the file and save the return status */
+ /* Close the underlying file descriptor and save the return status */
- int status = close(stream->fs_filedes);
+ status = close(stream->fs_filedes);
- /* If close() returns an error but flush() did not then make
- * sure that we return the close() error condition.
+ /* If close() returns an error but flush() did not then make sure
+ * that we return the close() error condition.
*/
- if (ret == 0)
+ if (ret == OK)
{
ret = status;
err = errno;
diff --git a/nuttx/lib/stdio/lib_libfflush.c b/nuttx/lib/stdio/lib_libfflush.c
index 3e9de538a..ddfc6c8ff 100644
--- a/nuttx/lib/stdio/lib_libfflush.c
+++ b/nuttx/lib/stdio/lib_libfflush.c
@@ -2,7 +2,7 @@
* lib/stdio/lib_libfflush.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ * Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions