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