Add Tramp distrobox method
[emacs.git] / m4 / utimensat.m4
bloba583f37668764563f8e4ca1930000f824c975d1d
1 # utimensat.m4
2 # serial 12
3 dnl Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 # See if we need to provide utimensat replacement.
10 # Written by Eric Blake.
12 AC_DEFUN([gl_FUNC_UTIMENSAT],
14   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17   gl_CHECK_FUNCS_ANDROID([utimensat], [[#include <sys/stat.h>]])
18   if test $ac_cv_func_utimensat = no; then
19     HAVE_UTIMENSAT=0
20     case "$gl_cv_onwards_func_utimensat" in
21       future*) REPLACE_UTIMENSAT=1 ;;
22     esac
23   else
24     AC_CACHE_CHECK([whether utimensat works],
25       [gl_cv_func_utimensat_works],
26       [AC_RUN_IFELSE(
27          [AC_LANG_PROGRAM([[
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 ]GL_MDA_DEFINES],
32             [[int result = 0;
33               const char *f = "conftest.file";
34               if (close (creat (f, 0600)))
35                 return 1;
36               /* Test whether a trailing slash is handled correctly.
37                  This fails on AIX 7.2.  */
38               {
39                 struct timespec ts[2];
40                 ts[0].tv_sec = 345183300; ts[0].tv_nsec = 0;
41                 ts[1] = ts[0];
42                 if (utimensat (AT_FDCWD, "conftest.file/", ts, 0) == 0)
43                   result |= 2;
44               }
45               /* Test whether the AT_SYMLINK_NOFOLLOW flag is supported.  */
46               {
47                 if (utimensat (AT_FDCWD, f, NULL, AT_SYMLINK_NOFOLLOW))
48                   result |= 4;
49               }
50               /* Test whether UTIME_NOW and UTIME_OMIT work.  */
51               {
52                 struct timespec ts[2];
53                 ts[0].tv_sec = 1;
54                 ts[0].tv_nsec = UTIME_OMIT;
55                 ts[1].tv_sec = 1;
56                 ts[1].tv_nsec = UTIME_NOW;
57                 if (utimensat (AT_FDCWD, f, ts, 0))
58                   result |= 8;
59               }
60               sleep (1);
61               {
62                 struct stat st;
63                 struct timespec ts[2];
64                 ts[0].tv_sec = 1;
65                 ts[0].tv_nsec = UTIME_NOW;
66                 ts[1].tv_sec = 1;
67                 ts[1].tv_nsec = UTIME_OMIT;
68                 if (utimensat (AT_FDCWD, f, ts, 0))
69                   result |= 16;
70                 if (stat (f, &st))
71                   result |= 32;
72                 else if (st.st_ctime < st.st_atime)
73                   result |= 64;
74               }
75               return result;
76             ]])],
77          [gl_cv_func_utimensat_works=yes],
78          [case $? in
79             2) gl_cv_func_utimensat_works='nearly' ;;
80             *) gl_cv_func_utimensat_works=no ;;
81           esac
82          ],
83          [case "$host_os" in
84             # Guess yes on Linux or glibc systems.
85             linux-* | linux | *-gnu* | gnu*)
86               gl_cv_func_utimensat_works="guessing yes" ;;
87             # Guess yes on systems that emulate the Linux system calls.
88             midipix*)
89               gl_cv_func_utimensat_works="guessing yes" ;;
90             # Guess 'nearly' on AIX.
91             aix*)
92               gl_cv_func_utimensat_works="guessing nearly" ;;
93             # If we don't know, obey --enable-cross-guesses.
94             *)
95               gl_cv_func_utimensat_works="$gl_cross_guess_normal" ;;
96           esac
97          ])
98       ])
99     case "$gl_cv_func_utimensat_works" in
100       *yes)
101         ;;
102       *nearly)
103         AC_DEFINE([HAVE_NEARLY_WORKING_UTIMENSAT], [1],
104           [Define to 1 if utimensat works, except for the trailing slash handling.])
105         REPLACE_UTIMENSAT=1
106         ;;
107       *)
108         REPLACE_UTIMENSAT=1
109         ;;
110     esac
111   fi