modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / etc / calc / have_varvs.c
blob066a01125fa0947a70ffa8cdd21ec336d88231f7
1 /*
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>
39 * include file.
43 #include <stdio.h>
45 #include "have_unistd.h"
46 #if defined(HAVE_UNISTD_H)
47 #include <unistd.h>
48 #endif
50 #include "have_string.h"
51 #ifdef HAVE_STRING_H
52 # include <string.h>
53 #endif
55 #undef VSPRINTF_SIZE_T
56 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
57 defined(__cplusplus)
58 # define VSPRINTF_SIZE_T size_t
59 #else
60 # define VSPRINTF_SIZE_T long
61 #endif
63 char buf[BUFSIZ];
65 #if !defined(STDARG) && !defined(SIMULATE_STDARG)
66 #include <varargs.h>
68 void
69 try_this(char *fmt, ...)
71 va_list ap;
73 va_start(ap);
75 #if !defined(DONT_HAVE_VSPRINTF)
76 vsprintf(buf, fmt, ap);
77 #else
78 sprintf(buf, fmt, ap);
79 #endif
81 va_end(ap);
84 void
85 try_nthis(char *fmt, VSPRINTF_SIZE_T size, ...)
87 va_list ap;
89 va_start(ap);
91 #if !defined(DONT_HAVE_VSPRINTF)
92 vsnprintf(buf, size, fmt, ap);
93 #else
94 snprintf(buf, size, fmt, ap);
95 #endif
97 va_end(ap);
100 #else
102 void
103 try_this(char *a, int b, char *c, int d)
105 return;
108 void
109 try_nthis(char *a, VSPRINTF_SIZE_T size, int b, char *c, int d)
111 return;
114 #endif
118 main(void)
121 * setup
123 buf[0] = '\0';
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 */
132 #else
133 /* <varargs.h> with sprintf() simulating vsprintf() didn't work */
134 #endif
135 exit(1);
137 try_this("%s %d%s%d%d %s",
138 "Landon Noll 1st proved that", 2, "^", 23209, -1, "was prime");
139 if (strcmp(buf,
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 */
143 #else
144 /* <varargs.h> with sprintf() simulating vsprintf() didn't work */
145 #endif
146 exit(1);
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 */
156 #else
157 /* <varargs.h> with snprintf() simulating vsnprintf() didn't work */
158 #endif
159 exit(1);
161 try_nthis("%s %d%s%d%d %s", sizeof(buf)-1,
162 "Landon Noll 1st proved that", 2, "^", 23209, -1, "was prime");
163 if (strcmp(buf,
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 */
167 #else
168 /* <varargs.h> with snprintf() simulating vsnprintf() didn't work */
169 #endif
170 exit(1);
174 * report the result
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 */");
182 #else
183 puts("/*");
184 puts(" * Hack aleart!!!");
185 puts(" *");
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!");
191 puts(" */");
192 puts("#define vsprintf sprintf");
193 puts("#define vsnprintf snprintf");
194 puts("#undef HAVE_VSPRINTF");
195 #endif
196 /* exit(0); */
197 return 0;