Do the Windows oldnames workaround through the C++ GNULIB_NAMESPACE.
[gnulib.git] / m4 / fcntl-o.m4
blob3ef061d84875f0a01b39876dc8a7061ec6efa3d3
1 # fcntl-o.m4 serial 7
2 dnl Copyright (C) 2006, 2009-2020 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 dnl Written by Paul Eggert.
9 AC_PREREQ([2.60])
11 # Test whether the flags O_NOATIME and O_NOFOLLOW actually work.
12 # Define HAVE_WORKING_O_NOATIME to 1 if O_NOATIME works, or to 0 otherwise.
13 # Define HAVE_WORKING_O_NOFOLLOW to 1 if O_NOFOLLOW works, or to 0 otherwise.
14 AC_DEFUN([gl_FCNTL_O_FLAGS],
16   dnl Persuade glibc <fcntl.h> to define O_NOATIME and O_NOFOLLOW.
17   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
19   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
20   AC_CHECK_HEADERS_ONCE([unistd.h])
21   AC_CHECK_FUNCS_ONCE([symlink])
22   AC_CACHE_CHECK([for working fcntl.h], [gl_cv_header_working_fcntl_h],
23     [AC_RUN_IFELSE(
24        [AC_LANG_PROGRAM(
25           [[#include <sys/types.h>
26            #include <sys/stat.h>
27            #if HAVE_UNISTD_H
28            # include <unistd.h>
29            #else /* on Windows with MSVC */
30            # include <io.h>
31            # include <stdlib.h>
32            # defined sleep(n) _sleep ((n) * 1000)
33            #endif
34            #include <fcntl.h>
35            ]GL_MDA_DEFINES[
36            #ifndef O_NOATIME
37             #define O_NOATIME 0
38            #endif
39            #ifndef O_NOFOLLOW
40             #define O_NOFOLLOW 0
41            #endif
42            static int const constants[] =
43             {
44               O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND,
45               O_NONBLOCK, O_SYNC, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY
46             };
47           ]],
48           [[
49             int result = !constants;
50             #if HAVE_SYMLINK
51             {
52               static char const sym[] = "conftest.sym";
53               if (symlink ("/dev/null", sym) != 0)
54                 result |= 2;
55               else
56                 {
57                   int fd = open (sym, O_WRONLY | O_NOFOLLOW | O_CREAT, 0);
58                   if (fd >= 0)
59                     {
60                       close (fd);
61                       result |= 4;
62                     }
63                 }
64               if (unlink (sym) != 0 || symlink (".", sym) != 0)
65                 result |= 2;
66               else
67                 {
68                   int fd = open (sym, O_RDONLY | O_NOFOLLOW);
69                   if (fd >= 0)
70                     {
71                       close (fd);
72                       result |= 4;
73                     }
74                 }
75               unlink (sym);
76             }
77             #endif
78             {
79               static char const file[] = "confdefs.h";
80               int fd = open (file, O_RDONLY | O_NOATIME);
81               if (fd < 0)
82                 result |= 8;
83               else
84                 {
85                   struct stat st0;
86                   if (fstat (fd, &st0) != 0)
87                     result |= 16;
88                   else
89                     {
90                       char c;
91                       sleep (1);
92                       if (read (fd, &c, 1) != 1)
93                         result |= 24;
94                       else
95                         {
96                           if (close (fd) != 0)
97                             result |= 32;
98                           else
99                             {
100                               struct stat st1;
101                               if (stat (file, &st1) != 0)
102                                 result |= 40;
103                               else
104                                 if (st0.st_atime != st1.st_atime)
105                                   result |= 64;
106                             }
107                         }
108                     }
109                 }
110             }
111             return result;]])],
112        [gl_cv_header_working_fcntl_h=yes],
113        [case $? in #(
114         4) gl_cv_header_working_fcntl_h='no (bad O_NOFOLLOW)';; #(
115         64) gl_cv_header_working_fcntl_h='no (bad O_NOATIME)';; #(
116         68) gl_cv_header_working_fcntl_h='no (bad O_NOATIME, O_NOFOLLOW)';; #(
117          *) gl_cv_header_working_fcntl_h='no';;
118         esac],
119        [case "$host_os" in
120                   # Guess 'no' on native Windows.
121           mingw*) gl_cv_header_working_fcntl_h='no' ;;
122           *)      gl_cv_header_working_fcntl_h=cross-compiling ;;
123         esac
124        ])
125     ])
127   case $gl_cv_header_working_fcntl_h in #(
128   *O_NOATIME* | no | cross-compiling) ac_val=0;; #(
129   *) ac_val=1;;
130   esac
131   AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOATIME], [$ac_val],
132     [Define to 1 if O_NOATIME works.])
134   case $gl_cv_header_working_fcntl_h in #(
135   *O_NOFOLLOW* | no | cross-compiling) ac_val=0;; #(
136   *) ac_val=1;;
137   esac
138   AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOFOLLOW], [$ac_val],
139     [Define to 1 if O_NOFOLLOW works.])