summaryrefslogtreecommitdiff
path: root/sntp/m4/snprintf.m4
blob: 15f06f0805c195eb168aa420f063eff76998bc83 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Modified by Dave Hart for integration into NTP 4.2.7 <hart@ntp.org>
#
# Changed in a backwards-incompatible way to separate HAVE_SNPRINTF from
# HW_WANT_RPL_SNPRINTF, etc. for each of the four replaced functions.
# HAVE_* will always be set if the corresponding HW_FUNC_* macro is
# invoked, directly or indirectly.  This allows 3rd-party modules like
# libopts to avoid their own replacement of snprintf.
#
# Changed to honor hw_nodef_snprintf, etc. which prevent config.h from
# aliasing snprintf to rpl_snprintf, etc.
#
# Changed to honor hw_force_rpl_snprintf=yes, etc.  This is used by NTP
# to test rpl_snprintf() and rpl_vsnprintf() on platforms which provide
# C99-compliant implementations.
#

# $Id: snprintf.m4,v 1.1.1.1 2008/01/06 03:24:00 holger 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.  If either $hw_force_rpl_vsnprintf is "yes" or
# $hw_cv_func_vsnprintf_c99 is "no", define HW_WANT_RPL_VSNPRINTF and
# define vsnprintf to rpl_vsnprintf.
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_vsnprintf_c99=no])
  AC_DEFINE([HAVE_VSNPRINTF], [1],
      [Define if C99-compliant `vsnprintf' is available.])
  AC_MSG_CHECKING([if C99-snprintf replacement vsnprintf will be used])
  AS_IF([test "${hw_force_rpl_vsnprintf=no}" = yes -o "$hw_cv_func_vsnprintf_c99" = no],
    [hw_use_rpl_vsnprintf=yes],
    [hw_use_rpl_vsnprintf=no])
  AC_MSG_RESULT([$hw_use_rpl_vsnprintf])
  AS_IF([test "$hw_use_rpl_vsnprintf" = yes],
    [AC_DEFINE([HW_WANT_RPL_VSNPRINTF], [1],
      [Define to provide `rpl_vsnprintf' function.])
    AS_IF([test ${hw_nodef_vsnprintf=no} = no],
      [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.  If either $hw_force_rpl_snprintf is "yes" or
# $hw_cv_func_snprintf_c99 is "no", define HW_WANT_RPL_SNPRINTF and
# define snprintf to rpl_snprintf.
# The same will be done for vsnprintf, as if HW_FUNC_VSNPRINTF were
# used.
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])
  AC_DEFINE([HAVE_SNPRINTF], [1],
      [Define if C99-compliant `snprintf' is available.])
  AC_MSG_CHECKING([if C99-snprintf replacement snprintf will be used])
  AS_IF([test "${hw_force_rpl_snprintf=no}" = yes -o "$hw_cv_func_snprintf_c99" = no],
    [hw_use_rpl_snprintf=yes],
    [hw_use_rpl_snprintf=no])
  AC_MSG_RESULT([$hw_use_rpl_snprintf])
  AS_IF([test "$hw_use_rpl_snprintf" = yes],
    [AC_DEFINE([HW_WANT_RPL_SNPRINTF], [1],
      [Define to provide `rpl_snprintf' function.])
    AS_IF([test ${hw_nodef_snprintf=no} = no],
      [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".  If either
# $hw_force_rpl_vasprintf is "yes" or $hw_cv_func_vasprintf is "no",
# define HW_WANT_RPL_VASPRINTF and define vasprintf to rpl_vasprintf.
# The same will be done for vsnprintf, as if HW_FUNC_VSNPRINTF were
# used.
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])
  AC_DEFINE([HAVE_VASPRINTF], [1],
      [Define if `vasprintf' is available.])
  AC_MSG_CHECKING([if C99-snprintf replacement vasprintf will be used])
  AS_IF([test "${hw_force_rpl_vasprintf=no}" = yes -o "$hw_cv_func_vasprintf" = no],
    [hw_use_rpl_vasprintf=yes],
    [hw_use_rpl_vasprintf=no])
  AC_MSG_RESULT([$hw_use_rpl_vasprintf])
  AS_IF([test "$hw_use_rpl_vasprintf" = yes],
    [AC_DEFINE([HW_WANT_RPL_VASPRINTF], [1],
      [Define to provide `rpl_vasprintf' function.])
    AS_IF([test ${hw_nodef_vasprintf=no} = 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".  If either
# $hw_force_rpl_asprintf is "yes" or $hw_cv_func_asprintf is "no",
# define HW_WANT_RPL_ASPRINTF and define asprintf to rpl_asprintf.
# The same will be done for vasprintf, as if HW_FUNC_VASPRINTF were
# used.
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])
  AC_DEFINE([HAVE_ASPRINTF], [1],
      [Define if `asprintf' is available.])
  AC_MSG_CHECKING([if C99-snprintf replacement asprintf will be used])
  AS_IF([test "${hw_force_rpl_asprintf=no}" = yes -o "$hw_cv_func_asprintf" = no],
    [hw_use_rpl_asprintf=yes],
    [hw_use_rpl_asprintf=no])
  AC_MSG_RESULT([$hw_use_rpl_asprintf])
  AS_IF([test "$hw_use_rpl_asprintf" = yes],
    [AC_DEFINE([HW_WANT_RPL_ASPRINTF], [1],
      [Define to provide `rpl_asprintf' function.])
    AS_IF([test ${hw_nodef_asprintf=no} = 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: