modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / etc / calc / fposval.c
blob62e5da8c841492cff0fd5be4c21d018dbc6e5e3f
1 /*
2 * fposval - Determine information about the file position type
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: fposval.c,v 30.2 2007/09/19 22:34:22 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/fposval.c,v $
24 * Under source code control: 1994/11/05 03:19:52
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 * The include file have_pos.h, as built during the make process will
33 * define the type FILEPOS as the type used to describe file positions.
34 * We will print information regarding the size and byte order
35 * of this definition.
37 * The stat system call returns a stat structure. One of the elements
38 * of the stat structure is the st_size element. We will print information
39 * regarding the size and byte order of st_size.
41 * We will #define of 8 symbols:
43 * FILEPOS_BITS length in bits of the type FILEPOS
44 * FILEPOS_LEN length in octets of the type FILEPOS
45 * SWAP_HALF_IN_FILEPOS will copy/swap FILEPOS into an HALF array
46 * OFF_T_BITS length in bits of the st_size stat element
47 * OFF_T_LEN length in octets of the st_size stat element
48 * SWAP_HALF_IN_OFF_T will copy/swap st_size into an HALF array
49 * DEV_BITS length in bits of the st_dev stat element
50 * DEV_LEN length in bits of the st_dev stat element
51 * SWAP_HALF_IN_DEV will copy/swap st_dev into an HALF array
52 * INODE_BITS length in bits of the st_ino stat element
53 * INODE_LEN length in octets of the st_ino stat element
54 * SWAP_HALF_IN_INODE will copy/swap st_ino into an HALF array
56 * With regards to 'will copy/swap ... into an HALF array'. Such macros
57 * will either be a copy or a copy with HALFs swapped depending on the
58 * Endian order of the hardware.
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #include "have_fpos.h"
67 #include "endian_calc.h"
68 #include "have_offscl.h"
69 #include "have_posscl.h"
70 #include "have_fpos_pos.h"
72 char *program; /* our name */
74 int
75 main(int argc, char **argv)
77 int stsizelen; /* bit length of st_size in buf */
78 int fileposlen; /* bit length of FILEPOS */
79 int devlen; /* bit length of st_dev in buf */
80 int inodelen; /* bit length of st_ino in buf */
81 struct stat buf; /* file status */
84 * parse args
86 program = argv[0];
89 * print the file position information
91 #if defined(HAVE_FPOS_POS)
92 fileposlen = FPOS_POS_BITS;
93 #else /* ! HAVE_FPOS_POS */
94 # if defined(FPOS_BITS)
95 fileposlen = FPOS_BITS;
96 # else
97 fileposlen = sizeof(FILEPOS)*8;
98 # endif
99 #endif /* ! HAVE_FPOS_POS */
100 printf("#undef FILEPOS_BITS\n");
101 printf("#define FILEPOS_BITS %d\n", fileposlen);
102 printf("#undef FILEPOS_LEN\n");
103 printf("#define FILEPOS_LEN %d\n", fileposlen/8);
104 #if CALC_BYTE_ORDER == BIG_ENDIAN
106 * Big Endian
108 if (fileposlen == 64) {
109 printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t\t%s\n",
110 "SWAP_HALF_IN_B64(dest, src)");
111 } else if (fileposlen == 32) {
112 printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t\t%s\n",
113 "SWAP_HALF_IN_B32(dest, src)");
114 } else {
115 fprintf(stderr, "%s: unexpected FILEPOS bit size: %d\n",
116 program, fileposlen);
117 exit(1);
119 #else /* CALC_BYTE_ORDER == BIG_ENDIAN */
121 * Little Endian
123 #if defined(HAVE_FILEPOS_SCALAR)
124 printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t\t%s\n",
125 "(*(dest) = *(src))");
126 #else /* HAVE_FILEPOS_SCALAR */
128 * Normally a "(*(dest) = *(src))" would do, but on some
129 * systems a FILEPOS is not a scalar hince we must memcpy.
131 printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t%s\n",
132 "memcpy((void *)(dest), (void *)(src), sizeof(FPOS_POS_LEN))");
133 #endif /* HAVE_FILEPOS_SCALAR */
134 #endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
135 putchar('\n');
138 * print the stat file size information
140 #if defined(OFF_T_BITS)
141 stsizelen = OFF_T_BITS;
142 #else
143 stsizelen = sizeof(buf.st_size)*8;
144 #endif
145 printf("#undef OFF_T_BITS\n");
146 printf("#define OFF_T_BITS %d\n", stsizelen);
147 printf("#undef OFF_T_LEN\n");
148 printf("#define OFF_T_LEN %d\n", stsizelen/8);
149 #if CALC_BYTE_ORDER == BIG_ENDIAN
151 * Big Endian
153 if (stsizelen == 64) {
154 printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t\t%s\n",
155 "SWAP_HALF_IN_B64(dest, src)");
156 } else if (stsizelen == 32) {
157 printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t\t%s\n",
158 "SWAP_HALF_IN_B32(dest, src)");
159 } else {
160 fprintf(stderr, "%s: unexpected st_size bit size: %d\n",
161 program, stsizelen);
162 exit(2);
164 #else /* CALC_BYTE_ORDER == BIG_ENDIAN */
166 * Little Endian
168 * Normally a "(*(dest) = *(src))" would do, but on some
169 * systems an off_t is not a scalar hince we must memcpy.
171 #if defined(HAVE_OFF_T_SCALAR)
172 printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t\t%s\n",
173 "(*(dest) = *(src))");
174 #else /* HAVE_OFF_T_SCALAR */
176 * Normally a "(*(dest) = *(src))" would do, but on some
177 * systems, a off_t is not a scalar hince we must memcpy.
179 printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t%s%d%s\n",
180 "memcpy((void *)(dest), (void *)(src), ", stsizelen/8, ")");
181 #endif /* HAVE_OFF_T_SCALAR */
182 #endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
183 putchar('\n');
186 * print the dev_t size
188 #if defined(DEV_BITS)
189 devlen = DEV_BITS;
190 #else
191 devlen = sizeof(buf.st_dev)*8;
192 #endif
193 printf("#undef DEV_BITS\n");
194 printf("#define DEV_BITS %d\n", devlen);
195 printf("#undef DEV_LEN\n");
196 printf("#define DEV_LEN %d\n", devlen/8);
197 #if CALC_BYTE_ORDER == BIG_ENDIAN
199 * Big Endian
201 if (devlen == 64) {
202 printf("#define SWAP_HALF_IN_DEV(dest, src)\t\t%s\n",
203 "SWAP_HALF_IN_B64(dest, src)");
204 } else if (devlen == 32) {
205 printf("#define SWAP_HALF_IN_DEV(dest, src)\t\t%s\n",
206 "SWAP_HALF_IN_B32(dest, src)");
207 } else if (devlen == 16) {
208 printf("#define SWAP_HALF_IN_DEV(dest, src)\t\t%s\n",
209 "(*(dest) = *(src))");
210 } else {
211 fprintf(stderr, "%s: unexpected st_dev bit size: %d\n",
212 program, devlen);
213 exit(3);
215 #else /* CALC_BYTE_ORDER == BIG_ENDIAN */
217 * Little Endian
219 * Normally a "(*(dest) = *(src))" would do, but on some
220 * systems, a DEV is not a scalar hince we must memcpy.
222 printf("#define SWAP_HALF_IN_DEV(dest, src)\t%s%d%s\n",
223 "memcpy((void *)(dest), (void *)(src), ", devlen/8, ")");
224 #endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
225 putchar('\n');
228 * print the ino_t size
230 #if defined(INODE_BITS)
231 inodelen = INODE_BITS;
232 #else
233 inodelen = sizeof(buf.st_ino)*8;
234 #endif
235 printf("#undef INODE_BITS\n");
236 printf("#define INODE_BITS %d\n", inodelen);
237 printf("#undef INODE_LEN\n");
238 printf("#define INODE_LEN %d\n", inodelen/8);
239 #if CALC_BYTE_ORDER == BIG_ENDIAN
241 * Big Endian
243 if (inodelen == 64) {
244 printf("#define SWAP_HALF_IN_INODE(dest, src)\t\t%s\n",
245 "SWAP_HALF_IN_B64(dest, src)");
246 } else if (inodelen == 32) {
247 printf("#define SWAP_HALF_IN_INODE(dest, src)\t\t%s\n",
248 "SWAP_HALF_IN_B32(dest, src)");
249 } else if (inodelen == 16) {
250 printf("#define SWAP_HALF_IN_INODE(dest, src)\t\t%s\n",
251 "(*(dest) = *(src))");
252 } else {
253 fprintf(stderr, "%s: unexpected st_ino bit size: %d\n",
254 program, inodelen);
255 exit(4);
257 #else /* CALC_BYTE_ORDER == BIG_ENDIAN */
259 * Little Endian
261 * Normally a "(*(dest) = *(src))" would do, but on some
262 * systems, a INODE is not a scalar hince we must memcpy.
264 printf("#define SWAP_HALF_IN_INODE(dest, src)\t%s%d%s\n",
265 "memcpy((void *)(dest), (void *)(src), ", inodelen/8, ")");
266 #endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
267 /* exit(0); */
268 return 0;