mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-26 15:33:33 +08:00
Manually fixup merge botches via direct comparison with NuttX r5447.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
############################################################################
|
||||
# libc/stdio/Make.defs
|
||||
#
|
||||
# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
# Add the stdio C files to the build
|
||||
# This first group of C files do not depend on having file descriptors or
|
||||
# C streams.
|
||||
|
||||
CSRCS += lib_fileno.c lib_printf.c lib_rawprintf.c lib_lowprintf.c \
|
||||
lib_sprintf.c lib_asprintf.c lib_snprintf.c lib_libsprintf.c \
|
||||
lib_vsprintf.c lib_avsprintf.c lib_vsnprintf.c lib_libvsprintf.c \
|
||||
lib_meminstream.c lib_memoutstream.c lib_lowinstream.c \
|
||||
lib_lowoutstream.c lib_zeroinstream.c lib_nullinstream.c \
|
||||
lib_nulloutstream.c lib_sscanf.c
|
||||
|
||||
# The remaining sources files depend upon file descriptors
|
||||
|
||||
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
|
||||
|
||||
CSRCS += lib_rawinstream.c lib_rawoutstream.c
|
||||
|
||||
# And these depend upon both file descriptors and C streams
|
||||
|
||||
ifneq ($(CONFIG_NFILE_STREAMS),0)
|
||||
|
||||
CSRCS += lib_fopen.c lib_fclose.c lib_fread.c lib_libfread.c lib_fseek.c \
|
||||
lib_ftell.c lib_fsetpos.c lib_fgetpos.c lib_fgetc.c lib_fgets.c \
|
||||
lib_gets.c lib_fwrite.c lib_libfwrite.c lib_fflush.c \
|
||||
lib_libflushall.c lib_libfflush.c lib_rdflush.c lib_wrflush.c \
|
||||
lib_fputc.c lib_puts.c lib_fputs.c lib_ungetc.c lib_vprintf.c \
|
||||
lib_fprintf.c lib_vfprintf.c lib_stdinstream.c lib_stdoutstream.c \
|
||||
lib_perror.c lib_feof.c lib_ferror.c lib_clearerr.c
|
||||
|
||||
endif
|
||||
endif
|
||||
|
||||
# Other support that depends on specific, configured features.
|
||||
|
||||
ifeq ($(CONFIG_SYSLOG),y)
|
||||
CSRCS += lib_syslogstream.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIBC_FLOATINGPOINT),y)
|
||||
CSRCS += lib_dtoa.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_STDIO_LINEBUFFER),y)
|
||||
CSRCS += lib_libnoflush.c
|
||||
endif
|
||||
|
||||
# Add the stdio directory to the build
|
||||
|
||||
DEPPATH += --dep-path stdio
|
||||
VPATH += :stdio
|
||||
@@ -0,0 +1,105 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_asprintf.c
|
||||
*
|
||||
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: asprintf
|
||||
*
|
||||
* Description:
|
||||
* This function is similar to sprintf, except that it dynamically
|
||||
* allocates a string (as with malloc) to hold the output, instead of
|
||||
* putting the output in a buffer you allocate in advance. The ptr
|
||||
* argument should be the address of a char * object, and a successful
|
||||
* call to asprintf stores a pointer to the newly allocated string at that
|
||||
* location.
|
||||
*
|
||||
* Returned Value:
|
||||
* The returned value is the number of characters allocated for the buffer,
|
||||
* or less than zero if an error occurred. Usually this means that the buffer
|
||||
* could not be allocated.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int asprintf (FAR char **ptr, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
/* Let avsprintf do all of the work */
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = avsprintf(ptr, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_avsprintf.c
|
||||
*
|
||||
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: avsprintf
|
||||
*
|
||||
* Description:
|
||||
* This function is similar to vsprintf, except that it dynamically
|
||||
* allocates a string (as with malloc) to hold the output, instead of
|
||||
* putting the output in a buffer you allocate in advance. The ptr
|
||||
* argument should be the address of a char * object, and a successful
|
||||
* call to avsprintf stores a pointer to the newly allocated string at that
|
||||
* location.
|
||||
*
|
||||
* Returned Value:
|
||||
* The returned value is the number of characters allocated for the buffer,
|
||||
* or less than zero if an error occurred. Usually this means that the buffer
|
||||
* could not be allocated.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int avsprintf(FAR char **ptr, const char *fmt, va_list ap)
|
||||
{
|
||||
struct lib_outstream_s nulloutstream;
|
||||
struct lib_memoutstream_s memoutstream;
|
||||
FAR char *buf;
|
||||
int nbytes;
|
||||
|
||||
DEBUGASSERT(ptr && fmt);
|
||||
|
||||
/* First, use a nullstream to get the size of the buffer. The number
|
||||
* of bytes returned may or may not include the null terminator.
|
||||
*/
|
||||
|
||||
lib_nulloutstream(&nulloutstream);
|
||||
nbytes = lib_vsprintf((FAR struct lib_outstream_s *)&nulloutstream, fmt, ap);
|
||||
|
||||
/* Then allocate a buffer to hold that number of characters, adding one
|
||||
* for the null terminator.
|
||||
*/
|
||||
|
||||
buf = (FAR char *)malloc(nulloutstream.nput + 1);
|
||||
if (!buf)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Initialize a memory stream to write into the allocated buffer. The
|
||||
* memory stream will reserve one byte at the end of the buffer for the
|
||||
* null terminator and will not report this in the number of output bytes.
|
||||
*/
|
||||
|
||||
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream,
|
||||
buf, nulloutstream.nput + 1);
|
||||
|
||||
/* Then let lib_vsprintf do it's real thing */
|
||||
|
||||
nbytes = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, fmt, ap);
|
||||
|
||||
/* Return a pointer to the string to the caller. NOTE: the memstream put()
|
||||
* method has already added the NUL terminator to the end of the string (not
|
||||
* included in the nput count).
|
||||
*
|
||||
* Hmmm.. looks like the memory would be stranded if lib_vsprintf() returned
|
||||
* an error. Does that ever happen?
|
||||
*/
|
||||
|
||||
DEBUGASSERT(nbytes < 0 || nbytes == nulloutstream.nput);
|
||||
*ptr = buf;
|
||||
return nbytes;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_clearerr.c
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: Functions
|
||||
*
|
||||
* Description:
|
||||
* Clear any end-of-file or error conditions.
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void clearerr(FILE *stream)
|
||||
{
|
||||
stream->fs_flags = 0;
|
||||
}
|
||||
#endif /* CONFIG_NFILE_STREAMS */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,154 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fclose.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fclose
|
||||
*
|
||||
* Description
|
||||
* The fclose() function will flush the stream pointed to by stream
|
||||
* (writing any buffered output data using lib_fflush()) and close the
|
||||
* underlying file descriptor.
|
||||
*
|
||||
* Returned Value:
|
||||
* Upon successful completion 0 is returned. Otherwise, EOF is returned
|
||||
* and the global variable errno is set to indicate the error. In either
|
||||
* case any further access (including another call to fclose()) to the
|
||||
* stream results in undefined behaviour.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int fclose(FAR FILE *stream)
|
||||
{
|
||||
int err = EINVAL;
|
||||
int ret = ERROR;
|
||||
int status;
|
||||
|
||||
/* Verify that a stream was provided. */
|
||||
|
||||
if (stream)
|
||||
{
|
||||
/* Check that the underlying file descriptor corresponds to an an open
|
||||
* file.
|
||||
*/
|
||||
|
||||
ret = OK;
|
||||
if (stream->fs_filedes >= 0)
|
||||
{
|
||||
/* If the stream was opened for writing, then flush the stream */
|
||||
|
||||
if ((stream->fs_oflags & O_WROK) != 0)
|
||||
{
|
||||
ret = lib_fflush(stream, true);
|
||||
err = errno;
|
||||
}
|
||||
|
||||
/* Close the underlying file descriptor and save the return status */
|
||||
|
||||
status = close(stream->fs_filedes);
|
||||
|
||||
/* If close() returns an error but flush() did not then make sure
|
||||
* that we return the close() error condition.
|
||||
*/
|
||||
|
||||
if (ret == OK)
|
||||
{
|
||||
ret = status;
|
||||
err = errno;
|
||||
}
|
||||
}
|
||||
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
/* Destroy the semaphore */
|
||||
|
||||
sem_destroy(&stream->fs_sem);
|
||||
|
||||
/* Release the buffer */
|
||||
|
||||
if (stream->fs_bufstart)
|
||||
{
|
||||
lib_free(stream->fs_bufstart);
|
||||
}
|
||||
|
||||
/* Clear the whole structure */
|
||||
|
||||
memset(stream, 0, sizeof(FILE));
|
||||
#else
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
/* Reset the number of ungetc characters */
|
||||
|
||||
stream->fs_nungotten = 0;
|
||||
#endif
|
||||
/* Reset the flags */
|
||||
|
||||
stream->fs_oflags = 0;
|
||||
#endif
|
||||
/* Setting the fs_filedescriptor to -1 makes the stream available for reuse */
|
||||
|
||||
stream->fs_filedes = -1;
|
||||
}
|
||||
|
||||
/* On an error, reset the errno to the first error encountered and return
|
||||
* EOF.
|
||||
*/
|
||||
|
||||
if (ret != OK)
|
||||
{
|
||||
set_errno(err);
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/* Return success */
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_feof.c
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: feof
|
||||
*
|
||||
* Description:
|
||||
* The feof() function shall test if the currently file pointer for the
|
||||
* stream is at the end of file.
|
||||
*
|
||||
* Returned Value:
|
||||
* This function will return non-zero if the the file pointer is positioned
|
||||
* at the end of file.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int feof(FILE *stream)
|
||||
{
|
||||
/* If the end-of-file condition is encountered by any of the C-buffered
|
||||
* I/O functions that perform read operations, they should set the
|
||||
* __FS_FLAG_EOF in the fs_flags field of struct file_struct.
|
||||
*/
|
||||
|
||||
return (stream->fs_flags & __FS_FLAG_EOF) != 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NFILE_STREAMS */
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_ferror.c
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ferror
|
||||
*
|
||||
* Description:
|
||||
* This function will test if the last operation resulted in an eror. This
|
||||
* is used to disambiguate EOF and error conditions.
|
||||
*
|
||||
* Return Value:
|
||||
* A non-zero value is returned to indicate the error condition.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ferror(FILE *stream)
|
||||
{
|
||||
#if 0
|
||||
/* If an error is encountered by any of the C-buffered I/O functions, they
|
||||
* should set the __FS_FLAG_ERROR in the fs_flags field of struct
|
||||
* file_struct.
|
||||
*/
|
||||
|
||||
return (stream->fs_flags & __FS_FLAG_ERROR) != 0;
|
||||
#else
|
||||
/* However, nothing currenlty sets the __FS_FLAG_ERROR flag (that is a job
|
||||
* for another day). The __FS_FLAG_EOF is set by operations that perform
|
||||
* read operations. Since ferror() is probably only called to disambiguate
|
||||
* the meaning of other functions that return EOF, to indicate either EOF or
|
||||
* an error, just testing for not EOF is probably sufficient for now.
|
||||
*
|
||||
* This approach would not work if ferror() is called in other contexts. In
|
||||
* those cases, ferror() will always report an error.
|
||||
*/
|
||||
|
||||
return (stream->fs_flags & __FS_FLAG_EOF) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NFILE_STREAMS */
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fflush.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fflush
|
||||
*
|
||||
* Description:
|
||||
* The function fflush() forces a write of all user-space buffered data for
|
||||
* the given output or update stream via the stream's underlying write
|
||||
* function. The open status of the stream is unaffected.
|
||||
*
|
||||
* If the stream argument is NULL, fflush() flushes all open output streams.
|
||||
*
|
||||
* Return:
|
||||
* OK on success EOF on failure (with errno set appropriately)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int fflush(FAR FILE *stream)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Is the stream argument NULL? */
|
||||
|
||||
if (!stream)
|
||||
{
|
||||
/* Yes... then this is a request to flush all streams */
|
||||
|
||||
ret = lib_flushall(sched_getstreams());
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = lib_fflush(stream, true);
|
||||
}
|
||||
|
||||
/* Check the return value */
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
/* An error occurred during the flush AND/OR we were unable to flush
|
||||
* all of the buffered write data. Set the errno value.
|
||||
*/
|
||||
|
||||
set_errno(-ret);
|
||||
|
||||
/* And return EOF on failure. */
|
||||
|
||||
return EOF;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fgetc.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Global Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* fgetc
|
||||
**************************************************************************/
|
||||
|
||||
int fgetc(FAR FILE *stream)
|
||||
{
|
||||
unsigned char ch;
|
||||
ssize_t ret;
|
||||
|
||||
ret = lib_fread(&ch, 1, stream);
|
||||
if (ret > 0)
|
||||
{
|
||||
return ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fgetpos.c
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fgetpos
|
||||
*
|
||||
* Description:
|
||||
* fgetpos() function is an alternate interfaces equivalent to ftell().
|
||||
* It gets the current value of the file offset and store it in the location
|
||||
* referenced by pos. On some non-UNIX systems an fpos_t object may be a
|
||||
* complex object and fsetpos may be the only way to portably reposition a
|
||||
* stream.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero on succes; -1 on failure with errno set appropriately.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int fgetpos(FAR FILE *stream, FAR fpos_t *pos)
|
||||
{
|
||||
long position;
|
||||
|
||||
#if CONFIG_DEBUG
|
||||
if (!stream || !pos)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
return ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
position = ftell(stream);
|
||||
if (position == -1)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
*pos = (fpos_t)position;
|
||||
return OK;
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fgets.c
|
||||
*
|
||||
* Copyright (C) 2007-2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <debug.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
/* Some environments may return CR as end-of-line, others LF, and others
|
||||
* both. If not specified, the logic here assumes either (but not both) as
|
||||
* the default.
|
||||
*/
|
||||
|
||||
#if defined(CONFIG_EOL_IS_CR)
|
||||
# undef CONFIG_EOL_IS_LF
|
||||
# undef CONFIG_EOL_IS_BOTH_CRLF
|
||||
# undef CONFIG_EOL_IS_EITHER_CRLF
|
||||
#elif defined(CONFIG_EOL_IS_LF)
|
||||
# undef CONFIG_EOL_IS_CR
|
||||
# undef CONFIG_EOL_IS_BOTH_CRLF
|
||||
# undef CONFIG_EOL_IS_EITHER_CRLF
|
||||
#elif defined(CONFIG_EOL_IS_BOTH_CRLF)
|
||||
# undef CONFIG_EOL_IS_CR
|
||||
# undef CONFIG_EOL_IS_LF
|
||||
# undef CONFIG_EOL_IS_EITHER_CRLF
|
||||
#elif defined(CONFIG_EOL_IS_EITHER_CRLF)
|
||||
# undef CONFIG_EOL_IS_CR
|
||||
# undef CONFIG_EOL_IS_LF
|
||||
# undef CONFIG_EOL_IS_BOTH_CRLF
|
||||
#else
|
||||
# undef CONFIG_EOL_IS_CR
|
||||
# undef CONFIG_EOL_IS_LF
|
||||
# undef CONFIG_EOL_IS_BOTH_CRLF
|
||||
# define CONFIG_EOL_IS_EITHER_CRLF 1
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fgets
|
||||
*
|
||||
* Description:
|
||||
* fgets() reads in at most one less than 'buflen' characters from stream
|
||||
* and stores them into the buffer pointed to by 'buf'. Reading stops after
|
||||
* an EOF or a newline. If a newline is read, it is stored into the
|
||||
* buffer. A null terminator is stored after the last character in the
|
||||
* buffer.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
char *fgets(FAR char *buf, int buflen, FILE *stream)
|
||||
{
|
||||
int nch = 0;
|
||||
|
||||
/* Sanity checks */
|
||||
|
||||
if (!stream || !buf || buflen < 1 || stream->fs_filedes < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (buflen < 2)
|
||||
{
|
||||
*buf = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* Read characters until we have a full line. On each the loop we must
|
||||
* be assured that there are two free bytes in the line buffer: One for
|
||||
* the next character and one for the null terminator.
|
||||
*/
|
||||
|
||||
for(;;)
|
||||
{
|
||||
/* Get the next character */
|
||||
|
||||
int ch = fgetc(stream);
|
||||
|
||||
/* Check for end-of-line. This is tricky only in that some
|
||||
* environments may return CR as end-of-line, others LF, and
|
||||
* others both.
|
||||
*/
|
||||
|
||||
#if defined(CONFIG_EOL_IS_LF) || defined(CONFIG_EOL_IS_BOTH_CRLF)
|
||||
if (ch == '\n')
|
||||
#elif defined(CONFIG_EOL_IS_CR)
|
||||
if (ch == '\r')
|
||||
#else /* elif CONFIG_EOL_IS_EITHER_CRLF */
|
||||
if (ch == '\n' || ch == '\r')
|
||||
#endif
|
||||
{
|
||||
/* The newline is stored in the buffer along with the null
|
||||
* terminator.
|
||||
*/
|
||||
|
||||
buf[nch++] = '\n';
|
||||
buf[nch] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* Check for end-of-file */
|
||||
|
||||
else if (ch == EOF)
|
||||
{
|
||||
/* End of file with no data? */
|
||||
|
||||
if (!nch)
|
||||
{
|
||||
/* Yes.. return NULL as the end of file mark */
|
||||
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Terminate the line */
|
||||
|
||||
buf[nch] = '\0';
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
/* Otherwise, check if the character is printable and, if so, put the
|
||||
* character in the line buffer
|
||||
*/
|
||||
|
||||
else if (isprint(ch))
|
||||
{
|
||||
buf[nch++] = ch;
|
||||
|
||||
/* Check if there is room for another character and the line's
|
||||
* null terminator. If not then we have to end the line now.
|
||||
*/
|
||||
|
||||
if (nch + 1 >= buflen)
|
||||
{
|
||||
buf[nch] = '\0';
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fileno.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
int fileno(FAR FILE *stream)
|
||||
{
|
||||
int ret = -1;
|
||||
if (stream)
|
||||
{
|
||||
ret = stream->fs_filedes;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
set_errno(EBADF);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* CONFIG_NFILE_STREAMS */
|
||||
|
||||
@@ -0,0 +1,299 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fopen.c
|
||||
*
|
||||
* Copyright (C) 2007-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
enum open_mode_e
|
||||
{
|
||||
MODE_NONE = 0, /* No access mode determined */
|
||||
MODE_R, /* "r" or "rb" open for reading */
|
||||
MODE_W, /* "w" or "wb" open for writing, truncating or creating file */
|
||||
MODE_A, /* "a" or "ab" open for writing, appending to file */
|
||||
MODE_RPLUS, /* "r+", "rb+", or "r+b" open for update (reading and writing) */
|
||||
MODE_WPLUS, /* "w+", "wb+", or "w+b" open for update, truncating or creating file */
|
||||
MODE_APLUS /* "a+", "ab+", or "a+b" open for update, appending to file */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_mode2oflags
|
||||
****************************************************************************/
|
||||
|
||||
static int lib_mode2oflags(FAR const char *mode)
|
||||
{
|
||||
enum open_mode_e state;
|
||||
int oflags;
|
||||
|
||||
/* Verify that a mode string was provided. No error is */
|
||||
|
||||
if (!mode)
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Parse the mode string to determine the corresponding open flags */
|
||||
|
||||
state = MODE_NONE;
|
||||
oflags = 0;
|
||||
|
||||
for (; *mode; mode++)
|
||||
{
|
||||
switch (*mode)
|
||||
{
|
||||
/* Open for read access ("r", "r[+]", "r[b]", "r[b+]", or "r[+b]") */
|
||||
|
||||
case 'r' :
|
||||
if (state == MODE_NONE)
|
||||
{
|
||||
/* Open for read access */
|
||||
|
||||
oflags = O_RDOK;
|
||||
state = MODE_R;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for write access ("w", "w[+]", "w[b]", "w[b+]", or "w[+b]") */
|
||||
|
||||
case 'w' :
|
||||
if (state == MODE_NONE)
|
||||
{
|
||||
/* Open for write access, truncating any existing file */
|
||||
|
||||
oflags = O_WROK|O_CREAT|O_TRUNC;
|
||||
state = MODE_W;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for write/append access ("a", "a[+]", "a[b]", "a[b+]", or "a[+b]") */
|
||||
|
||||
case 'a' :
|
||||
if (state == MODE_NONE)
|
||||
{
|
||||
/* Write to the end of the file */
|
||||
|
||||
oflags = O_WROK|O_CREAT|O_APPEND;
|
||||
state = MODE_A;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for update access ("[r]+", "[rb]+]", "[r]+[b]", "[w]+",
|
||||
* "[wb]+]", "[w]+[b]", "[a]+", "[ab]+]", "[a]+[b]")
|
||||
*/
|
||||
|
||||
case '+' :
|
||||
switch (state)
|
||||
{
|
||||
case MODE_R:
|
||||
{
|
||||
/* Retain any binary mode selection */
|
||||
|
||||
oflags &= O_BINARY;
|
||||
|
||||
/* Open for read/write access */
|
||||
|
||||
oflags |= O_RDWR;
|
||||
state = MODE_RPLUS;
|
||||
}
|
||||
break;
|
||||
|
||||
case MODE_W:
|
||||
{
|
||||
/* Retain any binary mode selection */
|
||||
|
||||
oflags &= O_BINARY;
|
||||
|
||||
/* Open for write read/access, truncating any existing file */
|
||||
|
||||
oflags |= O_RDWR|O_CREAT|O_TRUNC;
|
||||
state = MODE_WPLUS;
|
||||
}
|
||||
break;
|
||||
|
||||
case MODE_A:
|
||||
{
|
||||
/* Retain any binary mode selection */
|
||||
|
||||
oflags &= O_BINARY;
|
||||
|
||||
/* Read from the beginning of the file; write to the end */
|
||||
|
||||
oflags |= O_RDWR|O_CREAT|O_APPEND;
|
||||
state = MODE_APLUS;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
goto errout;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for binary access ("[r]b", "[r]b[+]", "[r+]b", "[w]b",
|
||||
* "[w]b[+]", "[w+]b", "[a]b", "[a]b[+]", "[a+]b")
|
||||
*/
|
||||
|
||||
case 'b' :
|
||||
if (state != MODE_NONE)
|
||||
{
|
||||
/* The file is opened in binary mode */
|
||||
|
||||
oflags |= O_BINARY;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Unrecognized or unsupported mode */
|
||||
|
||||
default:
|
||||
goto errout;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return oflags;
|
||||
|
||||
/* Both fopen and fdopen should fail with errno == EINVAL if the mode
|
||||
* string is invalid.
|
||||
*/
|
||||
|
||||
errout:
|
||||
set_errno(EINVAL);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fdopen
|
||||
****************************************************************************/
|
||||
|
||||
FAR FILE *fdopen(int fd, FAR const char *mode)
|
||||
{
|
||||
FAR FILE *ret = NULL;
|
||||
int oflags;
|
||||
|
||||
/* Map the open mode string to open flags */
|
||||
|
||||
oflags = lib_mode2oflags(mode);
|
||||
if (oflags >= 0)
|
||||
{
|
||||
ret = fs_fdopen(fd, oflags, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fopen
|
||||
****************************************************************************/
|
||||
|
||||
FAR FILE *fopen(FAR const char *path, FAR const char *mode)
|
||||
{
|
||||
FAR FILE *ret = NULL;
|
||||
int oflags;
|
||||
int fd;
|
||||
|
||||
/* Map the open mode string to open flags */
|
||||
|
||||
oflags = lib_mode2oflags(mode);
|
||||
if (oflags < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Open the file */
|
||||
|
||||
fd = open(path, oflags, 0666);
|
||||
|
||||
/* If the open was successful, then fdopen() the fil using the file
|
||||
* desciptor returned by open. If open failed, then just return the
|
||||
* NULL stream -- open() has already set the errno.
|
||||
*/
|
||||
|
||||
if (fd >= 0)
|
||||
{
|
||||
ret = fs_fdopen(fd, oflags, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
/* Don't forget to close the file descriptor if any other
|
||||
* failures are reported by fdopen().
|
||||
*/
|
||||
|
||||
(void)close(fd);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fprintf.c
|
||||
*
|
||||
* Copyright (C) 2007, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fprintf
|
||||
****************************************************************************/
|
||||
|
||||
int fprintf(FAR FILE *stream, FAR const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
/* vfprintf into the stream */
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = vfprintf(stream, fmt, ap);
|
||||
va_end(ap);
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fputc.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fputc
|
||||
****************************************************************************/
|
||||
|
||||
int fputc(int c, FAR FILE *stream)
|
||||
{
|
||||
unsigned char buf = (unsigned char)c;
|
||||
int ret;
|
||||
|
||||
ret = lib_fwrite(&buf, 1, stream);
|
||||
if (ret > 0)
|
||||
{
|
||||
/* Flush the buffer if a newline is output */
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
if (c == '\n')
|
||||
{
|
||||
ret = lib_fflush(stream, true);
|
||||
if (ret < 0)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return c;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fputs.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/arch.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fputs
|
||||
*
|
||||
* Description:
|
||||
* fputs() writes the string s to stream, without its trailing '\0'.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_ARCH_ROMGETC)
|
||||
int fputs(FAR const char *s, FAR FILE *stream)
|
||||
{
|
||||
int nput;
|
||||
int ret;
|
||||
char ch;
|
||||
|
||||
/* Make sure that a string was provided. */
|
||||
|
||||
#ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */
|
||||
if (!s)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
return EOF;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Write the string. Loop until the null terminator is encountered */
|
||||
|
||||
for (nput = 0, ch = up_romgetc(s); ch; nput++, s++, ch = up_romgetc(s))
|
||||
{
|
||||
/* Write the next character to the stream buffer */
|
||||
|
||||
ret = lib_fwrite(&ch, 1, stream);
|
||||
if (ret <= 0)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/* Flush the buffer if a newline was written to the buffer */
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
if (ch == '\n')
|
||||
{
|
||||
ret = lib_fflush(stream, true);
|
||||
if (ret < 0)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return nput;
|
||||
}
|
||||
|
||||
#elif defined(CONFIG_STDIO_LINEBUFFER)
|
||||
int fputs(FAR const char *s, FAR FILE *stream)
|
||||
{
|
||||
int nput;
|
||||
int ret;
|
||||
|
||||
/* Make sure that a string was provided. */
|
||||
|
||||
#ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */
|
||||
if (!s)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
return EOF;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Write the string. Loop until the null terminator is encountered */
|
||||
|
||||
for (nput = 0; *s; nput++, s++)
|
||||
{
|
||||
/* Write the next character to the stream buffer */
|
||||
|
||||
ret = lib_fwrite(s, 1, stream);
|
||||
if (ret <= 0)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/* Flush the buffer if a newline was written to the buffer */
|
||||
|
||||
if (*s == '\n')
|
||||
{
|
||||
ret = lib_fflush(stream, true);
|
||||
if (ret < 0)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nput;
|
||||
}
|
||||
|
||||
#else
|
||||
int fputs(FAR const char *s, FAR FILE *stream)
|
||||
{
|
||||
int ntowrite;
|
||||
int nput;
|
||||
|
||||
/* Make sure that a string was provided. */
|
||||
|
||||
#ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */
|
||||
if (!s)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
return EOF;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Get the length of the string. */
|
||||
|
||||
ntowrite = strlen(s);
|
||||
if (ntowrite == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Write the string */
|
||||
|
||||
nput = lib_fwrite(s, ntowrite, stream);
|
||||
if (nput < 0)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
return nput;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,101 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fread.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fread
|
||||
****************************************************************************/
|
||||
|
||||
size_t fread(FAR void *ptr, size_t size, size_t n_items, FAR FILE *stream)
|
||||
{
|
||||
size_t full_size = n_items * (size_t)size;
|
||||
ssize_t bytes_read;
|
||||
size_t items_read = 0;
|
||||
|
||||
/* Write the data into the stream buffer */
|
||||
|
||||
bytes_read = lib_fread(ptr, full_size, stream);
|
||||
if (bytes_read > 0)
|
||||
{
|
||||
/* Return the number of full items read */
|
||||
|
||||
items_read = bytes_read / size;
|
||||
}
|
||||
return items_read;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fseek.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fseek
|
||||
*
|
||||
* Description:
|
||||
* The fseek() function sets the file position indicator for the stream
|
||||
* pointed to by stream. The new position, measured in bytes, is obtained
|
||||
* by adding offset bytes to the position specified by whence. If whence is
|
||||
* set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the
|
||||
* start of the file, the current position indicator, or end-of-file,
|
||||
* respectively. A successful call to the fseek() function clears the
|
||||
* end-of-file indicator for the stream and undoes any effects of the ungetc(3)
|
||||
* function on the same stream.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero on succes; -1 on failure with errno set appropriately.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int fseek(FAR FILE *stream, long int offset, int whence)
|
||||
{
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
/* Flush any valid read/write data in the buffer (also verifies stream) */
|
||||
|
||||
if (lib_rdflush(stream) < 0 || lib_wrflush(stream) < 0)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
#else
|
||||
/* Verify that we were provided with a stream */
|
||||
|
||||
if (!stream)
|
||||
{
|
||||
set_errno(EBADF);
|
||||
return ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* On success or failure, discard any characters saved by ungetc() */
|
||||
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
stream->fs_nungotten = 0;
|
||||
#endif
|
||||
|
||||
/* Perform the fseek on the underlying file descriptor */
|
||||
|
||||
return lseek(stream->fs_filedes, offset, whence) == (off_t)-1 ? ERROR : OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fsetpos.c
|
||||
*
|
||||
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fsetpos
|
||||
*
|
||||
* Description:
|
||||
* fsetpos() function is an alternate interfaces equivalent to fseek()
|
||||
* (with whence set to SEEK_SET). It sets the current value of the file
|
||||
* offset to value in the location referenced by pos. On some non-UNIX
|
||||
* systems an fpos_t object may be a complex object and fsetpos may be the
|
||||
* only way to portably reposition a stream.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero on succes; -1 on failure with errno set appropriately.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int fsetpos(FAR FILE *stream, FAR fpos_t *pos)
|
||||
{
|
||||
#if CONFIG_DEBUG
|
||||
if (!stream || !pos)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
return ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
return fseek(stream, (FAR off_t)*pos, SEEK_SET);
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_ftell.c
|
||||
*
|
||||
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ftell
|
||||
*
|
||||
* Description:
|
||||
* ftell() returns the current value of the file position indicator for the
|
||||
* stream pointed to by stream.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero on succes; -1 on failure with errno set appropriately.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
long ftell(FAR FILE *stream)
|
||||
{
|
||||
off_t position;
|
||||
|
||||
/* 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
|
||||
*/
|
||||
|
||||
position = lseek(stream->fs_filedes, 0, SEEK_CUR);
|
||||
if (position != (off_t)-1)
|
||||
{
|
||||
return (long)position;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_fwrite.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fwrite
|
||||
****************************************************************************/
|
||||
|
||||
size_t fwrite(FAR const void *ptr, size_t size, size_t n_items, FAR FILE *stream)
|
||||
{
|
||||
size_t full_size = n_items * (size_t)size;
|
||||
ssize_t bytes_written;
|
||||
size_t items_written = 0;
|
||||
|
||||
/* Write the data into the stream buffer */
|
||||
|
||||
bytes_written = lib_fwrite(ptr, full_size, stream);
|
||||
if (bytes_written > 0)
|
||||
{
|
||||
/* Return the number of full items written */
|
||||
|
||||
items_written = bytes_written / size;
|
||||
}
|
||||
return items_written;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_gets.c
|
||||
*
|
||||
* Copyright (C) 2007-2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: gets
|
||||
*
|
||||
* Description:
|
||||
* gets() reads a line from stdin into the buffer pointed to by s until
|
||||
* either a terminating newline or EOF, which it replaces with '\0'. No
|
||||
* check for buffer overrun is performed
|
||||
*
|
||||
* This API should not be used because it is inherently unsafe. Consider
|
||||
* using fgets which is safer and slightly more efficient.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
FAR char *gets(FAR char *s)
|
||||
{
|
||||
/* gets is ALMOST the same as fgets using stdin and no length limit
|
||||
* (hence, the unsafeness of gets). So let fgets do most of the work.
|
||||
*/
|
||||
|
||||
FAR char *ret = fgets(s, INT_MAX, stdin);
|
||||
if (ret)
|
||||
{
|
||||
/* Another subtle difference from fgets is that gets replaces
|
||||
* end-of-line markers with null terminators. We will do that as
|
||||
* a second step (with some loss in performance).
|
||||
*/
|
||||
|
||||
int len = strlen(ret);
|
||||
if (len > 0 && ret[len-1] == '\n')
|
||||
{
|
||||
ret[len-1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
/****************************************************************************
|
||||
* libc/unistd/lib_libdtoa.c
|
||||
*
|
||||
* This file was ported to NuttX by Yolande Cates.
|
||||
*
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define MAX_PREC 16
|
||||
|
||||
#ifndef MIN
|
||||
# define MIN(a,b) (a < b ? a : b)
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
# define MAX(a,b) (a > b ? a : b)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: zeroes
|
||||
*
|
||||
* Description:
|
||||
* Print the specified number of zeres
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void zeroes(FAR struct lib_outstream_s *obj, int nzeroes)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = nzeroes; i > 0; i--)
|
||||
{
|
||||
obj->put(obj, '0');
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_dtoa
|
||||
*
|
||||
* Description:
|
||||
* This is part of lib_vsprintf(). It handles the floating point formats.
|
||||
* This version supports only the %f (with precision). If no precision
|
||||
* was provided in the format, this will use precision == 0 which is
|
||||
* probably not what you want.
|
||||
*
|
||||
* Input Parameters:
|
||||
* obj - The output stream object
|
||||
* fmt - The format character. Not used 'f' is always assumed
|
||||
* prec - The number of digits to the right of the decimal point. If no
|
||||
* precision is provided in the format, this will be zero. And,
|
||||
* unfortunately in this case, it will be treated literally as
|
||||
* a precision of zero.
|
||||
* flags - Only ALTFORM and SHOWPLUS flags are supported. ALTFORM only
|
||||
* applies if prec == 0 which is not supported anyway.
|
||||
* value - The floating point value to convert.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void lib_dtoa(FAR struct lib_outstream_s *obj, int fmt, int prec,
|
||||
uint8_t flags, double value)
|
||||
{
|
||||
FAR char *digits; /* String returned by __dtoa */
|
||||
FAR char *digalloc; /* Copy of digits to be freed after usage */
|
||||
FAR char *rve; /* Points to the end of the return value */
|
||||
int expt; /* Integer value of exponent */
|
||||
int numlen; /* Actual number of digits returned by cvt */
|
||||
int nchars; /* Number of characters to print */
|
||||
int dsgn; /* Unused sign indicator */
|
||||
int i;
|
||||
|
||||
/* Non-zero... positive or negative */
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
value = -value;
|
||||
SET_NEGATE(flags);
|
||||
}
|
||||
|
||||
/* Perform the conversion */
|
||||
|
||||
digits = __dtoa(value, 3, prec, &expt, &dsgn, &rve);
|
||||
digalloc = digits;
|
||||
numlen = rve - digits;
|
||||
|
||||
if (IS_NEGATE(flags))
|
||||
{
|
||||
obj->put(obj, '-');
|
||||
}
|
||||
else if (IS_SHOWPLUS(flags))
|
||||
{
|
||||
obj->put(obj, '+');
|
||||
}
|
||||
|
||||
/* Special case exact zero or the case where the number is smaller than
|
||||
* the print precision.
|
||||
*/
|
||||
|
||||
if (value == 0 || expt < -prec)
|
||||
{
|
||||
/* kludge for __dtoa irregularity */
|
||||
|
||||
obj->put(obj, '0');
|
||||
|
||||
/* A decimal point is printed only in the alternate form or if a
|
||||
* particular precision is requested.
|
||||
*/
|
||||
|
||||
if (prec > 0 || IS_ALTFORM(flags))
|
||||
{
|
||||
obj->put(obj, '.');
|
||||
|
||||
/* Always print at least one digit to the right of the decimal point. */
|
||||
|
||||
prec = MAX(1, prec);
|
||||
}
|
||||
}
|
||||
|
||||
/* A non-zero value will be printed */
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
/* Handle the case where the value is less than 1.0 (in magnitude) and
|
||||
* will need a leading zero.
|
||||
*/
|
||||
|
||||
if (expt <= 0)
|
||||
{
|
||||
/* Print a single zero to the left of the decimal point */
|
||||
|
||||
obj->put(obj, '0');
|
||||
|
||||
/* Print the decimal point */
|
||||
|
||||
obj->put(obj, '.');
|
||||
|
||||
/* Print any leading zeros to the right of the decimal point */
|
||||
|
||||
if (expt < 0)
|
||||
{
|
||||
nchars = MIN(-expt, prec);
|
||||
zeroes(obj, nchars);
|
||||
prec -= nchars;
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle the general case where the value is greater than 1.0 (in
|
||||
* magnitude).
|
||||
*/
|
||||
|
||||
else
|
||||
{
|
||||
/* Print the integer part to the left of the decimal point */
|
||||
|
||||
for (i = expt; i > 0; i--)
|
||||
{
|
||||
if (*digits != '\0')
|
||||
{
|
||||
obj->put(obj, *digits);
|
||||
digits++;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj->put(obj, '0');
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the fractional part */
|
||||
|
||||
numlen -= expt;
|
||||
|
||||
/* If there is no fractional part, then a decimal point is printed
|
||||
* only in the alternate form or if a particular precision is
|
||||
* requested.
|
||||
*/
|
||||
|
||||
if (numlen > 0 || prec > 0 || IS_ALTFORM(flags))
|
||||
{
|
||||
/* Print the decimal point */
|
||||
|
||||
obj->put(obj, '.');
|
||||
|
||||
/* Always print at least one digit to the right of the decimal
|
||||
* point.
|
||||
*/
|
||||
|
||||
prec = MAX(1, prec);
|
||||
}
|
||||
}
|
||||
|
||||
/* If a precision was specified, then limit the number digits to the
|
||||
* right of the decimal point.
|
||||
*/
|
||||
|
||||
if (prec > 0)
|
||||
{
|
||||
nchars = MIN(numlen, prec);
|
||||
}
|
||||
else
|
||||
{
|
||||
nchars = numlen;
|
||||
}
|
||||
|
||||
/* Print the fractional part to the right of the decimal point */
|
||||
|
||||
for (i = nchars; i > 0; i--)
|
||||
{
|
||||
obj->put(obj, *digits);
|
||||
digits++;
|
||||
}
|
||||
|
||||
/* Decremnt to get the number of trailing zeroes to print */
|
||||
|
||||
prec -= nchars;
|
||||
}
|
||||
|
||||
/* Finally, print any trailing zeroes */
|
||||
|
||||
zeroes(obj, prec);
|
||||
|
||||
/* Is this memory supposed to be freed or not? */
|
||||
|
||||
#if 0
|
||||
if (digalloc)
|
||||
{
|
||||
free(digalloc);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_libfflush.c
|
||||
*
|
||||
* Copyright (C) 2007-2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* 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 "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_fflush
|
||||
*
|
||||
* Description:
|
||||
* The function lib_fflush() forces a write of all user-space buffered data for
|
||||
* the given output or update stream via the stream's underlying write
|
||||
* function. The open status of the stream is unaffected.
|
||||
*
|
||||
* Parmeters:
|
||||
* stream - the stream to flush
|
||||
* bforce - flush must be complete.
|
||||
*
|
||||
* Return:
|
||||
* A negated errno value on failure, otherwise the number of bytes remaining
|
||||
* in the buffer.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t lib_fflush(FAR FILE *stream, bool bforce)
|
||||
{
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
FAR const unsigned char *src;
|
||||
ssize_t bytes_written;
|
||||
ssize_t nbuffer;
|
||||
|
||||
/* Return EBADF if the file is not opened for writing */
|
||||
|
||||
if (stream->fs_filedes < 0 || (stream->fs_oflags & O_WROK) == 0)
|
||||
{
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
/* Make sure that we have exclusive access to the stream */
|
||||
|
||||
lib_take_semaphore(stream);
|
||||
|
||||
/* Make sure that the buffer holds valid data */
|
||||
|
||||
if (stream->fs_bufpos != stream->fs_bufstart)
|
||||
{
|
||||
/* Make sure that the buffer holds buffered write data. We do not
|
||||
* support concurrent read/write buffer usage.
|
||||
*/
|
||||
|
||||
if (stream->fs_bufread != stream->fs_bufstart)
|
||||
{
|
||||
/* The buffer holds read data... just return zero meaning "no bytes
|
||||
* remaining in the buffer."
|
||||
*/
|
||||
|
||||
lib_give_semaphore(stream);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* How many bytes of write data are used in the buffer now */
|
||||
|
||||
nbuffer = stream->fs_bufpos - stream->fs_bufstart;
|
||||
|
||||
/* Try to write that amount */
|
||||
|
||||
src = stream->fs_bufstart;
|
||||
do
|
||||
{
|
||||
/* Perform the write */
|
||||
|
||||
bytes_written = write(stream->fs_filedes, src, nbuffer);
|
||||
if (bytes_written < 0)
|
||||
{
|
||||
/* Write failed. The cause of the failure is in 'errno'.
|
||||
* returned the negated errno value.
|
||||
*/
|
||||
|
||||
lib_give_semaphore(stream);
|
||||
return -get_errno();
|
||||
}
|
||||
|
||||
/* Handle partial writes. fflush() must either return with
|
||||
* an error condition or with the data successfully flushed
|
||||
* from the buffer.
|
||||
*/
|
||||
|
||||
src += bytes_written;
|
||||
nbuffer -= bytes_written;
|
||||
}
|
||||
while (bforce && nbuffer > 0);
|
||||
|
||||
/* Reset the buffer position to the beginning of the buffer */
|
||||
|
||||
stream->fs_bufpos = stream->fs_bufstart;
|
||||
|
||||
/* For the case of an incomplete write, nbuffer will be non-zero
|
||||
* It will hold the number of bytes that were not written.
|
||||
* Move the data down in the buffer to handle this (rare) case
|
||||
*/
|
||||
|
||||
while (nbuffer)
|
||||
{
|
||||
*stream->fs_bufpos++ = *src++;
|
||||
--nbuffer;
|
||||
}
|
||||
}
|
||||
|
||||
/* Restore normal access to the stream and return the number of bytes
|
||||
* remaining in the buffer.
|
||||
*/
|
||||
|
||||
lib_give_semaphore(stream);
|
||||
return stream->fs_bufpos - stream->fs_bufstart;
|
||||
#else
|
||||
/* Return no bytes remaining in the buffer */
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_libflushall.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_flushall
|
||||
*
|
||||
* Description:
|
||||
* Called either (1) by the OS when a task exits, or (2) from fflush()
|
||||
* when a NULL stream argument is provided.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lib_flushall(FAR struct streamlist *list)
|
||||
{
|
||||
int lasterrno = OK;
|
||||
int ret;
|
||||
|
||||
/* Make sure that there are streams associated with this thread */
|
||||
|
||||
if (list)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Process each stream in the thread's stream list */
|
||||
|
||||
stream_semtake(list);
|
||||
for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
|
||||
{
|
||||
FILE *stream = &list->sl_streams[i];
|
||||
|
||||
/* If the stream is open (i.e., assigned a non-negative file
|
||||
* descriptor) and opened for writing, then flush all of the pending
|
||||
* write data in the stream.
|
||||
*/
|
||||
|
||||
if (stream->fs_filedes >= 0 && (stream->fs_oflags & O_WROK) != 0)
|
||||
{
|
||||
/* Flush the writable FILE */
|
||||
|
||||
ret = lib_fflush(stream, true);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* An error occurred during the flush AND/OR we were unable
|
||||
* to flush all of the buffered write data. Remember the
|
||||
* last errcode.
|
||||
*/
|
||||
|
||||
lasterrno = ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stream_semgive(list);
|
||||
}
|
||||
|
||||
/* If any flush failed, return the errorcode of the last failed flush */
|
||||
|
||||
return lasterrno;
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_libfread.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_fread
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
|
||||
{
|
||||
unsigned char *dest = (unsigned char*)ptr;
|
||||
ssize_t bytes_read;
|
||||
int ret;
|
||||
|
||||
/* 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
|
||||
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
ret = lib_wrflush(stream);
|
||||
if (ret < 0)
|
||||
{
|
||||
lib_give_semaphore(stream);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Now get any other needed chars from the buffer or the file. */
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
/* Is there readable data in the buffer? */
|
||||
|
||||
while ((count > 0) && (stream->fs_bufpos < stream->fs_bufread))
|
||||
{
|
||||
/* Yes, copy a byte into the user buffer */
|
||||
|
||||
*dest++ = *stream->fs_bufpos++;
|
||||
count--;
|
||||
}
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
size_t buffer_available;
|
||||
|
||||
/* We need to read more data into the buffer from the file */
|
||||
|
||||
/* Mark the buffer empty */
|
||||
|
||||
stream->fs_bufpos = stream->fs_bufread = stream->fs_bufstart;
|
||||
|
||||
/* How much space is available in the buffer? */
|
||||
|
||||
buffer_available = stream->fs_bufend - stream->fs_bufread;
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
if (count > buffer_available)
|
||||
{
|
||||
bytes_read = read(stream->fs_filedes, dest, count);
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
/* An error occurred on the read. The error code is
|
||||
* in the 'errno' variable.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
goto shortread;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Some bytes were read. Adjust the dest pointer */
|
||||
|
||||
dest += bytes_read;
|
||||
|
||||
/* Were all of the requested bytes read? */
|
||||
|
||||
if (bytes_read < count)
|
||||
{
|
||||
/* No. We must be at the end of file. */
|
||||
|
||||
goto shortread;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Yes. We are done. */
|
||||
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
bytes_read = read(stream->fs_filedes, stream->fs_bufread, buffer_available);
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
/* An error occurred on the read. The error code is
|
||||
* in the 'errno' variable.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
goto shortread;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Some bytes were read */
|
||||
|
||||
stream->fs_bufread += bytes_read;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
/* Now get any other needed chars from the file. */
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
bytes_read = read(stream->fs_filedes, dest, count);
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
/* An error occurred on the read. The error code is
|
||||
* in the 'errno' variable.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
dest += bytes_read;
|
||||
count -= bytes_read;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Here after a successful (but perhaps short) read */
|
||||
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
shortread:
|
||||
#endif
|
||||
bytes_read = dest - (unsigned char*)ptr;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
lib_give_semaphore(stream);
|
||||
return bytes_read;
|
||||
|
||||
/* Error exits */
|
||||
|
||||
errout_with_errno:
|
||||
lib_give_semaphore(stream);
|
||||
return -get_errno();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_libfwrite.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_fwrite
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
{
|
||||
FAR const unsigned char *start = ptr;
|
||||
FAR const unsigned char *src = ptr;
|
||||
ssize_t ret = ERROR;
|
||||
unsigned char *dest;
|
||||
|
||||
/* Make sure that writing to this stream is allowed */
|
||||
|
||||
if (!stream || (stream->fs_oflags & O_WROK) == 0)
|
||||
{
|
||||
set_errno(EBADF);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Get exclusive access to the stream */
|
||||
|
||||
lib_take_semaphore(stream);
|
||||
|
||||
/* If the buffer is currently being used for read access, then
|
||||
* discard all of the read-ahead data. We do not support concurrent
|
||||
* buffered read/write access.
|
||||
*/
|
||||
|
||||
if (lib_rdflush(stream) < 0)
|
||||
{
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
|
||||
/* Loop until all of the bytes have been buffered */
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
/* Determine the number of bytes left in the buffer */
|
||||
|
||||
size_t gulp_size = stream->fs_bufend - stream->fs_bufpos;
|
||||
|
||||
/* Will the user data fit into the amount of buffer space
|
||||
* that we have left?
|
||||
*/
|
||||
|
||||
if (gulp_size > count)
|
||||
{
|
||||
/* Yes, clip the gulp to the size of the user data */
|
||||
|
||||
gulp_size = count;
|
||||
}
|
||||
|
||||
/* Adjust the number of bytes remaining to be transferred
|
||||
* on the next pass through the loop (might be zero).
|
||||
*/
|
||||
|
||||
count -= gulp_size;
|
||||
|
||||
/* Transfer the data into the buffer */
|
||||
|
||||
for (dest = stream->fs_bufpos; gulp_size > 0; gulp_size--)
|
||||
{
|
||||
*dest++ = *src++;
|
||||
}
|
||||
stream->fs_bufpos = dest;
|
||||
|
||||
/* Is the buffer full? */
|
||||
|
||||
if (dest >= stream->fs_bufend)
|
||||
{
|
||||
/* Flush the buffered data to the IO stream */
|
||||
|
||||
int bytes_buffered = lib_fflush(stream, false);
|
||||
if (bytes_buffered < 0)
|
||||
{
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the number of bytes written */
|
||||
|
||||
ret = src - start;
|
||||
|
||||
errout_with_semaphore:
|
||||
lib_give_semaphore(stream);
|
||||
|
||||
errout:
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
{
|
||||
return write(stream->fs_filedes, ptr, count);
|
||||
}
|
||||
#endif /* CONFIG_STDIO_BUFFER_SIZE */
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_libnoflush.c
|
||||
*
|
||||
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_noflush
|
||||
*
|
||||
* Description:
|
||||
* lib_noflush() provides a common, dummy flush method for output streams
|
||||
* that are not flushable. Only used if CONFIG_STDIO_LINEBUFFER is selected.
|
||||
*
|
||||
* Return:
|
||||
* Always returns OK
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lib_noflush(FAR struct lib_outstream_s *this)
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_STDIO_LINEBUFFER */
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_libsprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_sprintf
|
||||
****************************************************************************/
|
||||
|
||||
int lib_sprintf(FAR struct lib_outstream_s *obj, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
/* Let lib_vsprintf do the real work */
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = lib_vsprintf(obj, fmt, ap);
|
||||
va_end(ap);
|
||||
return n;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,102 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_lowinstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/arch.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
#ifdef CONFIG_ARCH_LOWGETC
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lowinstream_getc
|
||||
****************************************************************************/
|
||||
|
||||
static int lowinstream_getc(FAR struct lib_instream_s *this)
|
||||
{
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(this);
|
||||
|
||||
/* Get the next character from the incoming stream */
|
||||
|
||||
ret = up_getc(ch)
|
||||
if (ret != EOF)
|
||||
{
|
||||
this->nget++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_lowinstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with low-level, architecture-specific I/O.
|
||||
*
|
||||
* Input parameters:
|
||||
* lowoutstream - User allocated, uninitialized instance of struct
|
||||
* lib_lowoutstream_s to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_lowinstream(FAR struct lib_instream_s *stream)
|
||||
{
|
||||
stream->get = lowinstream_getc;
|
||||
stream->nget = 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ARCH_LOWGETC */
|
||||
@@ -0,0 +1,97 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_lowoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#ifdef CONFIG_ARCH_LOWPUTC
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <nuttx/arch.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lowoutstream_putc
|
||||
****************************************************************************/
|
||||
|
||||
static void lowoutstream_putc(FAR struct lib_outstream_s *this, int ch)
|
||||
{
|
||||
DEBUGASSERT(this);
|
||||
|
||||
if (up_putc(ch) != EOF)
|
||||
{
|
||||
this->nput++;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_lowoutstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with low-level, architecture-specific I/O.
|
||||
*
|
||||
* Input parameters:
|
||||
* lowoutstream - User allocated, uninitialized instance of struct
|
||||
* lib_lowoutstream_s to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_lowoutstream(FAR struct lib_outstream_s *stream)
|
||||
{
|
||||
stream->put = lowoutstream_putc;
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
stream->flush = lib_noflush;
|
||||
#endif
|
||||
stream->nput = 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ARCH_LOWPUTC */
|
||||
@@ -0,0 +1,128 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_lowprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdio.h>
|
||||
#include <debug.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/* This interface can only be used from within the kernel */
|
||||
|
||||
#if !defined(CONFIG_NUTTX_KERNEL) || defined(__KERNEL__)
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_lowvprintf
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_ARCH_LOWPUTC) || defined(CONFIG_SYSLOG)
|
||||
|
||||
int lib_lowvprintf(const char *fmt, va_list ap)
|
||||
{
|
||||
struct lib_outstream_s stream;
|
||||
|
||||
/* Wrap the stdout in a stream object and let lib_vsprintf do the work. */
|
||||
|
||||
#ifdef CONFIG_SYSLOG
|
||||
lib_syslogstream((FAR struct lib_outstream_s *)&stream);
|
||||
#else
|
||||
lib_lowoutstream((FAR struct lib_outstream_s *)&stream);
|
||||
#endif
|
||||
return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_lowprintf
|
||||
****************************************************************************/
|
||||
|
||||
int lib_lowprintf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
#ifdef CONFIG_DEBUG_ENABLE
|
||||
ret = 0;
|
||||
if (g_dbgenable)
|
||||
#endif
|
||||
{
|
||||
va_start(ap, fmt);
|
||||
ret = lib_lowvprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ARCH_LOWPUTC || CONFIG_SYSLOG */
|
||||
#endif /* __KERNEL__ */
|
||||
@@ -0,0 +1,104 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_meminstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: meminstream_getc
|
||||
****************************************************************************/
|
||||
|
||||
static int meminstream_getc(FAR struct lib_instream_s *this)
|
||||
{
|
||||
FAR struct lib_meminstream_s *mthis = (FAR struct lib_meminstream_s *)this;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(this);
|
||||
|
||||
/* Get the next character (if any) from the buffer */
|
||||
|
||||
if (this->nget < mthis->buflen)
|
||||
{
|
||||
ret = mthis->buffer[this->nget];
|
||||
this->nget++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = EOF;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_meminstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with a fixed-size memory buffer.
|
||||
*
|
||||
* Input parameters:
|
||||
* meminstream - User allocated, uninitialized instance of struct
|
||||
* lib_meminstream_s to be initialized.
|
||||
* bufstart - Address of the beginning of the fixed-size memory buffer
|
||||
* buflen - Size of the fixed-sized memory buffer in bytes
|
||||
*
|
||||
* Returned Value:
|
||||
* None (meminstream initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_meminstream(FAR struct lib_meminstream_s *meminstream,
|
||||
FAR const char *bufstart, int buflen)
|
||||
{
|
||||
meminstream->public.get = meminstream_getc;
|
||||
meminstream->public.nget = 0; /* Will be buffer index */
|
||||
meminstream->buffer = bufstart; /* Start of buffer */
|
||||
meminstream->buflen = buflen; /* Length of the buffer */
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_memoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: memoutstream_putc
|
||||
****************************************************************************/
|
||||
|
||||
static void memoutstream_putc(FAR struct lib_outstream_s *this, int ch)
|
||||
{
|
||||
FAR struct lib_memoutstream_s *mthis = (FAR struct lib_memoutstream_s *)this;
|
||||
|
||||
DEBUGASSERT(this);
|
||||
|
||||
/* If this will not overrun the buffer, then write the character to the
|
||||
* buffer. Not that buflen was pre-decremented when the stream was
|
||||
* created so it is okay to write past the end of the buflen by one.
|
||||
*/
|
||||
|
||||
if (this->nput < mthis->buflen)
|
||||
{
|
||||
mthis->buffer[this->nput] = ch;
|
||||
this->nput++;
|
||||
mthis->buffer[this->nput] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_memoutstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with a fixed-size memory buffer.
|
||||
*
|
||||
* Input parameters:
|
||||
* memoutstream - User allocated, uninitialized instance of struct
|
||||
* lib_memoutstream_s to be initialized.
|
||||
* bufstart - Address of the beginning of the fixed-size memory buffer
|
||||
* buflen - Size of the fixed-sized memory buffer in bytes
|
||||
*
|
||||
* Returned Value:
|
||||
* None (memoutstream initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_memoutstream(FAR struct lib_memoutstream_s *memoutstream,
|
||||
FAR char *bufstart, int buflen)
|
||||
{
|
||||
memoutstream->public.put = memoutstream_putc;
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
memoutstream->public.flush = lib_noflush;
|
||||
#endif
|
||||
memoutstream->public.nput = 0; /* Will be buffer index */
|
||||
memoutstream->buffer = bufstart; /* Start of buffer */
|
||||
memoutstream->buflen = buflen - 1; /* Save space for null terminator */
|
||||
memoutstream->buffer[0] = '\0'; /* Start with an empty string */
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_nullinstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static int nullinstream_getc(FAR struct lib_instream_s *this)
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_nullinstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a NULL stream. The initialized stream will will return only
|
||||
* EOF.
|
||||
*
|
||||
* Input parameters:
|
||||
* nullinstream - User allocated, uninitialized instance of struct
|
||||
* lib_instream_s to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_nullinstream(FAR struct lib_instream_s *nullinstream)
|
||||
{
|
||||
nullinstream->get = nullinstream_getc;
|
||||
nullinstream->nget = 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_nulloutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static void nulloutstream_putc(FAR struct lib_outstream_s *this, int ch)
|
||||
{
|
||||
DEBUGASSERT(this);
|
||||
this->nput++;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_nulloutstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a NULL streams. The initialized stream will write all data
|
||||
* to the bit-bucket.
|
||||
*
|
||||
* Input parameters:
|
||||
* nulloutstream - User allocated, uninitialized instance of struct
|
||||
* lib_outstream_s to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream)
|
||||
{
|
||||
nulloutstream->put = nulloutstream_putc;
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
nulloutstream->flush = lib_noflush;
|
||||
#endif
|
||||
nulloutstream->nput = 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_perror.c
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* POSIX requires that perror provide its output on stderr. This option may
|
||||
* be defined, however, to provide perror output that is serialized with
|
||||
* other stdout messages.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_LIBC_PERROR_STDOUT
|
||||
# define PERROR_STREAM stdout
|
||||
#else
|
||||
# define PERROR_STREAM stderr
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: perror
|
||||
****************************************************************************/
|
||||
|
||||
void perror(FAR const char *s)
|
||||
{
|
||||
|
||||
/* If strerror() is not enabled, then just print the error number */
|
||||
|
||||
#ifdef CONFIG_LIBC_STRERROR
|
||||
(void)fprintf(PERROR_STREAM, "%s: %s\n", s, strerror(errno));
|
||||
#else
|
||||
(void)fprintf(PERROR_STREAM, "%s: Error %d\n", s, errno);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_printf.c
|
||||
*
|
||||
* Copyright (C) 2007-2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Global Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: printf
|
||||
**************************************************************************/
|
||||
|
||||
int printf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
ret = vfprintf(stdout, fmt, ap);
|
||||
#elif CONFIG_NFILE_DESCRIPTORS > 0
|
||||
ret = lib_rawvprintf(fmt, ap);
|
||||
#elif defined(CONFIG_ARCH_LOWPUTC)
|
||||
ret = lib_lowvprintf(fmt, ap);
|
||||
#else
|
||||
# ifdef CONFIG_CPP_HAVE_WARNING
|
||||
# warning "printf has no data sink"
|
||||
# endif
|
||||
ret = 0;
|
||||
#endif
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_puts.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: puts
|
||||
*
|
||||
* Description:
|
||||
* puts() writes the string s and a trailing newline to stdout.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int puts(FAR const char *s)
|
||||
{
|
||||
FILE *stream = stdout;
|
||||
int nwritten;
|
||||
int nput = EOF;
|
||||
int ret;
|
||||
|
||||
/* Write the string (the next two steps must be atomic) */
|
||||
|
||||
lib_take_semaphore(stream);
|
||||
|
||||
/* Write the string without its trailing '\0' */
|
||||
|
||||
nwritten = fputs(s, stream);
|
||||
if (nwritten > 0)
|
||||
{
|
||||
/* Followed by a newline */
|
||||
|
||||
char newline = '\n';
|
||||
ret = lib_fwrite(&newline, 1, stream);
|
||||
if (ret > 0)
|
||||
{
|
||||
nput = nwritten + 1;
|
||||
|
||||
/* Flush the buffer after the newline is output. */
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
ret = lib_fflush(stream, true);
|
||||
if (ret < 0)
|
||||
{
|
||||
nput = EOF;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
lib_give_semaphore(stdout);
|
||||
return nput;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_rawinstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: rawinstream_getc
|
||||
****************************************************************************/
|
||||
|
||||
static int rawinstream_getc(FAR struct lib_instream_s *this)
|
||||
{
|
||||
FAR struct lib_rawinstream_s *rthis = (FAR struct lib_rawinstream_s *)this;
|
||||
int nwritten;
|
||||
char ch;
|
||||
|
||||
DEBUGASSERT(this && rthis->fd >= 0);
|
||||
|
||||
/* Attempt to read one character */
|
||||
|
||||
nwritten = read(rthis->fd, &ch, 1);
|
||||
if (nwritten == 1)
|
||||
{
|
||||
this->nget++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
/* Return EOF on any failure to read from the incoming byte stream. The
|
||||
* only expected error is EINTR meaning that the read was interrupted
|
||||
* by a signal. A Zero return value would indicated an end-of-file
|
||||
* confition.
|
||||
*/
|
||||
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_rawinstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with a file descriptor.
|
||||
*
|
||||
* Input parameters:
|
||||
* rawinstream - User allocated, uninitialized instance of struct
|
||||
* lib_rawinstream_s to be initialized.
|
||||
* fd - User provided file/socket descriptor (must have been opened
|
||||
* for the correct access).
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_rawinstream(FAR struct lib_rawinstream_s *rawinstream, int fd)
|
||||
{
|
||||
rawinstream->public.get = rawinstream_getc;
|
||||
rawinstream->public.nget = 0;
|
||||
rawinstream->fd = fd;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_rawoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: rawoutstream_putc
|
||||
****************************************************************************/
|
||||
|
||||
static void rawoutstream_putc(FAR struct lib_outstream_s *this, int ch)
|
||||
{
|
||||
FAR struct lib_rawoutstream_s *rthis = (FAR struct lib_rawoutstream_s *)this;
|
||||
int nwritten;
|
||||
char buffer = ch;
|
||||
|
||||
DEBUGASSERT(this && rthis->fd >= 0);
|
||||
|
||||
/* Loop until the character is successfully transferred or until an
|
||||
* irrecoverable error occurs.
|
||||
*/
|
||||
|
||||
do
|
||||
{
|
||||
nwritten = write(rthis->fd, &buffer, 1);
|
||||
if (nwritten == 1)
|
||||
{
|
||||
this->nput++;
|
||||
return;
|
||||
}
|
||||
|
||||
/* The only expected error is EINTR, meaning that the write operation
|
||||
* was awakened by a signal. Zero would not be a valid return value
|
||||
* from write().
|
||||
*/
|
||||
|
||||
DEBUGASSERT(nwritten < 0);
|
||||
}
|
||||
while (get_errno() == EINTR);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_rawoutstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with a file descriptor.
|
||||
*
|
||||
* Input parameters:
|
||||
* rawoutstream - User allocated, uninitialized instance of struct
|
||||
* lib_rawoutstream_s to be initialized.
|
||||
* fd - User provided file/socket descriptor (must have been opened
|
||||
* for write access).
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_rawoutstream(FAR struct lib_rawoutstream_s *rawoutstream, int fd)
|
||||
{
|
||||
rawoutstream->public.put = rawoutstream_putc;
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
rawoutstream->public.flush = lib_noflush;
|
||||
#endif
|
||||
rawoutstream->public.nput = 0;
|
||||
rawoutstream->fd = fd;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_rawprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <debug.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* Some output destinations are only available from within the kernel */
|
||||
|
||||
#if defined(CONFIG_NUTTX_KERNEL) && !defined(__KERNEL__)
|
||||
# undef CONFIG_SYSLOG
|
||||
# undef CONFIG_ARCH_LOWPUTC
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_rawvprintf
|
||||
****************************************************************************/
|
||||
|
||||
int lib_rawvprintf(const char *fmt, va_list ap)
|
||||
{
|
||||
#if defined(CONFIG_SYSLOG)
|
||||
|
||||
struct lib_outstream_s stream;
|
||||
|
||||
/* Wrap the low-level output in a stream object and let lib_vsprintf
|
||||
* do the work.
|
||||
*/
|
||||
|
||||
lib_syslogstream((FAR struct lib_outstream_s *)&stream);
|
||||
return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap);
|
||||
|
||||
#elif CONFIG_NFILE_DESCRIPTORS > 0
|
||||
|
||||
struct lib_rawoutstream_s rawoutstream;
|
||||
|
||||
/* Wrap the stdout in a stream object and let lib_vsprintf
|
||||
* do the work.
|
||||
*/
|
||||
|
||||
lib_rawoutstream(&rawoutstream, 1);
|
||||
return lib_vsprintf(&rawoutstream.public, fmt, ap);
|
||||
|
||||
#elif defined(CONFIG_ARCH_LOWPUTC)
|
||||
|
||||
struct lib_outstream_s stream;
|
||||
|
||||
/* Wrap the low-level output in a stream object and let lib_vsprintf
|
||||
* do the work.
|
||||
*/
|
||||
|
||||
lib_lowoutstream((FAR struct lib_outstream_s *)&stream);
|
||||
return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap);
|
||||
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_rawprintf
|
||||
****************************************************************************/
|
||||
|
||||
int lib_rawprintf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
#ifdef CONFIG_DEBUG_ENABLE
|
||||
ret = 0;
|
||||
if (g_dbgenable)
|
||||
#endif
|
||||
{
|
||||
va_start(ap, fmt);
|
||||
ret = lib_rawvprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_rdflush.c
|
||||
*
|
||||
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_rdflush
|
||||
*
|
||||
* Description:
|
||||
* Flush read data from the I/O buffer and adjust the file pointer to
|
||||
* account for the unread data
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
int lib_rdflush(FAR FILE *stream)
|
||||
{
|
||||
if (!stream)
|
||||
{
|
||||
set_errno(EBADF);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Get exclusive access to the stream */
|
||||
|
||||
lib_take_semaphore(stream);
|
||||
|
||||
/* If the buffer is currently being used for read access, then discard all
|
||||
* of the read-ahead data. We do not support concurrent buffered read/write
|
||||
* access.
|
||||
*/
|
||||
|
||||
if (stream->fs_bufread != stream->fs_bufstart)
|
||||
{
|
||||
/* Now adjust the stream pointer to account for the read-ahead data that
|
||||
* was not actually read by the user.
|
||||
*/
|
||||
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
off_t rdoffset = stream->fs_bufread - stream->fs_bufpos + stream->fs_nungotten;
|
||||
#else
|
||||
off_t rdoffset = stream->fs_bufread - stream->fs_bufpos;
|
||||
#endif
|
||||
/* Mark the buffer as empty (do this before calling fseek() because fseek()
|
||||
* also calls this function).
|
||||
*/
|
||||
|
||||
stream->fs_bufpos = stream->fs_bufread = stream->fs_bufstart;
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
stream->fs_nungotten = 0;
|
||||
#endif
|
||||
/* Then seek to the position corresponding to the last data read by the user */
|
||||
|
||||
if (fseek(stream, -rdoffset, SEEK_CUR) < 0)
|
||||
{
|
||||
lib_give_semaphore(stream);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
lib_give_semaphore(stream);
|
||||
return OK;
|
||||
}
|
||||
#endif /* CONFIG_STDIO_BUFFER_SIZE */
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_snprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* sprintf
|
||||
****************************************************************************/
|
||||
|
||||
int snprintf(FAR char *buf, size_t size, const char *format, ...)
|
||||
{
|
||||
struct lib_memoutstream_s memoutstream;
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
/* Initialize a memory stream to write to the buffer */
|
||||
|
||||
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, buf, size);
|
||||
|
||||
/* Then let lib_vsprintf do the real work */
|
||||
|
||||
va_start(ap, format);
|
||||
n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, format, ap);
|
||||
va_end(ap);
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_sprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* sprintf
|
||||
****************************************************************************/
|
||||
|
||||
int sprintf (FAR char *buf, const char *fmt, ...)
|
||||
{
|
||||
struct lib_memoutstream_s memoutstream;
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
/* Initialize a memory stream to write to the buffer */
|
||||
|
||||
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, buf, LIB_BUFLEN_UNKNOWN);
|
||||
|
||||
/* Then let lib_vsprintf do the real work */
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, fmt, ap);
|
||||
va_end(ap);
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,507 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_sscanf.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <debug.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define MAXLN 128
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
int vsscanf(char *buf, const char *fmt, va_list ap);
|
||||
|
||||
/**************************************************************************
|
||||
* Global Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
static const char spaces[] = " \t\n\r\f\v";
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: findwidth
|
||||
*
|
||||
* Description:
|
||||
* Try to figure out the width of the input data.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int findwidth(FAR const char *buf, FAR const char *fmt)
|
||||
{
|
||||
FAR const char *next = fmt + 1;
|
||||
|
||||
/* No... is there a space after the format? Or does the format string end
|
||||
* here?
|
||||
*/
|
||||
|
||||
if (isspace(*next) || *next == 0)
|
||||
{
|
||||
/* Use the input up until the first white space is encountered. */
|
||||
|
||||
return strcspn(buf, spaces);
|
||||
}
|
||||
|
||||
/* No.. Another possibility is the the format character is followed by
|
||||
* some recognizable delimiting value.
|
||||
*/
|
||||
|
||||
if (*next != '%')
|
||||
{
|
||||
/* If so we will say that the string ends there if we can find that
|
||||
* delimiter in the input string.
|
||||
*/
|
||||
|
||||
FAR const char *ptr = strchr(buf, *next);
|
||||
if (ptr)
|
||||
{
|
||||
return (int)(ptr - buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* No... the format has not delimiter and is back-to-back with the next
|
||||
* formats (or no is following by a delimiter that does not exist in the
|
||||
* input string). At this point we just bail and Use the input up until
|
||||
* the first white space is encountered.
|
||||
*
|
||||
* NOTE: This means that values from the following format may be
|
||||
* concatenated with the first. This is a bug. We have no generic way of
|
||||
* determining the width of the data if there is no fieldwith, no space
|
||||
* separating the input, and no usable delimiter character.
|
||||
*/
|
||||
|
||||
return strcspn(buf, spaces);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: sscanf
|
||||
*
|
||||
* Description:
|
||||
* ANSI standard sscanf implementation.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int sscanf(FAR const char *buf, FAR const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int count;
|
||||
|
||||
va_start(ap, fmt);
|
||||
count = vsscanf((FAR char*)buf, fmt, ap);
|
||||
va_end(ap);
|
||||
return count;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: vsscanf
|
||||
*
|
||||
* Description:
|
||||
* ANSI standard vsscanf implementation.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap)
|
||||
{
|
||||
FAR char *bufstart;
|
||||
FAR char *tv;
|
||||
FAR const char *tc;
|
||||
bool lflag;
|
||||
bool noassign;
|
||||
int count;
|
||||
int width;
|
||||
int base = 10;
|
||||
char tmp[MAXLN];
|
||||
|
||||
lvdbg("vsscanf: buf=\"%s\" fmt=\"%s\"\n", buf, fmt);
|
||||
|
||||
/* Remember the start of the input buffer. We will need this for %n
|
||||
* calculations.
|
||||
*/
|
||||
|
||||
bufstart = buf;
|
||||
|
||||
/* Parse the format, extracting values from the input buffer as needed */
|
||||
|
||||
count = 0;
|
||||
width = 0;
|
||||
noassign = false;
|
||||
lflag = false;
|
||||
|
||||
while (*fmt && *buf)
|
||||
{
|
||||
/* Skip over white space */
|
||||
|
||||
while (isspace(*fmt))
|
||||
{
|
||||
fmt++;
|
||||
}
|
||||
|
||||
/* Check for a conversion specifier */
|
||||
|
||||
if (*fmt == '%')
|
||||
{
|
||||
lvdbg("vsscanf: Specifier found\n");
|
||||
|
||||
/* Check for qualifiers on the conversion specifier */
|
||||
fmt++;
|
||||
for (; *fmt; fmt++)
|
||||
{
|
||||
lvdbg("vsscanf: Processing %c\n", *fmt);
|
||||
|
||||
if (strchr("dibouxcsefgn%", *fmt))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (*fmt == '*')
|
||||
{
|
||||
noassign = true;
|
||||
}
|
||||
else if (*fmt == 'l' || *fmt == 'L')
|
||||
{
|
||||
/* NOTE: Missing check for long long ('ll') */
|
||||
|
||||
lflag = true;
|
||||
}
|
||||
else if (*fmt >= '1' && *fmt <= '9')
|
||||
{
|
||||
for (tc = fmt; isdigit(*fmt); fmt++);
|
||||
strncpy(tmp, tc, fmt - tc);
|
||||
tmp[fmt - tc] = '\0';
|
||||
width = atoi(tmp);
|
||||
fmt--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Process %s: String conversion */
|
||||
|
||||
if (*fmt == 's')
|
||||
{
|
||||
lvdbg("vsscanf: Performing string conversion\n");
|
||||
|
||||
while (isspace(*buf))
|
||||
{
|
||||
buf++;
|
||||
}
|
||||
|
||||
/* Was a fieldwidth specified? */
|
||||
|
||||
if (!width)
|
||||
{
|
||||
/* No... Guess a field width using some heuristics */
|
||||
|
||||
width = findwidth(buf, fmt);
|
||||
}
|
||||
|
||||
if (!noassign)
|
||||
{
|
||||
tv = va_arg(ap, char*);
|
||||
strncpy(tv, buf, width);
|
||||
tv[width] = '\0';
|
||||
}
|
||||
|
||||
buf += width;
|
||||
}
|
||||
|
||||
/* Process %c: Character conversion */
|
||||
|
||||
else if (*fmt == 'c')
|
||||
{
|
||||
lvdbg("vsscanf: Performing character conversion\n");
|
||||
|
||||
/* Was a fieldwidth specified? */
|
||||
|
||||
if (!width)
|
||||
{
|
||||
/* No, then width is this one single character */
|
||||
|
||||
width = 1;
|
||||
}
|
||||
|
||||
if (!noassign)
|
||||
{
|
||||
tv = va_arg(ap, char*);
|
||||
strncpy(tv, buf, width);
|
||||
tv[width] = '\0';
|
||||
}
|
||||
|
||||
buf += width;
|
||||
}
|
||||
|
||||
/* Process %d, %o, %b, %x, %u: Various integer conversions */
|
||||
|
||||
else if (strchr("dobxu", *fmt))
|
||||
{
|
||||
lvdbg("vsscanf: Performing integer conversion\n");
|
||||
|
||||
/* Skip over any white space before the integer string */
|
||||
|
||||
while (isspace(*buf))
|
||||
{
|
||||
buf++;
|
||||
}
|
||||
|
||||
/* The base of the integer conversion depends on the specific
|
||||
* conversion specification.
|
||||
*/
|
||||
|
||||
if (*fmt == 'd' || *fmt == 'u')
|
||||
{
|
||||
base = 10;
|
||||
}
|
||||
else if (*fmt == 'x')
|
||||
{
|
||||
base = 16;
|
||||
}
|
||||
else if (*fmt == 'o')
|
||||
{
|
||||
base = 8;
|
||||
}
|
||||
else if (*fmt == 'b')
|
||||
{
|
||||
base = 2;
|
||||
}
|
||||
|
||||
/* Was a fieldwidth specified? */
|
||||
|
||||
if (!width)
|
||||
{
|
||||
/* No... Guess a field width using some heuristics */
|
||||
|
||||
width = findwidth(buf, fmt);
|
||||
}
|
||||
|
||||
/* Copy the numeric string into a temporary working buffer. */
|
||||
|
||||
strncpy(tmp, buf, width);
|
||||
tmp[width] = '\0';
|
||||
|
||||
lvdbg("vsscanf: tmp[]=\"%s\"\n", tmp);
|
||||
|
||||
/* Perform the integer conversion */
|
||||
|
||||
buf += width;
|
||||
if (!noassign)
|
||||
{
|
||||
#ifdef SDCC
|
||||
char *endptr;
|
||||
long tmplong = strtol(tmp, &endptr, base);
|
||||
#else
|
||||
long tmplong = strtol(tmp, NULL, base);
|
||||
#endif
|
||||
if (lflag)
|
||||
{
|
||||
long *plong = va_arg(ap, long*);
|
||||
lvdbg("vsscanf: Return %ld to 0x%p\n", tmplong, plong);
|
||||
*plong = tmplong;
|
||||
}
|
||||
else
|
||||
{
|
||||
int *pint = va_arg(ap, int*);
|
||||
lvdbg("vsscanf: Return %ld to 0x%p\n", tmplong, pint);
|
||||
*pint = (int)tmplong;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Process %f: Floating point conversion */
|
||||
|
||||
else if (*fmt == 'f')
|
||||
{
|
||||
#ifndef CONFIG_LIBC_FLOATINGPOINT
|
||||
/* No floating point conversions */
|
||||
|
||||
void *pv = va_arg(ap, void*);
|
||||
|
||||
lvdbg("vsscanf: Return 0.0 to %p\n", pv);
|
||||
*((double_t*)pv) = 0.0;
|
||||
#else
|
||||
lvdbg("vsscanf: Performing floating point conversion\n");
|
||||
|
||||
/* Skip over any white space before the real string */
|
||||
|
||||
while (isspace(*buf))
|
||||
{
|
||||
buf++;
|
||||
}
|
||||
|
||||
/* Was a fieldwidth specified? */
|
||||
|
||||
if (!width)
|
||||
{
|
||||
/* No... Guess a field width using some heuristics */
|
||||
|
||||
width = findwidth(buf, fmt);
|
||||
}
|
||||
|
||||
/* Copy the real string into a temporary working buffer. */
|
||||
|
||||
strncpy(tmp, buf, width);
|
||||
tmp[width] = '\0';
|
||||
buf += width;
|
||||
|
||||
lvdbg("vsscanf: tmp[]=\"%s\"\n", tmp);
|
||||
|
||||
/* Perform the floating point conversion */
|
||||
|
||||
if (!noassign)
|
||||
{
|
||||
/* strtod always returns a double */
|
||||
#ifdef SDCC
|
||||
char *endptr;
|
||||
double_t dvalue = strtod(tmp,&endptr);
|
||||
#else
|
||||
double_t dvalue = strtod(tmp, NULL);
|
||||
#endif
|
||||
void *pv = va_arg(ap, void*);
|
||||
|
||||
lvdbg("vsscanf: Return %f to %p\n", dvalue, pv);
|
||||
|
||||
/* But we have to check whether we need to return a
|
||||
* float or a double.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_HAVE_DOUBLE
|
||||
if (lflag)
|
||||
{
|
||||
*((double_t*)pv) = dvalue;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
*((float*)pv) = (float)dvalue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Process %n: Character count */
|
||||
|
||||
else if (*fmt == 'n')
|
||||
{
|
||||
lvdbg("vsscanf: Performing character count\n");
|
||||
|
||||
if (!noassign)
|
||||
{
|
||||
size_t nchars = (size_t)(buf - bufstart);
|
||||
|
||||
if (lflag)
|
||||
{
|
||||
long *plong = va_arg(ap, long*);
|
||||
*plong = (long)nchars;
|
||||
}
|
||||
else
|
||||
{
|
||||
int *pint = va_arg(ap, int*);
|
||||
*pint = (int)nchars;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Note %n does not count as a conversion */
|
||||
|
||||
if (!noassign && *fmt != 'n')
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
width = 0;
|
||||
noassign = false;
|
||||
lflag = false;
|
||||
|
||||
fmt++;
|
||||
}
|
||||
|
||||
/* Its is not a conversion specifier */
|
||||
|
||||
else
|
||||
{
|
||||
while (isspace(*buf))
|
||||
{
|
||||
buf++;
|
||||
}
|
||||
|
||||
if (*fmt != *buf)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
fmt++;
|
||||
buf++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_stdinstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stdinstream_getc
|
||||
****************************************************************************/
|
||||
|
||||
static int stdinstream_getc(FAR struct lib_instream_s *this)
|
||||
{
|
||||
FAR struct lib_stdinstream_s *sthis = (FAR struct lib_stdinstream_s *)this;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(this);
|
||||
|
||||
/* Get the next character from the incoming stream */
|
||||
|
||||
ret = getc(sthis->stream);
|
||||
if (ret != EOF)
|
||||
{
|
||||
this->nget++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_stdinstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with a FILE instance.
|
||||
*
|
||||
* Input parameters:
|
||||
* stdinstream - User allocated, uninitialized instance of struct
|
||||
* lib_stdinstream_s to be initialized.
|
||||
* stream - User provided stream instance (must have been opened for
|
||||
* read access).
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_stdinstream(FAR struct lib_stdinstream_s *stdinstream,
|
||||
FAR FILE *stream)
|
||||
{
|
||||
stdinstream->public.get = stdinstream_getc;
|
||||
stdinstream->public.nget = 0;
|
||||
stdinstream->stream = stream;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_stdoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stdoutstream_putc
|
||||
****************************************************************************/
|
||||
|
||||
static void stdoutstream_putc(FAR struct lib_outstream_s *this, int ch)
|
||||
{
|
||||
FAR struct lib_stdoutstream_s *sthis = (FAR struct lib_stdoutstream_s *)this;
|
||||
int result;
|
||||
|
||||
DEBUGASSERT(this && sthis->stream);
|
||||
|
||||
/* Loop until the character is successfully transferred or an irrecoverable
|
||||
* error occurs.
|
||||
*/
|
||||
|
||||
do
|
||||
{
|
||||
result = fputc(ch, sthis->stream);
|
||||
if (result != EOF)
|
||||
{
|
||||
this->nput++;
|
||||
return;
|
||||
}
|
||||
|
||||
/* EINTR (meaning that fputc was interrupted by a signal) is the only
|
||||
* recoverable error.
|
||||
*/
|
||||
}
|
||||
while (get_errno() == EINTR);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stdoutstream_flush
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_STDIO_LINEBUFFER) && CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
int stdoutstream_flush(FAR struct lib_outstream_s *this)
|
||||
{
|
||||
FAR struct lib_stdoutstream_s *sthis = (FAR struct lib_stdoutstream_s *)this;
|
||||
return lib_fflush(sthis->stream, true);
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_stdoutstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with a FILE instance.
|
||||
*
|
||||
* Input parameters:
|
||||
* stdoutstream - User allocated, uninitialized instance of struct
|
||||
* lib_stdoutstream_s to be initialized.
|
||||
* stream - User provided stream instance (must have been opened for
|
||||
* write access).
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_stdoutstream(FAR struct lib_stdoutstream_s *stdoutstream,
|
||||
FAR FILE *stream)
|
||||
{
|
||||
/* Select the put operation */
|
||||
|
||||
stdoutstream->public.put = stdoutstream_putc;
|
||||
|
||||
/* Select the correct flush operation. This flush is only called when
|
||||
* a newline is encountered in the output stream. However, we do not
|
||||
* want to support this line buffering behavior if the stream was
|
||||
* opened in binary mode. In binary mode, the newline has no special
|
||||
* meaning.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
if ((stream->fs_oflags & O_BINARY) == 0)
|
||||
{
|
||||
stdoutstream->public.flush = stdoutstream_flush;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
stdoutstream->public.flush = lib_noflush;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Set the number of bytes put to zero and remember the stream */
|
||||
|
||||
stdoutstream->public.nput = 0;
|
||||
stdoutstream->stream = stream;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_syslogstream.c
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/syslog.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
#ifdef CONFIG_SYSLOG
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor definition
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslogstream_putc
|
||||
****************************************************************************/
|
||||
|
||||
static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Try writing until the write was successful or until an irrecoverable
|
||||
* error occurs.
|
||||
*/
|
||||
|
||||
do
|
||||
{
|
||||
/* Write the character to the supported logging device */
|
||||
|
||||
ret = syslog_putc(ch);
|
||||
if (ret == OK)
|
||||
{
|
||||
this->nput++;
|
||||
return;
|
||||
}
|
||||
|
||||
/* On failure syslog_putc will return a negated errno value. The
|
||||
* errno variable will not be set. The special value -EINTR means that
|
||||
* syslog_putc() was awakened by a signal. This is not a real error and
|
||||
* must be ignored in this context.
|
||||
*/
|
||||
}
|
||||
while (ret == -EINTR);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_syslogstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a stream for use with the configured syslog interface.
|
||||
*
|
||||
* Input parameters:
|
||||
* lowoutstream - User allocated, uninitialized instance of struct
|
||||
* lib_lowoutstream_s to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_syslogstream(FAR struct lib_outstream_s *stream)
|
||||
{
|
||||
stream->put = syslogstream_putc;
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
stream->flush = lib_noflush;
|
||||
#endif
|
||||
stream->nput = 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SYSLOG */
|
||||
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_ungetc.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Global Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ungetc
|
||||
**************************************************************************/
|
||||
|
||||
int ungetc(int c, FAR FILE *stream)
|
||||
{
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
int nungotten;
|
||||
#endif
|
||||
|
||||
/* Stream must be open for read access */
|
||||
|
||||
if ((stream && stream->fs_filedes < 0) ||
|
||||
((stream->fs_oflags & O_RDOK) == 0))
|
||||
{
|
||||
set_errno(EBADF);
|
||||
return EOF;
|
||||
}
|
||||
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
nungotten = stream->fs_nungotten;
|
||||
if (stream->fs_nungotten < CONFIG_NUNGET_CHARS)
|
||||
{
|
||||
stream->fs_ungotten[nungotten] = c;
|
||||
stream->fs_nungotten = nungotten + 1;
|
||||
return c;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
set_errno(ENOMEM);
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_vfprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int vfprintf(FAR FILE *stream, FAR const char *fmt, va_list ap)
|
||||
{
|
||||
struct lib_stdoutstream_s stdoutstream;
|
||||
int n = ERROR;
|
||||
|
||||
if (stream)
|
||||
{
|
||||
/* Wrap the stream in a stream object and let lib_vsprintf
|
||||
* do the work.
|
||||
*/
|
||||
|
||||
lib_stdoutstream(&stdoutstream, stream);
|
||||
|
||||
/* Hold the stream semaphore throughout the lib_vsprintf
|
||||
* call so that this thread can get its entire message out
|
||||
* before being pre-empted by the next thread.
|
||||
*/
|
||||
|
||||
lib_take_semaphore(stream);
|
||||
n = lib_vsprintf(&stdoutstream.public, fmt, ap);
|
||||
lib_give_semaphore(stream);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_vprintf.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Global Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: vprintf
|
||||
**************************************************************************/
|
||||
|
||||
int vprintf(FAR const char *fmt, va_list ap)
|
||||
{
|
||||
/* vfprintf into stdout */
|
||||
|
||||
return vfprintf(stdout, fmt, ap);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_vsnprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: vsnprintf
|
||||
****************************************************************************/
|
||||
|
||||
int vsnprintf(FAR char *buf, size_t size, const char *format, va_list ap)
|
||||
{
|
||||
struct lib_memoutstream_s memoutstream;
|
||||
int n;
|
||||
|
||||
/* Initialize a memory stream to write to the buffer */
|
||||
|
||||
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, buf, size);
|
||||
|
||||
/* Then let lib_vsprintf do the real work */
|
||||
|
||||
n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, format, ap);
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_vsprintf.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: vsprintf
|
||||
****************************************************************************/
|
||||
|
||||
int vsprintf(FAR char *dest, const char *src, va_list ap)
|
||||
{
|
||||
struct lib_memoutstream_s memoutstream;
|
||||
|
||||
/* Wrap the destination buffer in a stream object and let
|
||||
* libc/stdio/lib_vsprintf do the work.
|
||||
*/
|
||||
|
||||
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, dest, LIB_BUFLEN_UNKNOWN);
|
||||
return lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, src, ap);
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_wrflush.c
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_wrflush
|
||||
*
|
||||
* Description:
|
||||
* This is simply a version of fflush that does not report an error if
|
||||
* the file is not open for writing.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lib_wrflush(FAR FILE *stream)
|
||||
{
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
/* Verify that we were passed a valid (i.e., non-NULL) stream */
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!stream)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Verify that the stream is opened for writing... lib_fflush will
|
||||
* return an error if it is called for a stream that is not opened for
|
||||
* writing. Check that first so that this function will not fail in
|
||||
* that case.
|
||||
*/
|
||||
|
||||
if ((stream->fs_oflags & O_WROK) == 0)
|
||||
{
|
||||
/* Report that the success was successful if we attempt to flush a
|
||||
* read-only stream.
|
||||
*/
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/* Flush the stream. Return success if there is no buffered write data
|
||||
* -- i.e., that the stream is opened for writing and that all of the
|
||||
* buffered write data was successfully flushed by lib_fflush().
|
||||
*/
|
||||
|
||||
return lib_fflush(stream, true);
|
||||
#else
|
||||
/* Verify that we were passed a valid (i.e., non-NULL) stream */
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!stream)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
||||
return OK;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/****************************************************************************
|
||||
* libc/stdio/lib_zeroinstream.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static int zeroinstream_getc(FAR struct lib_instream_s *this)
|
||||
{
|
||||
this->nget++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_zeroinstream
|
||||
*
|
||||
* Description:
|
||||
* Initializes a NULL stream. The initialized stream will return an
|
||||
* infinitely long stream of zeroes.
|
||||
*
|
||||
* Input parameters:
|
||||
* zeroinstream - User allocated, uninitialized instance of struct
|
||||
* lib_instream_s to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* None (User allocated instance initialized).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream)
|
||||
{
|
||||
zeroinstream->get = zeroinstream_getc;
|
||||
zeroinstream->nget = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user