2 * have_stdvs - try <stdarg.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_stdvs.c,v 30.2 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/have_stdvs.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 * On some systems that have both <stdarg.h> and <varargs.h>, vsprintf()
33 * does not work well under one type of include file.
35 * Some systems (such as UMIPS) have bugs in the <stdarg.h> implementation
36 * that show up in vsprintf(), so we may have to try to use sprintf()
37 * as if it were vsprintf() and hope for the best.
39 * This program will output #defines and exits 0 if vsprintf() (or sprintf())
40 * produces the results that we expect. This program exits 1 if vsprintf()
41 * (or sprintf()) produces unexpected results while using the <stdarg.h>
48 #include "have_unistd.h"
49 #if defined(HAVE_UNISTD_H)
54 #include "have_string.h"
59 #undef VSPRINTF_SIZE_T
60 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
62 # define VSPRINTF_SIZE_T size_t
64 # define VSPRINTF_SIZE_T long
71 try_this(char *fmt
, ...)
76 #if !defined(DONT_HAVE_VSPRINTF)
77 vsprintf(buf
, fmt
, ap
);
79 sprintf(buf
, fmt
, ap
);
86 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
);
109 * test variable args and vsprintf/sprintf
111 try_this("@%d:%s:%d@", 1, "hi", 2);
112 if (strcmp(buf
, "@1:hi:2@") != 0) {
113 #if !defined(DONT_HAVE_VSPRINTF)
114 /* <stdarg.h> with vsprintf() didn't work */
116 /* <stdarg.h> with sprintf() simulating vsprintf() didn't work */
120 try_this("%s %d%s%d%d %s",
121 "Landon Noll 1st coproved that", 2, "^", 21701, -1, "was prime");
123 "Landon Noll 1st coproved that 2^21701-1 was prime") != 0) {
124 #if !defined(DONT_HAVE_VSPRINTF)
125 /* <stdarg.h> with vsprintf() didn't work */
127 /* <stdarg.h> with sprintf() simulating vsprintf() didn't work */
133 * test variable args and vsnprintf/snprintf
135 try_nthis("@%d:%s:%d@", sizeof(buf
)-1, 1, "hello", 5);
136 if (strcmp(buf
, "@1:hello:5@") != 0) {
137 #if !defined(DONT_HAVE_VSPRINTF)
138 /* <stdarg.h> with vsnprintf() didn't work */
140 /* <stdarg.h> with snprintf() simulating vsnprintf() didn't work */
144 try_nthis("%s %d%s%d%d %s", sizeof(buf
)-1,
145 "Landon Noll 1st proved that", 2, "^", 23209, -1, "was prime");
147 "Landon Noll 1st proved that 2^23209-1 was prime") != 0) {
148 #if !defined(DONT_HAVE_VSPRINTF)
149 /* <stdarg.h> with vsnprintf() didn't work */
151 /* <stdarg.h> with snprintf() simulating vsnprintf() didn't work */
159 puts("/* what type of variable args do we have? */");
160 #if defined(DONT_HAVE_VSPRINTF)
162 puts(" * SIMULATE_STDARG");
164 puts(" * WARNING: This type of stdarg makes assumptions "
166 puts(" * that may not be true on your system. "
168 puts(" * define STDARG (if using ANSI C) or VARARGS.");
170 puts("typedef char *va_list;");
171 puts("#define va_start(ap,parmn) (void)((ap) = (char*)(&(parmn) + 1))");
172 puts("#define va_end(ap) (void)((ap) = 0)");
173 puts("#define va_arg(ap, type) \\");
174 puts(" (((type*)((ap) = ((ap) + sizeof(type))))[-1])");
175 puts("#define SIMULATE_STDARG "
176 "/* use std_arg.h to simulate <stdarg.h> */");
178 puts("#define STDARG /* use <stdarg.h> */");
179 puts("#include <stdarg.h>");
181 puts("\n/* should we use vsprintf() and vsnprintf()? */");
182 #if !defined(DONT_HAVE_VSPRINTF)
183 puts("#define HAVE_VSPRINTF /* yes */");
186 puts(" * Hack aleart!!!");
188 puts(" * Systems that do not have vsprintf() need something. In some");
189 puts(" * cases the sprintf function will deal correctly with the");
190 puts(" * va_alist 3rd arg. Same gors for a lack of an vsnprintf()");
191 puts(" * function. In either case we use the #defines below and");
192 puts(" * hope for the best!");
194 puts("#define vsprintf sprintf");
195 puts("#define vsnprintf snprintf");
196 puts("#undef HAVE_VSPRINTF");