2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2023-08-24 05:43:57 +02:00
|
|
|
* libs/libc/stdio/lib_rdflush_unlocked.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 <errno.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-08-24 05:43:57 +02:00
|
|
|
* Name: lib_rdflush_unlocked
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Flush read data from the I/O buffer and adjust the file pointer to
|
|
|
|
* account for the unread data
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-24 05:43:57 +02:00
|
|
|
int lib_rdflush_unlocked(FAR FILE *stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-08-27 21:33:44 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Sanity checking */
|
|
|
|
|
2017-02-09 16:53:50 +01:00
|
|
|
if (stream == NULL)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
set_errno(EBADF);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Do nothing if there is no I/O buffer */
|
|
|
|
|
|
|
|
if (stream->fs_bufstart == NULL)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* If the buffer is currently being used for read access, then discard all
|
2021-03-02 14:33:27 +01:00
|
|
|
* of the read-ahead data. We do not support concurrent buffered read/write
|
2012-11-10 17:06:01 +01:00
|
|
|
* access.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (stream->fs_bufread != stream->fs_bufstart)
|
|
|
|
{
|
2021-03-02 14:33:27 +01:00
|
|
|
/* Now adjust the stream pointer to account for the read-ahead data
|
|
|
|
* that was not actually read by the user.
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NUNGET_CHARS > 0
|
2021-03-02 14:33:27 +01:00
|
|
|
off_t rdoffset = stream->fs_bufread - stream->fs_bufpos +
|
|
|
|
stream->fs_nungotten;
|
2012-11-10 17:06:01 +01:00
|
|
|
#else
|
|
|
|
off_t rdoffset = stream->fs_bufread - stream->fs_bufpos;
|
|
|
|
#endif
|
2021-03-02 14:33:27 +01:00
|
|
|
/* Mark the buffer as empty (do this before calling fseek() because
|
|
|
|
* fseek() also calls this function).
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
stream->fs_bufpos = stream->fs_bufread = stream->fs_bufstart;
|
|
|
|
#if CONFIG_NUNGET_CHARS > 0
|
|
|
|
stream->fs_nungotten = 0;
|
|
|
|
#endif
|
2021-03-02 14:33:27 +01:00
|
|
|
/* Then seek to the position corresponding to the last data read by the
|
|
|
|
* user
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-08-27 21:33:44 +02:00
|
|
|
rdoffset = -rdoffset;
|
|
|
|
if (stream->fs_iofunc.seek != NULL)
|
|
|
|
{
|
|
|
|
ret = stream->fs_iofunc.seek(stream->fs_cookie, &rdoffset,
|
|
|
|
SEEK_CUR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = lseek((int)(intptr_t)stream->fs_cookie, rdoffset, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#endif /* CONFIG_STDIO_DISABLE_BUFFERING */
|