[PATCH] Fix: clear GEMDOS file errors

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Reported by Christian Zietz.

Unless error is cleared with clearerr(), ferror() continues reporting it.

Also, make sure that errno value doesn't get overwritten before it is used.
---
 src/gemdos.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/gemdos.c b/src/gemdos.c
index 825b9e1a..04301df9 100644
--- a/src/gemdos.c
+++ b/src/gemdos.c
@@ -1684,14 +1684,14 @@ static bool GemDOS_DFree(Uint32 Params)
 
 
 /*-----------------------------------------------------------------------*/
-/**
- * Helper to map Unix errno to GEMDOS error value
- */
 typedef enum {
 	ERROR_FILE,
 	ERROR_PATH
 } etype_t;
 
+/**
+ * Helper to log libc errno and map it to GEMDOS error value
+ */
 static Uint32 errno2gemdos(const int error, const etype_t etype)
 {
 	LOG_TRACE(TRACE_OS_GEMDOS, "-> ERROR (errno = %d)\n", error);
@@ -2334,10 +2334,13 @@ static bool GemDOS_Read(Uint32 Params)
 	
 	if (ferror(FileHandles[Handle].FileHandle))
 	{
+		int errnum = errno;
 		Log_Printf(LOG_WARN, "GEMDOS failed to read from '%s': %s\n",
 			   FileHandles[Handle].szActualName, strerror(errno));
-		Regs[REG_D0] = errno2gemdos(errno, ERROR_FILE);
-	} else
+		Regs[REG_D0] = errno2gemdos(errnum, ERROR_FILE);
+		clearerr(FileHandles[Handle].FileHandle);
+	}
+	else
 		/* Return number of bytes read */
 		Regs[REG_D0] = nBytesRead;
 
@@ -2402,9 +2405,11 @@ static bool GemDOS_Write(Uint32 Params)
 	nBytesWritten = fwrite(pBuffer, 1, Size, fp);
 	if (fh_idx >= 0 && ferror(fp))
 	{
+		int errnum = errno;
 		Log_Printf(LOG_WARN, "GEMDOS failed to write to '%s'\n",
 			   FileHandles[fh_idx].szActualName);
-		Regs[REG_D0] = errno2gemdos(errno, ERROR_FILE);
+		Regs[REG_D0] = errno2gemdos(errnum, ERROR_FILE);
+		clearerr(fp);
 	}
 	else
 	{
-- 
2.20.1


--------------D3FDACACAE3C11C2FFB7A7E0--



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/