New module 'fseterr'.
authorBruno Haible <bruno@clisp.org>
Fri, 9 Mar 2007 00:53:49 +0000 (00:53 +0000)
committerBruno Haible <bruno@clisp.org>
Fri, 9 Mar 2007 00:53:49 +0000 (00:53 +0000)
ChangeLog
lib/fseterr.c [new file with mode: 0644]
lib/fseterr.h [new file with mode: 0644]
modules/fseterr [new file with mode: 0644]

index f8b6a7bcb42ba85390ee461f68d65f84609d370e..8b308c17b180ddd13d71ce4ba0cb504903abadc6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-03-08  Bruno Haible  <bruno@clisp.org>
+
+       * modules/fseterr: New file.
+       * lib/fseterr.h: New file.
+       * lib/fseterr.c: New file.
+
 2007-03-08  Bruno Haible  <bruno@clisp.org>
 
        * lib/fnmatch_.h: Convert tabs in the middle of lines to spaces.
diff --git a/lib/fseterr.c b/lib/fseterr.c
new file mode 100644 (file)
index 0000000..5ff8ba7
--- /dev/null
@@ -0,0 +1,55 @@
+/* Set the error indicator of a stream.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+   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 <config.h>
+
+/* Specification.  */
+#include "fseterr.h"
+
+void
+fseterr (FILE *fp)
+{
+  /* Most systems provide FILE as a struct and the necessary bitmask in
+     <stdio.h>, because they need it for implementing getc() and putc() as
+     fast macros.  */
+#if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
+  fp->_flags |= _IO_ERR_SEEN;
+#elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
+  fp->_flags |= __SERR;
+#elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
+  fp->_flag |= _IOERR;
+#else                               /* unknown  */
+  /* Portable fallback, based on an idea by Rich Felker.
+     Wow! 6 system calls for something that is just a bit operation!  */
+  int fd;
+  int fd2;
+
+  fflush (fp);
+  fd = fileno (fp);
+  fd2 = dup (fd);
+  if (fd2 >= 0)
+    {
+      close (fd);
+      fputc ('\0', fp); /* This should set the error indicator.  */
+      fflush (fp);      /* Or this.  */
+      if (dup2 (fd2, fd) < 0)
+       /* Whee... we botched the stream and now cannot restore it!  */
+       abort ();
+      close (fd2);
+    }
+#endif
+}
diff --git a/lib/fseterr.h b/lib/fseterr.h
new file mode 100644 (file)
index 0000000..0ed749a
--- /dev/null
@@ -0,0 +1,38 @@
+/* Set the error indicator of a stream.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+   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.  */
+
+#ifndef _FSETERR_H
+#define _FSETERR_H
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Set the error indicator of the stream FP.
+   The "error indicator" is set when an I/O operation on the stream fails, and
+   is cleared (together with the "end-of-file" indicator) by clearerr (FP).  */
+extern void fseterr (FILE *fp);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _FSETERR_H */
diff --git a/modules/fseterr b/modules/fseterr
new file mode 100644 (file)
index 0000000..2ff5961
--- /dev/null
@@ -0,0 +1,23 @@
+Description:
+Set the error indicator of a stream.
+
+Files:
+lib/fseterr.h
+lib/fseterr.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += fseterr.c
+
+Include:
+"fseterr.h"
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+