futimens: fix configure check
[gnulib.git] / m4 / futimens.m4
1 # serial 4
2 # See if we need to provide futimens replacement.
3
4 dnl Copyright (C) 2009, 2010 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.
8
9 # Written by Eric Blake.
10
11 AC_DEFUN([gl_FUNC_FUTIMENS],
12 [
13   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   AC_CHECK_FUNCS_ONCE([futimens])
16   if test $ac_cv_func_futimens = no; then
17     HAVE_FUTIMENS=0
18     AC_LIBOBJ([futimens])
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 ]], [[struct timespec ts[2] = { { 1, UTIME_OMIT }, { 1, UTIME_NOW } };
27       int fd = creat ("conftest.file", 0600);
28       struct stat st;
29       if (fd < 0) return 1;
30       if (futimens (AT_FDCWD, NULL) == 0) return 2;
31       if (futimens (fd, ts)) return 3;
32       sleep (1);
33       ts[0].tv_nsec = UTIME_NOW;
34       ts[1].tv_nsec = UTIME_OMIT;
35       if (futimens (fd, ts)) return 4;
36       if (fstat (fd, &st)) return 5;
37       if (st.st_ctime < st.st_atime) return 6;
38       ]])],
39 dnl FIXME: simplify this in 2012, when file system bugs are no longer common
40          [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
41 #ifdef __linux__
42 /* The Linux kernel added futimens in 2.6.22, but has bugs with UTIME_OMIT
43    in several file systems as recently as 2.6.32.  Always replace futimens
44    to support older kernels.  */
45 choke me
46 #endif
47       ]])],
48            [gl_cv_func_futimens_works=yes],
49            [gl_cv_func_futimens_works="needs runtime check"])],
50          [gl_cv_func_futimens_works=no],
51          [gl_cv_func_futimens_works="guessing no"])
52       rm -f conftest.file])
53     if test "$gl_cv_func_futimens_works" != yes; then
54       REPLACE_FUTIMENS=1
55       AC_LIBOBJ([futimens])
56     fi
57   fi
58 ])