# HG changeset patch # User Bruno Haible # Date 1548622994 -3600 # Node ID 5c7e0484c1e9dcc95a29585e7a279db4caf49af3 # Parent 069b50a66104e273d168334873ff242341fb61bf tmpfile: Add support for Android. * m4/tmpfile.m4 (gl_FUNC_TMPFILE): Add a runtime test whether tmpfile() works. * lib/tmpfile.c (tmpfile): Add an alternative implementation for Android. * modules/tmpfile (Depends-on): Add 'stdbool'. * doc/posix-functions/tmpfile.texi: Mention the Android bug. * modules/argv-iter-tests (Depends-on): Add 'tmpfile'. diff -r 069b50a66104 -r 5c7e0484c1e9 ChangeLog --- a/ChangeLog Sun Jan 27 18:49:36 2019 +0100 +++ b/ChangeLog Sun Jan 27 22:03:14 2019 +0100 @@ -1,3 +1,14 @@ +2019-01-27 Bruno Haible + + tmpfile: Add support for Android. + * m4/tmpfile.m4 (gl_FUNC_TMPFILE): Add a runtime test whether tmpfile() + works. + * lib/tmpfile.c (tmpfile): Add an alternative implementation for + Android. + * modules/tmpfile (Depends-on): Add 'stdbool'. + * doc/posix-functions/tmpfile.texi: Mention the Android bug. + * modules/argv-iter-tests (Depends-on): Add 'tmpfile'. + 2019-01-27 Akim Demaille bitsetv: allow free on NULL. diff -r 069b50a66104 -r 5c7e0484c1e9 doc/posix-functions/tmpfile.texi --- a/doc/posix-functions/tmpfile.texi Sun Jan 27 18:49:36 2019 +0100 +++ b/doc/posix-functions/tmpfile.texi Sun Jan 27 22:03:14 2019 +0100 @@ -9,6 +9,9 @@ Portability problems fixed by Gnulib: @itemize @item +This function always fails on some platforms: +Android 4.3. +@item This function often fails for trivial reasons on some platforms: mingw, MSVC 14. @item diff -r 069b50a66104 -r 5c7e0484c1e9 lib/tmpfile.c --- a/lib/tmpfile.c Sun Jan 27 18:49:36 2019 +0100 +++ b/lib/tmpfile.c Sun Jan 27 22:03:14 2019 +0100 @@ -21,24 +21,36 @@ /* Specification. */ #include -/* This replacement is used only on native Windows platforms. */ +#include + +#if defined _WIN32 && ! defined __CYGWIN__ +/* A native Windows platforms. */ + +# include +# include +# include +# include -#include -#include -#include -#include +# include + +# define WIN32_LEAN_AND_MEAN /* avoid including junk */ +# include -#include +#else -#define WIN32_LEAN_AND_MEAN /* avoid including junk */ -#include +# include + +#endif #include "pathmax.h" #include "tempname.h" #include "tmpdir.h" /* PATH_MAX is guaranteed to be defined, because this replacement is only - used on native Windows. */ + used on native Windows and Android. */ + +#if defined _WIN32 && ! defined __CYGWIN__ +/* A native Windows platforms. */ /* On Windows, opening a file with _O_TEMPORARY has the effect of passing the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile(), which has the effect @@ -130,3 +142,38 @@ return NULL; } + +#else + +FILE * +tmpfile (void) +{ + char buf[PATH_MAX]; + int fd; + FILE *fp; + + /* Try $TMPDIR first, not /tmp nor P_tmpdir, because we need this replacement + on Android, and /tmp does not exist on Android. */ + + if (path_search (buf, sizeof buf, NULL, "tmpf", true)) + return NULL; + + fd = gen_tempname (buf, 0, 0, GT_FILE); + if (fd < 0) + return NULL; + + /* Note that this relies on the Unix semantics that + a file is not really removed until it is closed. */ + (void) unlink (buf); + + if ((fp = fdopen (fd, "w+b")) == NULL) + { + int saved_errno = errno; + close (fd); + errno = saved_errno; + } + + return fp; +} + +#endif diff -r 069b50a66104 -r 5c7e0484c1e9 m4/tmpfile.m4 --- a/m4/tmpfile.m4 Sun Jan 27 18:49:36 2019 +0100 +++ b/m4/tmpfile.m4 Sun Jan 27 22:03:14 2019 +0100 @@ -1,4 +1,4 @@ -# tmpfile.m4 serial 3 +# tmpfile.m4 serial 4 # Copyright (C) 2007, 2009-2019 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, @@ -18,20 +18,44 @@ # directory, even though tmpfile wouldn't work in general. Instead, # just test for a Windows platform (excluding Cygwin). +# On Android 4.3, tmpfile() always returns NULL, even if TMPDIR is set +# to a writable directory. + AC_DEFUN([gl_FUNC_TMPFILE], [ AC_REQUIRE([gl_STDIO_H_DEFAULTS]) - AC_CACHE_CHECK([whether tmpfile should be overridden], - [gl_cv_func_tmpfile_unusable], + AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles + AC_CACHE_CHECK([whether tmpfile works], + [gl_cv_func_tmpfile_works], [AC_EGREP_CPP([choke me], [ #if defined _WIN32 && !defined __CYGWIN__ choke me #endif ], - [gl_cv_func_tmpfile_unusable=yes], - [gl_cv_func_tmpfile_unusable=no])]) - if test $gl_cv_func_tmpfile_unusable = yes; then - REPLACE_TMPFILE=1 - fi + [gl_cv_func_tmpfile_works=no], + [AC_RUN_IFELSE( + [AC_LANG_SOURCE([[ +#include +#include +int +main (void) +{ + return tmpfile () == NULL; +}]])], + [gl_cv_func_tmpfile_works=yes], + [gl_cv_func_tmpfile_works=no], + [case "$host_os" in + # Guess no on Android. + linux*-android*) gl_cv_func_tmpfile_works="guessing no" ;; + # Guess yes otherwise. + *) gl_cv_func_tmpfile_works="guessing yes" ;; + esac + ]) + ]) + ]) + case "$gl_cv_func_tmpfile_works" in + *yes) ;; + *) REPLACE_TMPFILE=1 ;; + esac ]) # Prerequisites of lib/tmpfile.c. diff -r 069b50a66104 -r 5c7e0484c1e9 modules/argv-iter-tests --- a/modules/argv-iter-tests Sun Jan 27 18:49:36 2019 +0100 +++ b/modules/argv-iter-tests Sun Jan 27 22:03:14 2019 +0100 @@ -3,6 +3,7 @@ tests/macros.h Depends-on: +tmpfile configure.ac: diff -r 069b50a66104 -r 5c7e0484c1e9 modules/tmpfile --- a/modules/tmpfile Sun Jan 27 18:49:36 2019 +0100 +++ b/modules/tmpfile Sun Jan 27 22:03:14 2019 +0100 @@ -9,6 +9,7 @@ stdio largefile pathmax [test $REPLACE_TMPFILE = 1] +stdbool [test $REPLACE_TMPFILE = 1] tempname [test $REPLACE_TMPFILE = 1] tmpdir [test $REPLACE_TMPFILE = 1]