summaryrefslogtreecommitdiff
path: root/nuttx/lib
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-08-12 23:59:07 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-08-12 23:59:07 +0000
commit51172e243d849989e1c2f3a5ce7e570f054d84e3 (patch)
treee40a2a0158ba055d1e00ad23ace7fe3a683b4e6d /nuttx/lib
parentc1229c1f96ba3283c899fed462dd7d76cc99a621 (diff)
downloadpx4-nuttx-51172e243d849989e1c2f3a5ce7e570f054d84e3.tar.gz
px4-nuttx-51172e243d849989e1c2f3a5ce7e570f054d84e3.tar.bz2
px4-nuttx-51172e243d849989e1c2f3a5ce7e570f054d84e3.zip
Fix EOF detection
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@818 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib')
-rw-r--r--nuttx/lib/lib_fgetc.c56
-rw-r--r--nuttx/lib/lib_fgets.c82
2 files changed, 77 insertions, 61 deletions
diff --git a/nuttx/lib/lib_fgetc.c b/nuttx/lib/lib_fgetc.c
index 3fd2a5a2b..7360563ec 100644
--- a/nuttx/lib/lib_fgetc.c
+++ b/nuttx/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 <spudmonkey@racsa.co.cr>
*
* 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 <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(FILE *stream)
{
diff --git a/nuttx/lib/lib_fgets.c b/nuttx/lib/lib_fgets.c
index 613314191..b3e9e02be 100644
--- a/nuttx/lib/lib_fgets.c
+++ b/nuttx/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 <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <stdio.h>
@@ -44,9 +44,9 @@
#include <ctype.h>
#include <debug.h>
-/************************************************************
+/****************************************************************************
* 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
- ************************************************************/
+ ****************************************************************************/
/* <esc>[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,