Fix EOF detection

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@818 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2008-08-12 23:59:07 +00:00
parent f9f5454c2b
commit 57cb16a605
2 changed files with 77 additions and 61 deletions

View File

@ -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)
{

View File

@ -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,