mirror of
https://github.com/apache/nuttx.git
synced 2025-01-13 09:49:21 +08:00
C Library: Add a very limited, first step implementation of setvbuf(). This is a collaborative effort. Alan Carvalho de Assis did the initial prototype.
This commit is contained in:
parent
4700499b59
commit
51a14c9b2f
7 changed files with 308 additions and 30 deletions
|
@ -66,6 +66,14 @@
|
|||
# define RESET_BUF(b) memset((b), 0, sizeof(struct stat));
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static inline int statpseudo(FAR struct inode *inode, FAR struct stat *buf);
|
||||
static inline int statroot(FAR struct stat *buf);
|
||||
int stat_recursive(FAR const char *path, FAR struct stat *buf);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
@ -129,7 +137,7 @@ static inline int statpseudo(FAR struct inode *inode, FAR struct stat *buf)
|
|||
return -ELOOP;
|
||||
}
|
||||
|
||||
DEBUGASSERT(buff->st_count > 0); /* Check for unsigned integer overflow */
|
||||
DEBUGASSERT(buf->st_count > 0); /* Check for unsigned integer overflow */
|
||||
|
||||
/* stat() the target of the soft link. */
|
||||
|
||||
|
@ -307,8 +315,6 @@ errout_with_inode:
|
|||
|
||||
errout_with_search:
|
||||
RELEASE_SEARCH(&desc);
|
||||
|
||||
errout:
|
||||
set_errno(ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
|
|
@ -65,6 +65,7 @@
|
|||
|
||||
#define __FS_FLAG_EOF (1 << 0) /* EOF detected by a read operation */
|
||||
#define __FS_FLAG_ERROR (1 << 1) /* Error detected by any operation */
|
||||
#define __FS_FLAG_UBF (1 << 2) /* Buffer allocated by caller of setvbuf */
|
||||
|
||||
/* Inode i_flag values:
|
||||
*
|
||||
|
|
|
@ -64,15 +64,21 @@
|
|||
# define BUFSIZ CONFIG_STDIO_BUFFER_SIZE
|
||||
#endif
|
||||
|
||||
/* The following three definitions are for ANSI C, used by setvbuf */
|
||||
|
||||
#define _IOFBF 0 /* Fully buffered */
|
||||
#define _IOLBF 1 /* Line buffered */
|
||||
#define _IONBF 2 /* Unbuffered */
|
||||
|
||||
/* File system error values */
|
||||
|
||||
#define EOF (-1)
|
||||
|
||||
/* The first three _iob entries are reserved for standard I/O */
|
||||
|
||||
#define stdin (&sched_getstreams()->sl_streams[0])
|
||||
#define stdout (&sched_getstreams()->sl_streams[1])
|
||||
#define stderr (&sched_getstreams()->sl_streams[2])
|
||||
#define stdin (&sched_getstreams()->sl_streams[0])
|
||||
#define stdout (&sched_getstreams()->sl_streams[1])
|
||||
#define stderr (&sched_getstreams()->sl_streams[2])
|
||||
|
||||
/* These APIs are not implemented and/or can be synthesized from
|
||||
* supported APIs.
|
||||
|
@ -90,7 +96,7 @@
|
|||
# define CONFIG_LIBC_TMPDIR "/tmp"
|
||||
#endif
|
||||
|
||||
#define P_tmpdir CONFIG_LIBC_TMPDIR
|
||||
#define P_tmpdir CONFIG_LIBC_TMPDIR
|
||||
|
||||
/* Maximum size of character array to hold tmpnam() output. */
|
||||
|
||||
|
@ -98,7 +104,7 @@
|
|||
# define CONFIG_LIBC_MAX_TMPFILE 32
|
||||
#endif
|
||||
|
||||
#define L_tmpnam CONFIG_LIBC_MAX_TMPFILE
|
||||
#define L_tmpnam CONFIG_LIBC_MAX_TMPFILE
|
||||
|
||||
/* The maximum number of unique temporary file names that can be generated */
|
||||
|
||||
|
@ -156,6 +162,7 @@ size_t fwrite(FAR const void *ptr, size_t size, size_t n_items,
|
|||
FAR FILE *stream);
|
||||
FAR char *gets(FAR char *s);
|
||||
FAR char *gets_s(FAR char *s, rsize_t n);
|
||||
int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size);
|
||||
int ungetc(int c, FAR FILE *stream);
|
||||
|
||||
/* Operations on the stdout stream, buffers, paths, and the whole printf-family */
|
||||
|
|
|
@ -53,18 +53,6 @@
|
|||
#if (!defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)) || \
|
||||
defined(__KERNEL__)
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
@ -101,13 +89,15 @@ void lib_stream_initialize(FAR struct task_group_s *group)
|
|||
|
||||
for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
|
||||
{
|
||||
/* Clear the IOB */
|
||||
FAR struct file_struct *stream = &list->sl_streams[i];
|
||||
|
||||
memset(&list->sl_streams[i], 0, sizeof(FILE));
|
||||
/* Clear the IOB */
|
||||
|
||||
memset(stream, 0, sizeof(FILE));
|
||||
|
||||
/* Indicate not opened */
|
||||
|
||||
list->sl_streams[i].fs_fd = -1;
|
||||
stream->fs_fd = -1;
|
||||
|
||||
/* Initialize the stream semaphore to one to support one-at-
|
||||
* a-time access to private data sets.
|
||||
|
@ -154,18 +144,21 @@ void lib_stream_release(FAR struct task_group_s *group)
|
|||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
|
||||
{
|
||||
FAR struct file_struct *stream = &list->sl_streams[i];
|
||||
|
||||
/* Destroy the semaphore that protects the IO buffer */
|
||||
|
||||
(void)sem_destroy(&list->sl_streams[i].fs_sem);
|
||||
(void)sem_destroy(&stream->fs_sem);
|
||||
|
||||
/* Release the IO buffer */
|
||||
|
||||
if (list->sl_streams[i].fs_bufstart)
|
||||
if (stream->fs_bufstart != NULL &&
|
||||
(stream->fs_flags & __FS_FLAG_UBF) == 0)
|
||||
{
|
||||
#ifndef CONFIG_BUILD_KERNEL
|
||||
/* Release memory from the user heap */
|
||||
|
||||
sched_ufree(list->sl_streams[i].fs_bufstart);
|
||||
sched_ufree(stream->fs_bufstart);
|
||||
#else
|
||||
/* If the exiting group is unprivileged, then it has an address
|
||||
* environment. Don't bother to release the memory in this case...
|
||||
|
@ -177,7 +170,7 @@ void lib_stream_release(FAR struct task_group_s *group)
|
|||
|
||||
if ((group->tg_flags & GROUP_FLAG_PRIVILEGED) != 0)
|
||||
{
|
||||
sched_kfree(list->sl_streams[i].fs_bufstart);
|
||||
sched_kfree(stream->fs_bufstart);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ CSRCS += lib_rdflush.c lib_wrflush.c lib_fputc.c lib_puts.c lib_fputs.c
|
|||
CSRCS += lib_ungetc.c lib_vprintf.c lib_fprintf.c lib_vfprintf.c
|
||||
CSRCS += lib_stdinstream.c lib_stdoutstream.c lib_stdsistream.c
|
||||
CSRCS += lib_stdsostream.c lib_perror.c lib_feof.c lib_ferror.c
|
||||
CSRCS += lib_clearerr.c
|
||||
CSRCS += lib_clearerr.c lib_setvbuf.c
|
||||
|
||||
endif
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/****************************************************************************
|
||||
* libc/stdio/lib_fclose.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011, 3013 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007-2009, 2011, 2013, 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -114,7 +114,8 @@ int fclose(FAR FILE *stream)
|
|||
|
||||
/* Release the buffer */
|
||||
|
||||
if (stream->fs_bufstart)
|
||||
if (stream->fs_bufstart != NULL &&
|
||||
(stream->fs_flags & __FS_FLAG_UBF) == 0)
|
||||
{
|
||||
lib_free(stream->fs_bufstart);
|
||||
}
|
||||
|
|
270
libc/stdio/lib_setvbuf.c
Normal file
270
libc/stdio/lib_setvbuf.c
Normal file
|
@ -0,0 +1,270 @@
|
|||
/****************************************************************************
|
||||
* libc/stdio/lib_setvbuf.c
|
||||
*
|
||||
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Alan Carvalho de Assis <acassis@gmail.com>
|
||||
* Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* This code is based on Newlib implementation.
|
||||
*
|
||||
* 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 will 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "libc.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: setvbuf
|
||||
*
|
||||
* Description:
|
||||
* The setvbuf() function may be used after the stream pointed to by
|
||||
* stream is associated with an open file but before any other operation
|
||||
* (other than an unsuccessful call to setvbuf()) is performed on the
|
||||
* stream. The argument type determines how stream will be buffered, as
|
||||
* follows:
|
||||
*
|
||||
* _IOFBF - Will cause input/output to be fully buffered.
|
||||
* _IOLBF - Will cause input/output to be line buffered.
|
||||
* _IONBF - Will cause input/output to be unbuffered.
|
||||
*
|
||||
* If buf is not a null pointer, the array it points to may be used instead
|
||||
* of a buffer allocated by setvbuf() and the argument size specifies the size
|
||||
* of the array; otherwise, size may determine the size of a buffer allocated
|
||||
* by the setvbuf() function. The contents of the array at any time are
|
||||
* unspecified.
|
||||
*
|
||||
* REVISIT: This initial version of setvbuf has some severe limitations and
|
||||
* not all features features required by the specification are available.
|
||||
* These the limitations are the result of hard-coded stream logic based
|
||||
* the static configuration. This logic cannot support variable behaviors
|
||||
* without some additional logic. Specifically, we cannot support:
|
||||
*
|
||||
* - Any line buffering state that is inconsistent with the configuration
|
||||
* setting CONFIG_STDIO_LINEBUFFER
|
||||
* - We cannot support disabling stream buffering if
|
||||
* CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
*
|
||||
* Parmeters:
|
||||
* stream - the stream to flush
|
||||
* buffer - the user allocate buffer. If NULL, will allocates a buffer of
|
||||
* specified size
|
||||
* mode - specifies a mode for file buffering
|
||||
* size - size of buffer
|
||||
*
|
||||
* Return:
|
||||
* Upon successful completion, setvbuf() will return 0. Otherwise, it will
|
||||
* return a non-zero value if an invalid value is given for type or if the
|
||||
* request cannot be honored, [CX] and may set errno to indicate the error
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size)
|
||||
{
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
uint8_t flags;
|
||||
int errcode;
|
||||
int ret = OK;
|
||||
|
||||
/* Verify arguments */
|
||||
|
||||
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
|
||||
{
|
||||
errcode = EINVAL;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make sure that the size argument agrees with the mode */
|
||||
|
||||
if (((mode == _IOFBF || mode == _IOLBF) && size == 0) ||
|
||||
(mode == _IONBF && size > 0))
|
||||
{
|
||||
errcode = EINVAL;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make sure that the buffer argument agrees with mode */
|
||||
|
||||
if (mode == _IONBF && buffer != NULL)
|
||||
{
|
||||
errcode = EINVAL;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make sure that the buffer and size argeuments agree */
|
||||
|
||||
if (buffer != NULL && size == 0)
|
||||
{
|
||||
errcode = EINVAL;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Not all features are be available. Without some additional logic,
|
||||
* we cannot support (1) any line buffering state that is inconsistent
|
||||
* with CONFIG_STDIO_LINEBUFFER nor can we (2) disable buffering if
|
||||
* CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
if (mode != _IOLBF)
|
||||
#else
|
||||
if (mode != _IOFBF)
|
||||
#endif
|
||||
{
|
||||
errcode = ENOSYS;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make sure that we have exclusive access to the stream */
|
||||
|
||||
lib_take_semaphore(stream);
|
||||
|
||||
/* setvbuf() may only be called AFTER the file has been opened and BEFORE
|
||||
* any operations have been performed on the string.
|
||||
*/
|
||||
|
||||
/* Return EBADF if the file is not open */
|
||||
|
||||
if (stream->fs_fd < 0)
|
||||
{
|
||||
errcode = EBADF;
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
|
||||
/* Return EBUSY if operations have already been performed on the buffer.
|
||||
* Here we really only verify that there is no valid data in the existing
|
||||
* buffer.
|
||||
*/
|
||||
|
||||
if (stream->fs_bufpos != stream->fs_bufstart)
|
||||
{
|
||||
errcode = EBUSY;
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
|
||||
/* Initialize by clearing related flags. We try to avoid any permanent
|
||||
* changes to the stream structure until we know that we will be
|
||||
* successful.
|
||||
*/
|
||||
|
||||
flags = stream->fs_flags & ~__FS_FLAG_UBF;
|
||||
|
||||
/* Allocate a new buffer if one is needed. We have already verified that:
|
||||
*
|
||||
* If a buffer is needed, then size > 0
|
||||
* If a buffer is NOT needed, then buffer is NULL
|
||||
* If buffer != NULL, then size > 0
|
||||
*
|
||||
* That simplifies the following check:
|
||||
*/
|
||||
|
||||
#if 0 /* size should always be > 0 in the currently supported configurations */
|
||||
if (size > 0)
|
||||
#else
|
||||
DEBUGASSERT(size > 0);
|
||||
#endif
|
||||
{
|
||||
FAR unsigned char *newbuf;
|
||||
|
||||
/* A buffer is needed. Did the caller provide the buffer memory? */
|
||||
|
||||
DEBUGASSERT(mode == _IOFBF || mode == _IOLBF);
|
||||
if (buffer != NULL)
|
||||
{
|
||||
newbuf = (FAR unsigned char *)buffer;
|
||||
|
||||
/* Indicate that we have an I/O buffer managed by the caller of
|
||||
* setvbuf.
|
||||
*/
|
||||
|
||||
flags |= __FS_FLAG_UBF;
|
||||
}
|
||||
else
|
||||
{
|
||||
newbuf = (FAR unsigned char *)lib_malloc(size);
|
||||
if (newbuf == NULL)
|
||||
{
|
||||
errcode = ENOMEM;
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
}
|
||||
|
||||
/* Do not release the previous buffer if it was allocated by the user
|
||||
* on a previous call to setvbuf().
|
||||
*/
|
||||
|
||||
if ((stream->fs_flags & __FS_FLAG_UBF) != 0)
|
||||
{
|
||||
lib_free(stream->fs_bufstart);
|
||||
}
|
||||
|
||||
/* Use the new buffer */
|
||||
|
||||
stream->fs_bufstart = newbuf;
|
||||
stream->fs_bufend = newbuf;
|
||||
stream->fs_bufpos = newbuf;
|
||||
stream->fs_bufread = newbuf;
|
||||
}
|
||||
|
||||
/* Update the stream flags and return success */
|
||||
|
||||
stream->fs_flags = flags;
|
||||
lib_give_semaphore(stream);
|
||||
return OK;
|
||||
|
||||
errout_with_semaphore:
|
||||
lib_give_semaphore(stream);
|
||||
|
||||
errout:
|
||||
set_errno(errcode);
|
||||
return ERROR;
|
||||
|
||||
#else
|
||||
/* Return that setvbuf() is not supported */
|
||||
|
||||
set_errno(ENOSYS);
|
||||
return ERROR;
|
||||
#endif
|
||||
}
|
Loading…
Reference in a new issue