modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / have_stdvs.c
blob8b704f95f2f06009b570e50f0d27e5586be01000
1 /*
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>
42 * include file.
46 #include <stdio.h>
48 #include "have_unistd.h"
49 #if defined(HAVE_UNISTD_H)
50 #include <unistd.h>
51 #endif
52 #include <stdarg.h>
54 #include "have_string.h"
55 #ifdef HAVE_STRING_H
56 # include <string.h>
57 #endif
59 #undef VSPRINTF_SIZE_T
60 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
61 defined(__cplusplus)
62 # define VSPRINTF_SIZE_T size_t
63 #else
64 # define VSPRINTF_SIZE_T long
65 #endif
67 char buf[BUFSIZ];
70 void
71 try_this(char *fmt, ...)
73 va_list ap;
75 va_start(ap, fmt);
76 #if !defined(DONT_HAVE_VSPRINTF)
77 vsprintf(buf, fmt, ap);
78 #else
79 sprintf(buf, fmt, ap);
80 #endif
81 va_end(ap);
85 void
86 try_nthis(char *fmt, VSPRINTF_SIZE_T size, ...)
88 va_list ap;
90 va_start(ap, fmt);
91 #if !defined(DONT_HAVE_VSPRINTF)
92 vsnprintf(buf, size, fmt, ap);
93 #else
94 snprintf(buf, size, fmt, ap);
95 #endif
96 va_end(ap);
101 main(void)
104 * setup
106 buf[0] = '\0';
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 */
115 #else
116 /* <stdarg.h> with sprintf() simulating vsprintf() didn't work */
117 #endif
118 exit(1);
120 try_this("%s %d%s%d%d %s",
121 "Landon Noll 1st coproved that", 2, "^", 21701, -1, "was prime");
122 if (strcmp(buf,
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 */
126 #else
127 /* <stdarg.h> with sprintf() simulating vsprintf() didn't work */
128 #endif
129 exit(1);
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 */
139 #else
140 /* <stdarg.h> with snprintf() simulating vsnprintf() didn't work */
141 #endif
142 exit(1);
144 try_nthis("%s %d%s%d%d %s", sizeof(buf)-1,
145 "Landon Noll 1st proved that", 2, "^", 23209, -1, "was prime");
146 if (strcmp(buf,
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 */
150 #else
151 /* <stdarg.h> with snprintf() simulating vsnprintf() didn't work */
152 #endif
153 exit(1);
157 * report the result
159 puts("/* what type of variable args do we have? */");
160 #if defined(DONT_HAVE_VSPRINTF)
161 puts("/*");
162 puts(" * SIMULATE_STDARG");
163 puts(" *");
164 puts(" * WARNING: This type of stdarg makes assumptions "
165 "about the stack");
166 puts(" * that may not be true on your system. "
167 "You may want to");
168 puts(" * define STDARG (if using ANSI C) or VARARGS.");
169 puts(" */");
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> */");
177 #else
178 puts("#define STDARG /* use <stdarg.h> */");
179 puts("#include <stdarg.h>");
180 #endif
181 puts("\n/* should we use vsprintf() and vsnprintf()? */");
182 #if !defined(DONT_HAVE_VSPRINTF)
183 puts("#define HAVE_VSPRINTF /* yes */");
184 #else
185 puts("/*");
186 puts(" * Hack aleart!!!");
187 puts(" *");
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!");
193 puts(" */");
194 puts("#define vsprintf sprintf");
195 puts("#define vsnprintf snprintf");
196 puts("#undef HAVE_VSPRINTF");
197 #endif
198 /* exit(0); */
199 return 0;