2 * have_varvs - try <varargs.h> to see if it really works with vsprintf()
4 * Copyright (C) 1999 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.2 $
21 * @(#) $Id: have_varvs.c,v 30.2 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/have_varvs.c,v $
24 * Under source code control: 1995/09/09 22:41:10
25 * File existed as early as: 1995
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 * Some systems have bugs in the <varargs.h> implementation that show up in
33 * vsprintf(), so we may have to try to use sprintf() as if it were vsprintf()
34 * and hope for the best.
36 * This program will output #defines and exits 0 if vsprintf() (or sprintf())
37 * produces the results that we expect. This program exits 1 if vsprintf()
38 * (or sprintf()) produces unexpected results while using the <stdarg.h>
45 #include "have_unistd.h"
46 #if defined(HAVE_UNISTD_H)
50 #include "have_string.h"
55 #undef VSPRINTF_SIZE_T
56 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
58 # define VSPRINTF_SIZE_T size_t
60 # define VSPRINTF_SIZE_T long
65 #if !defined(STDARG) && !defined(SIMULATE_STDARG)
69 try_this(char *fmt
, ...)
75 #if !defined(DONT_HAVE_VSPRINTF)
76 vsprintf(buf
, fmt
, ap
);
78 sprintf(buf
, fmt
, ap
);
85 try_nthis(char *fmt
, VSPRINTF_SIZE_T size
, ...)
91 #if !defined(DONT_HAVE_VSPRINTF)
92 vsnprintf(buf
, size
, fmt
, ap
);
94 snprintf(buf
, size
, fmt
, ap
);
103 try_this(char *a
, int b
, char *c
, int d
)
109 try_nthis(char *a
, VSPRINTF_SIZE_T size
, int b
, char *c
, int d
)
126 * test variable args and vsprintf/sprintf
128 try_this("@%d:%s:%d@", 1, "hi", 2);
129 if (strcmp(buf
, "@1:hi:2@") != 0) {
130 #if !defined(DONT_HAVE_VSPRINTF)
131 /* <varargs.h> with vsprintf() didn't work */
133 /* <varargs.h> with sprintf() simulating vsprintf() didn't work */
137 try_this("%s %d%s%d%d %s",
138 "Landon Noll 1st proved that", 2, "^", 23209, -1, "was prime");
140 "Landon Noll 1st proved that 2^23209-1 was prime") != 0) {
141 #if !defined(DONT_HAVE_VSPRINTF)
142 /* <varargs.h> with vsprintf() didn't work */
144 /* <varargs.h> with sprintf() simulating vsprintf() didn't work */
150 * test variable args and vsnprintf/snprintf
152 try_nthis("@%d:%s:%d@", sizeof(buf
)-1, 1, "hello", 5);
153 if (strcmp(buf
, "@1:hello:5@") != 0) {
154 #if !defined(DONT_HAVE_VSPRINTF)
155 /* <varargs.h> with vsnprintf() didn't work */
157 /* <varargs.h> with snprintf() simulating vsnprintf() didn't work */
161 try_nthis("%s %d%s%d%d %s", sizeof(buf
)-1,
162 "Landon Noll 1st proved that", 2, "^", 23209, -1, "was prime");
164 "Landon Noll 1st proved that 2^23209-1 was prime") != 0) {
165 #if !defined(DONT_HAVE_VSPRINTF)
166 /* <varargs.h> with vsnprintf() didn't work */
168 /* <varargs.h> with snprintf() simulating vsnprintf() didn't work */
176 puts("/* what type of variable args do we have? */");
177 puts("#define VARARGS /* use <varargs.h> */");
178 puts("#include <varargs.h>");
179 puts("\n/* should we use vsprintf() and vsnprintf()? */");
180 #if !defined(DONT_HAVE_VSPRINTF)
181 puts("#define HAVE_VSPRINTF /* yes */");
184 puts(" * Hack aleart!!!");
186 puts(" * Systems that do not have vsprintf() need something. In some");
187 puts(" * cases the sprintf function will deal correctly with the");
188 puts(" * va_alist 3rd arg. Same gors for a lack of an vsnprintf()");
189 puts(" * function. In either case we use the #defines below and");
190 puts(" * hope for the best!");
192 puts("#define vsprintf sprintf");
193 puts("#define vsnprintf snprintf");
194 puts("#undef HAVE_VSPRINTF");