libc: update stream getoffset to handle write case

Change-Id: I80b01b446446d5a631d40822f220a0177a95e7e6
Signed-off-by: liuhaitao <liuhaitao@xiaomi.com>
This commit is contained in:
liuhaitao 2021-03-19 12:12:32 +08:00 committed by Xiang Xiao
parent dc14f89909
commit bcaf5d75e5

View File

@ -37,7 +37,7 @@
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: lib_getrdoffset * Name: lib_getoffset
* *
* Description: * Description:
* It is insufficient to simply use the file offset; we must also account * It is insufficient to simply use the file offset; we must also account
@ -50,9 +50,9 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_STDIO_DISABLE_BUFFERING #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); lib_take_semaphore(stream);
if (stream->fs_bufstart != if (stream->fs_bufstart !=
@ -60,18 +60,22 @@ static off_t lib_getrdoffset(FAR FILE *stream)
stream->fs_bufstart) stream->fs_bufstart)
{ {
#if CONFIG_NUNGET_CHARS > 0 #if CONFIG_NUNGET_CHARS > 0
rdoffset = stream->fs_bufread - stream->fs_bufpos + offset = stream->fs_bufread - stream->fs_bufpos +
stream->fs_nungotten; stream->fs_nungotten;
#else #else
rdoffset = stream->fs_bufread - stream->fs_bufpos; offset = stream->fs_bufread - stream->fs_bufpos;
#endif #endif
} }
else
{
offset = -(stream->fs_bufpos - stream->fs_bufstart);
}
lib_give_semaphore(stream); lib_give_semaphore(stream);
return rdoffset; return offset;
} }
#else #else
# define lib_getrdoffset(stream) (0) # define lib_getoffset(stream) (0)
#endif #endif
/**************************************************************************** /****************************************************************************
@ -109,7 +113,7 @@ long ftell(FAR FILE *stream)
position = lseek(stream->fs_fd, 0, SEEK_CUR); position = lseek(stream->fs_fd, 0, SEEK_CUR);
if (position != (off_t)-1) if (position != (off_t)-1)
{ {
return (long)(position - lib_getrdoffset(stream)); return (long)(position - lib_getoffset(stream));
} }
else else
{ {