modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / etc / calc / longbits.c
blob5be5cf1f57ff945a0a65974480468e2018a6ee2f
1 /*
2 * longbits - Determine the number if bits in a char, short, int or long
4 * Copyright (C) 1999-2007 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: longbits.c,v 30.2 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/longbits.c,v $
24 * Under source code control: 1994/03/18 03:06:18
25 * File existed as early as: 1994
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 * usage:
33 * longbits [long_bit_size]
35 * long_bit_size force size of long (must be 32 or 64)
37 * NOTE: If long_bit_size arg is empty (0 chars long) or it begins with
38 * a whitespace character, it will be ignored and no forcing will
39 * be done.
41 * Not all (in fact very few) C pre-processors can do:
43 * #if sizeof(long) == 8
45 * so we have to form LONG_BITS ahead of time.
47 * This prog outputs several defines and typedefs:
49 * LONG_BITS
50 * Number of bits in a long. Not all (in fact very few) C
51 * pre-processors can do #if sizeof(long) == 8.
53 * USB8 unsigned 8 bit value
54 * SB8 signed 8 bit value
56 * USB16 unsigned 16 bit value
57 * SB16 signed 16 bit value
59 * USB32 unsigned 32 bit value
60 * SB32 signed 32 bit value
62 * HAVE_B64
63 * defined ==> ok to use USB64 (unsigned 64 bit value)
64 * and SB64 (signed 64 bit value)
65 * undefined ==> do not use USB64 nor SB64
67 * BOOL_B84
68 * If HAVE_B64 undefined ==> FALSE
69 * If HAVE_B64 defined ==> TRUE
71 * USB64 unsigned 64 bit value if HAVE_B64 is defined
72 * SB64 signed 64 bit value if HAVE_B64 is defined
74 * L(x) form a 33 to 64 bit signed constant
75 * U(x) form a 33 to 64 bit unsigned constant
77 * We hide the comments within strings to avoid complaints from some snitty
78 * compilers. We also hide 3 X's which is the calc symbol for "something bogus
79 * this way comes". In such error cases, we add -=*#*=- to force a syntax
80 * error in the resulting .h file.
82 * We will exit 0 if all is well, non-zero with an error to stderr otherwise.
86 #include <stdio.h>
87 #include <ctype.h>
89 #include "have_unistd.h"
90 #if defined(HAVE_UNISTD_H)
91 #include <unistd.h>
92 #endif
94 #include "have_stdlib.h"
95 #ifdef HAVE_STDLIB_H
96 # include <stdlib.h>
97 #endif
99 #if defined(__linux)
100 # if !defined(isascii)
101 E_FUNC int isascii(int c);
102 # endif /* !isascii */
103 #endif /* __linux */
105 char *program; /* our name */
108 main(int argc, char **argv)
110 int exitcode = 0; /* how we will exit */
111 size_t long_bits = 0; /* bit length of a long */
112 int forced_size = 0; /* 1 => size of long was forced via arg */
113 char value; /* signed or maybe unsigned character */
116 * parse args
118 program = argv[0];
119 switch (argc) {
120 case 1:
121 long_bits = sizeof(long)*8;
122 break;
123 case 2:
124 /* ignore empty or leading space args */
125 if (argv[1][0] == '\0' ||
126 (isascii((int)argv[1][0]) && isspace((int)argv[1][0]))) {
127 long_bits = sizeof(long)*8;
128 /* process the forced size arg */
129 } else {
130 forced_size = 1;
131 long_bits = atoi(argv[1]);
132 if (long_bits != 32 && long_bits != 64) {
133 fprintf(stderr,
134 "usage: %s [32 or 64]\n", program);
135 exit(1);
138 break;
139 default:
140 fprintf(stderr, "usage: %s [32 or 64]\n", program);
141 exit(2);
145 * report size of long
147 printf("#undef LONG_BITS\n");
148 printf("#define LONG_BITS %ld\t\t/%s/\n",
149 (long int)long_bits, "* bit length of a long *");
150 putchar('\n');
153 * If we are forcing the size of a long, then do not check
154 * sizes of other values but instead assume that the user
155 * knows what they are doing.
157 if (forced_size) {
160 * note that the size was forced
162 printf("/%s/\n\n", "* size of long was forced *");
165 * forced forming of USB8, SB8, USB16 and SB16
167 printf("typedef unsigned char USB8;\t/%s/\n",
168 "* unsigned 8 bits *");
169 printf("typedef signed char SB8;\t/%s/\n\n",
170 "* signed 8 bits *");
172 printf("typedef unsigned short USB16;\t/%s/\n",
173 "* unsigned 16 bits *");
174 printf("typedef short SB16;\t\t/%s/\n\n",
175 "* signed 16 bits *");
178 * forced forming of USB32 and SB32
180 if (long_bits == 32) {
181 /* forced 32 bit long mode assumptions */
182 printf("typedef unsigned long USB32;\t/%s/\n",
183 "* unsigned 32 bits *");
184 printf("typedef long SB32;\t\t/%s/\n\n",
185 "* signed 32 bits *");
186 } else {
187 /* forced 64 bit long mode assumptions */
188 printf("typedef unsigned int USB32;\t/%s/\n",
189 "* unsigned 32 bits *");
190 printf("typedef int SB32;\t\t/%s/\n\n",
191 "* signed 32 bits *");
195 * forced forming of HAVE_B64, USB64, SB64, U(x) and L(x)
197 printf("#undef HAVE_B64\n");
198 printf("#define HAVE_B64\t\t/%s/\n",
199 "* have USB64 and SB64 types *");
200 printf("typedef unsigned long long USB64;\t/%s/\n",
201 "* unsigned 64 bits *");
202 printf("typedef long long SB64;\t\t/%s/\n",
203 "* signed 64 bits *");
204 putchar('\n');
205 printf("/%s/\n","* how to form 64 bit constants *");
206 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
207 defined(__cplusplus)
208 printf("#define U(x) x ## ULL\n");
209 printf("#define L(x) x ## LL\n");
210 #else
211 printf("#define U(x) ((unsigned long long)x)\n");
212 printf("#define L(x) ((long long)x)\n");
213 #endif
216 * all done
218 exit(0);
222 * look for 8 bit values
224 value = (char)-1;
225 if (sizeof(char) != 1) {
226 fprintf(stderr,
227 "%s: OUCH!!! - char is not a single byte!\n", program);
228 fprintf(stderr,
229 "%s: fix the USB8 typedef by hand\n", program);
230 printf("typedef unsigned char USB8;\t/* XX%s/ -=*#*=-\n",
231 "X - should be 8 unsigned bits but is not *");
232 if (value < 1) {
233 printf("typedef char SB8;\t\t/* XX%s/ -=*#*=-\n",
234 "X - should be 8 signed bits but is not *");
235 } else {
236 printf("typedef signed char SB8;\t\t/* XX%s/ -=*#*=-\n",
237 "X - should be 8 signed bits but is not *");
239 exitcode = 3;
240 } else {
241 printf("typedef unsigned char USB8;\t/%s/\n",
242 "* unsigned 8 bits *");
243 if (value < 1) {
244 printf("typedef char SB8;\t\t/%s/\n",
245 "* signed 8 bits *");
246 } else {
247 printf("typedef signed char SB8;\t\t/%s/\n",
248 "* signed 8 bits *");
251 putchar('\n');
254 * look for 16 bit values
256 if (sizeof(short) != 2) {
257 fprintf(stderr,
258 "%s: OUCH!!! - short is not a 2 bytes!\n", program);
259 fprintf(stderr,
260 "%s: fix the USB16 typedef by hand\n", program);
261 printf("typedef unsigned short USB16;\t/* XX%s/ -=*#*=-\n",
262 "X - should be 16 unsigned bits but is not *");
263 printf("typedef signed char SB16;\t/* XX%s/ -=*#*=-\n",
264 "X - should be 16 signed bits but is not *");
265 exitcode = 4;
266 } else {
267 printf("typedef unsigned short USB16;\t/%s/\n",
268 "* unsigned 16 bits *");
269 printf("typedef short SB16;\t\t/%s/\n",
270 "* signed 16 bits *");
272 putchar('\n');
275 * look for 32 bit values
277 if (sizeof(long) == 4) {
278 printf("typedef unsigned long USB32;\t/%s/\n",
279 "* unsigned 32 bits *");
280 printf("typedef long SB32;\t\t/%s/\n",
281 "* signed 32 bits *");
282 } else if (sizeof(int) == 4) {
283 printf("typedef unsigned int USB32;\t/%s/\n",
284 "* unsigned 32 bits *");
285 printf("typedef int SB32;\t\t/%s/\n",
286 "* signed 32 bits *");
287 } else {
288 fprintf(stderr,
289 "%s: OUCH!!! - neither int nor long are 4 bytes!\n", program);
290 printf("typedef unsigned int USB32;\t/* XX%s/ -=*#*=-\n",
291 "X - should be 32 unsigned bits but is not *");
292 printf("typedef signed int SB32;\t/* XX%s/ -=*#*=-\n",
293 "X - should be 32 signed bits but is not *");
294 exitcode = 5;
296 putchar('\n');
299 * look for 64 bit values
301 if (sizeof(long) == 8) {
302 printf("#undef HAVE_B64\n");
303 printf("#define HAVE_B64\t\t/%s/\n",
304 "* have USB64 and SB64 types *");
305 printf("typedef unsigned long USB64;\t/%s/\n",
306 "* unsigned 64 bits *");
307 printf("typedef long SB64;\t\t/%s/\n",
308 "* signed 64 bits *");
309 putchar('\n');
310 printf("/%s/\n","* how to form 64 bit constants *");
311 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
312 defined(__cplusplus)
313 printf("#define U(x) x ## UL\n");
314 printf("#define L(x) x ## L\n");
315 #else
316 printf("#define U(x) ((unsigned long)x)\n");
317 printf("#define L(x) ((long)x)\n");
318 #endif
319 } else {
320 printf("#undef HAVE_B64\n");
321 printf("#define HAVE_B64\t\t/%s/\n",
322 "* have USB64 and SB64 types *");
323 printf("typedef unsigned long long USB64;\t/%s/\n",
324 "* unsigned 64 bits *");
325 printf("typedef long long SB64;\t\t/%s/\n",
326 "* signed 64 bits *");
327 putchar('\n');
328 printf("/%s/\n","* how to form 64 bit constants *");
329 #if defined(FORCE_STDC) || (defined(__STDC__) && __STDC__ != 0) || \
330 defined(__cplusplus)
331 printf("#define U(x) x ## ULL\n");
332 printf("#define L(x) x ## LL\n");
333 #else
334 printf("#define U(x) ((unsigned long long)x)\n");
335 printf("#define L(x) ((long long)x)\n");
336 #endif
339 /* all done */
340 /* exit(exitcode); */
341 return exitcode;