xref: /freebsd/contrib/ntp/sntp/m4/snprintf.m4 (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1# Modified by Dave Hart for integration into NTP 4.2.7 <hart@ntp.org>
2#
3# Changed in a backwards-incompatible way to separate HAVE_SNPRINTF from
4# HW_WANT_RPL_SNPRINTF, etc. for each of the four replaced functions.
5# HAVE_* will always be set if the corresponding HW_FUNC_* macro is
6# invoked, directly or indirectly.  This allows 3rd-party modules like
7# libopts to avoid their own replacement of snprintf.
8#
9# Changed to honor hw_nodef_snprintf, etc. which prevent config.h from
10# aliasing snprintf to rpl_snprintf, etc.
11#
12# Changed to honor hw_force_rpl_snprintf=yes, etc.  This is used by NTP
13# to test rpl_snprintf() and rpl_vsnprintf() on platforms which provide
14# C99-compliant implementations.
15#
16
17# $Id: snprintf.m4,v 1.1.1.1 2008/01/06 03:24:00 holger Exp $
18
19# Copyright (c) 2008 Holger Weiss <holger@jhweiss.de>.
20#
21# This code may freely be used, modified and/or redistributed for any purpose.
22# It would be nice if additions and fixes to this file (including trivial code
23# cleanups) would be sent back in order to let me include them in the version
24# available at <http://www.jhweiss.de/software/snprintf.html>.  However, this is
25# not a requirement for using or redistributing (possibly modified) versions of
26# this file, nor is leaving this notice intact mandatory.
27
28# HW_HEADER_STDARG_H
29# ------------------
30# Define HAVE_STDARG_H to 1 if <stdarg.h> is available.
31AC_DEFUN([HW_HEADER_STDARG_H],
32[
33  AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
34  AC_CHECK_HEADERS_ONCE([stdarg.h])
35])# HW_HEADER_STDARG_H
36
37# HW_HEADER_VARARGS_H
38# -------------------
39# Define HAVE_VARARGS_H to 1 if <varargs.h> is available.
40AC_DEFUN([HW_HEADER_VARARGS_H],
41[
42  AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
43  AC_CHECK_HEADERS_ONCE([varargs.h])
44])# HW_HEADER_VARARGS_H
45
46# HW_FUNC_VA_COPY
47# ---------------
48# Set $hw_cv_func_va_copy to "yes" or "no".  Define HAVE_VA_COPY to 1 if
49# $hw_cv_func_va_copy is set to "yes".  Note that it's "unspecified whether
50# va_copy and va_end are macros or identifiers declared with external linkage."
51# (C99: 7.15.1, 1)  Therefore, the presence of va_copy(3) cannot simply "be
52# tested with #ifdef", as suggested by the Autoconf manual (5.5.1).
53AC_DEFUN([HW_FUNC_VA_COPY],
54[
55  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
56  AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
57  AC_CACHE_CHECK([for va_copy],
58    [hw_cv_func_va_copy],
59    [AC_RUN_IFELSE(
60      [AC_LANG_PROGRAM(
61        [[#if HAVE_STDARG_H
62        #include <stdarg.h>
63        #elif HAVE_VARARGS_H
64        #include <varargs.h>
65        #endif]],
66        [[va_list ap, aq; va_copy(aq, ap);]])],
67      [hw_cv_func_va_copy=yes],
68      [hw_cv_func_va_copy=no],
69      [hw_cv_func_va_copy=no])])
70  AS_IF([test "$hw_cv_func_va_copy" = yes],
71    [AC_DEFINE([HAVE_VA_COPY], [1],
72      [Define to 1 if you have the `va_copy' function or macro.])])
73])# HW_FUNC_VA_COPY
74
75# HW_FUNC___VA_COPY
76# -----------------
77# Set $hw_cv_func___va_copy to "yes" or "no".  Define HAVE___VA_COPY to 1 if
78# $hw_cv_func___va_copy is set to "yes".
79AC_DEFUN([HW_FUNC___VA_COPY],
80[
81  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
82  AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
83  AC_CACHE_CHECK([for __va_copy],
84    [hw_cv_func___va_copy],
85    [AC_RUN_IFELSE(
86      [AC_LANG_PROGRAM(
87        [[#if HAVE_STDARG_H
88        #include <stdarg.h>
89        #elif HAVE_VARARGS_H
90        #include <varargs.h>
91        #endif]],
92        [[va_list ap, aq; __va_copy(aq, ap);]])],
93      [hw_cv_func___va_copy=yes],
94      [hw_cv_func___va_copy=no],
95      [hw_cv_func___va_copy=no])])
96  AS_IF([test "$hw_cv_func___va_copy" = yes],
97    [AC_DEFINE([HAVE___VA_COPY], [1],
98      [Define to 1 if you have the `__va_copy' function or macro.])])
99])# HW_FUNC___VA_COPY
100
101# HW_FUNC_VSNPRINTF
102# -----------------
103# Set $hw_cv_func_vsnprintf and $hw_cv_func_vsnprintf_c99 to "yes" or
104# "no", respectively.  If either $hw_force_rpl_vsnprintf is "yes" or
105# $hw_cv_func_vsnprintf_c99 is "no", define HW_WANT_RPL_VSNPRINTF and
106# define vsnprintf to rpl_vsnprintf.
107AC_DEFUN([HW_FUNC_VSNPRINTF],
108[
109  AC_PREREQ([2.60])dnl 2.59 should work if some AC_TYPE_* macros are replaced.
110  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
111  AC_CHECK_FUNC([vsnprintf],
112    [hw_cv_func_vsnprintf=yes],
113    [hw_cv_func_vsnprintf=no])
114  AS_IF([test "$hw_cv_func_vsnprintf" = yes],
115    [AC_CACHE_CHECK([whether vsnprintf is C99 compliant],
116      [hw_cv_func_vsnprintf_c99],
117      [AC_RUN_IFELSE(
118        [AC_LANG_PROGRAM(
119          [[#if HAVE_STDARG_H
120          #include <stdarg.h>
121          #endif
122          #include <stdio.h>
123          static int testprintf(char *buf, size_t size, const char *format, ...)
124          {
125            int result;
126            va_list ap;
127            va_start(ap, format);
128            result = vsnprintf(buf, size, format, ap);
129            va_end(ap);
130            return result;
131          }]],
132          [[char buf[43];
133          if (testprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
134              testprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
135              buf[0] != 'T' || buf[3] != '\0')
136            return 1;]])],
137        [hw_cv_func_vsnprintf_c99=yes],
138        [hw_cv_func_vsnprintf_c99=no],
139        [hw_cv_func_vsnprintf_c99=no])])],
140    [hw_cv_func_vsnprintf_c99=no])
141  AC_DEFINE([HAVE_VSNPRINTF], [1],
142      [Define if C99-compliant `vsnprintf' is available.])
143  AC_MSG_CHECKING([if C99-snprintf replacement vsnprintf will be used])
144  AS_IF([test "${hw_force_rpl_vsnprintf=no}" = yes -o "$hw_cv_func_vsnprintf_c99" = no],
145    [hw_use_rpl_vsnprintf=yes],
146    [hw_use_rpl_vsnprintf=no])
147  AC_MSG_RESULT([$hw_use_rpl_vsnprintf])
148  AS_IF([test "$hw_use_rpl_vsnprintf" = yes],
149    [AC_DEFINE([HW_WANT_RPL_VSNPRINTF], [1],
150      [Define to provide `rpl_vsnprintf' function.])
151    AS_IF([test ${hw_nodef_vsnprintf=no} = no],
152      [AC_DEFINE([vsnprintf], [rpl_vsnprintf],
153        [Define to rpl_vsnprintf if the replacement function should be used.])])
154    AC_CHECK_HEADERS([inttypes.h locale.h stddef.h stdint.h])
155    AC_CHECK_MEMBERS([struct lconv.decimal_point, struct lconv.thousands_sep],
156      [], [], [#include <locale.h>])
157    AC_TYPE_LONG_DOUBLE
158    AC_TYPE_LONG_LONG_INT
159    AC_TYPE_UNSIGNED_LONG_LONG_INT
160    AC_TYPE_SIZE_T
161    AC_TYPE_INTMAX_T
162    AC_TYPE_UINTMAX_T
163    AC_TYPE_UINTPTR_T
164    AC_CHECK_TYPES([ptrdiff_t])
165    AC_CHECK_FUNCS([localeconv])
166    _HW_FUNC_XPRINTF_REPLACE])
167])# HW_FUNC_VSNPRINTF
168
169# HW_FUNC_SNPRINTF
170# ----------------
171# Set $hw_cv_func_snprintf and $hw_cv_func_snprintf_c99 to "yes" or
172# "no", respectively.  If either $hw_force_rpl_snprintf is "yes" or
173# $hw_cv_func_snprintf_c99 is "no", define HW_WANT_RPL_SNPRINTF and
174# define snprintf to rpl_snprintf.
175# The same will be done for vsnprintf, as if HW_FUNC_VSNPRINTF were
176# used.
177AC_DEFUN([HW_FUNC_SNPRINTF],
178[
179  AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our snprintf(3) calls vsnprintf(3).
180  AC_CHECK_FUNC([snprintf],
181    [hw_cv_func_snprintf=yes],
182    [hw_cv_func_snprintf=no])
183  AS_IF([test "$hw_cv_func_snprintf" = yes],
184    [AC_CACHE_CHECK([whether snprintf is C99 compliant],
185      [hw_cv_func_snprintf_c99],
186      [AC_RUN_IFELSE(
187        [AC_LANG_PROGRAM([[#include <stdio.h>]],
188          [[char buf[43];
189          if (snprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
190              snprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
191              buf[0] != 'T' || buf[3] != '\0')
192            return 1;]])],
193        [hw_cv_func_snprintf_c99=yes],
194        [hw_cv_func_snprintf_c99=no],
195        [hw_cv_func_snprintf_c99=no])])],
196    [hw_cv_func_snprintf_c99=no])
197  AC_DEFINE([HAVE_SNPRINTF], [1],
198      [Define if C99-compliant `snprintf' is available.])
199  AC_MSG_CHECKING([if C99-snprintf replacement snprintf will be used])
200  AS_IF([test "${hw_force_rpl_snprintf=no}" = yes -o "$hw_cv_func_snprintf_c99" = no],
201    [hw_use_rpl_snprintf=yes],
202    [hw_use_rpl_snprintf=no])
203  AC_MSG_RESULT([$hw_use_rpl_snprintf])
204  AS_IF([test "$hw_use_rpl_snprintf" = yes],
205    [AC_DEFINE([HW_WANT_RPL_SNPRINTF], [1],
206      [Define to provide `rpl_snprintf' function.])
207    AS_IF([test ${hw_nodef_snprintf=no} = no],
208      [AC_DEFINE([snprintf], [rpl_snprintf],
209        [Define to rpl_snprintf if the replacement function should be used.])])
210    _HW_FUNC_XPRINTF_REPLACE])
211])# HW_FUNC_SNPRINTF
212
213# HW_FUNC_VASPRINTF
214# -----------------
215# Set $hw_cv_func_vasprintf to "yes" or "no".  If either
216# $hw_force_rpl_vasprintf is "yes" or $hw_cv_func_vasprintf is "no",
217# define HW_WANT_RPL_VASPRINTF and define vasprintf to rpl_vasprintf.
218# The same will be done for vsnprintf, as if HW_FUNC_VSNPRINTF were
219# used.
220AC_DEFUN([HW_FUNC_VASPRINTF],
221[
222  AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our vasprintf(3) calls vsnprintf(3).
223  AC_CHECK_FUNCS([vasprintf],
224    [hw_cv_func_vasprintf=yes],
225    [hw_cv_func_vasprintf=no])
226  AC_DEFINE([HAVE_VASPRINTF], [1],
227      [Define if `vasprintf' is available.])
228  AC_MSG_CHECKING([if C99-snprintf replacement vasprintf will be used])
229  AS_IF([test "${hw_force_rpl_vasprintf=no}" = yes -o "$hw_cv_func_vasprintf" = no],
230    [hw_use_rpl_vasprintf=yes],
231    [hw_use_rpl_vasprintf=no])
232  AC_MSG_RESULT([$hw_use_rpl_vasprintf])
233  AS_IF([test "$hw_use_rpl_vasprintf" = yes],
234    [AC_DEFINE([HW_WANT_RPL_VASPRINTF], [1],
235      [Define to provide `rpl_vasprintf' function.])
236    AS_IF([test ${hw_nodef_vasprintf=no} = no],
237      [AC_DEFINE([vasprintf], [rpl_vasprintf],
238      [Define to rpl_vasprintf if the replacement function should be used.])])
239    AC_CHECK_HEADERS([stdlib.h])
240    HW_FUNC_VA_COPY
241    AS_IF([test "$hw_cv_func_va_copy" = no],
242      [HW_FUNC___VA_COPY])
243    _HW_FUNC_XPRINTF_REPLACE])
244])# HW_FUNC_VASPRINTF
245
246# HW_FUNC_ASPRINTF
247# ----------------
248# Set $hw_cv_func_asprintf to "yes" or "no".  If either
249# $hw_force_rpl_asprintf is "yes" or $hw_cv_func_asprintf is "no",
250# define HW_WANT_RPL_ASPRINTF and define asprintf to rpl_asprintf.
251# The same will be done for vasprintf, as if HW_FUNC_VASPRINTF were
252# used.
253AC_DEFUN([HW_FUNC_ASPRINTF],
254[
255  AC_REQUIRE([HW_FUNC_VASPRINTF])dnl Our asprintf(3) calls vasprintf(3).
256  AC_CHECK_FUNCS([asprintf],
257    [hw_cv_func_asprintf=yes],
258    [hw_cv_func_asprintf=no])
259  AC_DEFINE([HAVE_ASPRINTF], [1],
260      [Define if `asprintf' is available.])
261  AC_MSG_CHECKING([if C99-snprintf replacement asprintf will be used])
262  AS_IF([test "${hw_force_rpl_asprintf=no}" = yes -o "$hw_cv_func_asprintf" = no],
263    [hw_use_rpl_asprintf=yes],
264    [hw_use_rpl_asprintf=no])
265  AC_MSG_RESULT([$hw_use_rpl_asprintf])
266  AS_IF([test "$hw_use_rpl_asprintf" = yes],
267    [AC_DEFINE([HW_WANT_RPL_ASPRINTF], [1],
268      [Define to provide `rpl_asprintf' function.])
269    AS_IF([test ${hw_nodef_asprintf=no} = no],
270      [AC_DEFINE([asprintf], [rpl_asprintf],
271      [Define to rpl_asprintf if the replacement function should be used.])])
272    _HW_FUNC_XPRINTF_REPLACE])
273])# HW_FUNC_ASPRINTF
274
275# _HW_FUNC_XPRINTF_REPLACE
276# ------------------------
277# Arrange for building snprintf.c.  Must be called if one or more of the
278# functions provided by snprintf.c are needed.
279AC_DEFUN([_HW_FUNC_XPRINTF_REPLACE],
280[
281  AS_IF([test "x$_hw_cv_func_xprintf_replace_done" != xyes],
282    [AC_C_CONST
283    HW_HEADER_STDARG_H
284    AC_LIBOBJ([snprintf])
285    _hw_cv_func_xprintf_replace_done=yes])
286])# _HW_FUNC_XPRINTF_REPLACE
287
288dnl vim: set joinspaces textwidth=80:
289