From: Bruno Haible Date: Sat, 17 Sep 2011 16:28:17 +0000 (+0200) Subject: popen: Support for MSVC. X-Git-Tag: v0.1~1884 X-Git-Url: https://erislabs.net/gitweb/?a=commitdiff_plain;h=25399c1aba359704fab2f4f5164011df43e1795e;p=gnulib.git popen: Support for MSVC. * lib/stdio.in.h (popen): Declare it if the system lacks this function. * lib/popen.c (popen): Provide alternate definition for native Windows. * m4/popen.m4 (gl_FUNC_POPEN): Test if popen exists. Set HAVE_POPEN. * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Initialize HAVE_POPEN. * modules/popen (Depends-on, configure.ac): Update condition. * modules/stdio (Makefile.am): Substitute HAVE_POPEN. * doc/posix-functions/popen.texi: Mention that the MSVC problem is fixed. --- diff --git a/ChangeLog b/ChangeLog index 62d4027cd..4410912ba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2011-09-17 Bruno Haible + popen: Support for MSVC. + * lib/stdio.in.h (popen): Declare it if the system lacks this function. + * lib/popen.c (popen): Provide alternate definition for native Windows. + * m4/popen.m4 (gl_FUNC_POPEN): Test if popen exists. Set HAVE_POPEN. + * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Initialize HAVE_POPEN. + * modules/popen (Depends-on, configure.ac): Update condition. + * modules/stdio (Makefile.am): Substitute HAVE_POPEN. + * doc/posix-functions/popen.texi: Mention that the MSVC problem is + fixed. + +2011-09-17 Bruno Haible + isnanl, isnand, isnanf: Work around MSVC bug. * lib/isnan.c (FUNC): Use alternate ways of computing NaN and Infinity. diff --git a/doc/posix-functions/popen.texi b/doc/posix-functions/popen.texi index 8629c5875..e9fac3be8 100644 --- a/doc/posix-functions/popen.texi +++ b/doc/posix-functions/popen.texi @@ -9,6 +9,9 @@ Gnulib module: popen Portability problems fixed by Gnulib: @itemize @item +This function is missing on some platforms: +MSVC 9. +@item Some platforms start the child with closed stdin or stdout if the standard descriptors were closed in the parent: Cygwin 1.5.x. @@ -17,9 +20,6 @@ Cygwin 1.5.x. Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -MSVC 9. -@item Some platforms mistakenly set the close-on-exec bit, then if it is cleared by the application, the platform then leaks file descriptors from earlier @code{popen} calls into subsequent @code{popen} children: diff --git a/lib/popen.c b/lib/popen.c index 64a4f619e..433047996 100644 --- a/lib/popen.c +++ b/lib/popen.c @@ -21,12 +21,14 @@ /* Specification. */ #include -#include -#include -#include -#include +#if HAVE_POPEN -#undef popen +# include +# include +# include +# include + +# undef popen FILE * rpl_popen (const char *filename, const char *mode) @@ -80,3 +82,22 @@ rpl_popen (const char *filename, const char *mode) errno = saved_errno; return result; } + +#else +/* Native Woe32 API. */ + +# include + +FILE * +popen (const char *filename, const char *mode) +{ + /* Use binary mode by default. */ + if (strcmp (mode, "r") == 0) + mode = "rb"; + else if (strcmp (mode, "w") == 0) + mode = "wb"; + + return _popen (filename, mode); +} + +#endif diff --git a/lib/stdio.in.h b/lib/stdio.in.h index edf521a8b..a2808fb57 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -781,6 +781,10 @@ _GL_FUNCDECL_RPL (popen, FILE *, (const char *cmd, const char *mode) _GL_ARG_NONNULL ((1, 2))); _GL_CXXALIAS_RPL (popen, FILE *, (const char *cmd, const char *mode)); # else +# if !@HAVE_POPEN@ +_GL_FUNCDECL_SYS (popen, FILE *, (const char *cmd, const char *mode) + _GL_ARG_NONNULL ((1, 2))); +# endif _GL_CXXALIAS_SYS (popen, FILE *, (const char *cmd, const char *mode)); # endif _GL_CXXALIASWARN (popen); diff --git a/m4/popen.m4 b/m4/popen.m4 index c45298145..c958f1950 100644 --- a/m4/popen.m4 +++ b/m4/popen.m4 @@ -1,4 +1,4 @@ -# popen.m4 serial 4 +# popen.m4 serial 5 dnl Copyright (C) 2009-2011 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -7,26 +7,35 @@ dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_POPEN], [ AC_REQUIRE([gl_STDIO_H_DEFAULTS]) - AC_CACHE_CHECK([whether popen works with closed stdin], - [gl_cv_func_popen_works], - [ - AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include -]], [int result = 0; - FILE *child; - fclose (stdin); - fclose (stdout); - child = popen ("echo a", "r"); - if (fgetc (child) != 'a') - result |= 1; - if (pclose (child) != 0) - result |= 2; - return result; -])], [gl_cv_func_popen_works=yes], [gl_cv_func_popen_works=no], - dnl For now, only cygwin 1.5 or older is known to be broken. - [gl_cv_func_popen_works='guessing yes']) - ]) - if test "$gl_cv_func_popen_works" = no; then - REPLACE_POPEN=1 + AC_CHECK_FUNCS_ONCE([popen]) + if test $ac_cv_func_popen = no; then + HAVE_POPEN=0 + else + AC_CACHE_CHECK([whether popen works with closed stdin], + [gl_cv_func_popen_works], + [ + AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include ]], + [[int result = 0; + FILE *child; + fclose (stdin); + fclose (stdout); + child = popen ("echo a", "r"); + if (fgetc (child) != 'a') + result |= 1; + if (pclose (child) != 0) + result |= 2; + return result; + ]])], + [gl_cv_func_popen_works=yes], + [gl_cv_func_popen_works=no], + dnl For now, only cygwin 1.5 or older is known to be broken. + [gl_cv_func_popen_works='guessing yes']) + ]) + if test "$gl_cv_func_popen_works" = no; then + REPLACE_POPEN=1 + fi fi ]) diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index a8326f3c3..d637e1390 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -1,4 +1,4 @@ -# stdio_h.m4 serial 37 +# stdio_h.m4 serial 38 dnl Copyright (C) 2007-2011 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -153,6 +153,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], HAVE_DPRINTF=1; AC_SUBST([HAVE_DPRINTF]) HAVE_FSEEKO=1; AC_SUBST([HAVE_FSEEKO]) HAVE_FTELLO=1; AC_SUBST([HAVE_FTELLO]) + HAVE_POPEN=1; AC_SUBST([HAVE_POPEN]) HAVE_RENAMEAT=1; AC_SUBST([HAVE_RENAMEAT]) HAVE_VASPRINTF=1; AC_SUBST([HAVE_VASPRINTF]) HAVE_VDPRINTF=1; AC_SUBST([HAVE_VDPRINTF]) diff --git a/modules/popen b/modules/popen index 37b3cf6ca..426589ac8 100644 --- a/modules/popen +++ b/modules/popen @@ -7,11 +7,11 @@ m4/popen.m4 Depends-on: stdio -open [test $REPLACE_POPEN = 1] +open [test $HAVE_POPEN = 0 || $REPLACE_POPEN = 1] configure.ac: gl_FUNC_POPEN -if test $REPLACE_POPEN = 1; then +if test $HAVE_POPEN = 0 || $REPLACE_POPEN = 1; then AC_LIBOBJ([popen]) gl_PREREQ_POPEN fi diff --git a/modules/stdio b/modules/stdio index 5fae1b95c..cd21661e7 100644 --- a/modules/stdio +++ b/modules/stdio @@ -93,6 +93,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) -e 's|@''HAVE_DPRINTF''@|$(HAVE_DPRINTF)|g' \ -e 's|@''HAVE_FSEEKO''@|$(HAVE_FSEEKO)|g' \ -e 's|@''HAVE_FTELLO''@|$(HAVE_FTELLO)|g' \ + -e 's|@''HAVE_POPEN''@|$(HAVE_POPEN)|g' \ -e 's|@''HAVE_RENAMEAT''@|$(HAVE_RENAMEAT)|g' \ -e 's|@''HAVE_VASPRINTF''@|$(HAVE_VASPRINTF)|g' \ -e 's|@''HAVE_VDPRINTF''@|$(HAVE_VDPRINTF)|g' \