From 57cb16a6059c7a399f9f42ebe9fe50adb04a7538 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 12 Aug 2008 23:59:07 +0000 Subject: [PATCH] Fix EOF detection git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@818 42af7a65-404d-4744-a932-0658087f49c3 --- lib/lib_fgetc.c | 56 ++++++++++++++++----------------- lib/lib_fgets.c | 82 +++++++++++++++++++++++++++++-------------------- 2 files changed, 77 insertions(+), 61 deletions(-) diff --git a/lib/lib_fgetc.c b/lib/lib_fgetc.c index 3fd2a5a2b8..7360563ec4 100644 --- a/lib/lib_fgetc.c +++ b/lib/lib_fgetc.c @@ -1,7 +1,7 @@ -/************************************************************ +/**************************************************************************** * lib_fgetc.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 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. * @@ -31,58 +31,58 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Compilation Switches - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include #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(FILE *stream) { diff --git a/lib/lib_fgets.c b/lib/lib_fgets.c index 6133141911..b3e9e02be6 100644 --- a/lib/lib_fgets.c +++ b/lib/lib_fgets.c @@ -1,7 +1,7 @@ -/************************************************************ +/**************************************************************************** * lib_fgets.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 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. * @@ -31,11 +31,11 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include #include @@ -44,9 +44,9 @@ #include #include -/************************************************************ +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ /* In some systems, the underlying serial logic may * automatically echo characters back to the console. We @@ -64,44 +64,49 @@ #undef CONFIG_EOL_IS_BOTH_CRLF #define CONFIG_EOL_IS_EITHER_CRLF 1 -/************************************************************ +/**************************************************************************** * Private Type Declarations - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Function Prototypes - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Public Data - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Data - ************************************************************/ + ****************************************************************************/ /* [K is the VT100 command erases to the end of the line. */ static const char g_erasetoeol[] = "\033[K"; -/************************************************************ +/**************************************************************************** * Private Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Name: _lib_rawgetc - ************************************************************/ + ****************************************************************************/ static inline int _lib_rawgetc(int fd) { char buffer; - (void)read(fd, &buffer, 1); - return buffer; + if (read(fd, &buffer, 1) < 1) + { + /* Return EOF if the end of file (0) or error (-1) occurs */ + + return EOF; + } + return (int)buffer; } -/************************************************************ +/**************************************************************************** * Name: _lib_consoleputc - ************************************************************/ + ****************************************************************************/ #ifdef CONFIG_FGETS_ECHO static inline void _lib_consoleputc(int ch) @@ -111,9 +116,9 @@ static inline void _lib_consoleputc(int ch) } #endif -/************************************************************ +/**************************************************************************** * Name: _lib_consoleputs - ************************************************************/ + ****************************************************************************/ #ifdef CONFIG_FGETS_ECHO static inline void _lib_consoleputs(const char *s) @@ -122,11 +127,11 @@ static inline void _lib_consoleputs(const char *s) } #endif -/************************************************************ +/**************************************************************************** * Global Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Name: fgets * * Description: @@ -144,7 +149,7 @@ static inline void _lib_consoleputs(const char *s) * This will not work well if fd=0 corresponds to a raw * byte steam. * - **********************************************************/ + **************************************************************************/ char *fgets(FAR char *s, int n, FILE *stream) { @@ -283,10 +288,21 @@ char *fgets(FAR char *s, int n, FILE *stream) else if (ch == EOF) { - /* Terminate the line */ + /* End of file with no data? */ - s[nch] = '\0'; - return s; + if (!nch) + { + /* Yes.. return NULL as the end of file mark */ + + return NULL; + } + else + { + /* Terminate the line */ + + s[nch] = '\0'; + return s;\ + } } /* Otherwise, check if the character is printable and,