Ensure Automake does not drop ~~gnulib.m4.
[gnulib.git] / m4 / futimens.m4
blobdc5cfa94119b851b73141d5cc43607050d3c7f92
1 # serial 8
2 # See if we need to provide futimens replacement.
4 dnl Copyright (C) 2009-2020 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 ]], [[struct timespec ts[2];
28       int fd = creat ("conftest.file", 0600);
29       struct stat st;
30       if (fd < 0) return 1;
31       ts[0].tv_sec = 1;
32       ts[0].tv_nsec = UTIME_OMIT;
33       ts[1].tv_sec = 1;
34       ts[1].tv_nsec = UTIME_NOW;
35       errno = 0;
36       if (futimens (AT_FDCWD, NULL) == 0) return 2;
37       if (errno != EBADF) return 3;
38       if (futimens (fd, ts)) return 4;
39       sleep (1);
40       ts[0].tv_nsec = UTIME_NOW;
41       ts[1].tv_nsec = UTIME_OMIT;
42       if (futimens (fd, ts)) return 5;
43       if (fstat (fd, &st)) return 6;
44       if (st.st_ctime < st.st_atime) return 7;
45       ]])],
46          [gl_cv_func_futimens_works=yes],
47          [gl_cv_func_futimens_works=no],
48          [case "$host_os" in
49                            # Guess no on glibc systems.
50             *-gnu* | gnu*) gl_cv_func_futimens_works="guessing no" ;;
51                            # Guess no on musl systems.
52             *-musl*)       gl_cv_func_futimens_works="guessing no" ;;
53                            # Guess yes otherwise.
54             *)             gl_cv_func_futimens_works="guessing yes" ;;
55           esac
56          ])
57       rm -f conftest.file])
58     case "$gl_cv_func_futimens_works" in
59       *yes) ;;
60       *)
61         REPLACE_FUTIMENS=1
62         ;;
63     esac
64   fi