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