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