This source file includes following definitions.
- sim_finit
- sim_buf_swap_data
- sim_fread
- sim_buf_copy_swapped
- sim_fwrite
- sim_fsize_ex
- sim_fsize_name_ex
- sim_fsize_name
- sim_fsize
- sim_fopen
- sim_fseeko
- sim_ftell
- sim_fseeko
- sim_ftell
- sim_fseeko
- sim_ftell
- sim_fseeko
- sim_ftell
- sim_fseeko
- sim_ftell
- sim_fseek
- sim_set_fsize
- sim_set_fifo_nonblock
- sim_set_fsize
- sim_set_fifo_nonblock
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 #include <fcntl.h>
57 #include <sys/file.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60
61 #include "sim_defs.h"
62
63 #include "../decNumber/decContext.h"
64 #include "../decNumber/decNumberLocal.h"
65
66 #ifndef DECLITEND
67 # error Unknown platform endianness
68 #endif
69
70 #ifdef TESTING
71 # undef FREE
72 # define FREE(p) free(p)
73 #endif
74
75 t_bool sim_end;
76 t_bool sim_taddr_64;
77 t_bool sim_toffset_64;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 int32 sim_finit (void)
98 {
99 sim_end = DECLITEND;
100 sim_toffset_64 = (sizeof(t_offset) > sizeof(int32));
101
102 sim_taddr_64 = sim_toffset_64 && (sizeof(t_addr) > sizeof(int32));
103 return sim_end;
104 }
105
106 void sim_buf_swap_data (void *bptr, size_t size, size_t count)
107 {
108 uint32 j;
109 int32 k;
110 unsigned char by, *sptr, *dptr;
111
112 if (sim_end || (count == 0) || (size == sizeof (char)))
113 return;
114 for (j = 0, dptr = sptr = (unsigned char *) bptr;
115 j < count; j++) {
116 for (k = (int32)(size - 1); k >= (((int32) size + 1) / 2); k--) {
117 by = *sptr;
118 *sptr++ = *(dptr + k);
119 *(dptr + k) = by;
120 }
121 sptr = dptr = dptr + size;
122 }
123 }
124
125 size_t sim_fread (void *bptr, size_t size, size_t count, FILE *fptr)
126 {
127 size_t c;
128
129 if ((size == 0) || (count == 0))
130 return 0;
131 c = fread (bptr, size, count, fptr);
132 if (sim_end || (size == sizeof (char)) || (c == 0))
133 return c;
134 sim_buf_swap_data (bptr, size, count);
135 return c;
136 }
137
138 void sim_buf_copy_swapped (void *dbuf, const void *sbuf, size_t size, size_t count)
139 {
140 size_t j;
141 int32 k;
142 const unsigned char *sptr = (const unsigned char *)sbuf;
143 unsigned char *dptr = (unsigned char *)dbuf;
144
145 if (sim_end || (size == sizeof (char))) {
146 memcpy (dptr, sptr, size * count);
147 return;
148 }
149 for (j = 0; j < count; j++) {
150 for (k = (int32)(size - 1); k >= 0; k--)
151 *(dptr + k) = *sptr++;
152 dptr = dptr + size;
153 }
154 }
155
156 size_t sim_fwrite (const void *bptr, size_t size, size_t count, FILE *fptr)
157 {
158 size_t c, nelem, nbuf, lcnt, total;
159 int32 i;
160 const unsigned char *sptr;
161 unsigned char *sim_flip;
162
163 if ((size == 0) || (count == 0))
164 return 0;
165 if (sim_end || (size == sizeof (char)))
166 return fwrite (bptr, size, count, fptr);
167 sim_flip = (unsigned char *)malloc(FLIP_SIZE);
168 if (!sim_flip)
169 return 0;
170 nelem = FLIP_SIZE / size;
171 nbuf = count / nelem;
172 lcnt = count % nelem;
173 if (lcnt) nbuf = nbuf + 1;
174 else lcnt = nelem;
175 total = 0;
176 sptr = (const unsigned char *) bptr;
177 for (i = (int32)nbuf; i > 0; i--) {
178 c = (i == 1)? lcnt: nelem;
179 sim_buf_copy_swapped (sim_flip, sptr, size, c);
180 sptr = sptr + size * count;
181 c = fwrite (sim_flip, size, c, fptr);
182 if (c == 0) {
183 FREE(sim_flip);
184 return total;
185 }
186 total = total + c;
187 }
188 FREE(sim_flip);
189 return total;
190 }
191
192
193
194 t_offset sim_ftell (FILE *st);
195
196
197
198 t_offset sim_fsize_ex (FILE *fp)
199 {
200 t_offset pos, sz;
201
202 if (fp == NULL)
203 return 0;
204 pos = sim_ftell (fp);
205 if (sim_fseek (fp, 0, SEEK_END))
206 return 0;
207 sz = sim_ftell (fp);
208 if (sim_fseeko (fp, pos, SEEK_SET))
209 return 0;
210 return sz;
211 }
212
213 t_offset sim_fsize_name_ex (const char *fname)
214 {
215 FILE *fp;
216 t_offset sz;
217
218 if ((fp = sim_fopen (fname, "rb")) == NULL)
219 return 0;
220 sz = sim_fsize_ex (fp);
221 fclose (fp);
222 return sz;
223 }
224
225 uint32 sim_fsize_name (const char *fname)
226 {
227 return (uint32)(sim_fsize_name_ex (fname));
228 }
229
230 uint32 sim_fsize (FILE *fp)
231 {
232 return (uint32)(sim_fsize_ex (fp));
233 }
234
235
236
237
238
239 FILE *sim_fopen (const char *file, const char *mode)
240 {
241 FILE *fsc = NULL;
242 #if defined(USE_FCNTL) || defined(USE_FLOCK)
243 # include <fcntl.h>
244 # include <sys/stat.h>
245 # include <sys/types.h>
246 int writable = 0;
247 int rc = 0;
248 if (strstr(mode, "+") != NULL)
249 writable = 1;
250 if (strstr(mode, "w") != NULL)
251 writable = 1;
252 if (strstr(mode, "a") != NULL)
253 writable = 1;
254 #endif
255 #if (defined (__linux) || defined (__linux__) || defined (_AIX)) && !defined (DONT_DO_LARGEFILE)
256 fsc = fopen64 (file, mode);
257 #else
258 fsc = fopen (file, mode);
259 #endif
260 #if defined(USE_FCNTL)
261 struct flock lock;
262 memset (&lock, 0, sizeof(lock));
263 lock.l_type = F_WRLCK;
264 if (writable && !sim_nolock) {
265 if (fsc != NULL)
266 rc = fcntl (fileno(fsc), F_SETLK, &lock);
267 if (rc < 0) {
268 if (!sim_quiet) {
269 sim_printf ("%s(%s): %s",
270 __func__, mode, strerror(errno));
271 if (fcntl(fileno(fsc), F_GETLK, &lock) == 0 && lock.l_pid > 0)
272 sim_printf (" (locked by PID %lu)",
273 (unsigned long)lock.l_pid);
274 sim_printf ("\r\n");
275 }
276 if (!sim_iglock) return NULL;
277 }
278 }
279 #elif defined(USE_FLOCK)
280 if (writable && !sim_nolock) {
281 if (fsc != NULL)
282 rc = flock (fileno(fsc), LOCK_EX | LOCK_NB);
283 if (rc < 0) {
284 if (!sim_quiet)
285 sim_printf ("%s(%s): %s (locked?)\r\n",
286 __func__, mode, strerror(errno));
287 if (!sim_iglock) return NULL;
288 }
289 }
290 #endif
291 return fsc;
292 }
293
294 #if !defined (DONT_DO_LARGEFILE)
295
296 # if ((defined(__sun) || defined(__sun__)) && defined(_LARGEFILE_SOURCE))
297 # define S_SIM_IO_FSEEK_EXT_ 1
298 int sim_fseeko (FILE *st, t_offset offset, int whence)
299 {
300 return fseeko (st, (off_t)offset, whence);
301 }
302
303 t_offset sim_ftell (FILE *st)
304 {
305 return (t_offset)(ftello (st));
306 }
307
308 # endif
309
310
311
312 # if defined (_WIN32)
313 # define S_SIM_IO_FSEEK_EXT_ 1
314 # include <sys/stat.h>
315
316 int sim_fseeko (FILE *st, t_offset offset, int whence)
317 {
318 fpos_t fileaddr;
319 struct _stati64 statb;
320
321 switch (whence) {
322
323 case SEEK_SET:
324 fileaddr = (fpos_t)offset;
325 break;
326
327 case SEEK_END:
328 if (_fstati64 (_fileno (st), &statb))
329 return (-1);
330 fileaddr = statb.st_size + offset;
331 break;
332 case SEEK_CUR:
333 if (fgetpos (st, &fileaddr))
334 return (-1);
335 fileaddr = fileaddr + offset;
336 break;
337
338 default:
339 errno = EINVAL;
340 return (-1);
341 }
342
343 return fsetpos (st, &fileaddr);
344 }
345
346 t_offset sim_ftell (FILE *st)
347 {
348 fpos_t fileaddr;
349 if (fgetpos (st, &fileaddr))
350 return (-1);
351 return (t_offset)fileaddr;
352 }
353
354 # endif
355
356
357
358 # if defined (__linux) || defined (__linux__) || defined (_AIX)
359 # define S_SIM_IO_FSEEK_EXT_ 1
360 int sim_fseeko (FILE *st, t_offset xpos, int origin)
361 {
362 return fseeko64 (st, (off64_t)xpos, origin);
363 }
364
365 t_offset sim_ftell (FILE *st)
366 {
367 return (t_offset)(ftello64 (st));
368 }
369
370 # endif
371
372
373
374 # if defined (__APPLE__) || \
375 defined (__FreeBSD__) || \
376 defined (__NetBSD__) || \
377 defined (__OpenBSD__) || \
378 defined (__CYGWIN__)
379 # define S_SIM_IO_FSEEK_EXT_ 1
380 int sim_fseeko (FILE *st, t_offset xpos, int origin)
381 {
382 return fseeko (st, (off_t)xpos, origin);
383 }
384
385 t_offset sim_ftell (FILE *st)
386 {
387 return (t_offset)(ftello (st));
388 }
389
390 # endif
391 #endif
392
393
394
395 #if !defined (S_SIM_IO_FSEEK_EXT_)
396 int sim_fseeko (FILE *st, t_offset xpos, int origin)
397 {
398 return fseek (st, (long) xpos, origin);
399 }
400
401 t_offset sim_ftell (FILE *st)
402 {
403 return (t_offset)(ftell (st));
404 }
405 #endif
406
407 int sim_fseek (FILE *st, t_addr offset, int whence)
408 {
409 return sim_fseeko (st, (t_offset)offset, whence);
410 }
411
412 #if defined(_WIN32)
413 # include <io.h>
414 int sim_set_fsize (FILE *fptr, t_addr size)
415 {
416 return _chsize(_fileno(fptr), (long)size);
417 }
418
419 int sim_set_fifo_nonblock (FILE *fptr)
420 {
421 return -1;
422 }
423
424 #else
425 # include <unistd.h>
426 int sim_set_fsize (FILE *fptr, t_addr size)
427 {
428 return ftruncate(fileno(fptr), (off_t)size);
429 }
430
431 # include <sys/types.h>
432 # include <sys/stat.h>
433 # include <fcntl.h>
434
435 int sim_set_fifo_nonblock (FILE *fptr)
436 {
437 struct stat stbuf;
438
439 if (!fptr || fstat (fileno(fptr), &stbuf))
440 return -1;
441 # if defined(S_IFIFO) && defined(O_NONBLOCK)
442 if ((stbuf.st_mode & S_IFIFO)) {
443 int flags = fcntl(fileno(fptr), F_GETFL, 0);
444 return fcntl(fileno(fptr), F_SETFL, flags | O_NONBLOCK);
445 }
446 # endif
447 return -1;
448 }
449
450 #endif