[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: stdin seekable failure
From: |
Eric Blake |
Subject: |
Re: stdin seekable failure |
Date: |
Thu, 12 Apr 2007 10:11:50 -0600 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.10) Gecko/20070221 Thunderbird/1.5.0.10 Mnenhy/0.7.5.666 |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
According to Paul Eggert on 4/11/2007 12:41 PM:
> Eric Blake <address@hidden> writes:
>
>> Maybe the closeout module needs to be updated to worry about stdin as well
>> as stdout/stderr?
>
> Coreutils doesn't need that, as it doesn't have the problem. The two
> apps that I thought might have the problem solve it in a different
> way. 'head' bypasses stdio. 'od -N' sets the stdio input buffer size
> to 1 (which is slow and goes beyond what POSIX requires, but it does
> work).
>
> So I suggest creating a new module 'closein' instead.
Done like so.
2007-04-12 Eric Blake <address@hidden>
Work around glibc's failure to reset seekable stdin on exit.
* modules/closein: New module.
* lib/closein.c: New file.
* lib/closein.h: Likewise.
* m4/closein.m4: Likewise.
* MODULES.html.sh (File stream based Input/Output): Document it.
- --
Don't work too hard, make some time for fun as well!
Eric Blake address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGHlpF84KuGfSFAYARAuSUAJ0YcV89PP9GeG7BW2+PqkVfJesE6QCfVQUb
IAqcwHU0Mm9mGk0VxQxO8uA=
=om3i
-----END PGP SIGNATURE-----
diff --git a/ChangeLog b/ChangeLog
index efda62b..fe02dd9 100644
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 3f13fa3..277b09d 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2192,6 +2192,7 @@ func_all_modules ()
func_begin_table
func_module close-stream
+ func_module closein
func_module closeout
func_module fopen-safer
func_module fpending
diff --git a/lib/closein.c b/lib/closein.c
new file mode 100644
index 0000000..1aade1d
--- /dev/null
+++ b/lib/closein.c
@@ -0,0 +1,100 @@
+/* Close standard input, rewinding seekable stdin if necessary.
+
+ 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>
+
+#include "closein.h"
+#include "closeout.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+#include "close-stream.h"
+#include "error.h"
+#include "exitfail.h"
+#include "quotearg.h"
+
+static const char *file_name;
+
+/* Set the file name to be reported in the event an error is detected
+ on stdin by close_stdin. See also close_stdout_set_file_name, if
+ an error is detected when closing stdout. */
+void
+close_stdin_set_file_name (const char *file)
+{
+ file_name = file;
+}
+
+/* Close standard input, rewinding any unused input if stdin is
+ seekable. On error, issue a diagnostic and _exit with status
+ 'exit_failure'. Then call close_stdout.
+
+ Most programs can get by with close_stdout. close_stdin is only
+ needed when a program wants to guarantee that partially read input
+ from seekable stdin is not consumed, for any subsequent clients.
+ For example, POSIX requires that these two commands behave alike:
+
+ (sed -ne 1q; cat) < file
+ tail -n 1 file
+
+ Since close_stdin is commonly registered via 'atexit', POSIX
+ and the C standard both say that it should not call 'exit',
+ because the behavior is undefined if 'exit' is called more than
+ once. So it calls '_exit' instead of 'exit'. If close_stdin
+ is registered via atexit before other functions are registered,
+ the other functions can act before this _exit is invoked.
+
+ Applications that use close_stdout should flush any streams other
+ than stdin, stdout, and stderr before exiting, since the call to
+ _exit will bypass other buffer flushing. Applications should be
+ flushing and closing other streams anyway, to check for I/O errors.
+ Also, applications should not use tmpfile, since _exit can bypass
+ the removal of these files.
+
+ It's important to detect such failures and exit nonzero because many
+ tools (most notably `make' and other build-management systems) depend
+ on being able to detect failure in other tools via their exit status. */
+
+void
+close_stdin (void)
+{
+ bool fail = false;
+ if (close_stream (stdin) != 0)
+ {
+ char const *close_error = _("error closing file");
+ if (file_name)
+ error (0, errno, "%s: %s", quotearg_colon (file_name),
+ close_error);
+ else
+ error (0, errno, "%s", close_error);
+
+ /* Defer failure until after closing stdout, since the output
+ can still usefully be flushed. */
+ fail = true;
+ }
+
+ close_stdout ();
+
+ if (fail)
+ _exit (exit_failure);
+}
diff --git a/lib/closein.h b/lib/closein.h
new file mode 100644
index 0000000..43a713d
--- /dev/null
+++ b/lib/closein.h
@@ -0,0 +1,33 @@
+/* Close standard input, rewinding seekable stdin if necessary.
+
+ 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 _GL_CLOSEIN_H
+# define _GL_CLOSEIN_H 1
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+void close_stdin_set_file_name (const char *file);
+void close_stdin (void);
+
+# ifdef __cplusplus
+}
+# endif
+
+#endif
diff --git a/m4/closein.m4 b/m4/closein.m4
new file mode 100644
index 0000000..bf8ff38
--- /dev/null
+++ b/m4/closein.m4
@@ -0,0 +1,13 @@
+# closein.m4 serial 1
+dnl Copyright (C) 2007 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_CLOSEIN],
+[
+ AC_LIBOBJ([closein])
+
+ dnl Prerequisites of lib/closein.c.
+ :
+])
diff --git a/modules/closein b/modules/closein
new file mode 100644
index 0000000..fefeae3
--- /dev/null
+++ b/modules/closein
@@ -0,0 +1,26 @@
+Description:
+Close all standard streams, resetting seekable stdin if needed, and exiting
with a diagnostic on error.
+
+Files:
+lib/closein.h
+lib/closein.c
+m4/closein.m4
+
+Depends-on:
+closeout
+fflush
+stdbool
+
+configure.ac:
+gl_CLOSEIN
+
+Makefile.am:
+
+Include:
+"closein.h"
+
+License:
+GPL
+
+Maintainer:
+Eric Blake
- stdin seekable failure [was: HEAD: inclusion order wrong for input.c], Eric Blake, 2007/04/11
- Re: stdin seekable failure, Paul Eggert, 2007/04/11
- Re: stdin seekable failure,
Eric Blake <=
- Re: stdin seekable failure, Eric Blake, 2007/04/12
- Re: stdin seekable failure, Eric Blake, 2007/04/27
- Re: stdin seekable failure, Bruno Haible, 2007/04/27
- Re: stdin seekable failure, Eric Blake, 2007/04/28
- Re: stdin seekable failure, Bruno Haible, 2007/04/28