diff --git a/libc/stdio/Make.defs b/libc/stdio/Make.defs index 876a7347489..9a741581f54 100644 --- a/libc/stdio/Make.defs +++ b/libc/stdio/Make.defs @@ -50,7 +50,8 @@ CSRCS += lib_sscanf.c ifneq ($(CONFIG_NFILE_DESCRIPTORS),0) -CSRCS += lib_rawinstream.c lib_rawoutstream.c +CSRCS += lib_rawinstream.c lib_rawoutstream.c lib_rawsistream.c +CSRCS += lib_rawsostream.c # And these depend upon both file descriptors and C streams diff --git a/libc/stdio/lib_rawinstream.c b/libc/stdio/lib_rawinstream.c index 55456769ebd..19a980787d8 100644 --- a/libc/stdio/lib_rawinstream.c +++ b/libc/stdio/lib_rawinstream.c @@ -1,7 +1,7 @@ /**************************************************************************** - * libc/stdio/lib_rawinstream.c + * libc/stdio/lib_rawsistream.c * - * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -48,12 +48,12 @@ ****************************************************************************/ /**************************************************************************** - * Name: rawinstream_getc + * Name: rawsistream_getc ****************************************************************************/ -static int rawinstream_getc(FAR struct lib_instream_s *this) +static int rawsistream_getc(FAR struct lib_sistream_s *this) { - FAR struct lib_rawinstream_s *rthis = (FAR struct lib_rawinstream_s *)this; + FAR struct lib_rawsistream_s *rthis = (FAR struct lib_rawsistream_s *)this; int nwritten; char ch; @@ -77,31 +77,44 @@ static int rawinstream_getc(FAR struct lib_instream_s *this) return EOF; } +/**************************************************************************** + * Name: rawsistream_seek + ****************************************************************************/ + +static off_t rawsistream_seek(FAR struct lib_sistream_s *this, off_t offset, + int whence) +{ + FAR struct lib_rawsistream_s *mthis = (FAR struct lib_rawsistream_s *)this; + + DEBUGASSERT(this); + return lseek(mthis->fd, offset, whence); +} + /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: lib_rawinstream + * Name: lib_rawsistream * * 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). + * instream - User allocated, uninitialized instance of struct + * lib_rawsistream_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) +void lib_rawsistream(FAR struct lib_rawsistream_s *instream, int fd) { - rawinstream->public.get = rawinstream_getc; - rawinstream->public.nget = 0; - rawinstream->fd = fd; + instream->public.get = rawsistream_getc; + instream->public.seek = rawsistream_seek; + instream->public.nget = 0; + instream->fd = fd; } - diff --git a/libc/stdio/lib_rawoutstream.c b/libc/stdio/lib_rawoutstream.c index e1fe371346f..7f2c5224746 100644 --- a/libc/stdio/lib_rawoutstream.c +++ b/libc/stdio/lib_rawoutstream.c @@ -1,7 +1,7 @@ /**************************************************************************** - * libc/stdio/lib_rawoutstream.c + * libc/stdio/lib_rawsostream.c * - * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -48,12 +48,12 @@ ****************************************************************************/ /**************************************************************************** - * Name: rawoutstream_putc + * Name: rawsostream_putc ****************************************************************************/ -static void rawoutstream_putc(FAR struct lib_outstream_s *this, int ch) +static void rawsostream_putc(FAR struct lib_sostream_s *this, int ch) { - FAR struct lib_rawoutstream_s *rthis = (FAR struct lib_rawoutstream_s *)this; + FAR struct lib_rawsostream_s *rthis = (FAR struct lib_rawsostream_s *)this; int nwritten; char buffer = ch; @@ -82,34 +82,47 @@ static void rawoutstream_putc(FAR struct lib_outstream_s *this, int ch) while (get_errno() == EINTR); } +/**************************************************************************** + * Name: rawsostream_seek + ****************************************************************************/ + +static off_t rawsostream_seek(FAR struct lib_sostream_s *this, off_t offset, + int whence) +{ + FAR struct lib_rawsostream_s *mthis = (FAR struct lib_rawsostream_s *)this; + + DEBUGASSERT(this); + return lseek(mthis->fd, offset, whence); +} + /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: lib_rawoutstream + * Name: lib_rawsostream * * 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). + * outstream - User allocated, uninitialized instance of struct + * lib_rawsostream_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) +void lib_rawsostream(FAR struct lib_rawsostream_s *outstream, int fd) { - rawoutstream->public.put = rawoutstream_putc; + outstream->public.put = rawsostream_putc; #ifdef CONFIG_STDIO_LINEBUFFER - rawoutstream->public.flush = lib_noflush; + outstream->public.flush = lib_snoflush; #endif - rawoutstream->public.nput = 0; - rawoutstream->fd = fd; + outstream->public.seek = rawsostream_seek; + outstream->public.nput = 0; + outstream->fd = fd; } - diff --git a/libc/stdio/lib_rawsistream.c b/libc/stdio/lib_rawsistream.c new file mode 100644 index 00000000000..b013e43a80f --- /dev/null +++ b/libc/stdio/lib_rawsistream.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * libc/stdio/lib_rawinstream.c + * + * Copyright (C) 2007-2009, 2011-2012, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include +#include + +#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: + * instream - 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 *instream, int fd) +{ + instream->public.get = rawinstream_getc; + instream->public.nget = 0; + instream->fd = fd; +} diff --git a/libc/stdio/lib_rawsostream.c b/libc/stdio/lib_rawsostream.c new file mode 100644 index 00000000000..489a0fe9b20 --- /dev/null +++ b/libc/stdio/lib_rawsostream.c @@ -0,0 +1,114 @@ +/**************************************************************************** + * libc/stdio/lib_rawoutstream.c + * + * Copyright (C) 2007-2009, 2011-2012, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include +#include + +#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: + * outstream - 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 *outstream, int fd) +{ + outstream->public.put = rawoutstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER + outstream->public.flush = lib_noflush; +#endif + outstream->public.nput = 0; + outstream->fd = fd; +}