-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_fio.c
3138 lines (2820 loc) · 102 KB
/
sim_fio.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sim_fio.c: simulator file I/O library
Copyright (c) 1993-2008, Robert M Supnik
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Robert M Supnik shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Robert M Supnik.
03-Jun-11 MP Simplified VMS 64b support and made more portable
02-Feb-11 MP Added sim_fsize_ex and sim_fsize_name_ex returning t_addr
Added export of sim_buf_copy_swapped and sim_buf_swap_data
28-Jun-07 RMS Added VMS IA64 support (from Norm Lastovica)
10-Jul-06 RMS Fixed linux conditionalization (from Chaskiel Grundman)
15-May-06 RMS Added sim_fsize_name
21-Apr-06 RMS Added FreeBSD large file support (from Mark Martinec)
19-Nov-05 RMS Added OS/X large file support (from Peter Schorn)
16-Aug-05 RMS Fixed C++ declaration and cast problems
17-Jul-04 RMS Fixed bug in optimized sim_fread (reported by Scott Bailey)
26-May-04 RMS Optimized sim_fread (suggested by John Dundas)
02-Jan-04 RMS Split out from SCP
This library includes:
sim_finit - initialize package
sim_fopen - open file
sim_fread - endian independent read (formerly fxread)
sim_fwrite - endian independent write (formerly fxwrite)
sim_fseek - conditionally extended (>32b) seek (
sim_fseeko - extended seek (>32b if available)
sim_can_seek - test for seekable (regular file)
sim_fsize - get file size
sim_fsize_name - get file size of named file
sim_fsize_ex - get file size as a t_offset
sim_fsize_name_ex - get file size as a t_offset of named file
sim_buf_copy_swapped - copy data swapping elements along the way
sim_buf_swap_data - swap data elements inplace in buffer if needed
sim_byte_swap_data - swap data elements inplace in buffer
sim_buf_pack_unpack - pack or unpack data between buffers
sim_shmem_open create or attach to a shared memory region
sim_shmem_close close a shared memory region
sim_chdir change working directory
sim_mkdir create a directory
sim_rmdir remove a directory
sim_getcwd get the current working directory
sim_copyfile copy a file
sim_filepath_parts expand and extract filename/path parts
sim_dirscan scan for a filename pattern
sim_get_filelist get a list of files matching a pattern
sim_free_filelist free a filelist
sim_print_filelist print the elements of a filelist
sim_fopen and sim_fseek are OS-dependent. The other routines are not.
sim_fsize is always a 32b routine (it is used only with small capacity random
access devices like fixed head disks and DECtapes).
*/
#define IN_SIM_FIO_C 1 /* Include from sim_fio.c */
#include "sim_defs.h"
#include "sim_scp_private.h"
t_bool sim_end; /* TRUE = little endian, FALSE = big endian */
t_bool sim_taddr_64; /* t_addr is > 32b and Large File Support available */
t_bool sim_toffset_64; /* Large File (>2GB) file I/O Support available */
const char sim_file_path_separator /* Platform specific value \ or / as appropriate */
#if defined (_WIN32)
= '\\';
#else
= '/';
#endif
#if defined(fprintf) /* Make sure to only use the C rtl stream I/O routines */
#undef fprintf
#undef fputs
#undef fputc
#endif
#ifndef MAX
#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
#endif
#define FIO_DBG_PACK 1 /* Pack/Unpack Test Detail */
#define FIO_DBG_SCAN 2 /* File/Directory Scan Detail */
static DEBTAB fio_debug[] = {
{"PACK", FIO_DBG_PACK, "Pack/Unpack Test Detail"},
{"SCAN", FIO_DBG_SCAN, "File/Directory Scan Detail"},
{0}
};
static const char *sim_fio_test_description (DEVICE *dptr)
{
return "SCP FIO Testing";
}
static UNIT sim_fio_unit = { 0 };
static DEVICE sim_fio_test_dev = {
"SCP-FIO", &sim_fio_unit, NULL, NULL,
1, 0, 0, 0, 0, 0,
NULL, NULL, NULL, NULL, NULL, NULL,
NULL, DEV_NOSAVE|DEV_DEBUG, 0,
fio_debug, NULL, NULL, NULL, NULL, NULL,
sim_fio_test_description};
/* OS-independent, endian independent binary I/O package
For consistency, all binary data read and written by the simulator
is stored in little endian data order. That is, in a multi-byte
data item, the bytes are written out right to left, low order byte
to high order byte. On a big endian host, data is read and written
from high byte to low byte. Consequently, data written on a little
endian system must be byte reversed to be usable on a big endian
system, and vice versa.
These routines are analogs of the standard C runtime routines
fread and fwrite. If the host is little endian, or the data items
are size char, then the calls are passed directly to fread or
fwrite. Otherwise, these routines perform the necessary byte swaps.
Sim_fread swaps in place, sim_fwrite uses an intermediate buffer.
*/
int32 sim_finit (void)
{
union {int32 i; char c[sizeof (int32)]; } end_test;
end_test.i = 1; /* test endian-ness */
sim_end = (end_test.c[0] != 0);
sim_toffset_64 = (sizeof(t_offset) > sizeof(int32)); /* Large File (>2GB) support */
sim_taddr_64 = sim_toffset_64 && (sizeof(t_addr) > sizeof(int32));
return sim_end;
}
/* Copy little endian data to local buffer swapping if needed */
void sim_buf_swap_data (void *bptr, size_t size, size_t count)
{
if (sim_end || (count == 0) || (size == sizeof (char)))
return;
sim_byte_swap_data (bptr, size, count);
}
void sim_byte_swap_data (void *bptr, size_t size, size_t count)
{
uint32 j;
int32 k;
unsigned char by, *sptr, *dptr;
if (sim_end || (count == 0) || (size == sizeof (char)))
return;
for (j = 0, dptr = sptr = (unsigned char *) bptr; /* loop on items */
j < count; j++) {
for (k = (int32)(size - 1); k >= (((int32) size + 1) / 2); k--) {
by = *sptr; /* swap end-for-end */
*sptr++ = *(dptr + k);
*(dptr + k) = by;
}
sptr = dptr = dptr + size; /* next item */
}
}
size_t sim_fread (void *bptr, size_t size, size_t count, FILE *fptr)
{
size_t c;
if ((size == 0) || (count == 0)) /* check arguments */
return 0;
c = fread (bptr, size, count, fptr); /* read buffer */
if (sim_end || (size == sizeof (char)) || (c == 0)) /* le, byte, or err? */
return c; /* done */
sim_buf_swap_data (bptr, size, c);
return c;
}
void sim_buf_copy_swapped (void *dbuf, const void *sbuf, size_t size, size_t count)
{
size_t j;
int32 k;
const unsigned char *sptr = (const unsigned char *)sbuf;
unsigned char *dptr = (unsigned char *)dbuf;
if (sim_end || (size == sizeof (char))) {
memcpy (dptr, sptr, size * count);
return;
}
for (j = 0; j < count; j++) { /* loop on items */
for (k = (int32)(size - 1); k >= 0; k--)
*(dptr + k) = *sptr++;
dptr = dptr + size;
}
}
static uint32 _bit_index (uint32 bit, uint32 bits, t_bool LSB)
{
uint32 base, offset;
if (LSB)
return bit;
//return (bits * (bit / bits)) + bits - ((bit % bits) + 1); /* Reverse bit ordering - likely not useful */
base = (bits * (bit / bits)) - (((bits % 8) == 0) ? 8 : 0);
offset = ((base / bits) * (bits % 8));
bit = (bit % bits) + offset;
return base + (bits - (((bit + (bits % 8)) / 8) * 8) - (bits % 8)) + offset + ((bit + (bits % 8)) % 8);
}
t_bool sim_buf_pack_unpack (const void *sptr, /* source buffer pointer */
void *dptr, /* destination buffer pointer */
uint32 sbits, /* source buffer element size in bits */
t_bool sLSB_o_numbering, /* source numbered using LSB ordering */
uint32 scount, /* count of source elements */
uint32 dbits, /* interesting bits of each destination element */
t_bool dLSB_o_numbering) /* destination numbered using LSB ordering */
{
const uint8 *s = (const uint8 *)sptr;
uint8 *d = (uint8 *)dptr;
uint32 bits_to_process; /* bits in current source element remaining to be processed */
uint32 sbit_offset, dbit_offset;/* source and destination bit offsets */
uint32 sx; /* source byte index */
uint32 dx; /* destination byte index */
uint32 bit; /* Current Bit number */
uint32 element; /* Current element number */
sim_debug (FIO_DBG_PACK, &sim_fio_test_dev, "sim_buf_pack_unpack(sbits=%d, dLSB_o=%s, scount=%d, dbits=%d, dLSB_o=%s)\n", sbits, sLSB_o_numbering ? "True" : "False", scount, dbits, dLSB_o_numbering ? "True" : "False");
if (((dbits * scount) & 7) != 0)
return TRUE; /* Error - Can't process all source elements */
memset (d, 0, (dbits * scount) >> 3);
if (((sbits % 8) == 0) &&
(sbits == dbits) &&
(sLSB_o_numbering == dLSB_o_numbering)) {
sim_buf_copy_swapped (dptr, sptr, sbits >> 3, scount);
return FALSE;
}
bits_to_process = MIN (sbits, dbits);
for (element = 0; element < scount; element++) {
sbit_offset = element * sbits;
dbit_offset = element * dbits;
for (bit = 0; bit < bits_to_process; bit++, sbit_offset++, dbit_offset++) {
sx = _bit_index (sbit_offset, sbits, sLSB_o_numbering);
dx = _bit_index (dbit_offset, dbits, dLSB_o_numbering);
d[dx >> 3] |= (((s[sx >> 3] >> (sx & 7)) & 1) << (dx & 7));
}
}
return FALSE;
}
size_t sim_fwrite (const void *bptr, size_t size, size_t count, FILE *fptr)
{
size_t c, nelem, nbuf, lcnt, total;
int32 i;
const unsigned char *sptr;
unsigned char *sim_flip;
if ((size == 0) || (count == 0)) /* check arguments */
return 0;
if (sim_end || (size == sizeof (char))) /* le or byte? */
return fwrite (bptr, size, count, fptr); /* done */
sim_flip = (unsigned char *)malloc(FLIP_SIZE);
if (!sim_flip)
return 0;
nelem = FLIP_SIZE / size; /* elements in buffer */
nbuf = count / nelem; /* number buffers */
lcnt = count % nelem; /* count in last buf */
if (lcnt) nbuf = nbuf + 1;
else lcnt = nelem;
total = 0;
sptr = (const unsigned char *) bptr; /* init input ptr */
for (i = (int32)nbuf; i > 0; i--) { /* loop on buffers */
c = (i == 1)? lcnt: nelem;
sim_buf_copy_swapped (sim_flip, sptr, size, c);
sptr = sptr + size * count;
c = fwrite (sim_flip, size, c, fptr);
if (c == 0) {
free(sim_flip);
return total;
}
total = total + c;
}
free(sim_flip);
return total;
}
/* Forward Declaration */
t_offset sim_ftell (FILE *st);
static int _sim_filename_compare (const char *filename1, const char *filename2);
static void _flush_filelist_directory_cache_entry (const char *directory);
/* Get file size */
t_offset sim_fsize_ex (FILE *fp)
{
t_offset pos, sz;
if (fp == NULL)
return 0;
pos = sim_ftell (fp);
if (sim_fseeko (fp, 0, SEEK_END))
return 0;
sz = sim_ftell (fp);
if (sim_fseeko (fp, pos, SEEK_SET))
return 0;
return sz;
}
t_offset sim_fsize_name_ex (const char *fname)
{
FILE *fp;
t_offset sz;
if ((fp = sim_fopen (fname, "rb")) == NULL)
return 0;
sz = sim_fsize_ex (fp);
fclose (fp);
return sz;
}
uint32 sim_fsize_name (const char *fname)
{
return (uint32)(sim_fsize_name_ex (fname));
}
uint32 sim_fsize (FILE *fp)
{
return (uint32)(sim_fsize_ex (fp));
}
t_bool sim_can_seek (FILE *fp)
{
struct stat statb;
if ((0 != fstat (fileno (fp), &statb)) ||
(0 == (statb.st_mode & S_IFREG)))
return FALSE;
return TRUE;
}
#if defined(_WIN32)
#include <direct.h>
#include <io.h>
#include <fcntl.h>
#else
#include <unistd.h>
#endif
static char *_sim_expand_homedir (const char *file, char *dest, size_t dest_size)
{
char *without_quotes = NULL;
errno = 0;
if (((*file == '"') && (file[strlen (file) - 1] == '"')) ||
((*file == '\'') && (file[strlen (file) - 1] == '\''))) {
size_t offset = 1;
const char *end = &file[strlen (file) - 1];
char quote = *file;
without_quotes = (char *)malloc (strlen (file) + 1);
if (without_quotes == NULL)
return NULL;
strcpy (without_quotes, file + 1);
without_quotes[strlen (without_quotes) - 1] = '\0';
file = (const char*)without_quotes;
}
if (memcmp (file, "~/", 2) != 0)
strlcpy (dest, file, dest_size);
else {
char *cptr = getenv("HOME");
char *cptr2;
if (cptr == NULL) {
cptr = getenv("HOMEPATH");
cptr2 = getenv("HOMEDRIVE");
}
else
cptr2 = NULL;
if (cptr && (dest_size > strlen (cptr) + strlen (file) + 3))
snprintf(dest, dest_size, "%s%s%s%s", cptr2 ? cptr2 : "", cptr, strchr (cptr, '/') ? "/" : "\\", file + 2);
else
strlcpy (dest, file, dest_size);
while ((strchr (dest, '\\') != NULL) && ((cptr = strchr (dest, '/')) != NULL))
*cptr = '\\';
}
free (without_quotes);
return dest;
}
/*
* DBD9 packing/encoding is:
* 9 character per pair of 36 bit words.
*
* 36b Bit numbers using bit
* word standard bit numbering byte offset
* 0 - 35 34 33 32 31 30 29 28 0 0
* 0 - 27 26 25 24 23 22 21 20 1 8
* 0 - 19 18 17 16 15 14 13 12 2 16
* 0 - 11 10 9 8 7 6 5 4 3 24
* 0 - 3 2 1 0 35 34 33 32 4 32
* 1 - 31 30 29 28 27 26 25 24 5 40
* 1 - 23 22 21 20 19 18 17 16 6 48
* 1 - 15 14 13 12 11 10 9 8 7 56
* 1 - 7 6 5 4 3 2 1 0 8 64
*
* word Bit numbers using PDP10 bit numbering
* 0 - B0 1 2 3 4 5 6 7
* 0 - 8 9 10 11 12 13 14 15
* 0 - 16 17 18 19 20 21 22 23
* 0 - 24 25 26 27 28 29 30 31
* 0 - 32 33 34 35 B0 1 2 3
* 1 - 4 5 6 7 8 9 10 11
* 1 - 12 13 14 15 16 17 18 19
* 1 - 20 21 22 23 24 25 26 27
* 1 - 28 29 30 31 32 33 34 35
*
* DLD9 packing/encoding is:
* 9 character per pair of 36 bit words.
*
* 36b Bit numbers using bit
* word standard bit numbering byte offset
* 0 - 7 6 5 4 3 2 1 0 0 0
* 0 - 15 14 13 12 11 10 9 8 1 8
* 0 - 23 22 21 20 19 18 17 16 2 16
* 0 - 31 30 29 28 27 26 25 24 3 24
* 0 - 3 2 1 0 35 34 33 32 4 32
* 1 - 11 10 9 8 7 6 5 4 5 40
* 1 - 19 18 17 16 15 14 13 12 6 48
* 1 - 27 26 25 24 23 22 21 20 7 56
* 1 - 35 34 33 32 31 30 29 28 8 64
*
* word Bit numbers using PDP10 bit numbering
* 0 - 28 29 30 31 32 33 34 35
* 0 - 20 21 22 23 24 25 26 27
* 0 - 12 13 14 15 16 17 18 19
* 0 - 4 5 6 7 8 9 10 11
* 0 - 32 33 34 35 B0 1 2 3
* 1 - 24 25 26 27 28 29 30 31
* 1 - 16 17 18 19 20 21 22 23
* 1 - 8 9 10 11 12 13 14 15
* 1 - B0 1 2 3 4 5 6 7
*/
uint32 int32_data[] = { 0x00000000, 0x00000001, 0x00000002, 0x00000003,
0x00000004, 0x00000005, 0x00000006, 0x00000007,
0x00000008, 0x00000009, 0x0000000A, 0x0000000B,
0x0000000C, 0x0000000D, 0x0000000E, 0x0000000F};
uint32 res_32bitM[] = { 0x00000000, 0x01000000, 0x02000000, 0x03000000,
0x04000000, 0x05000000, 0x06000000, 0x07000000,
0x08000000, 0x09000000, 0x0A000000, 0x0B000000,
0x0C000000, 0x0D000000, 0x0E000000, 0x0F000000};
uint32 res_32_1[] = { 0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 0, 1};
uint16 int16_data[] = { 0x1234, 0x5678,
0x9ABC, 0xDEF0};
uint16 res_16bit[] = { 0x3412, 0x7856,
0xBC9A, 0xF0DE};
uint8 res_8bit[] = { 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15};
uint8 res_4bit[] = { 0x10, 0x32, 0x54, 0x76,
0x98, 0xba, 0xdc, 0xfe};
uint8 res_2bit[] = { 0xE4, 0xE4, 0xE4, 0xE4};
uint8 res_1bit[] = { 0xAA, 0xAA};
#if defined (USE_INT64)
t_uint64 int64_data[] = { 0x876543210, 0x012345678, 0x987654321, 0x123456789};
uint8 res_36bit[] = {0x10, 0x32, 0x54, 0x76, 0x88, 0x67, 0x45, 0x23, 0x01,
0x21, 0x43, 0x65, 0x87, 0x99, 0x78, 0x56, 0x34, 0x12};
uint8 res_36bitM[]= {0x87, 0x65, 0x43, 0x21, 0x00, 0x12, 0x34, 0x56, 0x78,
0x98, 0x76, 0x54, 0x32, 0x11, 0x23, 0x45, 0x67, 0x89};
uint8 int64_data_dbd9[18];
uint8 int64_data_dld9[18];
#endif
static struct pack_test {
const void *src;
const void *exp_dst;
uint32 sbits;
t_bool slsb;
uint32 dbits;
t_bool dlsb;
uint32 scount;
t_bool exp_stat;
} p_test[] = {
#if defined (USE_INT64)
{&int64_data, &res_36bitM, 64, TRUE, 36, FALSE, 4, FALSE},
{&res_36bitM, &int64_data, 36, FALSE, 64, TRUE, 4, FALSE},
{&int64_data, &res_36bit, 64, TRUE, 36, TRUE, 4, FALSE},
{&res_36bit, &int64_data, 36, TRUE, 64, TRUE, 4, FALSE},
#endif
{&int16_data, &res_16bit, 16, TRUE, 16, FALSE, 4, FALSE},
{&int16_data, &res_16bit, 16, FALSE, 16, TRUE, 4, FALSE},
{&int16_data, &int16_data, 16, TRUE, 16, TRUE, 4, FALSE},
{&int16_data, &int16_data, 16, FALSE, 16, FALSE, 4, FALSE},
{&int32_data, &int32_data, 32, FALSE, 32, FALSE,16, FALSE},
{&int32_data, &int32_data, 32, TRUE, 32, TRUE, 16, FALSE},
{&int32_data, &res_32bitM, 32, TRUE, 32, FALSE,16, FALSE},
{&res_32bitM, &int32_data, 32, FALSE, 32, TRUE, 16, FALSE},
{&res_8bit, &res_8bit, 8, TRUE, 8, FALSE,16, FALSE},
{&res_8bit, &res_8bit, 8, FALSE, 8, TRUE, 16, FALSE},
{&res_8bit, &res_8bit, 8, FALSE, 8, FALSE,16, FALSE},
{&res_8bit, &res_8bit, 8, TRUE, 8, TRUE, 16, FALSE},
{&res_8bit, &res_8bit, 16, TRUE, 16, TRUE, 8, FALSE},
{&res_8bit, &res_8bit, 16, FALSE, 16, FALSE, 8, FALSE},
{&res_1bit, &res_32_1, 1, TRUE, 32, TRUE, 16, FALSE},
{&res_8bit, &int32_data, 8, TRUE, 32, TRUE, 2, FALSE},
{&res_4bit, &int32_data, 4, TRUE, 32, TRUE, 16, FALSE},
{&int32_data, &res_8bit, 32, TRUE, 8, TRUE, 16, FALSE},
{&int32_data, &int32_data, 32, TRUE, 32, TRUE, 16, FALSE},
{&int32_data, &int32_data, 16, TRUE, 16, TRUE, 32, FALSE},
{&int32_data, &int32_data, 8, TRUE, 8, TRUE, 64, FALSE},
{&int32_data, &res_8bit, 32, TRUE, 8, TRUE, 16, FALSE},
{&int32_data, &res_4bit, 32, TRUE, 4, TRUE, 16, FALSE},
{&int32_data, &res_2bit, 32, TRUE, 2, TRUE, 16, FALSE},
{&int32_data, &res_1bit, 32, TRUE, 1, TRUE, 16, FALSE},
{NULL},
};
static struct relative_path_test {
const char *input;
t_bool prepend_orig_cwd;
const char *working_dir;
t_bool prepend_working_cwd;
const char *extra_dir;
const char *result;
} r_test[] = {
{"../../../xyzz/*", FALSE, "xya/b/c", TRUE, "xyzz", "../../../xyzz/*"},
{"../xyzz/*", FALSE, "xya/b/c", FALSE, "xyzz", "../xyzz/*"},
{"/xx.dat", TRUE, "xx", FALSE, NULL, "../xx.dat"},
{"/file.dat", TRUE, "xx/t", FALSE, NULL, "../../file.dat"},
{"/../../xxx/file.dat", TRUE, NULL, FALSE, NULL, "../../xxx/file.dat"},
{"\\..\\..\\xxx\\file.dat", TRUE, NULL, FALSE, NULL, "../../xxx/file.dat"},
{"file.dat", FALSE, NULL, FALSE, NULL, "./file.dat"},
{"\\file.dat", TRUE, NULL, FALSE, NULL, "./file.dat"},
{"C:/XXX/yyy/file.dat", FALSE, NULL, FALSE, NULL, "C:/XXX/yyy/file.dat"},
{"C:/Users/yyy/file.dat", FALSE, NULL, FALSE, NULL, "C:/Users/yyy/file.dat"},
{"W:/XXX/yyy/file.dat", FALSE, NULL, FALSE, NULL, "W:/XXX/yyy/file.dat"},
{"/file.dat", TRUE, NULL, FALSE, NULL, "./file.dat"},
{"/x/filepath/file.dat", FALSE, NULL, FALSE, NULL, "/x/filepath/file.dat"},
{NULL},
};
static struct filename_compare_test {
const char *testname;
const char *filename1;
const char *filename2;
int expected_result;
} name_compare_test[] = {
{"name-equal-drive letter different case",
"C:\\Xyz\\zzz.x",
"c:\\Xyz\\zzz.x",
0},
{"name-diff-drive letter different",
"C:\\Xyz\\zzz.x",
"E:\\Xyz\\zzz.x",
-1},
{"name-diff-drive letter different-vs-path",
"C:\\Xyz\\zzz.x",
"\\Xyz\\zzz.x",
-1},
{"name-equal-separator-different-2",
"C:/Xyz/zzz.x",
"c:\\Xyz\\zzz.x",
#if defined(_WIN32)
2},
#else
1},
#endif
{"name-equal-separator-different-1",
"c:\\Xyz\\zzz.x",
"C:/Xyz/zzz.x",
#if defined(_WIN32)
1},
#else
2},
#endif
{"name-different-equal-path-diff-filename",
"/a/b/cdd/dzzz.x",
"/a/b/cdd/zzzz.x",
-1},
{"name-diff-nostarting-separator",
"a/b/cdd/dzzz.x",
"a/b/cdd/zzzz.x",
-1},
{"name-equal-nostarting-separator",
"a/b/cdd/dzzz.x",
"a/b/cdd/dzzz.x",
0},
{"name-equal-noseparator",
"zzz.x",
"zzz.x",
0},
{"name-diff-nostarting-separator",
"a/b/cdd/dzzz.x",
"abcddzzzz.x",
-1},
{"name-diff-nostarting-same-length",
"abcddzzzz.x/b/cdd/dzzz.x",
"abcddzzzz.x",
-1},
{"name-diff-nostarting-same-length-firsttoken",
"abcde.x/b/cdd/dzzz.x",
"abcde.x",
-1},
{NULL},
};
static struct get_filelist_test {
const char *name;
const char *files[10];
const char *search;
int expected_count;
} get_test[] = {
{"test-single file in subdirectory",
{"a0a/file.txt",
NULL},
"file.txt", 1},
{"test-similar deep file names",
{"aab/bbc/ccd/eef/file.txt",
"aab/bbc/ccd/eef/file2.txt",
"aac/bbd/cce/eef/file2.txt",
NULL},
"file.txt", 1},
{"test-single file no subdirectories",
{"file.txt",
NULL},
"file.txt", 1},
{"test-3 text files in the same 4 deep subdirectory",
{"aab/bbc/ccd/eef/file.txt",
"aab/bbc/ccd/eef/file2.txt",
"aac/bbd/cce/eef/file2.txt",
NULL},
"*.txt", 3},
{"test-2 text files",
{"xab/bbc/ccd/eef/file.txt",
"xab/bbc/ccd/eef/file2.bbb",
"xac/bbd/cce/eef/file2.txt",
NULL},
"*.txt", 2},
{NULL},
};
#if !defined (NO_FIO_TEST_CODE)
t_stat sim_fio_test (const char *cptr)
{
struct pack_test *pt;
struct relative_path_test *rt;
struct filename_compare_test *nt;
struct get_filelist_test *gt;
t_stat r = SCPE_OK;
char test_desc[512];
uint8 result[512];
int tests;
sim_register_internal_device (&sim_fio_test_dev);
sim_fio_test_dev.dctrl |= (sim_switches & SWMASK ('D')) ? FIO_DBG_PACK : 0;
sim_fio_test_dev.dctrl |= (sim_switches & SWMASK ('S')) ? FIO_DBG_SCAN : 0;
sim_set_deb_switches (SWMASK ('F'));
sim_messagef (SCPE_OK, "*** Running sim_buf_pack_unpack - tests\n");
for (pt = p_test, tests = 0; pt->src; ++pt) {
t_bool res;
++tests;
snprintf (test_desc, sizeof (test_desc), "%dbit%s->%dbit%s %d words", pt->sbits, pt->slsb ? "LSB" : "MSB", pt->dbits, pt->dlsb ? "LSB" : "MSB", pt->scount);
memset (result, 0x80, sizeof (result));
res = sim_buf_pack_unpack (pt->src, result, pt->sbits, pt->slsb, pt->scount, pt->dbits, pt->dlsb);
if (res == pt->exp_stat) {
if (!res) {
if (0 == memcmp (pt->exp_dst, result, (pt->scount * pt->dbits) / 8))
sim_messagef (SCPE_OK, "%s - GOOD\n", test_desc);
else {
uint32 i;
r = sim_messagef (SCPE_IERR, "%s - BAD Data:\n", test_desc);
sim_messagef (SCPE_IERR, "Off: Exp: Got:\n");
for (i = 0; i < ((pt->scount * pt->dbits) / 8); i++)
sim_messagef (SCPE_IERR, "%3d 0x%02X%s0x%02X\n", i, ((uint8 *)pt->exp_dst)[i], (((uint8 *)pt->exp_dst)[i] == result[i]) ? " " : " != ", result[i]);
}
}
}
else
r = sim_messagef (SCPE_IERR, "%s - BAD Status - Expected: %s got %s\n", test_desc, pt->exp_stat ? "True" : "False", res ? "True" : "False");
}
if (r != SCPE_OK)
return r;
sim_messagef (SCPE_OK, "*** All %d sim_buf_pack_unpack tests GOOD\n", tests);
sim_messagef (SCPE_OK, "*** Testing relative path logic:\n");
for (rt = r_test, tests = 0; rt->input; ++rt) {
char input[PATH_MAX + 1];
char cmpbuf[PATH_MAX + 1];
char cwd[PATH_MAX + 1];
char origcwd[PATH_MAX + 1];
char *wd = sim_getcwd(cwd, sizeof (cwd));
char *cp;
const char *result;
static const char seperators[] = "/\\";
const char *sep;
t_stat mkdir_stat = SCPE_OK;
++tests;
strlcpy (origcwd, cwd, sizeof (origcwd));
if (rt->extra_dir != NULL)
mkdir_stat = mkdir_cmd (0, rt->extra_dir);
if (rt->working_dir != NULL) {
mkdir_stat = mkdir_cmd (0, rt->working_dir);
sim_chdir (rt->working_dir);
wd = sim_getcwd(cwd, sizeof (cwd));
}
if (rt->prepend_orig_cwd) {
strlcpy (input, origcwd, sizeof (input));
strlcat (input, "/", sizeof (input));
strlcat (input, rt->input, sizeof (input));
}
else {
if (rt->prepend_working_cwd) {
strlcpy (input, cwd, sizeof (input));
strlcat (input, "/", sizeof (input));
strlcat (input, rt->input, sizeof (input));
}
else
strlcpy (input, rt->input, sizeof (input));
}
for (sep = seperators; *sep != '\0'; ++sep) {
while ((cp = strchr (input, *sep)))
*cp = (*sep == '/') ? '\\' : '/';
while ((cp = strchr (cwd, *sep)))
*cp = (*sep == '/') ? '\\' : '/';
result = sim_relative_path (input);
strlcpy (cmpbuf, rt->result, sizeof (cmpbuf));
if (strchr (input, *sep) != NULL) { /* Input has separators? */
while ((cp = strchr (cmpbuf, *sep)))
*cp = (*sep == '/') ? '\\' : '/'; /* Change the expected result to match */
}
if (strcmp (result, cmpbuf) != 0) {
r = sim_messagef (SCPE_IERR, "Relative Path Unexpected Result:\n");
sim_messagef (SCPE_IERR, " input: %s\n", input);
sim_messagef (SCPE_IERR, " result: %s\n", result);
sim_messagef (SCPE_IERR, " expected: %s\n", cmpbuf);
sim_messagef (SCPE_IERR, " cwd: %s\n", cwd);
}
else {
sim_messagef (SCPE_OK, "Relative Path Good Result:\n");
sim_messagef (SCPE_OK, " input: %s\n", input);
sim_messagef (SCPE_OK, " result: %s\n", result);
}
}
sim_chdir (origcwd);
if ((rt->extra_dir != NULL) && (mkdir_stat == SCPE_OK)) {
char *xdir = strdup (rt->extra_dir);
sim_rmdir (rt->extra_dir);
while ((cp = strrchr (xdir, '/')) != NULL) {
*cp = '\0';
sim_rmdir (xdir);
}
free (xdir);
}
if ((rt->working_dir != NULL) && (mkdir_stat == SCPE_OK)) {
char *xdir = strdup (rt->working_dir);
sim_rmdir (rt->working_dir);
while ((cp = strrchr (xdir, '/')) != NULL) {
*cp = '\0';
sim_rmdir (xdir);
}
free (xdir);
}
}
if (r != SCPE_OK)
return r;
sim_messagef (SCPE_OK, "*** All %d relative path logic tests GOOD\n", tests);
sim_messagef (SCPE_OK, "*** Testing filename compare:\n");
for (nt = name_compare_test, tests = 0; nt->testname; ++nt) {
int result;
++tests;
result = _sim_filename_compare (nt->filename1, nt->filename2);
if (result != nt->expected_result) {
sim_messagef (SCPE_IERR, "Name Compare test %d %s\n", tests, nt->testname);
sim_messagef (SCPE_IERR, " filename1: %s\n", nt->filename1);
sim_messagef (SCPE_IERR, " filename2: %s\n", nt->filename2);
r = sim_messagef (SCPE_IERR, " BAD result: %d\n", result);
}
}
if (r != SCPE_OK)
return r;
sim_messagef (SCPE_OK, "*** All %d filename compare tests GOOD\n", tests);
sim_messagef (SCPE_OK, "*** Testing sim_get_filelist:\n");
for (gt = get_test, tests = 0; gt->name; ++gt) {
char xpath[PATH_MAX + 1];
int i;
char **filelist;
++tests;
sim_messagef (r, "FileList test %s\n", gt->name);
for (i=0; gt->files[i]; ++i) {
char *c, *end;
char *filename = sim_filepath_parts (gt->files[i], "nx");
sim_messagef (r, "Creating: %s\n", gt->files[i]);
snprintf (xpath, sizeof (xpath), "testfiles/%s", gt->files[i]);
end = strrchr (xpath, '/');
*end = '\0';
c = xpath;
while ((c = strchr (c, '/'))) {
*c = '\0';
sim_mkdir (xpath);
*c++ = '/';
}
sim_mkdir (xpath);
*end = '/';
fclose (fopen (xpath, "w"));
free (filename);
}
sim_chdir ("testfiles");
snprintf (xpath, sizeof (xpath), "%s", gt->search);
filelist = sim_get_filelist (xpath);
sim_chdir ("..");
r |= sim_messagef ((gt->expected_count != sim_count_filelist (filelist)) ? SCPE_IERR : SCPE_OK,
"sim_get_filelist (\"%s\") yielded %d entries, expected %d entries:\n", xpath, sim_count_filelist (filelist), gt->expected_count);
sim_print_filelist (filelist);
sim_free_filelist (&filelist);
/* Cleanup created test files and directories */
for (i=0; gt->files[i]; ++i) {
char *c;
char *filename = sim_filepath_parts (gt->files[i], "nx");
snprintf (xpath, sizeof (xpath), "testfiles/%s", gt->files[i]);
sim_messagef (r, "Removing: %s\n", gt->files[i]);
unlink (xpath);
c = strrchr (xpath, '/');
*c = '\0';
sim_rmdir (xpath);
while ((c = strrchr (xpath, '/'))) {
*c = '\0';
sim_rmdir (xpath);
}
sim_rmdir (xpath);
free (filename);
}
}
if (r == SCPE_OK)
sim_messagef (SCPE_OK, "All %d sim_get_filelist tests GOOD\n", tests);
return r;
}
#endif /* NO_FIO_TEST_CODE */
int sim_stat (const char *fname, struct stat *stat_str)
{
char namebuf[PATH_MAX + 1];
if (NULL == _sim_expand_homedir (fname, namebuf, sizeof (namebuf)))
return -1;
return stat (namebuf, stat_str);
}
int sim_chdir(const char *path)
{
char pathbuf[PATH_MAX + 1];
if (NULL == _sim_expand_homedir (path, pathbuf, sizeof (pathbuf)))
return -1;
return chdir (pathbuf);
}
int sim_mkdir(const char *path)
{
char pathbuf[PATH_MAX + 1];
if (NULL == _sim_expand_homedir (path, pathbuf, sizeof (pathbuf)))
return -1;
#if defined(_WIN32)
return mkdir (pathbuf);
#else
return mkdir (pathbuf, 0777);
#endif
}
int sim_rmdir(const char *path)
{
char pathbuf[PATH_MAX + 1];
struct stat statb;
char *fullpath;
char FullPath[PATH_MAX + 1];
if (NULL == _sim_expand_homedir (path, pathbuf, sizeof (pathbuf)))
return -1;
memset (&statb, 0, sizeof (statb));
(void)stat (pathbuf, &statb);
if ((statb.st_mode & S_IFDIR) == 0)
return rmdir (pathbuf); /* This is an error, but let rmdir() set errno */
fullpath = sim_filepath_parts (pathbuf, "f");
snprintf (FullPath, sizeof (FullPath), "%s/*", fullpath);
free (fullpath);
_flush_filelist_directory_cache_entry (FullPath);
return rmdir (pathbuf);
}
typedef struct FILELIST_DIRECTORY_CACHE {
struct FILELIST_DIRECTORY_CACHE *next;
char *directory;
char **dirlist;
} FILELIST_DIRECTORY_CACHE;
static FILELIST_DIRECTORY_CACHE *_filelist_directory_cache = NULL;
static char **_check_filelist_directory_cache (const char *directory)
{
FILELIST_DIRECTORY_CACHE *entry = _filelist_directory_cache;
while (entry != NULL) {
if (strcmp (directory, entry->directory) == 0) {
sim_debug (FIO_DBG_SCAN, &sim_fio_test_dev, "_check_filelist_directory_cache(directory=\"%s\") found with %d entries\n", directory, sim_count_filelist (entry->dirlist));
return entry->dirlist;
}
entry = entry->next;
}
sim_debug (FIO_DBG_SCAN, &sim_fio_test_dev, "_check_filelist_directory_cache(directory=\"%s\") not found\n", directory);
return NULL;
}
static void _save_filelist_directory_cache (const char *directory, char **dirlist)
{
FILELIST_DIRECTORY_CACHE *entry;
if (_check_filelist_directory_cache (directory) != NULL) {
sim_debug (FIO_DBG_SCAN, &sim_fio_test_dev, "_save_filelist_directory_cache(directory=\"%s\") previously saved with %d directories\n", directory, sim_count_filelist (dirlist));
return;
}
entry = (FILELIST_DIRECTORY_CACHE *)calloc (1, sizeof (*entry));
entry->directory = strdup (directory);
entry->dirlist = dirlist;
entry->next = _filelist_directory_cache;
_filelist_directory_cache = entry;
sim_debug (FIO_DBG_SCAN, &sim_fio_test_dev, "_save_filelist_directory_cache(directory=\"%s\") saved with %d directories\n", directory, sim_count_filelist (dirlist));
}
static void _flush_filelist_directory_cache_entry (const char *directory)
{
FILELIST_DIRECTORY_CACHE *entry = _filelist_directory_cache;
FILELIST_DIRECTORY_CACHE *last = NULL;
while (entry != NULL) {
if (strcmp (entry->directory, directory) == 0) {
if (last == NULL)
_filelist_directory_cache = entry->next;
else
last->next = entry->next;
free (entry->directory);
sim_free_filelist (&entry->dirlist);
free (entry);
return;
}
last = entry;
entry = entry->next;
}
}
static void _flush_filelist_directory_cache (void)
{
FILELIST_DIRECTORY_CACHE *entry = _filelist_directory_cache;
while (entry != NULL) {
_filelist_directory_cache = entry->next;
free (entry->directory);
sim_free_filelist (&entry->dirlist);
free (entry);
entry = _filelist_directory_cache;
}
}