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