summaryrefslogtreecommitdiff
path: root/acinclude.m4
blob: cd69d7b4bc19c56d9adf3c19ef93f0685f7854d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# $Id: acinclude.m4,v 1.1 2009/04/10 13:39:22 broeker Exp $

# Copyright (c) 2008 Holger Weiss <holger@jhweiss.de>.
#
# This code may freely be used, modified and/or redistributed for any purpose.
# It would be nice if additions and fixes to this file (including trivial code
# cleanups) would be sent back in order to let me include them in the version
# available at <http://www.jhweiss.de/software/snprintf.html>.  However, this is
# not a requirement for using or redistributing (possibly modified) versions of
# this file, nor is leaving this notice intact mandatory.

# HW_HEADER_STDARG_H
# ------------------
# Define HAVE_STDARG_H to 1 if <stdarg.h> is available.
AC_DEFUN([HW_HEADER_STDARG_H],
[
  AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
  AC_CHECK_HEADERS_ONCE([stdarg.h])
])# HW_HEADER_STDARG_H

# HW_HEADER_VARARGS_H
# -------------------
# Define HAVE_VARARGS_H to 1 if <varargs.h> is available.
AC_DEFUN([HW_HEADER_VARARGS_H],
[
  AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
  AC_CHECK_HEADERS_ONCE([varargs.h])
])# HW_HEADER_VARARGS_H

# HW_FUNC_VA_COPY
# ---------------
# Set $hw_cv_func_va_copy to "yes" or "no".  Define HAVE_VA_COPY to 1 if
# $hw_cv_func_va_copy is set to "yes".  Note that it's "unspecified whether
# va_copy and va_end are macros or identifiers declared with external linkage."
# (C99: 7.15.1, 1)  Therefore, the presence of va_copy(3) cannot simply "be
# tested with #ifdef", as suggested by the Autoconf manual (5.5.1).
AC_DEFUN([HW_FUNC_VA_COPY],
[
  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
  AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
  AC_CACHE_CHECK([for va_copy],
    [hw_cv_func_va_copy],
    [AC_RUN_IFELSE(
      [AC_LANG_PROGRAM(
        [[#if HAVE_STDARG_H
        #include <stdarg.h>
        #elif HAVE_VARARGS_H
        #include <varargs.h>
        #endif]],
        [[va_list ap, aq; va_copy(aq, ap);]])],
      [hw_cv_func_va_copy=yes],
      [hw_cv_func_va_copy=no],
      [hw_cv_func_va_copy=no])])
  AS_IF([test "$hw_cv_func_va_copy" = yes],
    [AC_DEFINE([HAVE_VA_COPY], [1],
      [Define to 1 if you have the `va_copy' function or macro.])])
])# HW_FUNC_VA_COPY

# HW_FUNC___VA_COPY
# -----------------
# Set $hw_cv_func___va_copy to "yes" or "no".  Define HAVE___VA_COPY to 1 if
# $hw_cv_func___va_copy is set to "yes".
AC_DEFUN([HW_FUNC___VA_COPY],
[
  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
  AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
  AC_CACHE_CHECK([for __va_copy],
    [hw_cv_func___va_copy],
    [AC_RUN_IFELSE(
      [AC_LANG_PROGRAM(
        [[#if HAVE_STDARG_H
        #include <stdarg.h>
        #elif HAVE_VARARGS_H
        #include <varargs.h>
        #endif]],
        [[va_list ap, aq; __va_copy(aq, ap);]])],
      [hw_cv_func___va_copy=yes],
      [hw_cv_func___va_copy=no],
      [hw_cv_func___va_copy=no])])
  AS_IF([test "$hw_cv_func___va_copy" = yes],
    [AC_DEFINE([HAVE___VA_COPY], [1],
      [Define to 1 if you have the `__va_copy' function or macro.])])
])# HW_FUNC___VA_COPY

# HW_FUNC_VSNPRINTF
# -----------------
# Set $hw_cv_func_vsnprintf and $hw_cv_func_vsnprintf_c99 to "yes" or "no",
# respectively.  Define HAVE_VSNPRINTF to 1 only if $hw_cv_func_vsnprintf_c99
# is set to "yes".  Otherwise, define vsnprintf to rpl_vsnprintf and make sure
# the replacement function will be built.
AC_DEFUN([HW_FUNC_VSNPRINTF],
[
  AC_PREREQ([2.60])dnl 2.59 should work if some AC_TYPE_* macros are replaced.
  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
  AC_CHECK_FUNC([vsnprintf],
    [hw_cv_func_vsnprintf=yes],
    [hw_cv_func_vsnprintf=no])
  AS_IF([test "$hw_cv_func_vsnprintf" = yes],
    [AC_CACHE_CHECK([whether vsnprintf is C99 compliant],
      [hw_cv_func_vsnprintf_c99],
      [AC_RUN_IFELSE(
        [AC_LANG_PROGRAM(
          [[#if HAVE_STDARG_H
          #include <stdarg.h>
          #endif
          #include <stdio.h>
          static int testprintf(char *buf, size_t size, const char *format, ...)
          {
            int result;
            va_list ap;
            va_start(ap, format);
            result = vsnprintf(buf, size, format, ap);
            va_end(ap);
            return result;
          }]],
          [[char buf[43];
          if (testprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
              testprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
              buf[0] != 'T' || buf[3] != '\0')
            return 1;]])],
        [hw_cv_func_vsnprintf_c99=yes],
        [hw_cv_func_vsnprintf_c99=no],
        [hw_cv_func_vsnprintf_c99=no])])],
    [hw_cv_func_snprintf_c99=no])
  AS_IF([test "$hw_cv_func_vsnprintf_c99" = yes],
    [AC_DEFINE([HAVE_VSNPRINTF], [1],
      [Define to 1 if you have a C99 compliant `vsnprintf' function.])],
    [AC_DEFINE([vsnprintf], [rpl_vsnprintf],
      [Define to rpl_vsnprintf if the replacement function should be used.])
    AC_CHECK_HEADERS([inttypes.h locale.h stddef.h stdint.h])
    AC_CHECK_MEMBERS([struct lconv.decimal_point, struct lconv.thousands_sep],
      [], [], [#include <locale.h>])
    AC_TYPE_LONG_DOUBLE
    AC_TYPE_LONG_LONG_INT
    AC_TYPE_UNSIGNED_LONG_LONG_INT
    AC_TYPE_SIZE_T
    AC_TYPE_INTMAX_T
    AC_TYPE_UINTMAX_T
    AC_TYPE_UINTPTR_T
    AC_CHECK_TYPES([ptrdiff_t])
    AC_CHECK_FUNCS([localeconv])
    _HW_FUNC_XPRINTF_REPLACE])
])# HW_FUNC_VSNPRINTF

# HW_FUNC_SNPRINTF
# ----------------
# Set $hw_cv_func_snprintf and $hw_cv_func_snprintf_c99 to "yes" or "no",
# respectively.  Define HAVE_SNPRINTF to 1 only if $hw_cv_func_snprintf_c99
# is set to "yes".  Otherwise, define snprintf to rpl_snprintf and make sure
# the replacement function will be built.
AC_DEFUN([HW_FUNC_SNPRINTF],
[
  AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our snprintf(3) calls vsnprintf(3).
  AC_CHECK_FUNC([snprintf],
    [hw_cv_func_snprintf=yes],
    [hw_cv_func_snprintf=no])
  AS_IF([test "$hw_cv_func_snprintf" = yes],
    [AC_CACHE_CHECK([whether snprintf is C99 compliant],
      [hw_cv_func_snprintf_c99],
      [AC_RUN_IFELSE(
        [AC_LANG_PROGRAM([[#include <stdio.h>]],
          [[char buf[43];
          if (snprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
              snprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
              buf[0] != 'T' || buf[3] != '\0')
            return 1;]])],
        [hw_cv_func_snprintf_c99=yes],
        [hw_cv_func_snprintf_c99=no],
        [hw_cv_func_snprintf_c99=no])])],
    [hw_cv_func_snprintf_c99=no])
  AS_IF([test "$hw_cv_func_snprintf_c99" = yes],
    [AC_DEFINE([HAVE_SNPRINTF], [1],
      [Define to 1 if you have a C99 compliant `snprintf' function.])],
    [AC_DEFINE([snprintf], [rpl_snprintf],
      [Define to rpl_snprintf if the replacement function should be used.])
    _HW_FUNC_XPRINTF_REPLACE])
])# HW_FUNC_SNPRINTF

# HW_FUNC_VASPRINTF
# -----------------
# Set $hw_cv_func_vasprintf to "yes" or "no".  Define HAVE_VASPRINTF to 1 if
# $hw_cv_func_vasprintf is set to "yes".  Otherwise, define vasprintf to
# rpl_vasprintf and make sure the replacement function will be built.
AC_DEFUN([HW_FUNC_VASPRINTF],
[
  AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our vasprintf(3) calls vsnprintf(3).
  AC_CHECK_FUNCS([vasprintf],
    [hw_cv_func_vasprintf=yes],
    [hw_cv_func_vasprintf=no])
  AS_IF([test "$hw_cv_func_vasprintf" = no],
    [AC_DEFINE([vasprintf], [rpl_vasprintf],
      [Define to rpl_vasprintf if the replacement function should be used.])
    AC_CHECK_HEADERS([stdlib.h])
    HW_FUNC_VA_COPY
    AS_IF([test "$hw_cv_func_va_copy" = no],
      [HW_FUNC___VA_COPY])
    _HW_FUNC_XPRINTF_REPLACE])
])# HW_FUNC_VASPRINTF

# HW_FUNC_ASPRINTF
# ----------------
# Set $hw_cv_func_asprintf to "yes" or "no".  Define HAVE_ASPRINTF to 1 if
# $hw_cv_func_asprintf is set to "yes".  Otherwise, define asprintf to
# rpl_asprintf and make sure the replacement function will be built.
AC_DEFUN([HW_FUNC_ASPRINTF],
[
  AC_REQUIRE([HW_FUNC_VASPRINTF])dnl Our asprintf(3) calls vasprintf(3).
  AC_CHECK_FUNCS([asprintf],
    [hw_cv_func_asprintf=yes],
    [hw_cv_func_asprintf=no])
  AS_IF([test "$hw_cv_func_asprintf" = no],
    [AC_DEFINE([asprintf], [rpl_asprintf],
      [Define to rpl_asprintf if the replacement function should be used.])
    _HW_FUNC_XPRINTF_REPLACE])
])# HW_FUNC_ASPRINTF

# _HW_FUNC_XPRINTF_REPLACE
# ------------------------
# Arrange for building snprintf.c.  Must be called if one or more of the
# functions provided by snprintf.c are needed.
AC_DEFUN([_HW_FUNC_XPRINTF_REPLACE],
[
  AS_IF([test "x$_hw_cv_func_xprintf_replace_done" != xyes],
    [AC_C_CONST
    HW_HEADER_STDARG_H
    AC_LIBOBJ([snprintf])
    _hw_cv_func_xprintf_replace_done=yes])
])# _HW_FUNC_XPRINTF_REPLACE

dnl vim: set joinspaces textwidth=80: