lib/stdio: Handle 64bits off_t correctly

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2021-11-15 03:46:06 +08:00 committed by hartmannathan
parent bd2327cc2d
commit fe94670ca9
6 changed files with 150 additions and 120 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}
}