immutable: Add tests.
[gnulib.git] / m4 / futimens.m4
blob2e9f53e5a81e2adb7fb853a4763bb5b95e598cec
1 # serial 9
2 # See if we need to provide futimens replacement.
4 dnl Copyright (C) 2009-2021 Free Software Foundation, Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
9 # Written by Eric Blake.
11 AC_DEFUN([gl_FUNC_FUTIMENS],
13   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
14   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   AC_CHECK_FUNCS_ONCE([futimens])
17   if test $ac_cv_func_futimens = no; then
18     HAVE_FUTIMENS=0
19   else
20     AC_CACHE_CHECK([whether futimens works],
21       [gl_cv_func_futimens_works],
22       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27 ]GL_MDA_DEFINES],
28      [[struct timespec ts[2];
29       int fd = creat ("conftest.file", 0600);
30       struct stat st;
31       if (fd < 0) return 1;
32       ts[0].tv_sec = 1;
33       ts[0].tv_nsec = UTIME_OMIT;
34       ts[1].tv_sec = 1;
35       ts[1].tv_nsec = UTIME_NOW;
36       errno = 0;
37       if (futimens (AT_FDCWD, NULL) == 0) return 2;
38       if (errno != EBADF) return 3;
39       if (futimens (fd, ts)) return 4;
40       sleep (1);
41       ts[0].tv_nsec = UTIME_NOW;
42       ts[1].tv_nsec = UTIME_OMIT;
43       if (futimens (fd, ts)) return 5;
44       if (fstat (fd, &st)) return 6;
45       if (st.st_ctime < st.st_atime) return 7;
46       ]])],
47          [gl_cv_func_futimens_works=yes],
48          [gl_cv_func_futimens_works=no],
49          [case "$host_os" in
50                            # Guess no on glibc systems.
51             *-gnu* | gnu*) gl_cv_func_futimens_works="guessing no" ;;
52                            # Guess no on musl systems.
53             *-musl*)       gl_cv_func_futimens_works="guessing no" ;;
54                            # Guess yes otherwise.
55             *)             gl_cv_func_futimens_works="guessing yes" ;;
56           esac
57          ])
58       rm -f conftest.file])
59     case "$gl_cv_func_futimens_works" in
60       *yes) ;;
61       *)
62         REPLACE_FUTIMENS=1
63         ;;
64     esac
65   fi