2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2012-11-10 17:34:46 +01:00
|
|
|
* libc/stdio/lib_libfread.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2017-02-09 16:24:44 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights reserved.
|
2012-11-10 17:06:01 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
|
|
|
#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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_fread
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
FAR unsigned char *dest = (FAR unsigned char*)ptr;
|
|
|
|
ssize_t bytes_read;
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
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 */
|
|
|
|
|
|
|
|
if (!stream || (stream->fs_oflags & O_RDOK) == 0)
|
|
|
|
{
|
|
|
|
set_errno(EBADF);
|
|
|
|
bytes_read = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The stream must be stable until we complete the read */
|
|
|
|
|
|
|
|
lib_take_semaphore(stream);
|
|
|
|
|
|
|
|
#if CONFIG_NUNGET_CHARS > 0
|
|
|
|
/* First, re-read any previously ungotten characters */
|
|
|
|
|
|
|
|
while ((stream->fs_nungotten > 0) && (count > 0))
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
* flush all of the buffered write data. We do not support concurrent
|
|
|
|
* buffered read/write access.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
ret = lib_wrflush(stream);
|
|
|
|
if (ret < 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
lib_give_semaphore(stream);
|
|
|
|
return ret;
|
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
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
while (count > 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
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
while ((count > 0) && (stream->fs_bufpos < stream->fs_bufread))
|
|
|
|
{
|
|
|
|
/* Yes, copy a byte into the user buffer */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
*dest++ = *stream->fs_bufpos++;
|
|
|
|
count--;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* The buffer is empty OR we have already supplied the number of
|
|
|
|
* bytes requested in the read. Check if we need to read
|
|
|
|
* more from the file.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
size_t buffer_available;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01: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
|
|
|
|
2017-02-09 16:24:44 +01: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
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
if (count > buffer_available)
|
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
bytes_read = _NX_READ(stream->fs_fd, dest, count);
|
2017-02-09 16:24:44 +01:00
|
|
|
if (bytes_read < 0)
|
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
/* An error occurred on the read. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
_NX_SETERRNO(bytes_read);
|
2017-02-09 16:24:44 +01:00
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
else if (bytes_read == 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-09 16:24:44 +01: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.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Some bytes were read. Adjust the dest pointer */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
dest += bytes_read;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Were all of the requested bytes read? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
if ((size_t)bytes_read < count)
|
|
|
|
{
|
|
|
|
/* No. We must be at the end of file. */
|
|
|
|
|
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes. We are done. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
/* An error occurred on the read. The error code is
|
|
|
|
* in the 'errno' variable.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
_NX_SETERRNO(bytes_read);
|
2017-02-09 16:24:44 +01:00
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
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
|
|
|
goto shortread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Some bytes were read */
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
while (count > 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
bytes_read = _NX_READ(stream->fs_fd, dest, count);
|
2017-02-09 16:24:44 +01:00
|
|
|
if (bytes_read < 0)
|
|
|
|
{
|
|
|
|
/* An error occurred on the read. The error code is
|
|
|
|
* in the 'errno' variable.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
_NX_SETERRNO(bytes_read);
|
2017-02-09 16:24:44 +01:00
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
|
|
|
dest += bytes_read;
|
|
|
|
count -= bytes_read;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* Here after a successful (but perhaps short) read */
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2012-11-10 17:06:01 +01:00
|
|
|
shortread:
|
|
|
|
#endif
|
2017-02-09 16:24:44 +01:00
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
bytes_read = dest - (FAR unsigned char *)ptr;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Set or clear the EOF indicator. If we get here because of a
|
|
|
|
* short read and the total number of* bytes read is zero, then
|
|
|
|
* we must be at the end-of-file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (bytes_read > 0)
|
|
|
|
{
|
|
|
|
stream->fs_flags &= ~__FS_FLAG_EOF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream->fs_flags |= __FS_FLAG_EOF;
|
|
|
|
}
|
2013-01-11 20:08:51 +01:00
|
|
|
|
|
|
|
lib_give_semaphore(stream);
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
return bytes_read;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
|
|
|
errout_with_errno:
|
2014-03-31 17:39:10 +02:00
|
|
|
stream->fs_flags |= __FS_FLAG_ERROR;
|
2012-11-10 17:06:01 +01:00
|
|
|
lib_give_semaphore(stream);
|
2013-09-29 00:50:07 +02:00
|
|
|
return -get_errno();
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|