2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2023-08-30 09:40:39 +02:00
|
|
|
* libs/libc/stdio/lib_libfread_unlocked.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2020-07-30 09:56:29 +02: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
|
|
|
*
|
2020-07-30 09:56:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2020-07-30 09:56:29 +02: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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-31 17:39:10 +02:00
|
|
|
#include <nuttx/config.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-08-30 09:40:39 +02:00
|
|
|
* Name: lib_fread_unlocked
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-30 09:40:39 +02:00
|
|
|
ssize_t lib_fread_unlocked(FAR void *ptr, size_t count, FAR FILE *stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2020-07-30 09:55:28 +02:00
|
|
|
FAR unsigned char *dest = (FAR unsigned char *)ptr;
|
2015-10-12 15:45:02 +02:00
|
|
|
ssize_t bytes_read;
|
2018-12-07 19:18:40 +01:00
|
|
|
size_t remaining = count;
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2023-08-23 06:49:22 +02:00
|
|
|
size_t gulp_size;
|
2015-10-12 15:45:02 +02:00
|
|
|
int ret;
|
2013-01-24 19:39:53 +01:00
|
|
|
#endif
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Make sure that reading from this stream is allowed */
|
|
|
|
|
2022-10-18 10:39:52 +02:00
|
|
|
if (!stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2021-08-30 12:24:03 +02:00
|
|
|
_NX_SETERRNO(EBADF);
|
|
|
|
return ERROR;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2022-10-18 10:39:52 +02:00
|
|
|
else if ((stream->fs_oflags & O_RDOK) == 0)
|
|
|
|
{
|
|
|
|
stream->fs_flags |= __FS_FLAG_ERROR;
|
|
|
|
_NX_SETERRNO(EBADF);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#if CONFIG_NUNGET_CHARS > 0
|
|
|
|
/* First, re-read any previously ungotten characters */
|
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
while (stream->fs_nungotten > 0 && remaining > 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
/* Decrement the count of ungotten bytes to get an index */
|
|
|
|
|
|
|
|
stream->fs_nungotten--;
|
|
|
|
|
|
|
|
/* Return the last ungotten byte */
|
|
|
|
|
|
|
|
*dest++ = stream->fs_ungotten[stream->fs_nungotten];
|
|
|
|
|
|
|
|
/* That's one less byte that we have to read */
|
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
remaining--;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
|
|
|
/* Is there an I/O buffer? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:53:50 +01:00
|
|
|
if (stream->fs_bufstart != NULL)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
/* If the buffer is currently being used for write access, then
|
2020-07-30 09:55:28 +02:00
|
|
|
* flush all of the buffered write data. We do not support
|
|
|
|
* concurrent buffered read/write access.
|
2017-02-09 16:24:44 +01:00
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-08-30 09:40:39 +02:00
|
|
|
ret = lib_wrflush_unlocked(stream);
|
2017-02-09 16:24:44 +01:00
|
|
|
if (ret < 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2021-08-30 12:24:03 +02:00
|
|
|
if (count - remaining > 0)
|
|
|
|
{
|
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_NX_SETERRNO(ret);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Now get any other needed chars from the buffer or the file. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
while (remaining > 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Is there readable data in the buffer? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-08-23 06:49:22 +02:00
|
|
|
gulp_size = stream->fs_bufread - stream->fs_bufpos;
|
|
|
|
|
|
|
|
/* Avoid empty buffers or read requests greater than the size
|
|
|
|
* buffer remaining
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (gulp_size > 0)
|
2017-02-09 16:24:44 +01:00
|
|
|
{
|
2023-09-13 12:12:20 +02:00
|
|
|
if (gulp_size > remaining)
|
2023-08-23 06:49:22 +02:00
|
|
|
{
|
|
|
|
/* Clip the gulp size to the requested byte count */
|
|
|
|
|
2023-09-13 12:12:20 +02:00
|
|
|
gulp_size = remaining;
|
2023-08-23 06:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dest, stream->fs_bufpos, gulp_size);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-08-23 06:49:22 +02:00
|
|
|
remaining -= gulp_size;
|
|
|
|
stream->fs_bufpos += gulp_size;
|
2023-09-07 04:50:14 +02:00
|
|
|
dest += gulp_size;
|
2017-02-09 16:24:44 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-07-30 09:55:28 +02:00
|
|
|
/* The buffer is empty OR we have already supplied the number
|
|
|
|
* of bytes requested in the read. Check if we need to read
|
2017-02-09 16:24:44 +01:00
|
|
|
* more from the file.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
if (remaining > 0)
|
2017-02-09 16:24:44 +01:00
|
|
|
{
|
|
|
|
size_t buffer_available;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-07-30 09:55:28 +02:00
|
|
|
/* We need to read more data into the buffer from the
|
|
|
|
* file
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Mark the buffer empty */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-07-30 09:55:28 +02:00
|
|
|
stream->fs_bufpos = stream->fs_bufread =
|
|
|
|
stream->fs_bufstart;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* How much space is available in the buffer? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
buffer_available = stream->fs_bufend - stream->fs_bufread;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Will the number of bytes that we need to read fit into
|
|
|
|
* the buffer space that is available? If the read size is
|
|
|
|
* larger than the buffer, then read some of the data
|
|
|
|
* directly into the user's buffer.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
if (remaining > buffer_available)
|
2017-02-09 16:24:44 +01:00
|
|
|
{
|
2018-12-07 19:18:40 +01:00
|
|
|
bytes_read = _NX_READ(stream->fs_fd, dest, remaining);
|
2017-02-09 16:24:44 +01:00
|
|
|
if (bytes_read < 0)
|
|
|
|
{
|
2021-08-30 12:24:03 +02:00
|
|
|
if (count - remaining > 0)
|
|
|
|
{
|
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* An error occurred on the read. */
|
|
|
|
|
|
|
|
_NX_SETERRNO(bytes_read);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if (bytes_read == 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2020-07-30 09:55:28 +02:00
|
|
|
/* We are at the end of the file. But we may
|
|
|
|
* already have buffered data. In that case,
|
|
|
|
* we will report the EOF indication later.
|
2017-02-09 16:24:44 +01:00
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-30 09:55:28 +02:00
|
|
|
/* Some (perhaps all) bytes were read. Adjust the
|
|
|
|
* dest pointer and remaining bytes to be read.
|
2018-12-07 19:18:40 +01:00
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
DEBUGASSERT(bytes_read <= remaining);
|
|
|
|
dest += bytes_read;
|
|
|
|
remaining -= bytes_read;
|
2017-02-09 16:24:44 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
/* The number of bytes required to satisfy the read
|
|
|
|
* is less than or equal to the size of the buffer
|
|
|
|
* space that we have left. Read as much as we can
|
|
|
|
* into the buffer.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
bytes_read = _NX_READ(stream->fs_fd,
|
|
|
|
stream->fs_bufread,
|
|
|
|
buffer_available);
|
2017-02-09 16:24:44 +01:00
|
|
|
if (bytes_read < 0)
|
|
|
|
{
|
2021-08-30 12:24:03 +02:00
|
|
|
if (count - remaining > 0)
|
|
|
|
{
|
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* An error occurred on the read. The error
|
|
|
|
* code is in the 'errno' variable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_NX_SETERRNO(bytes_read);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if (bytes_read == 0)
|
|
|
|
{
|
2020-07-30 09:55:28 +02:00
|
|
|
/* We are at the end of the file. But we may
|
|
|
|
* already have buffered data. In that case,
|
|
|
|
* we will report the EOF indication later.
|
2017-02-09 16:24:44 +01:00
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-07 19:18:40 +01:00
|
|
|
/* Some (perhaps all) bytes were read */
|
2017-02-09 16:24:44 +01:00
|
|
|
|
|
|
|
stream->fs_bufread += bytes_read;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 16:53:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Now get any other needed chars from the file. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
while (remaining > 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2018-12-07 19:18:40 +01:00
|
|
|
bytes_read = _NX_READ(stream->fs_fd, dest, remaining);
|
2017-02-09 16:24:44 +01:00
|
|
|
if (bytes_read < 0)
|
|
|
|
{
|
2021-08-30 12:24:03 +02:00
|
|
|
if (count - remaining > 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* An error occurred on the read. The error code is
|
|
|
|
* in the 'errno' variable.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2021-08-30 12:24:03 +02:00
|
|
|
_NX_SETERRNO(bytes_read);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if (bytes_read == 0)
|
|
|
|
{
|
|
|
|
/* We are at the end of the file. But we may already
|
|
|
|
* have buffered data. In that case, we will report
|
|
|
|
* the EOF indication later.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-07 19:18:40 +01:00
|
|
|
DEBUGASSERT(bytes_read <= remaining);
|
|
|
|
dest += bytes_read;
|
|
|
|
remaining -= bytes_read;
|
2017-02-09 16:24:44 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2018-12-07 19:18:40 +01:00
|
|
|
/* Here after a successful (but perhaps short) read. A short read can
|
|
|
|
* only occur is read() returns 0 (end-of-file).
|
|
|
|
*/
|
2017-02-09 16:24:44 +01:00
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
shortread:
|
|
|
|
#endif
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Set or clear the EOF indicator. If we get here because of a
|
2018-12-07 19:18:40 +01:00
|
|
|
* short read and the total number of bytes read is zero, then
|
2012-11-10 17:06:01 +01:00
|
|
|
* we must be at the end-of-file.
|
|
|
|
*/
|
|
|
|
|
2018-12-07 19:18:40 +01:00
|
|
|
if (remaining == 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
stream->fs_flags &= ~__FS_FLAG_EOF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream->fs_flags |= __FS_FLAG_EOF;
|
|
|
|
}
|
2013-01-11 20:08:51 +01:00
|
|
|
|
2021-08-30 12:24:03 +02:00
|
|
|
return count - remaining;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:55:28 +02:00
|
|
|
/* Error exits */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
errout_with_errno:
|
2014-03-31 17:39:10 +02:00
|
|
|
stream->fs_flags |= __FS_FLAG_ERROR;
|
2021-08-30 12:24:03 +02:00
|
|
|
return ERROR;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|