From: Bruno Haible Date: Sun, 7 Oct 2007 02:46:57 +0000 (+0200) Subject: New modules 'fopen' and 'freopen'. X-Git-Tag: v0.0~123 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=86f87b4967587bcac4fae90ee86c6fce24e3a4f3;p=gnulib.git New modules 'fopen' and 'freopen'. --- diff --git a/ChangeLog b/ChangeLog index 43291d4d0..ae51c7334 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2007-10-06 Bruno Haible + * modules/fopen: New file. + * lib/fopen.c: New file. + * m4/fopen.m4: New file. + * modules/freopen: New file. + * lib/freopen.c: New file. + * m4/freopen.m4: New file. + * lib/stdio.in.h (fopen, freopen): New declarations. + * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Initialize also GNULIB_FOPEN, + GNULIB_FREOPEN, REPLACE_FOPEN, REPLACE_FREOPEN. + * modules/stdio (Makefile.am): Substitute also GNULIB_FOPEN, + GNULIB_FREOPEN, REPLACE_FOPEN, REPLACE_FREOPEN. + * doc/functions/fopen.texi: Mention the 'fopen' module. + * doc/functions/freopen.texi: Mention the 'freopen' module. + +2007-10-06 Bruno Haible + * modules/open-tests: New file. * tests/test-open.c: New file. diff --git a/doc/functions/fopen.texi b/doc/functions/fopen.texi index 272638e7d..37edf9ce5 100644 --- a/doc/functions/fopen.texi +++ b/doc/functions/fopen.texi @@ -4,10 +4,13 @@ POSIX specification: @url{http://www.opengroup.org/susv3xsh/fopen.html} -Gnulib module: --- +Gnulib module: fopen Portability problems fixed by Gnulib: @itemize +@item +On Windows platforms (excluding Cygwin), this function does usually not +recognize the @file{/dev/null} filename. @end itemize Portability problems not fixed by Gnulib: diff --git a/doc/functions/freopen.texi b/doc/functions/freopen.texi index b126f7005..ccb25eace 100644 --- a/doc/functions/freopen.texi +++ b/doc/functions/freopen.texi @@ -4,10 +4,13 @@ POSIX specification: @url{http://www.opengroup.org/susv3xsh/freopen.html} -Gnulib module: --- +Gnulib module: freopen Portability problems fixed by Gnulib: @itemize +@item +On Windows platforms (excluding Cygwin), this function does usually not +recognize the @file{/dev/null} filename. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/fopen.c b/lib/fopen.c new file mode 100644 index 000000000..2faad5902 --- /dev/null +++ b/lib/fopen.c @@ -0,0 +1,37 @@ +/* Open a stream to a file. + 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. */ + +/* Written by Bruno Haible , 2007. */ + +#include + +/* Specification. */ +#include + +#include + +FILE * +fopen (const char *filename, const char *mode) +#undef fopen +{ +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + if (strcmp (filename, "/dev/null") == 0) + filename = "NUL"; +#endif + + return fopen (filename, mode); +} diff --git a/lib/freopen.c b/lib/freopen.c new file mode 100644 index 000000000..4ae24096e --- /dev/null +++ b/lib/freopen.c @@ -0,0 +1,37 @@ +/* Open a stream to a file. + 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. */ + +/* Written by Bruno Haible , 2007. */ + +#include + +/* Specification. */ +#include + +#include + +FILE * +freopen (const char *filename, const char *mode, FILE *stream) +#undef freopen +{ +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + if (strcmp (filename, "/dev/null") == 0) + filename = "NUL"; +#endif + + return freopen (filename, mode, stream); +} diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 5264194bd..772709cbd 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -212,6 +212,32 @@ extern int vsprintf (char *str, const char *format, va_list args) # endif #endif +#if @GNULIB_FOPEN@ +# if @REPLACE_FOPEN@ +# define fopen rpl_fopen +extern FILE * fopen (const char *filename, const char *mode); +# endif +#elif defined GNULIB_POSIXCHECK +# undef fopen +# define fopen(f,m) \ + (GL_LINK_WARNING ("fopen on Win32 platforms is not POSIX compatible - " \ + "use gnulib module fopen for portability"), \ + fopen (f, m)) +#endif + +#if @GNULIB_FREOPEN@ +# if @REPLACE_FREOPEN@ +# define freopen rpl_freopen +extern FILE * freopen (const char *filename, const char *mode, FILE *stream); +# endif +#elif defined GNULIB_POSIXCHECK +# undef freopen +# define freopen(f,m,s) \ + (GL_LINK_WARNING ("freopen on Win32 platforms is not POSIX compatible - " \ + "use gnulib module freopen for portability"), \ + freopen (f, m, s)) +#endif + #if @GNULIB_FSEEKO@ # if @REPLACE_FSEEKO@ /* Provide fseek, fseeko functions that are aware of a preceding diff --git a/m4/fopen.m4 b/m4/fopen.m4 new file mode 100644 index 000000000..1ec011bff --- /dev/null +++ b/m4/fopen.m4 @@ -0,0 +1,17 @@ +# fopen.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_FUNC_FOPEN], +[ + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + case "$host_os" in + mingw* | pw*) + REPLACE_FOPEN=1 + AC_LIBOBJ([fopen]) + ;; + esac +]) diff --git a/m4/freopen.m4 b/m4/freopen.m4 new file mode 100644 index 000000000..0149ebd65 --- /dev/null +++ b/m4/freopen.m4 @@ -0,0 +1,17 @@ +# freopen.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_FUNC_FREOPEN], +[ + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + case "$host_os" in + mingw* | pw*) + REPLACE_FREOPEN=1 + AC_LIBOBJ([freopen]) + ;; + esac +]) diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index b9a69984f..45e54e2a6 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -1,4 +1,4 @@ -# stdio_h.m4 serial 7 +# stdio_h.m4 serial 8 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, @@ -28,6 +28,8 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], GNULIB_VSNPRINTF=0; AC_SUBST([GNULIB_VSNPRINTF]) GNULIB_VSPRINTF_POSIX=0; AC_SUBST([GNULIB_VSPRINTF_POSIX]) GNULIB_VASPRINTF=0; AC_SUBST([GNULIB_VASPRINTF]) + GNULIB_FOPEN=0; AC_SUBST([GNULIB_FOPEN]) + GNULIB_FREOPEN=0; AC_SUBST([GNULIB_FREOPEN]) GNULIB_FSEEK=0; AC_SUBST([GNULIB_FSEEK]) GNULIB_FSEEKO=0; AC_SUBST([GNULIB_FSEEKO]) GNULIB_FTELL=0; AC_SUBST([GNULIB_FTELL]) @@ -48,6 +50,8 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], REPLACE_VSPRINTF=0; AC_SUBST([REPLACE_VSPRINTF]) HAVE_VASPRINTF=1; AC_SUBST([HAVE_VASPRINTF]) REPLACE_VASPRINTF=0; AC_SUBST([REPLACE_VASPRINTF]) + REPLACE_FOPEN=0; AC_SUBST([REPLACE_FOPEN]) + REPLACE_FREOPEN=0; AC_SUBST([REPLACE_FREOPEN]) HAVE_FSEEKO=1; AC_SUBST([HAVE_FSEEKO]) REPLACE_FSEEKO=0; AC_SUBST([REPLACE_FSEEKO]) REPLACE_FSEEK=0; AC_SUBST([REPLACE_FSEEK]) diff --git a/modules/fopen b/modules/fopen new file mode 100644 index 000000000..434cf1786 --- /dev/null +++ b/modules/fopen @@ -0,0 +1,25 @@ +Description: +fopen() function: open a stream to a file. + +Files: +lib/fopen.c +m4/fopen.m4 + +Depends-on: +stdio + +configure.ac: +gl_FUNC_FOPEN +gl_STDIO_MODULE_INDICATOR([fopen]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/freopen b/modules/freopen new file mode 100644 index 000000000..5c556e4e0 --- /dev/null +++ b/modules/freopen @@ -0,0 +1,25 @@ +Description: +freopen() function: open a stream to a file. + +Files: +lib/freopen.c +m4/freopen.m4 + +Depends-on: +stdio + +configure.ac: +gl_FUNC_FREOPEN +gl_STDIO_MODULE_INDICATOR([freopen]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/stdio b/modules/stdio index 5aaf973ed..d6a154943 100644 --- a/modules/stdio +++ b/modules/stdio @@ -31,6 +31,8 @@ stdio.h: stdio.in.h -e 's|@''GNULIB_VSNPRINTF''@|$(GNULIB_VSNPRINTF)|g' \ -e 's|@''GNULIB_VSPRINTF_POSIX''@|$(GNULIB_VSPRINTF_POSIX)|g' \ -e 's|@''GNULIB_VASPRINTF''@|$(GNULIB_VASPRINTF)|g' \ + -e 's|@''GNULIB_FOPEN''@|$(GNULIB_FOPEN)|g' \ + -e 's|@''GNULIB_FREOPEN''@|$(GNULIB_FREOPEN)|g' \ -e 's|@''GNULIB_FSEEK''@|$(GNULIB_FSEEK)|g' \ -e 's|@''GNULIB_FSEEKO''@|$(GNULIB_FSEEKO)|g' \ -e 's|@''GNULIB_FTELL''@|$(GNULIB_FTELL)|g' \ @@ -50,6 +52,8 @@ stdio.h: stdio.in.h -e 's|@''REPLACE_VSPRINTF''@|$(REPLACE_VSPRINTF)|g' \ -e 's|@''HAVE_VASPRINTF''@|$(HAVE_VASPRINTF)|g' \ -e 's|@''REPLACE_VASPRINTF''@|$(REPLACE_VASPRINTF)|g' \ + -e 's|@''REPLACE_FOPEN''@|$(REPLACE_FOPEN)|g' \ + -e 's|@''REPLACE_FREOPEN''@|$(REPLACE_FREOPEN)|g' \ -e 's|@''REPLACE_FSEEKO''@|$(REPLACE_FSEEKO)|g' \ -e 's|@''REPLACE_FSEEK''@|$(REPLACE_FSEEK)|g' \ -e 's|@''REPLACE_FTELLO''@|$(REPLACE_FTELLO)|g' \