From d4cb713197c88e09f0d873a330d1532abe119705 Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 24 Mar 2002 13:23:59 +0000 Subject: [PATCH] Fix return codes git-svn-id: svn://svn.cc65.org/cc65/trunk@1204 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- libsrc/common/fread.c | 33 +++++++++++++++++++-------------- libsrc/common/fwrite.c | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/libsrc/common/fread.c b/libsrc/common/fread.c index a8b402af3..d0d3df3bc 100644 --- a/libsrc/common/fread.c +++ b/libsrc/common/fread.c @@ -20,7 +20,7 @@ size_t fread (void* buf, size_t size, size_t count, FILE* f) /* Is the file open? */ if ((f->f_flags & _FOPEN) == 0) { _errno = EINVAL; /* File not open */ - return (size_t) -1; + return 0; } /* Did we have an error or EOF? */ @@ -33,21 +33,26 @@ size_t fread (void* buf, size_t size, size_t count, FILE* f) bytes = size * count; if (bytes) { + /* Read the data. */ bytes = read (f->f_fd, buf, bytes); - if (bytes == -1) { - /* Read error */ - f->f_flags |= _FERROR; - return (size_t) -1; - } - if (bytes == 0) { - /* End of file */ - f->f_flags |= _FEOF; - return (size_t) -1; - } - - /* Unfortunately, we cannot avoid the divide here... */ - return bytes / size; + + switch (bytes) { + + case -1: + /* Read error */ + f->f_flags |= _FERROR; + return 0; + + case 0: + /* End of file */ + f->f_flags |= _FEOF; + /* FALLTHROUGH */ + + default: + /* Unfortunately, we cannot avoid the divide here... */ + return bytes / size; + } } else { diff --git a/libsrc/common/fwrite.c b/libsrc/common/fwrite.c index 30ceaea0a..a363ad362 100644 --- a/libsrc/common/fwrite.c +++ b/libsrc/common/fwrite.c @@ -20,7 +20,7 @@ size_t fwrite (const void* buf, size_t size, size_t count, FILE* f) /* Is the file open? */ if ((f->f_flags & _FOPEN) == 0) { _errno = EINVAL; /* File not open */ - return -1; + return 0; } /* Did we have an error */ @@ -37,7 +37,7 @@ size_t fwrite (const void* buf, size_t size, size_t count, FILE* f) if (write (f->f_fd, buf, bytes) == -1) { /* Write error */ f->f_flags |= _FERROR; - return -1; + return 0; } } -- 2.39.5