2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_ftell.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-31 17:47:28 +02:00
|
|
|
/****************************************************************************
|
2021-03-19 05:12:32 +01:00
|
|
|
* Name: lib_getoffset
|
2014-03-31 17:47:28 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2021-03-19 05:12:32 +01:00
|
|
|
static off_t lib_getoffset(FAR FILE *stream)
|
2014-03-31 17:47:28 +02:00
|
|
|
{
|
2021-03-19 05:12:32 +01:00
|
|
|
off_t offset = 0;
|
2014-03-31 17:47:28 +02:00
|
|
|
lib_take_semaphore(stream);
|
|
|
|
|
2021-03-02 14:33:27 +01:00
|
|
|
if (stream->fs_bufstart !=
|
|
|
|
NULL && stream->fs_bufread !=
|
|
|
|
stream->fs_bufstart)
|
2014-03-31 17:47:28 +02:00
|
|
|
{
|
|
|
|
#if CONFIG_NUNGET_CHARS > 0
|
2021-03-19 05:12:32 +01:00
|
|
|
offset = stream->fs_bufread - stream->fs_bufpos +
|
2021-03-02 14:33:27 +01:00
|
|
|
stream->fs_nungotten;
|
2014-03-31 17:47:28 +02:00
|
|
|
#else
|
2021-03-19 05:12:32 +01:00
|
|
|
offset = stream->fs_bufread - stream->fs_bufpos;
|
2014-03-31 17:47:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
2021-03-19 05:12:32 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = -(stream->fs_bufpos - stream->fs_bufstart);
|
|
|
|
}
|
2014-03-31 17:47:28 +02:00
|
|
|
|
|
|
|
lib_give_semaphore(stream);
|
2021-03-19 05:12:32 +01:00
|
|
|
return offset;
|
2014-03-31 17:47:28 +02:00
|
|
|
}
|
|
|
|
#else
|
2021-03-19 05:12:32 +01:00
|
|
|
# define lib_getoffset(stream) (0)
|
2014-03-31 17:47:28 +02:00
|
|
|
#endif
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftell
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* ftell() returns the current value of the file position indicator for the
|
|
|
|
* stream pointed to by stream.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2014-03-31 17:47:28 +02:00
|
|
|
* Zero on success; -1 on failure with errno set appropriately.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
position = lseek(stream->fs_fd, 0, SEEK_CUR);
|
2012-11-10 17:06:01 +01:00
|
|
|
if (position != (off_t)-1)
|
|
|
|
{
|
2021-03-19 05:12:32 +01:00
|
|
|
return (long)(position - lib_getoffset(stream));
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|