forked from community-ssu/osso-calculator-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfposval.c
257 lines (247 loc) · 7.57 KB
/
fposval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* fposval - Determine information about the file position type
*
* Copyright (C) 1999 Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Revision: 29.6 $
* @(#) $Id: fposval.c,v 29.6 2001/03/18 03:01:41 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/RCS/fposval.c,v $
*
* Under source code control: 1994/11/05 03:19:52
* File existed as early as: 1994
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
/*
* The include file have_pos.h, as built during the make process will
* define the type FILEPOS as the type used to describe file positions.
* We will print information regarding the size and byte order
* of this definition.
*
* The stat system call returns a stat structure. One of the elements
* of the stat structure is the st_size element. We will print information
* regarding the size and byte order of st_size.
*
* We will #define of 8 symbols:
*
* FILEPOS_BITS length in bits of the type FILEPOS
* SWAP_HALF_IN_FILEPOS will copy/swap FILEPOS into an HALF array
* OFF_T_BITS length in bits of the st_size stat element
* SWAP_HALF_IN_OFF_T will copy/swap st_size into an HALF array
* DEV_BITS length in bits of the st_dev stat element
* SWAP_HALF_IN_DEV will copy/swap st_dev into an HALF array
* INODE_BITS length in bits of the st_ino stat element
* SWAP_HALF_IN_INODE will copy/swap st_ino into an HALF array
*
* With regards to 'will copy/swap ... into an HALF array'. Such macros
* will either be a copy or a copy with HALFs swapped depending on the
* Endian order of the hardware.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "have_fpos.h"
#include "endian_calc.h"
#include "have_offscl.h"
#include "have_posscl.h"
#include "have_fpos_pos.h"
char *program; /* our name */
int
main(int argc, char **argv)
{
int stsizelen; /* bit length of st_size in buf */
int fileposlen; /* bit length of FILEPOS */
int devlen; /* bit length of st_dev in buf */
int inodelen; /* bit length of st_ino in buf */
struct stat buf; /* file status */
/*
* parse args
*/
program = argv[0];
/*
* print the file position information
*/
#if defined(HAVE_FPOS_POS)
fileposlen = FPOS_POS_BITS;
#else /* ! HAVE_FPOS_POS */
# if defined(FPOS_BITS)
fileposlen = FPOS_BITS;
# else
fileposlen = sizeof(FILEPOS)*8;
# endif
#endif /* ! HAVE_FPOS_POS */
printf("#undef FILEPOS_BITS\n");
printf("#define FILEPOS_BITS %d\n", fileposlen);
#if CALC_BYTE_ORDER == BIG_ENDIAN
/*
* Big Endian
*/
if (fileposlen == 64) {
printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B64(dest, src)");
} else if (fileposlen == 32) {
printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B32(dest, src)");
} else {
fprintf(stderr, "%s: unexpected FILEPOS bit size: %d\n",
program, fileposlen);
exit(1);
}
#else /* CALC_BYTE_ORDER == BIG_ENDIAN */
/*
* Little Endian
*/
#if defined(HAVE_FILEPOS_SCALAR)
printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t\t%s\n",
"(*(dest) = *(src))");
#else /* HAVE_FILEPOS_SCALAR */
/*
* Normally a "(*(dest) = *(src))" would do, but on some
* systems a FILEPOS is not a scalar hince we must memcpy.
*/
printf("#define SWAP_HALF_IN_FILEPOS(dest, src)\t%s\n",
"memcpy((void *)(dest), (void *)(src), sizeof(FPOS_POS_BITS))");
#endif /* HAVE_FILEPOS_SCALAR */
#endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
putchar('\n');
/*
* print the stat file size information
*/
#if defined(OFF_T_BITS)
stsizelen = OFF_T_BITS;
#else
stsizelen = sizeof(buf.st_size)*8;
#endif
printf("#undef OFF_T_BITS\n");
printf("#define OFF_T_BITS %d\n", stsizelen);
#if CALC_BYTE_ORDER == BIG_ENDIAN
/*
* Big Endian
*/
if (stsizelen == 64) {
printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B64(dest, src)");
} else if (stsizelen == 32) {
printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B32(dest, src)");
} else {
fprintf(stderr, "%s: unexpected st_size bit size: %d\n",
program, stsizelen);
exit(2);
}
#else /* CALC_BYTE_ORDER == BIG_ENDIAN */
/*
* Little Endian
*
* Normally a "(*(dest) = *(src))" would do, but on some
* systems an off_t is not a scalar hince we must memcpy.
*/
#if defined(HAVE_OFF_T_SCALAR)
printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t\t%s\n",
"(*(dest) = *(src))");
#else /* HAVE_OFF_T_SCALAR */
/*
* Normally a "(*(dest) = *(src))" would do, but on some
* systems, a off_t is not a scalar hince we must memcpy.
*/
printf("#define SWAP_HALF_IN_OFF_T(dest, src)\t%s%d%s\n",
"memcpy((void *)(dest), (void *)(src), ", stsizelen/8, ")");
#endif /* HAVE_OFF_T_SCALAR */
#endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
putchar('\n');
/*
* print the dev_t size
*/
#if defined(DEV_BITS)
devlen = DEV_BITS;
#else
devlen = sizeof(buf.st_dev)*8;
#endif
printf("#undef DEV_BITS\n");
printf("#define DEV_BITS %d\n", devlen);
#if CALC_BYTE_ORDER == BIG_ENDIAN
/*
* Big Endian
*/
if (devlen == 64) {
printf("#define SWAP_HALF_IN_DEV(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B64(dest, src)");
} else if (devlen == 32) {
printf("#define SWAP_HALF_IN_DEV(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B32(dest, src)");
} else if (devlen == 16) {
printf("#define SWAP_HALF_IN_DEV(dest, src)\t\t%s\n",
"(*(dest) = *(src))");
} else {
fprintf(stderr, "%s: unexpected st_dev bit size: %d\n",
program, devlen);
exit(3);
}
#else /* CALC_BYTE_ORDER == BIG_ENDIAN */
/*
* Little Endian
*
* Normally a "(*(dest) = *(src))" would do, but on some
* systems, a DEV is not a scalar hince we must memcpy.
*/
printf("#define SWAP_HALF_IN_DEV(dest, src)\t%s%d%s\n",
"memcpy((void *)(dest), (void *)(src), ", devlen/8, ")");
#endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
putchar('\n');
/*
* print the ino_t size
*/
#if defined(INODE_BITS)
inodelen = INODE_BITS;
#else
inodelen = sizeof(buf.st_ino)*8;
#endif
printf("#undef INODE_BITS\n");
printf("#define INODE_BITS %d\n", inodelen);
#if CALC_BYTE_ORDER == BIG_ENDIAN
/*
* Big Endian
*/
if (inodelen == 64) {
printf("#define SWAP_HALF_IN_INODE(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B64(dest, src)");
} else if (inodelen == 32) {
printf("#define SWAP_HALF_IN_INODE(dest, src)\t\t%s\n",
"SWAP_HALF_IN_B32(dest, src)");
} else if (inodelen == 16) {
printf("#define SWAP_HALF_IN_INODE(dest, src)\t\t%s\n",
"(*(dest) = *(src))");
} else {
fprintf(stderr, "%s: unexpected st_ino bit size: %d\n",
program, inodelen);
exit(4);
}
#else /* CALC_BYTE_ORDER == BIG_ENDIAN */
/*
* Little Endian
*
* Normally a "(*(dest) = *(src))" would do, but on some
* systems, a INODE is not a scalar hince we must memcpy.
*/
printf("#define SWAP_HALF_IN_INODE(dest, src)\t%s%d%s\n",
"memcpy((void *)(dest), (void *)(src), ", inodelen/8, ")");
#endif /* CALC_BYTE_ORDER == BIG_ENDIAN */
/* exit(0); */
return 0;
}