diff --git a/libs/libc/stdio/lib_fgetpos.c b/libs/libc/stdio/lib_fgetpos.c index d5a76711b9..6a77e77d8e 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 b10ca41c21..9dd5dcfe4b 100644 --- a/libs/libc/stdio/lib_fseek.c +++ b/libs/libc/stdio/lib_fseek.c @@ -22,15 +22,7 @@ * Included Files ****************************************************************************/ -#include - -#include #include -#include -#include -#include - -#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 50ac3702a7..69dc01e295 100644 --- a/libs/libc/stdio/lib_fseeko.c +++ b/libs/libc/stdio/lib_fseeko.c @@ -22,13 +22,66 @@ * Included Files ****************************************************************************/ +#include + +#include #include +#include +#include +#include + +#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 5c8111b7b7..6e6a063e5f 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 2df6d78b85..dc40fb578a 100644 --- a/libs/libc/stdio/lib_ftell.c +++ b/libs/libc/stdio/lib_ftell.c @@ -22,61 +22,7 @@ * Included Files ****************************************************************************/ -#include - -#include #include -#include -#include -#include - -#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 a2876fa033..d9651f91ab 100644 --- a/libs/libc/stdio/lib_ftello.c +++ b/libs/libc/stdio/lib_ftello.c @@ -22,13 +22,101 @@ * Included Files ****************************************************************************/ +#include + +#include #include +#include +#include +#include + +#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; + } }