1 # Modified by Dave Hart for integration into NTP 4.2.7 <hart@ntp.org>
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.
9 # Changed to honor hw_nodef_snprintf, etc. which prevent config.h from
10 # aliasing snprintf to rpl_snprintf, etc.
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.
17 # $Id: snprintf.m4,v 1.1.1.1 2008/01/06 03:24:00 holger Exp $
19 # Copyright (c) 2008 Holger Weiss <holger@jhweiss.de>.
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.
30 # Define HAVE_STDARG_H to 1 if <stdarg.h> is available.
31 AC_DEFUN([HW_HEADER_STDARG_H],
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
39 # Define HAVE_VARARGS_H to 1 if <varargs.h> is available.
40 AC_DEFUN([HW_HEADER_VARARGS_H],
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
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).
53 AC_DEFUN([HW_FUNC_VA_COPY],
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],
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.])])
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".
79 AC_DEFUN([HW_FUNC___VA_COPY],
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],
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.])])
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.
107 AC_DEFUN([HW_FUNC_VSNPRINTF],
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],
123 static int testprintf(char *buf, size_t size, const char *format, ...)
127 va_start(ap, format);
128 result = vsnprintf(buf, size, format, ap);
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')
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>])
158 AC_TYPE_LONG_LONG_INT
159 AC_TYPE_UNSIGNED_LONG_LONG_INT
164 AC_CHECK_TYPES([ptrdiff_t])
165 AC_CHECK_FUNCS([localeconv])
166 _HW_FUNC_XPRINTF_REPLACE])
167 ])# HW_FUNC_VSNPRINTF
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
177 AC_DEFUN([HW_FUNC_SNPRINTF],
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],
187 [AC_LANG_PROGRAM([[#include <stdio.h>]],
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')
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])
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
220 AC_DEFUN([HW_FUNC_VASPRINTF],
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])
241 AS_IF([test "$hw_cv_func_va_copy" = no],
243 _HW_FUNC_XPRINTF_REPLACE])
244 ])# HW_FUNC_VASPRINTF
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
253 AC_DEFUN([HW_FUNC_ASPRINTF],
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])
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.
279 AC_DEFUN([_HW_FUNC_XPRINTF_REPLACE],
281 AS_IF([test "x$_hw_cv_func_xprintf_replace_done" != xyes],
284 AC_LIBOBJ([snprintf])
285 _hw_cv_func_xprintf_replace_done=yes])
286 ])# _HW_FUNC_XPRINTF_REPLACE
288 dnl vim: set joinspaces textwidth=80: