nuttx/libs/libc/stdio/lib_ftell.c
liuhaitao bcaf5d75e5 libc: update stream getoffset to handle write case
Change-Id: I80b01b446446d5a631d40822f220a0177a95e7e6
Signed-off-by: liuhaitao <liuhaitao@xiaomi.com>
2021-04-07 00:59:36 -05:00

123 lines
3.5 KiB
C

/****************************************************************************
* libs/libc/stdio/lib_ftell.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* 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: ftell
*
* Description:
* ftell() 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.
*
****************************************************************************/
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;
}
}