From fb4554347cc7b5d85a04213ea3747ef3a34ba2a4 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 31 Mar 2006 18:38:59 +0000 Subject: [PATCH] Add freaderror() analogous to fwriteerror() in gnulib. --- src/libpspp/ChangeLog | 8 ++++++ src/libpspp/automake.mk | 2 ++ src/libpspp/freaderror.c | 62 ++++++++++++++++++++++++++++++++++++++++ src/libpspp/freaderror.h | 22 ++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/libpspp/freaderror.c create mode 100644 src/libpspp/freaderror.h diff --git a/src/libpspp/ChangeLog b/src/libpspp/ChangeLog index 98bd3dd0..2dc9e0ba 100644 --- a/src/libpspp/ChangeLog +++ b/src/libpspp/ChangeLog @@ -1,3 +1,11 @@ +Fri Mar 31 10:38:46 2006 Ben Pfaff + + Add freaderror() analogous to fwriteerror() in gnulib. + + * freaderror.c: New file. + + * freaderror.h: New file. + Thu Mar 30 16:15:37 2006 Ben Pfaff * str.c: (ds_create) Adjust capacity selection. diff --git a/src/libpspp/automake.mk b/src/libpspp/automake.mk index 5341c072..6e988a9d 100644 --- a/src/libpspp/automake.mk +++ b/src/libpspp/automake.mk @@ -13,6 +13,8 @@ src_libpspp_libpspp_a_SOURCES = \ src/libpspp/copyleft.h \ src/libpspp/compiler.h \ src/libpspp/debug-print.h \ + src/libpspp/freaderror.c \ + src/libpspp/freaderror.h \ src/libpspp/hash.c \ src/libpspp/hash.h \ src/libpspp/magic.c \ diff --git a/src/libpspp/freaderror.c b/src/libpspp/freaderror.c new file mode 100644 index 00000000..73e5346a --- /dev/null +++ b/src/libpspp/freaderror.c @@ -0,0 +1,62 @@ +/* Detect read error on a stream. + Copyright (C) 2003-2005, 2006 Free Software Foundation, Inc. + Written by Bruno Haible , 2003. + Modified by Ben Pfaff for PSPP. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +/* Specification. */ +#include "freaderror.h" + +#include +#include + +/* Close the stream FP, and test whether some error occurred on + the stream FP. + FP must be a stream opened for reading. + Return 0 if no error occurred and fclose (fp) succeeded. + Return -1 and set errno if there was an error. The errno value will be 0 + if the cause of the error cannot be determined. */ +int +freaderror (FILE *fp) +{ + /* Need to + 1. test the error indicator of the stream, + 2. flush the buffers both in userland and in the kernel, through fclose, + testing for error again. */ + + /* Clear errno, so that on non-POSIX systems the caller doesn't see a + wrong value of errno when we return -1. */ + errno = 0; + + if (ferror (fp)) + { + /* The stream had an error earlier, but its errno was lost. + If the error was not temporary, we can get the same + errno by reading one more byte. */ + getc (fp); + fclose (fp); + return -1; + } + + if (fclose (fp)) + return -1; /* errno is set here */ + + return 0; +} diff --git a/src/libpspp/freaderror.h b/src/libpspp/freaderror.h new file mode 100644 index 00000000..875d19df --- /dev/null +++ b/src/libpspp/freaderror.h @@ -0,0 +1,22 @@ +/* Detect read error on a stream. + Copyright (C) 2003, 2005, 2006 Free Software Foundation, Inc. + Written by Bruno Haible , 2003. + Modified by Ben Pfaff for PSPP. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +extern int freaderror (FILE *fp); -- 2.30.2