[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
new module 'fclose'
From: |
Bruno Haible |
Subject: |
new module 'fclose' |
Date: |
Sat, 11 Oct 2008 14:19:17 +0200 |
User-agent: |
KMail/1.5.4 |
We're overriding close(). But the system's fclose(), when called on a FILE
stream that was created with fdopen() from a socket descriptor, will not
invoke our overridden close(). So we have to override fclose() as well.
I'm committing this. It has a circular dependency between the modules
'close' and 'fclose'. That seems the safest to me.
2008-10-11 Bruno Haible <address@hidden>
New module 'fclose'.
* modules/fclose: New file.
* lib/stdio.in.h (fclose): New declaration.
* lib/fclose.c: New file.
* m4/fclose.m4: New file.
* m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Initialize GNULIB_FCLOSE,
REPLACE_FCLOSE.
* m4/close.m4 (gl_REPLACE_CLOSE): Invoke gl_REPLACE_FCLOSE.
* modules/stdio (Makefile.am): Substitute GNULIB_FCLOSE,
REPLACE_FCLOSE.
* modules/close (Depends-on): fclose.
* doc/posix-functions/fclose.texi: Mention the problem on Windows.
========================== modules/fclose ===============================
Description:
fclose() function: close a stream.
Files:
lib/fclose.c
m4/fclose.m4
Depends-on:
stdio
close
configure.ac:
gl_FUNC_FCLOSE
gl_STDIO_MODULE_INDICATOR([fclose])
Makefile.am:
Include:
<stdio.h>
License:
LGPLv2+
Maintainer:
Bruno Haible
========================== lib/fclose.c =================================
/* close replacement.
Copyright (C) 2008 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
/* Override fclose() to call the overridden close(). */
int
rpl_fclose (FILE *fp)
#undef fclose
{
int saved_errno = 0;
if (fflush (fp))
saved_errno = errno;
if (close (fileno (fp)) < 0 && saved_errno == 0)
saved_errno = errno;
fclose (fp); /* will fail with errno = EBADF */
if (saved_errno != 0)
{
errno = saved_errno;
return EOF;
}
return 0;
}
========================== m4/fclose.m4 =================================
# fclose.m4 serial 1
dnl Copyright (C) 2008 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_FUNC_FCLOSE],
[
])
AC_DEFUN([gl_REPLACE_FCLOSE],
[
AC_REQUIRE([gl_STDIO_H_DEFAULTS])
if test $REPLACE_FCLOSE != 1; then
AC_LIBOBJ([fclose])
fi
REPLACE_FCLOSE=1
])
=========================================================================
*** lib/stdio.in.h.orig 2008-10-11 14:03:09.000000000 +0200
--- lib/stdio.in.h 2008-10-11 12:47:44.000000000 +0200
***************
*** 373,378 ****
--- 373,393 ----
fflush (f))
#endif
+ #if @GNULIB_FCLOSE@
+ # if @REPLACE_FCLOSE@
+ # define fclose rpl_fclose
+ /* Close STREAM and its underlying file descriptor. */
+ extern int fclose (FILE *stream);
+ # endif
+ #elif defined GNULIB_POSIXCHECK
+ # undef fclose
+ # define fclose(f) \
+ (GL_LINK_WARNING ("fclose is not always POSIX compliant - " \
+ "use gnulib module fclose for portable " \
+ "POSIX compliance"), \
+ fclose (f))
+ #endif
+
#if @GNULIB_FPUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
# undef fputc
# define fputc rpl_fputc
*** m4/stdio_h.m4.orig 2008-10-11 14:03:09.000000000 +0200
--- m4/stdio_h.m4 2008-10-11 12:49:26.000000000 +0200
***************
*** 1,4 ****
! # stdio_h.m4 serial 13
dnl Copyright (C) 2007-2008 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
--- 1,4 ----
! # stdio_h.m4 serial 14
dnl Copyright (C) 2007-2008 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
***************
*** 63,68 ****
--- 63,69 ----
GNULIB_FTELL=0; AC_SUBST([GNULIB_FTELL])
GNULIB_FTELLO=0; AC_SUBST([GNULIB_FTELLO])
GNULIB_FFLUSH=0; AC_SUBST([GNULIB_FFLUSH])
+ GNULIB_FCLOSE=0; AC_SUBST([GNULIB_FCLOSE])
GNULIB_FPUTC=0; AC_SUBST([GNULIB_FPUTC])
GNULIB_PUTC=0; AC_SUBST([GNULIB_PUTC])
GNULIB_PUTCHAR=0; AC_SUBST([GNULIB_PUTCHAR])
***************
*** 98,103 ****
--- 99,105 ----
REPLACE_FTELLO=0; AC_SUBST([REPLACE_FTELLO])
REPLACE_FTELL=0; AC_SUBST([REPLACE_FTELL])
REPLACE_FFLUSH=0; AC_SUBST([REPLACE_FFLUSH])
+ REPLACE_FCLOSE=0; AC_SUBST([REPLACE_FCLOSE])
HAVE_DECL_GETDELIM=1; AC_SUBST([HAVE_DECL_GETDELIM])
HAVE_DECL_GETLINE=1; AC_SUBST([HAVE_DECL_GETLINE])
REPLACE_GETLINE=0; AC_SUBST([REPLACE_GETLINE])
*** m4/close.m4.orig 2008-10-11 14:03:09.000000000 +0200
--- m4/close.m4 2008-10-11 14:02:55.000000000 +0200
***************
*** 1,4 ****
! # close.m4 serial 1
dnl Copyright (C) 2008 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
--- 1,4 ----
! # close.m4 serial 2
dnl Copyright (C) 2008 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
***************
*** 21,24 ****
--- 21,25 ----
AC_LIBOBJ([close])
fi
REPLACE_CLOSE=1
+ gl_REPLACE_FCLOSE
])
*** modules/stdio.orig 2008-10-11 14:03:09.000000000 +0200
--- modules/stdio 2008-10-11 12:48:51.000000000 +0200
***************
*** 46,51 ****
--- 46,52 ----
-e 's|@''GNULIB_FTELL''@|$(GNULIB_FTELL)|g' \
-e 's|@''GNULIB_FTELLO''@|$(GNULIB_FTELLO)|g' \
-e 's|@''GNULIB_FFLUSH''@|$(GNULIB_FFLUSH)|g' \
+ -e 's|@''GNULIB_FCLOSE''@|$(GNULIB_FCLOSE)|g' \
-e 's|@''GNULIB_FPUTC''@|$(GNULIB_FPUTC)|g' \
-e 's|@''GNULIB_PUTC''@|$(GNULIB_PUTC)|g' \
-e 's|@''GNULIB_PUTCHAR''@|$(GNULIB_PUTCHAR)|g' \
***************
*** 78,83 ****
--- 79,85 ----
-e 's|@''REPLACE_FTELLO''@|$(REPLACE_FTELLO)|g' \
-e 's|@''REPLACE_FTELL''@|$(REPLACE_FTELL)|g' \
-e 's|@''REPLACE_FFLUSH''@|$(REPLACE_FFLUSH)|g' \
+ -e 's|@''REPLACE_FCLOSE''@|$(REPLACE_FCLOSE)|g' \
-e 's|@''HAVE_DECL_GETDELIM''@|$(HAVE_DECL_GETDELIM)|g' \
-e 's|@''HAVE_DECL_GETLINE''@|$(HAVE_DECL_GETLINE)|g' \
-e 's|@''REPLACE_GETLINE''@|$(REPLACE_GETLINE)|g' \
*** modules/close.orig 2008-10-11 14:03:09.000000000 +0200
--- modules/close 2008-10-11 13:44:12.000000000 +0200
***************
*** 7,12 ****
--- 7,13 ----
Depends-on:
unistd
+ fclose
configure.ac:
gl_FUNC_CLOSE
*** doc/posix-functions/fclose.texi.orig 2008-10-11 14:03:09.000000000
+0200
--- doc/posix-functions/fclose.texi 2008-10-11 12:30:35.000000000 +0200
***************
*** 4,13 ****
POSIX specification: @url{http://www.opengroup.org/susv3xsh/fclose.html}
! Gnulib module: ---
Portability problems fixed by Gnulib:
@itemize
@end itemize
Portability problems not fixed by Gnulib:
--- 4,17 ----
POSIX specification: @url{http://www.opengroup.org/susv3xsh/fclose.html}
! Gnulib module: fclose
Portability problems fixed by Gnulib:
@itemize
+ @item
+ On Windows platforms (excluding Cygwin), @code{socket} and @code{accept}
+ followed by @code{fdopen} do not return streams that can be closed by
+ @code{fclose}.
@end itemize
Portability problems not fixed by Gnulib:
- new module 'fclose',
Bruno Haible <=