You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ha...@apache.org on 2021/11/18 15:06:34 UTC

[incubator-nuttx] branch master updated: lib/stdio: Handle 64bits off_t correctly

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

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


The following commit(s) were added to refs/heads/master by this push:
     new fe94670  lib/stdio: Handle 64bits off_t correctly
fe94670 is described below

commit fe94670ca99191acf6f89df38fef3d543cb0504e
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Nov 15 03:46:06 2021 +0800

    lib/stdio: Handle 64bits off_t correctly
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 libs/libc/stdio/lib_fgetpos.c |  8 ++--
 libs/libc/stdio/lib_fseek.c   | 37 +-----------------
 libs/libc/stdio/lib_fseeko.c  | 55 +++++++++++++++++++++++++-
 libs/libc/stdio/lib_fsetpos.c |  2 +-
 libs/libc/stdio/lib_ftell.c   | 78 +------------------------------------
 libs/libc/stdio/lib_ftello.c  | 90 ++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 150 insertions(+), 120 deletions(-)

diff --git a/libs/libc/stdio/lib_fgetpos.c b/libs/libc/stdio/lib_fgetpos.c
index d5a7671..6a77e77 100644
--- a/libs/libc/stdio/lib_fgetpos.c
+++ b/libs/libc/stdio/lib_fgetpos.c
@@ -51,7 +51,7 @@
 
 int fgetpos(FAR FILE *stream, FAR fpos_t *pos)
 {
-  long position;
+  off_t position;
 
 #ifdef CONFIG_DEBUG_FEATURES
   if (!stream || !pos)
@@ -61,12 +61,12 @@ int fgetpos(FAR FILE *stream, FAR fpos_t *pos)
     }
 #endif
 
-  position = ftell(stream);
-  if (position == -1)
+  position = ftello(stream);
+  if (position == (off_t)-1)
     {
       return ERROR;
     }
 
-  *pos = (fpos_t)position;
+  *pos = position;
   return OK;
 }
diff --git a/libs/libc/stdio/lib_fseek.c b/libs/libc/stdio/lib_fseek.c
index b10ca41..9dd5dcf 100644
--- a/libs/libc/stdio/lib_fseek.c
+++ b/libs/libc/stdio/lib_fseek.c
@@ -22,15 +22,7 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#include <sys/types.h>
 #include <stdio.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include "libc.h"
 
 /****************************************************************************
  * Public Functions
@@ -56,32 +48,5 @@
 
 int fseek(FAR FILE *stream, long int offset, int whence)
 {
-#ifdef CONFIG_DEBUG_FEATURES
-  /* Verify that we were provided with a stream */
-
-  if (!stream)
-    {
-      set_errno(EBADF);
-      return ERROR;
-    }
-#endif
-
-#ifndef CONFIG_STDIO_DISABLE_BUFFERING
-  /* Flush any valid read/write data in the buffer (also verifies stream) */
-
-  if (lib_rdflush(stream) < 0 || lib_wrflush(stream) < 0)
-    {
-      return ERROR;
-    }
-#endif
-
-  /* On success or failure, discard any characters saved by ungetc() */
-
-#if CONFIG_NUNGET_CHARS > 0
-  stream->fs_nungotten = 0;
-#endif
-
-  /* Perform the fseek on the underlying file descriptor */
-
-  return lseek(stream->fs_fd, offset, whence) == (off_t)-1 ? ERROR : OK;
+  return fseeko(stream, offset, whence);
 }
diff --git a/libs/libc/stdio/lib_fseeko.c b/libs/libc/stdio/lib_fseeko.c
index 50ac370..69dc01e 100644
--- a/libs/libc/stdio/lib_fseeko.c
+++ b/libs/libc/stdio/lib_fseeko.c
@@ -22,13 +22,66 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
+#include <sys/types.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "libc.h"
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: fseeko
+ *
+ * Description:
+ *   The fseeko() function sets the file position indicator for the stream
+ *   pointed to by stream. The new position, measured in bytes, is obtained
+ *   by adding offset bytes to the position specified by whence. If whence is
+ *   set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the
+ *   start of the file, the current position indicator, or end-of-file,
+ *   respectively. A successful call to the fseeko() function clears the
+ *   end-of-file indicator for the stream and undoes any effects of the
+ *   ungetc(3) function on the same stream.
+ *
+ * Returned Value:
+ *   Zero on success; -1 on failure with errno set appropriately.
+ *
+ ****************************************************************************/
+
 int fseeko(FAR FILE *stream, off_t offset, int whence)
 {
-  return fseek(stream, offset, whence);
+#ifdef CONFIG_DEBUG_FEATURES
+  /* Verify that we were provided with a stream */
+
+  if (!stream)
+    {
+      set_errno(EBADF);
+      return ERROR;
+    }
+#endif
+
+#ifndef CONFIG_STDIO_DISABLE_BUFFERING
+  /* Flush any valid read/write data in the buffer (also verifies stream) */
+
+  if (lib_rdflush(stream) < 0 || lib_wrflush(stream) < 0)
+    {
+      return ERROR;
+    }
+#endif
+
+  /* On success or failure, discard any characters saved by ungetc() */
+
+#if CONFIG_NUNGET_CHARS > 0
+  stream->fs_nungotten = 0;
+#endif
+
+  /* Perform the fseeko on the underlying file descriptor */
+
+  return lseek(stream->fs_fd, offset, whence) == (off_t)-1 ? ERROR : OK;
 }
diff --git a/libs/libc/stdio/lib_fsetpos.c b/libs/libc/stdio/lib_fsetpos.c
index 5c8111b..6e6a063 100644
--- a/libs/libc/stdio/lib_fsetpos.c
+++ b/libs/libc/stdio/lib_fsetpos.c
@@ -61,5 +61,5 @@ int fsetpos(FAR FILE *stream, FAR fpos_t *pos)
     }
 #endif
 
-  return fseek(stream, (FAR off_t)*pos, SEEK_SET);
+  return fseeko(stream, *pos, SEEK_SET);
 }
diff --git a/libs/libc/stdio/lib_ftell.c b/libs/libc/stdio/lib_ftell.c
index 2df6d78..dc40fb5 100644
--- a/libs/libc/stdio/lib_ftell.c
+++ b/libs/libc/stdio/lib_ftell.c
@@ -22,61 +22,7 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#include <sys/types.h>
 #include <stdio.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include "libc.h"
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: lib_getoffset
- *
- * Description:
- *   It is insufficient to simply use the file offset; we must also account
- *   for the data offset in the any buffered data.  This function calculates
- *   that offset.
- *
- * Returned Value:
- *   The file position offset due to buffered data.
- *
- ****************************************************************************/
-
-#ifndef CONFIG_STDIO_DISABLE_BUFFERING
-static off_t lib_getoffset(FAR FILE *stream)
-{
-  off_t offset = 0;
-  lib_take_semaphore(stream);
-
-  if (stream->fs_bufstart !=
-      NULL && stream->fs_bufread !=
-      stream->fs_bufstart)
-    {
-#if CONFIG_NUNGET_CHARS > 0
-      offset = stream->fs_bufread - stream->fs_bufpos +
-                 stream->fs_nungotten;
-#else
-      offset = stream->fs_bufread - stream->fs_bufpos;
-#endif
-    }
-  else
-    {
-      offset = -(stream->fs_bufpos - stream->fs_bufstart);
-    }
-
-  lib_give_semaphore(stream);
-  return offset;
-}
-#else
-#  define lib_getoffset(stream) (0)
-#endif
 
 /****************************************************************************
  * Public Functions
@@ -96,27 +42,5 @@ static off_t lib_getoffset(FAR FILE *stream)
 
 long ftell(FAR FILE *stream)
 {
-  off_t position;
-
-  /* Verify that we were provided with a stream */
-
-  if (!stream)
-    {
-      set_errno(EBADF);
-      return ERROR;
-    }
-
-  /* Perform the lseek to the current position.  This will not move the
-   * file pointer, but will return its current setting
-   */
-
-  position = lseek(stream->fs_fd, 0, SEEK_CUR);
-  if (position != (off_t)-1)
-    {
-      return (long)(position - lib_getoffset(stream));
-    }
-  else
-    {
-      return ERROR;
-    }
+  return ftello(stream);
 }
diff --git a/libs/libc/stdio/lib_ftello.c b/libs/libc/stdio/lib_ftello.c
index a2876fa..d9651f9 100644
--- a/libs/libc/stdio/lib_ftello.c
+++ b/libs/libc/stdio/lib_ftello.c
@@ -22,13 +22,101 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
+#include <sys/types.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "libc.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: lib_getoffset
+ *
+ * Description:
+ *   It is insufficient to simply use the file offset; we must also account
+ *   for the data offset in the any buffered data.  This function calculates
+ *   that offset.
+ *
+ * Returned Value:
+ *   The file position offset due to buffered data.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_STDIO_DISABLE_BUFFERING
+static off_t lib_getoffset(FAR FILE *stream)
+{
+  off_t offset = 0;
+  lib_take_semaphore(stream);
+
+  if (stream->fs_bufstart !=
+      NULL && stream->fs_bufread !=
+      stream->fs_bufstart)
+    {
+#if CONFIG_NUNGET_CHARS > 0
+      offset = stream->fs_bufread - stream->fs_bufpos +
+                 stream->fs_nungotten;
+#else
+      offset = stream->fs_bufread - stream->fs_bufpos;
+#endif
+    }
+  else
+    {
+      offset = -(stream->fs_bufpos - stream->fs_bufstart);
+    }
+
+  lib_give_semaphore(stream);
+  return offset;
+}
+#else
+#  define lib_getoffset(stream) (0)
+#endif
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: ftello
+ *
+ * Description:
+ *   ftello() returns the current value of the file position indicator for
+ *   the stream pointed to by stream.
+ *
+ * Returned Value:
+ *   Zero on success; -1 on failure with errno set appropriately.
+ *
+ ****************************************************************************/
+
 off_t ftello(FAR FILE *stream)
 {
-  return ftell(stream);
+  off_t position;
+
+  /* Verify that we were provided with a stream */
+
+  if (!stream)
+    {
+      set_errno(EBADF);
+      return ERROR;
+    }
+
+  /* Perform the lseek to the current position.  This will not move the
+   * file pointer, but will return its current setting
+   */
+
+  position = lseek(stream->fs_fd, 0, SEEK_CUR);
+  if (position != (off_t)-1)
+    {
+      return position - lib_getoffset(stream);
+    }
+  else
+    {
+      return ERROR;
+    }
 }