Imported Upstream version 3.8
[platform/upstream/diffutils.git] / m4 / time_r.m4
1 dnl Reentrant time functions: localtime_r, gmtime_r.
2
3 dnl Copyright (C) 2003, 2006-2021 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.
7
8 dnl Written by Paul Eggert.
9
10 AC_DEFUN([gl_TIME_R],
11 [
12   dnl Persuade glibc and Solaris <time.h> to declare localtime_r.
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
15   AC_REQUIRE([gl_TIME_H_DEFAULTS])
16   AC_REQUIRE([AC_C_RESTRICT])
17
18   dnl Some systems don't declare localtime_r() and gmtime_r() if _REENTRANT is
19   dnl not defined.
20   AC_CHECK_DECLS([localtime_r], [], [],
21     [[/* mingw's <time.h> provides the functions asctime_r, ctime_r,
22          gmtime_r, localtime_r only if <unistd.h> or <pthread.h> has
23          been included before.  */
24       #if defined __MINGW32__
25       # include <unistd.h>
26       #endif
27       #include <time.h>
28     ]])
29   if test $ac_cv_have_decl_localtime_r = no; then
30     HAVE_DECL_LOCALTIME_R=0
31   fi
32
33   AC_CHECK_FUNCS_ONCE([localtime_r])
34   if test $ac_cv_func_localtime_r = yes; then
35     HAVE_LOCALTIME_R=1
36     AC_CACHE_CHECK([whether localtime_r is compatible with its POSIX signature],
37       [gl_cv_time_r_posix],
38       [AC_COMPILE_IFELSE(
39          [AC_LANG_PROGRAM(
40             [[/* mingw's <time.h> provides the functions asctime_r, ctime_r,
41                  gmtime_r, localtime_r only if <unistd.h> or <pthread.h> has
42                  been included before.  */
43               #if defined __MINGW32__
44               # include <unistd.h>
45               #endif
46               #include <time.h>
47             ]],
48             [[/* We don't need to append 'restrict's to the argument types,
49                  even though the POSIX signature has the 'restrict's,
50                  since C99 says they can't affect type compatibility.  */
51               struct tm * (*ptr) (time_t const *, struct tm *) = localtime_r;
52               if (ptr) return 0;
53               /* Check the return type is a pointer.
54                  On HP-UX 10 it is 'int'.  */
55               *localtime_r (0, 0);]])
56          ],
57          [gl_cv_time_r_posix=yes],
58          [gl_cv_time_r_posix=no])
59       ])
60     if test $gl_cv_time_r_posix = yes; then
61       REPLACE_LOCALTIME_R=0
62     else
63       REPLACE_LOCALTIME_R=1
64     fi
65   else
66     HAVE_LOCALTIME_R=0
67     dnl On mingw, localtime_r() is defined as an inline function; use through a
68     dnl direct function call works but the use as a function pointer leads to a
69     dnl link error.
70     AC_CACHE_CHECK([whether localtime_r exists as an inline function],
71       [gl_cv_func_localtime_r_inline],
72       [AC_LINK_IFELSE(
73          [AC_LANG_PROGRAM(
74             [[/* mingw's <time.h> provides the functions asctime_r, ctime_r,
75                  gmtime_r, localtime_r only if <unistd.h> or <pthread.h> has
76                  been included before.  */
77               #if defined __MINGW32__
78               # include <unistd.h>
79               #endif
80               #include <time.h>
81             ]],
82             [[time_t a;
83               struct tm r;
84               localtime_r (&a, &r);
85             ]])
86          ],
87          [gl_cv_func_localtime_r_inline=yes],
88          [gl_cv_func_localtime_r_inline=no])
89       ])
90     if test $gl_cv_func_localtime_r_inline = yes; then
91       REPLACE_LOCALTIME_R=1
92     fi
93   fi
94 ])
95
96 # Prerequisites of lib/time_r.c.
97 AC_DEFUN([gl_PREREQ_TIME_R], [
98   :
99 ])