Merge branch 'stable'
[gnulib.git] / m4 / mkstemp.m4
1 #serial 22
2
3 # Copyright (C) 2001, 2003-2007, 2009-2012 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
7
8 # On some hosts (e.g., HP-UX 10.20, SunOS 4.1.4, Solaris 2.5.1), mkstemp has a
9 # silly limit that it can create no more than 26 files from a given template.
10 # Other systems lack mkstemp altogether.
11 # On OSF1/Tru64 V4.0F, the system-provided mkstemp function can create
12 # only 32 files per process.
13 # On some hosts, mkstemp creates files with mode 0666, which is a security
14 # problem and a violation of POSIX 2008.
15 # On systems like the above, arrange to use the replacement function.
16 AC_DEFUN([gl_FUNC_MKSTEMP],
17 [
18   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
19
20   AC_CHECK_FUNCS_ONCE([mkstemp])
21   if test $ac_cv_func_mkstemp = yes; then
22     AC_CACHE_CHECK([for working mkstemp],
23       [gl_cv_func_working_mkstemp],
24       [
25         mkdir conftest.mkstemp
26         AC_RUN_IFELSE(
27           [AC_LANG_PROGRAM(
28             [AC_INCLUDES_DEFAULT],
29             [[int result = 0;
30               int i;
31               off_t large = (off_t) 4294967295u;
32               if (large < 0)
33                 large = 2147483647;
34               umask (0);
35               for (i = 0; i < 70; i++)
36                 {
37                   char templ[] = "conftest.mkstemp/coXXXXXX";
38                   int (*mkstemp_function) (char *) = mkstemp;
39                   int fd = mkstemp_function (templ);
40                   if (fd < 0)
41                     result |= 1;
42                   else
43                     {
44                       struct stat st;
45                       if (lseek (fd, large, SEEK_SET) != large)
46                         result |= 2;
47                       if (fstat (fd, &st) < 0)
48                         result |= 4;
49                       else if (st.st_mode & 0077)
50                         result |= 8;
51                       if (close (fd))
52                         result |= 16;
53                     }
54                 }
55               return result;]])],
56           [gl_cv_func_working_mkstemp=yes],
57           [gl_cv_func_working_mkstemp=no],
58           [gl_cv_func_working_mkstemp="guessing no"])
59         rm -rf conftest.mkstemp
60       ])
61     if test "$gl_cv_func_working_mkstemp" != yes; then
62       REPLACE_MKSTEMP=1
63     fi
64   else
65     HAVE_MKSTEMP=0
66   fi
67 ])
68
69 # Prerequisites of lib/mkstemp.c.
70 AC_DEFUN([gl_PREREQ_MKSTEMP],
71 [
72 ])