mirror of
https://https.git.savannah.gnu.org/git/gnulib.git
synced 2026-09-01 02:34:55 +00:00
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.
This commit is contained in:
@@ -1,3 +1,12 @@
|
||||
2007-04-12 Eric Blake <ebb9@byu.net>
|
||||
|
||||
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.
|
||||
|
||||
2007-04-12 Simon Josefsson <simon@josefsson.org>
|
||||
|
||||
* gnulib-tool: Rename generated 'autobuild' script to
|
||||
|
||||
@@ -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
|
||||
|
||||
+100
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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.
|
||||
:
|
||||
])
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user