0a107ca6d9
This commit adds support for custom stream via fopencookie function. The function allows the programmer the create his own custom stream for IO operations and hook his custom functions to it. This is a non POSIX interface defined in Standard C library and implemented according to it. The only difference is in usage of off_t instead of off64_t. Programmer can use 64 bits offset if CONFIG_FS_LARGEFILE is enabled. In that case off_t is defined as int64_t (int32_t otherwise). Field fs_fd is removed from file_struct and fs_cookie is used instead as a shared variable for file descriptor or user defined cookie. The interface will be useful for future fmemopen implementation. Signed-off-by: Michal Lenc <michallenc@seznam.cz>
133 lines
3.7 KiB
C
133 lines
3.7 KiB
C
/****************************************************************************
|
|
* libs/libc/stdio/lib_ftello.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;
|
|
flockfile(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);
|
|
}
|
|
|
|
funlockfile(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)
|
|
{
|
|
off_t position;
|
|
off_t offset = 0;
|
|
|
|
/* 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
|
|
*/
|
|
|
|
if (stream->fs_iofunc.seek != NULL)
|
|
{
|
|
position = stream->fs_iofunc.seek(stream->fs_cookie, &offset,
|
|
SEEK_CUR);
|
|
}
|
|
else
|
|
{
|
|
position = lseek((int)(intptr_t)stream->fs_cookie, 0, SEEK_CUR);
|
|
}
|
|
|
|
if (position != (off_t)-1)
|
|
{
|
|
return position - lib_getoffset(stream);
|
|
}
|
|
else
|
|
{
|
|
return ERROR;
|
|
}
|
|
}
|