From bcaf5d75e5e4974dcc4b16e5e9966e2230cb8fd1 Mon Sep 17 00:00:00 2001 From: liuhaitao Date: Fri, 19 Mar 2021 12:12:32 +0800 Subject: [PATCH] libc: update stream getoffset to handle write case Change-Id: I80b01b446446d5a631d40822f220a0177a95e7e6 Signed-off-by: liuhaitao --- libs/libc/stdio/lib_ftell.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libs/libc/stdio/lib_ftell.c b/libs/libc/stdio/lib_ftell.c index ff93622fb4..2df6d78b85 100644 --- a/libs/libc/stdio/lib_ftell.c +++ b/libs/libc/stdio/lib_ftell.c @@ -37,7 +37,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: lib_getrdoffset + * Name: lib_getoffset * * Description: * It is insufficient to simply use the file offset; we must also account @@ -50,9 +50,9 @@ ****************************************************************************/ #ifndef CONFIG_STDIO_DISABLE_BUFFERING -static off_t lib_getrdoffset(FAR FILE *stream) +static off_t lib_getoffset(FAR FILE *stream) { - off_t rdoffset = 0; + off_t offset = 0; lib_take_semaphore(stream); if (stream->fs_bufstart != @@ -60,18 +60,22 @@ static off_t lib_getrdoffset(FAR FILE *stream) stream->fs_bufstart) { #if CONFIG_NUNGET_CHARS > 0 - rdoffset = stream->fs_bufread - stream->fs_bufpos + + offset = stream->fs_bufread - stream->fs_bufpos + stream->fs_nungotten; #else - rdoffset = stream->fs_bufread - stream->fs_bufpos; + offset = stream->fs_bufread - stream->fs_bufpos; #endif } + else + { + offset = -(stream->fs_bufpos - stream->fs_bufstart); + } lib_give_semaphore(stream); - return rdoffset; + return offset; } #else -# define lib_getrdoffset(stream) (0) +# define lib_getoffset(stream) (0) #endif /**************************************************************************** @@ -109,7 +113,7 @@ long ftell(FAR FILE *stream) position = lseek(stream->fs_fd, 0, SEEK_CUR); if (position != (off_t)-1) { - return (long)(position - lib_getrdoffset(stream)); + return (long)(position - lib_getoffset(stream)); } else {