From 235736ed9342f7fd2cf1a4df19091e2d1e57111f Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sat, 12 Feb 2011 02:42:17 +0000 Subject: [PATCH] Fixed some problems with getc() not removing EOL as it should. git-svn-id: http://picoc.googlecode.com/svn/trunk@512 21eae674-98b7-11dd-bd71-f92a316d2d60 --- clibrary.c | 19 ++++++++----------- cstdlib/stdio.c | 6 ++++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/clibrary.c b/clibrary.c index fef1690..74ed04d 100644 --- a/clibrary.c +++ b/clibrary.c @@ -322,16 +322,13 @@ void LibSPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Val /* get a line of input. protected from buffer overrun */ void LibGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { - struct Value *CharArray = (struct Value *)(Param[0]->Val->Pointer); - char *ReadBuffer = &CharArray->Val->ArrayMem[0]; - char *Result; - - ReturnValue->Val->Pointer = NULL; - Result = PlatformGetLine(ReadBuffer, GETS_BUF_MAX, NULL); - if (Result == NULL) - return; - - ReturnValue->Val->Pointer = Param[0]->Val->Pointer; + ReturnValue->Val->Pointer = PlatformGetLine(Param[0]->Val->Pointer, GETS_BUF_MAX, NULL); + if (ReturnValue->Val->Pointer != NULL) + { + char *EOLPos = strchr(Param[0]->Val->Pointer, '\n'); + if (EOLPos != NULL) + *EOLPos = '\0'; + } } void LibGetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) @@ -601,7 +598,7 @@ struct LibraryFunction CLibrary[] = { { LibPrintf, "void printf(char *, ...);" }, { LibSPrintf, "char *sprintf(char *, char *, ...);" }, - { LibGets, "void gets(char *, int);" }, + { LibGets, "void gets(char *);" }, { LibGetc, "int getchar();" }, { LibExit, "void exit(int);" }, #ifdef PICOC_MATH_LIBRARY diff --git a/cstdlib/stdio.c b/cstdlib/stdio.c index bd42661..bd8ed0e 100644 --- a/cstdlib/stdio.c +++ b/cstdlib/stdio.c @@ -490,6 +490,12 @@ void StdioPuts(struct ParseState *Parser, struct Value *ReturnValue, struct Valu void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin); + if (ReturnValue->Val->Pointer != NULL) + { + char *EOLPos = strchr(Param[0]->Val->Pointer, '\n'); + if (EOLPos != NULL) + *EOLPos = '\0'; + } } void StdioGetchar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)