aboutsummaryrefslogtreecommitdiff
path: root/ksiders/atr.c
blob: 2c3942f1878b92d9d9426bc313bd585721230029 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
/*********************************************************************
   ATR/XFD File handling library
   (C) Copyright 1997 Ken Siders    

   This file can be used in any freeware or public domain software
   as long as credit is given.


History

00.000  6/16/97    Initial version
00.001  6/24/97    Fixed AtariFileSize, Added EofAtariFile and
                   ExtractAtariFile functions
00.002  6/26/97    Added SortDirectory Function
00.003  7/14/97    Fixed Double density to treat first 3 sectors as  
                   Single Density.
00.004  7/16/97    Added CreateBootAtr
00.005  8/22/97    Added ExtractExeFromBootAtr
00.006  9/04/97    Fix signature check

*********************************************************************

To do:

1 Clean up warnings + make more portable
2 Allow opening write-protected ATRs
3 Allow more than one reference to an ATR file to be opened so more 
   than one atari file can be opened at once. (Keep a count)
4 More specific error returns
5 Implement XFD handling
6 Create documentation
7 Optimize if necessary
8 Implement DCM images (maybe)


*********************************************************************/

#define wide 1


/* compile with /Zp option */
/* 20070518 bkw: or -fpack-struct on gcc */


#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include "atr.h"
#include "atdos.h"
#include "kboot.h"

AtrHeader hdr;

int lastAtariError = 0;

static unsigned char sectorBuffer[256];
static int CompareName( const void *a, const void *b );
static int verbose = 0;

/* 20070518 bkw: min() isn't standard on Linux. If you're porting to
	some other UNIX and get errors about min() already being defined,
	just comment this out. */
static int min(int a, int b) {
	return ((a < b) ? a : b);
}

/* 20070518 bkw: As written, the DOS sources aren't portable to
	big-endian architectures. To fix this, EndianFix() will swap
	the high and low bytes of startSector and sectorCount in a
	dir entry on a big-endian arch (and leave them alone on a little-
	endian arch). Must be called just after reading, and just before
	writing.

	The Intel x86 arch and the Atari's 6502 both happen to be
	little-endian, which is why Ken could ignore the endianness
	issue in the original MS-DOS sources.
 */
#define LITTLE_END 0
#define BIG_END 1

static int InitEndian() {
	/* code based on Wikipedia "Endianness" entry */
	union {
		short s;
		char c[sizeof(short)];
	} un;

	un.s = 0x0102;

	if(sizeof(short) == 2) {
		if(un.c[0] == 1 && un.c[1] == 2)
			return BIG_END;
		else if(un.c[0] == 2 && un.c[1] == 1)
			return LITTLE_END;
	}

	printf("WARNING: Can't determine endianness of this platform!\n");
	printf("Assuming little-endian, hoping for the best. Expect trouble.\n");
	return LITTLE_END;
}

static void EndianFix(AtariDosDirEntryPtr entry) {
	static int endian = -1;
	unsigned short tmp;

	if(endian == -1)
		endian = InitEndian();

	/* don't do anything on little-endian platforms */
	if(endian == LITTLE_END)
		return;

	tmp = entry->startSector;
	entry->startSector = ((tmp & 0xff) << 8) | ((tmp & 0xff00) >> 8);

	tmp = entry->sectorCount;
	entry->sectorCount = ((tmp & 0xff) << 8) | ((tmp & 0xff00) >> 8);
}

/******************************************************************
SetVerbose - Sets verbose flag, returns last value of flag
******************************************************************/

int SetVerbose( int verb )
   {
   int last;

   last = verbose;
   verbose = verb;
   return(last);
   }



/******************************************************************
 OpenAtr - opens ATR file specified for read/write (if writeable).
           Returns Atr Pointer if successful, 0 if not
******************************************************************/

AtrFilePtr OpenAtr(char *file )
   {
   AtrFilePtr atr;
   int bytes;
   unsigned short signature;

   atr = malloc(sizeof(AtrFile));

   atr->dosType = DOS_ATARI; /* assume atari dos disk */


   atr->atrIn = fopen(file, "rb+");
   if ( !atr->atrIn )
      {
      free(atr);
      return 0;
      }
   bytes = fread(&hdr, 1, 16, atr->atrIn);
   if ( !bytes )
      {
      free(atr);
      return 0;
      }
   
   signature = hdr.idLow | hdr.idHigh << 8;
   atr->imageSize = 16L * (hdr.paraLow | hdr.paraHigh * 256L | hdr.paraHigher * 65536L);
   atr->secSize = hdr.secSizeLow | hdr.secSizeHigh << 8; 
   atr->crc = hdr.crc1 | hdr.crc2 * 256L | hdr.crc3 * 65536L | hdr.crc4 *256L * 65536L ;
   atr->sectorCount = atr->imageSize / atr->secSize;
   atr->flags = hdr.flags;
   atr->writeProtect = atr->flags&1;
   atr->authenticated = (atr->flags >> 1) & 1;
   
   if ( atr->sectorCount > 721 ) 
      atr->dosType = DOS_MYDOS;

   if ( signature == 0x296 )
      return atr;
   else
      {
      free(atr);
      return 0;
      }
   return atr;
   }


/******************************************************************
 CloseAtr - closes ATR file specified in an Atr Pointer from 
            and Atr Open.  Returns 0 if successful
******************************************************************/

int CloseAtr( AtrFilePtr atr )
   {
   if ( atr )
      return(fclose(atr->atrIn));
   else
      return 1;
    }


/******************************************************************
 ReadSector - Reads specified sector from the ATR file specified
              info buffer which must be big enough for the sector
              size of teh file.  Returns number of bytes read or
              0 if error.
******************************************************************/

int ReadSector(AtrFilePtr atr, unsigned short sector, char *buffer)
   {
   unsigned long pos;
   size_t bytes;
                                                             
   if ( !atr )                                               
      {                                                      
      lastAtariError = 12;                                   
      return 0;                                              
      }            
/* calculate offset into file */                                          
   if ( atr->secSize > 128 && sector > 3 )
      pos = (unsigned long)(sector-4) * atr->secSize + 400L;
   else
      pos = (unsigned long)(sector-1) * 128L + 16;

/* position file pointer at that offset */
   if ( fseek(atr->atrIn, pos, SEEK_SET) )
      {
      lastAtariError = 13;
      return 0;
      }

/* read the data */
   bytes = fread(buffer, 1, atr->secSize, atr->atrIn);
   if ( bytes & 127 )
      {
      lastAtariError = 14;
      return 0;
      }
   return bytes;
   }

/******************************************************************
 WriteSector - Writes specified sector from the ATR file specified
               from buffer specified.  Returns number of bytes
               written or 0 if error.  Image must be writeable.
******************************************************************/

int WriteSector(AtrFilePtr atr, unsigned short sector, char *buffer)
   {
   unsigned long pos;
   size_t bytes;

   if ( !atr )
      {
      lastAtariError = 12;
      return 0;
      }

/* calculate offset into file */
   if ( atr->secSize > 128 && sector > 3 )
      pos = (unsigned long)(sector-4) * atr->secSize + 400L;
   else
      pos = (unsigned long)(sector-1) * 128L + 16;

/* set file pointer to that position */
   if ( fseek(atr->atrIn, pos, SEEK_SET) )
      {
      lastAtariError = 13;
      return 0;
      }

/* sector # to high? */
   if ( pos + atr->secSize > atr->imageSize )
      {
      lastAtariError = 15;
      return 0;
      }

/* write the data */
   bytes = fwrite(buffer, 1, atr->secSize, atr->atrIn);
   if ( bytes & 127 )
      {
      lastAtariError = 14;
      return 0;
      }
   return bytes;
}

/******************************************************************
 CreateAtr - Creates an ATR file with parameters specified.  Sector
             size must be a multiple of 128 bytes.  Return 0 for
             success
******************************************************************/

int CreateAtr( char *file, unsigned short sectors, 
               unsigned short sectorSize )
   {
   FILE *fp;
   AtrHeader hdr = {0};
   unsigned long imageSize;
   int bytes;

/* sector size must be a multiple of 128 */
   if ( sectorSize & 127 )
      return 1;
/* determine the file size for the image */

   if ( sectorSize > 128 && sectors > 2)
      imageSize = (unsigned long)(sectorSize-3) * sectors + 384;
   else
      imageSize = (unsigned long)sectorSize * sectors;

/* create the file */
   fp = fopen(file, "wb");
   if ( !fp )
      return 1;

/* set up the ATR header */
   hdr.idHigh = 0x02;
   hdr.idLow = 0x96;
   hdr.paraLow = (imageSize >> 4) & 255;
   hdr.paraHigh = (imageSize >> 12) & 255; 
   hdr.paraHigher = imageSize >> 20;
   hdr.secSizeLow = sectorSize & 255;
   hdr.secSizeHigh = sectorSize >> 8;
   bytes = fwrite(&hdr, 1, 16, fp);
   if ( bytes != 16 )
      return 1;

/* seek to last position needed in file - 1 */
   if ( fseek(fp,(unsigned long)sectors * sectorSize - 1 , SEEK_SET) )
      return 1;
/* write one null byte */
   if ( fputc( 0, fp ) == EOF )
      return 1;
   if ( fclose(fp) )
      return 1;
   return 0;
   }
   
/******************************************************************
 GetAtrInfo - returns info for an open ATR image via pointers.
              non 0 returned is error. 
******************************************************************/

int GetAtrInfo( AtrFilePtr atr, unsigned short *sectorSize,
                unsigned short *sectorCount, byte  *protected)
   {
   if ( !atr )
      return 1;
/* duh */
   *sectorSize = atr->secSize;
   *sectorCount = atr->sectorCount;
   *protected = atr->writeProtect;
   return 0;
   }





/*-----------------------------------------------------------------*/
/*   ATARI 8-bit File IO routines                                  */
/*-----------------------------------------------------------------*/

/******************************************************************
 MakeFileName - Creates a filename.ext string from a zero padded
                raw fileName and extender.  Result is stored in
                string pointed to be result.  There is no return
                value.
******************************************************************/

void MakeFileName( char *result, char *fileName, char *extender )
   {
   int i;
   
   for(i=0; i<8; i++)
      {
      if (fileName[i] == ' ' || !fileName[i] )
         break;
      *(result++) = fileName[i];
      }      
   *(result++) = '.';
   for(i=0; i<3; i++)
      {
      if (extender[i] == ' ' || !extender[i] )
         break;
      *(result++) = extender[i];
      }      
   *(result++) = 0;
   }


/******************************************************************
 PatternMatch - Returns 1 if fileName+extender matches pattern in
                pattern. Wildcards are the standard '?' and '*'
                as supported by all Atari Dos's.  Returns 0 if
                it does not match.
******************************************************************/
int PatternMatch( char *pattern, char *fileName, char *extender)
   {
   int i=0;
   char file[13];

	/* 20070518 bkw: Special case: "*" and "*.*" always match anything */
	if(strcmp(pattern, "*.*") == 0 || strcmp(pattern, "*") == 0)
		return 1;

   MakeFileName(file, fileName, extender);

   while (*pattern && file[i] )
      {
      if ( !file[i] && *pattern )
         return 0;
      if ( file[i] && !*pattern )
         return 0;

      if ( *pattern == '*')
         {
         while ( file[i] && file[i] != '.')
            i++;
         while ( *pattern && *pattern != '.')
            pattern++;
         if ( file[i] == '.' && *pattern == '.' )
            {
            pattern++;
            i++;
            continue;
            }
         continue;
         }

      if ( *pattern == '?' && file[i] != '.' )
         {
         i++;
         pattern++;
         continue;
         }

      if ( toupper(*pattern) != file[i] )
         return 0;

      i++; 
      pattern++;
      }

   if ( !*pattern && !file[i] )
      return 1;
   else
      return 0;
   }


/******************************************************************
 AtariFindFirst - Finds first match for pattern and sets struct
                  with file information.  returns 0 for success,
                  -1 if not found, other for error.  This is 
                  similiar to _dosfindfist in the DOS world.
******************************************************************/

int AtariFindFirst( char *atrName, unsigned attrib,
    char *pattern, AtariFileInfoPtr fileInfo )
   {
   char buffer[256];
   AtrFilePtr atr;
   int i,j;
   AtariDosDirEntryPtr dirEntry;
   unsigned short sectorSize, sectorCount;
   byte protected;

/* open the ATR image */
   atr = OpenAtr(atrName);
   if ( atr == NULL )
      return 2;

/* Get some info about the ATR image and save */
   if ( GetAtrInfo( atr, &sectorSize, &sectorCount, &protected) )
      {
      free(atr);
      return 3;
      }

/* look for the file in the directory, if found initilize the fileInfo
   structure with data from the directory sector */

   for( i = firstDirSector, fileInfo->fileNo = 0; i <= lastDirSector; i++ )
      {
      if (! ReadSector(atr, (unsigned short) i, buffer) )
         return 4;
      for( j=0; j< dirEntriesPerSector; j++, fileInfo->fileNo++ )
         {
         dirEntry = (AtariDosDirEntryPtr)(buffer + dirEntrySize * j );
			EndianFix(dirEntry);
         fileInfo->locked = (dirEntry->flag & LOCKED_FLAG) ? 1 : 0;
         if (dirEntry->flag & DELETED_FLAG )
            continue; 
         if ( (/* (dirEntry->flag == DOS25EXT_FLAGS) || */ (dirEntry->flag & INUSE_FLAG)) &&
					PatternMatch(pattern,
             dirEntry->fileName, dirEntry->extender) )
            {
            fileInfo->flag = dirEntry->flag;
            fileInfo->startSector = dirEntry->startSector;
            fileInfo->sectorCount = dirEntry->sectorCount;
            fileInfo->dirSector = i;
            fileInfo->dirEntry = j;
            fileInfo->attrib = attrib;
            fileInfo->pattern = pattern;
            fileInfo->atrName = atrName;
            MakeFileName( fileInfo->fileName, dirEntry->fileName,
                dirEntry->extender);
            if ( CloseAtr(atr) )
                return 5;
            return( 0 ); /* success */
            }
         }
      }
   if ( CloseAtr(atr) )
      return 6;

   return -1;
   }

/******************************************************************
 AtariFindNext - Returns next matching file after previous 
                 AtariFindFirst or AtariFindNext call.  The fileinfo
                 structure passed should not be altered from the
                 previous call.  Also the ATR file name and pattern
                 from the initial AtariFindFirst call must still be
                 in scope. Similiar to _dosfindnext in DOS world.
******************************************************************/

int AtariFindNext( AtariFileInfoPtr fileInfo )
   {
   char buffer[256];
   AtrFilePtr atr;
   int i,j;
   AtariDosDirEntryPtr dirEntry;
   unsigned short sectorSize, sectorCount;
   byte protected;

   atr = OpenAtr(fileInfo->atrName);
   if ( atr == NULL )
      return 1;

   if ( GetAtrInfo( atr, &sectorSize, &sectorCount, &protected) )
      {
      free(atr);
      return 2;
      }
   i = fileInfo->dirSector;
   j = fileInfo->dirEntry;

   j++;
   if ( j >= dirEntriesPerSector )
      {
      j=0;
      i++;
      }

   for( ; i <= lastDirSector; i++ , j = 0)
      {
      if (! ReadSector(atr, (unsigned short) i, buffer) )
         return 3;
      for( ; j< dirEntriesPerSector; j++, fileInfo->fileNo++ )
         {
         dirEntry = (AtariDosDirEntryPtr)(buffer + dirEntrySize * j );
			EndianFix(dirEntry);
         fileInfo->locked = (dirEntry->flag & LOCKED_FLAG) ? 1 : 0;
         if (dirEntry->flag & DELETED_FLAG )
            continue; 
         if ( (dirEntry->flag & INUSE_FLAG) && PatternMatch(fileInfo->pattern,
             dirEntry->fileName, dirEntry->extender) )
            {
            fileInfo->flag = dirEntry->flag;
            fileInfo->startSector = dirEntry->startSector;
            fileInfo->sectorCount = dirEntry->sectorCount;
            fileInfo->dirSector = i;
            fileInfo->dirEntry = j;
            MakeFileName( fileInfo->fileName, dirEntry->fileName,
                dirEntry->extender);
            if ( CloseAtr(atr) )
               return 4;
            return( 0 );
            }
         }
      }
   if ( CloseAtr(atr) )
      return 5;

   return -1;
   }


/******************************************************************
 OpenAtariFile - Opens file in an ATR image in the mode specified
                 (ATARI_OPEN_READ, ATARI_OPEN_WRITE, or
                 ATARI_OPEN_DIR.  Returns pointer to atari file
                 structure or 0 on error. 
******************************************************************/

AtariFilePtr OpenAtariFile( char *atrName, char *fileName, byte mode)
   {
   AtariFilePtr atFile;
   byte protected;
   unsigned short sectorSize;
   unsigned short sectorCount;
   AtariFileInfo fileInfo;

/* bad open mode? */
   if ( mode != ATARI_OPEN_READ && mode != ATARI_OPEN_WRITE &&
        mode != ATARI_OPEN_DIR )
      {
      lastAtariError = 2;
      return NULL;
      }

   atFile = malloc(sizeof(AtariFile));
/* open the atr image */
   atFile->atr = OpenAtr(atrName);
   if ( atFile->atr == NULL )
      {
      free(atFile);
      lastAtariError = 1;
      return NULL;
      }
/* get some info on the ATR file and store */
   if ( GetAtrInfo( atFile->atr, &sectorSize, &sectorCount, &protected) )
      {
      CloseAtr(atFile->atr);
      free(atFile);
      lastAtariError = 3;
      return NULL;
      }

/* set file parameters */
   atFile->sectorSize = sectorSize;
   atFile->openFlag = mode;
   atFile->eofFlag = 0;

/* is ATR write protected? (APE extension?) */
   if ( protected && (mode & ATARI_OPEN_WRITE) )
      {
      CloseAtr(atFile->atr);
      free(atFile);
      lastAtariError = 4;
      return NULL;
      }

/* read directory, find start sector and number of sectors and set
   in atFile.  Initialize current sector to start sector also */

   if ( AtariFindFirst(atrName, 0, fileName, &fileInfo) )
      {
      lastAtariError = 5;
      return NULL;
      }

/* is the file the ATR is lcoated in write protected? */   
   if ( fileInfo.locked && (mode & ATARI_OPEN_WRITE) )
      {
      lastAtariError = 6;
      return NULL;
      }

/* set some file info data in the structure */
   atFile->startSector = atFile->currentSector = fileInfo.startSector;
   atFile->fileNo = fileInfo.fileNo;
   atFile->numberOfSectors = fileInfo.sectorCount;
   atFile->openFlag = mode;
   atFile->currentOffset = 0;
   if (sectorSize == 128)
      atFile->sectorLinkOffset = 125;
   else if (sectorSize == 256 )
      atFile->sectorLinkOffset = 253;
   else
      {
      lastAtariError = 7;
      return NULL;
      }
   return atFile;
   }


/******************************************************************
 ReadAtariFile - reads bytes bytes from the open atari file specified
   in atFile and stores them in buffer.  buffer must be big enough.
   Returns bytes actually read. 
******************************************************************/

long ReadAtariFile( AtariFilePtr atFile, char *buffer, long bytes )
{
	/* int lastSector = 0; */
   long bytesRead = 0;

   if ( !bytes || atFile->eofFlag)
      return 0;
   if ( !(atFile->openFlag & ATARI_OPEN_READ) )
      return 0;
   if ( !atFile->currentOffset )
      {
      /* read sector */
      if (ReadSector(atFile->atr, atFile->currentSector, atFile->sectorBuffer) != atFile->sectorSize )
         {
         if ( !lastAtariError )
            lastAtariError = 19;
         return 0;
         }
      if ( atFile->sectorSize == 128 )
         atFile->bytesData = atFile->sectorBuffer[atFile->sectorLinkOffset+2]
         & 127;
      else
         atFile->bytesData = atFile->sectorBuffer[atFile->sectorLinkOffset+2]; 
      }
   while( bytes )
      {
      while ( atFile->currentOffset < atFile->bytesData && bytes)
         {
         *(buffer++) = atFile->sectorBuffer[atFile->currentOffset++];
         bytes--;
         bytesRead++;
         }

      if ( bytes )
         {
         /* read next sector */
         atFile->currentOffset = 0;
         if (atFile->atr->dosType == DOS_MYDOS)
            atFile->currentSector =
                (atFile->sectorBuffer[atFile->sectorLinkOffset] << 8) |
                atFile->sectorBuffer[atFile->sectorLinkOffset+1];
         else /* assume atari dos */
            atFile->currentSector =
                ((atFile->sectorBuffer[atFile->sectorLinkOffset] & 3) << 8) |
                (atFile->sectorBuffer[atFile->sectorLinkOffset+1]);

         if (!atFile->currentSector )
            {
            atFile->eofFlag = 1;
            return bytesRead;
            }

         ReadSector(atFile->atr, atFile->currentSector, atFile->sectorBuffer);
         if ( atFile->sectorSize == 128 )
            atFile->bytesData = atFile->sectorBuffer[atFile->sectorLinkOffset+2] & 127;
         else
            atFile->bytesData = atFile->sectorBuffer[atFile->sectorLinkOffset+2]; 
         }

      }
   return bytesRead;
   }


/******************************************************************
 CloseAtariFile - Closes Atari File 
******************************************************************/

int CloseAtariFile( AtariFilePtr atFile )
   {
   int stat;
   /* simple enough */
   stat = CloseAtr( atFile->atr );
   free( atFile );
   return stat;
   }


/******************************************************************
 EofAtariFile - Returns 1 if at EOF of atari file, 0 if not 
******************************************************************/

int EofAtariFile( AtariFilePtr atFile )
   {
   /* simple enough */
   return atFile->eofFlag;
   }

/******************************************************************
 AtariDirectory - Displays atari directory of disk image to screen
                  return 0 for success.  atrName is the ATR file
                  name, pattern is mask to use.  use "*.*" for all
                  files.  Wide if non zero displays actual file
                  length instead of just a sector count.
******************************************************************/

int AtariDirectory( char *atrName, char *pattern)
   {
   char buffer[256];
   char fileName[14];
   byte protected;
   unsigned short sectorSize;
   unsigned short sectorCount;
   AtrFilePtr atr;
   int i,j,cnt = 0;
   char locked;
   long fileSize;
   AtariDosDirEntryPtr dirEntry;

   atr = OpenAtr(atrName);
   if ( atr == NULL )
      return 1;

   if ( GetAtrInfo( atr, &sectorSize, &sectorCount, &protected) )
      {
      free(atr);
      return 1;
      }
   
   printf("sector size = %hu    sector count = %hu\n\n", sectorSize,
       sectorCount);


   printf("\nDirectory of '%s':\n\n", atrName);

   if ( !wide )
      {
      printf("no f filename ext  secs  startSec\n");
      printf("-- - -------- ---  ----  --------\n");
      }
   else
      {
      printf("no f filename ext  secs  length  startSec\n");
      printf("-- - -------- ---  ----  ------  --------\n");
      }
   for( i = firstDirSector; i <= lastDirSector; i++ )
      {
		char printbuf[101];
		int done = 0;
      if (! ReadSector(atr, (unsigned short) i, buffer) )
         return 1;
      for( j=0; j< dirEntriesPerSector; j++ )
         {
         dirEntry = (AtariDosDirEntryPtr)(buffer + dirEntrySize * j );
			EndianFix(dirEntry);
         locked = (dirEntry->flag & LOCKED_FLAG) ? '*' : ' ';
         if (dirEntry->flag & DELETED_FLAG )
            locked = 'D'; 
			else if (dirEntry->flag & MYDOSDIR_FLAG ) {
            locked = ':'; 
			} else if(dirEntry->flag == DOS25EXT_FLAGS) {
				locked = '<';
			}
         if ( (dirEntry->flag == DOS25EXT_FLAGS) ||
					((dirEntry->flag & (INUSE_FLAG | MYDOSDIR_FLAG)) &&
					PatternMatch(pattern, dirEntry->fileName, dirEntry->extender)) )
            {
            if ( wide )
               {
               CloseAtr(atr);
               MakeFileName(fileName, dirEntry->fileName, dirEntry->extender);
					fileSize = AtariFileSize(atrName, fileName);
					if(fileSize < 0)
						sprintf(printbuf, "??");
					else
						sprintf(printbuf, "%-6ld", fileSize);

               atr = OpenAtr(atrName);
               if ( !atr )
                  return 1;
               printf("%2u %c %-8.8s %-3.3s  %4.3hu  %-6s  %hu\n", cnt, locked,
                  dirEntry->fileName, dirEntry->extender,
                  dirEntry->sectorCount, printbuf, dirEntry->startSector);
               }
            else
               printf("%2u %c %-8.8s %-3.3s  %4.3hu  %hu\n", cnt, locked,
                  dirEntry->fileName, dirEntry->extender,
                  dirEntry->sectorCount, dirEntry->startSector);
            }  else {
					/* 20070518 bkw: Atari DOS stops at the first "never used"
						dirent, so do the same here.  */
					done = 1;
					break;
				}
         cnt++;
         }
		if(done) break;
      }

   if ( CloseAtr(atr) )
      return 1;

   return 0;
   }


/******************************************************************
 AtariFileSize - Returns size of atari file or -1 on error
******************************************************************/

long AtariFileSize( char *atrFile, char *fileName )
   {
   long count = 0;
   long bytes;
   static char buffer[16];
   AtariFilePtr input;

   /* open the atari file on the ATR image */
   input = OpenAtariFile(atrFile, fileName, ATARI_OPEN_READ);
   if ( input == NULL )
      return -1;  
   /* count how many bytes we can actually read */
   while( (bytes=ReadAtariFile(input, buffer, sizeof(buffer))) > 0 )
      count+= bytes; 
   CloseAtariFile(input);
   return(count);
   }


/******************************************************************
 ExtractAtariFile - returns no. files extracted, -no for error.
                    file is stored with same name in dosPath
                    directory.  (don't add the trailing '\').
                    Wildcards are allowed for atari file. Use NULL
                    for dosPath to extract to current directory
******************************************************************/

int ExtractAtariFile( char *atrFile, char *fileName, char *unixPath )
{
   int count = 0;
   long bytes, bytesOut;
   static char buffer[16];
   AtariFilePtr input;
   char outName[4096];
   FILE *output;
   AtariFileInfo info;
   

   if ( !AtariFindFirst(atrFile, 0, fileName, &info) )
      {
      do {
         if ( unixPath != NULL)
            {
            strcpy(outName, unixPath);
            strcat(outName,"/");
            }
         else
            outName[0] = 0;
         strcat(outName, info.fileName);

			/* 20070518 bkw: Get rid of trailing dots for files with no extension */
			if(outName[strlen(outName)-1] == '.')
				outName[strlen(outName)-1] = '\0';
         
         output = fopen(outName, "wb");
         if (output == NULL )
            {
            lastAtariError = 30;
            return -count-1;
            }
         input = OpenAtariFile(atrFile, info.fileName, ATARI_OPEN_READ);
         if ( input == NULL )
            {
            return -count-1;  
            }
         if ( verbose )
            printf("Extracting '%s'...", outName);
         while( (bytes=ReadAtariFile(input, buffer, sizeof(buffer))) > 0 )
            {
            bytesOut = fwrite(buffer, 1, (int)bytes, output);
            if ( bytes != bytesOut )
               {
               fclose( output );
               CloseAtariFile(input);
               lastAtariError = 31;
               if (verbose )
                  printf("\n");
               return -count-1;
               }
            }
         fclose( output );
         CloseAtariFile(input);
         if ( verbose )
            printf(" done\n");
         count ++;
         } while ( !AtariFindNext(&info) );
      }
   else
      {
      return 0;
      }
   return(count);
}


/******************************************************************
 UpdateAtariFileNo - For atari dos, will fix the file no in each
                     sector within the file.  For use after a
                     directory is sorted.  returns 0 for success
******************************************************************/

int FixAtariFileNo( char *atrName, char *fileName, int fileNo )
{
	/* int cnt=0; */
   AtariFilePtr atFile;

   atFile = OpenAtariFile( atrName, fileName, ATARI_OPEN_READ);
   if ( atFile == NULL )
      return 1;

   if ( atFile->atr->dosType != DOS_ATARI )
      {
      CloseAtariFile(atFile);
      return 0;
      }


   while (atFile->currentSector) 
      {   
      if (ReadSector(atFile->atr, atFile->currentSector, sectorBuffer) != atFile->sectorSize )
         {
         if ( !lastAtariError )
            lastAtariError = 19;
         CloseAtariFile(atFile);
         return 1;
         }

      /* set the file no in the file */
      sectorBuffer[atFile->sectorLinkOffset] &= 3;
      sectorBuffer[atFile->sectorLinkOffset] |= (atFile->fileNo<<2);

      /* write the sector */
      if (WriteSector(atFile->atr, atFile->currentSector, sectorBuffer) != atFile->sectorSize )
         {
         if ( !lastAtariError )
            lastAtariError = 19;
         CloseAtariFile(atFile);
         return 1;
         }

      /* get next sector in link */
      atFile->currentSector =
             ((sectorBuffer[atFile->sectorLinkOffset] & 3) << 8) |
             sectorBuffer[atFile->sectorLinkOffset+1];
      }
   CloseAtariFile(atFile);
   return 0;
   }







static int ClearAtariDirectory( char *file );


/******************************************************************
 SortAtariDir - 
******************************************************************/

int SortAtariDir( char *atrName )
   {
   AtrFilePtr atr;
   char *pos;
   AtariFileInfoPtr files[64];
   AtariFileInfo info;
   AtariDosDirEntryPtr entry;
   int offset;
   unsigned short sector;   

   int i, cnt = 0;

   /* read file info for all files, allocate memory and store in array */

   if ( !AtariFindFirst(atrName, 0, "*.*", &info) )
      {
      do {
         files[cnt] = malloc( sizeof(info) );
         memcpy( files[cnt], &info, sizeof(info) );
         cnt ++;
         } while ( !AtariFindNext(&info) );
      }
   else
      {
      return 1;
      }

   /* sort the files by name */

   qsort( (void *)files, (size_t) cnt, (size_t) sizeof(AtariFileInfoPtr), CompareName );

   /* clear out the directory */
   if ( ClearAtariDirectory(atrName) )
      return 1;

   /* write the entries in sorted order to the directory sectors */

   offset = 0;
   sector = firstDirSector;

   atr = OpenAtr( atrName );

   for( i=0; i<cnt; i++ )
      {
      entry = (AtariDosDirEntryPtr) (sectorBuffer + offset );
      entry->startSector = files[i]->startSector;
      entry->sectorCount = files[i]->sectorCount;
      entry->flag = files[i]->flag;
		EndianFix(entry);
      pos = strchr( files[i]->fileName, '.' );

		/* 20070518 bkw: init to all spaces, else you get filenames
			like "DOS\0\0\0\0\0SYS" (\0 shows up as Atari heart character) */
		memset(entry->fileName, ' ', 8);
		memset(entry->extender, ' ', 3);

      if ( pos != NULL)
         {

         strncpy( entry->fileName, files[i]->fileName, 
                  min(pos-files[i]->fileName,8) );
         strncpy( entry->extender, pos+1, 3 );     
         }
      else
         {
         strncpy( entry->fileName, files[i]->fileName, 8);
         }
      offset += 16;
      if (offset >= 128)
         {
         if ( WriteSector( atr, sector, sectorBuffer) != atr->secSize )
            {
            CloseAtr( atr );
            return 1;
            }
         sector ++;
         offset = 0;
         memset(sectorBuffer, 0, sizeof(sectorBuffer));
         }      
      }
   if ( offset > 0 )
      if ( WriteSector( atr, sector, sectorBuffer) != atr->secSize )
         {
         CloseAtr( atr );
         return 1;
         }

   CloseAtr( atr );


   /* This should have no effect on Mydos extended format disks */
   for( i=0; i<cnt; i++ )
      {
      if ( FixAtariFileNo( atrName, files[i]->fileName, i) )
         return 1;
      free( files[i] );
      }
   return 0;
   }

/* function for above used as arg to qsort */
static int CompareName( const void *a, const void *b )
   {
   AtariFileInfoPtr aa = *(AtariFileInfoPtr *)a,
                    bb = *(AtariFileInfoPtr *)b;

   return strcmp( aa->fileName, bb->fileName );
   }

/*******************************************************************
ClearAtariDirectory - internal routine
*******************************************************************/

static int ClearAtariDirectory( char *file )
   {
   AtrFilePtr atr;
   int i; 
  
   memset(sectorBuffer, 0, sizeof(sectorBuffer) );
   atr = OpenAtr( file );
   
   for( i = firstDirSector; i <= lastDirSector; i++ )
      if ( WriteSector(atr, i, sectorBuffer) != atr->secSize )
         return 1;
   return 0;
   CloseAtr(atr);
   }


/********************************************************************
 CreateBootAtr - creates a minimally sized bootable ATR image from
                 an atari executable.  The executable must not need
                 DOS to run.
********************************************************************/

int CreateBootAtr( char *atrName, char *fileName)
   {
   unsigned long fileSize;
   unsigned long sectorCnt;
   AtrHeader hdr;
   unsigned long paras;
   FILE * atrFile, *inFile;
   size_t padding, bytes, bytes2;
   struct stat fileInfo;
   int status;
	int first = 1;

/* get file's size */

   status = stat(fileName, &fileInfo);
   if ( status )
      return 11;
   fileSize = (unsigned long) fileInfo.st_size;
   if ( !fileSize )
      return 12;

/* determine number of sectors required  */

   sectorCnt = (unsigned short) ((fileSize + 127L) / 128L + 3L);
   paras = sectorCnt * 16;

/* create ATR header */
   memset(&hdr, 0, sizeof(hdr));
   hdr.idLow      = (byte) 0x96;
   hdr.idHigh     = (byte) 0x2;
   hdr.paraLow    = (byte) (paras & 0xFF);
   hdr.paraHigh   = (byte) ((paras >> 8) & 0xFF);
   hdr.paraHigher = (byte) ((paras >> 16) & 0xFF);
   hdr.secSizeLow = (byte) 128;

/* open output file */
   atrFile = fopen(atrName, "wb");
   if ( atrFile == NULL )
      return 1;

/* Write the ATR Header */
   bytes = fwrite(&hdr, 1, sizeof(hdr), atrFile);
   if ( bytes != sizeof(hdr) )
      {
      fclose(atrFile);
      return 2;
      }

/* plug the file size into the boot sectors at offset 9 (4 bytes)*/
   bootData[9]  = (byte)(fileSize & 255);
   bootData[10] = (byte)((fileSize >> 8) & 255);
   bootData[11] = (byte)((fileSize >> 16) & 255);
   bootData[12] = 0;

/* write the three boot sectors */
   bytes = fwrite(bootData, 1, 384, atrFile);
   if ( bytes != 384 )
      {
      fclose(atrFile);
      return 6;
      }

/* open the input file and copy/append the file's data to output file */

   inFile = fopen(fileName, "rb");
   if ( inFile == NULL )
      {
      fclose(atrFile);
      return 13;
      }

   bytes = 384;
   while (bytes == 384)
      {
      bytes = fread(bootData, 1, 384, inFile);
      if ( !bytes )
         break;

		/* 20070518 bkw: Make sure it really is an Atari bin load file */
		if(first) {
			if((bootData[0] != 0xff) && (bootData[1] != 0xff)) {
				fclose(inFile);
				fclose(atrFile);
				return 20;
			}

			first = 0;
		}

      bytes2 = fwrite(bootData, 1, bytes, atrFile);
      if ( bytes != bytes2 )
         {
         fclose(inFile);
         fclose(atrFile);
         return 19;
         }
      }
   if ( !feof(inFile) )
      {
      fclose(inFile);
      fclose(atrFile);
      return 19;
      }

  fclose(inFile); 


/* pad to even sector size (data has no meaning) */
   padding = (size_t) ((sectorCnt-3) * 128 - fileSize ); 
   if ( padding )
      {
      bytes = fwrite(bootData, 1, padding, atrFile);
      if ( bytes != padding )
         {
         fclose(atrFile);
         return 7;
         }
      }  

/* close output */
   fclose(atrFile);

   return 0;
   }

/********************************************************************
ExtractExeFromBootAtr - undoes a CreateBootAtr by extracting the
                        original executable
returns 0 for error, or file length in bytes of file extracted
!!This function needs to have code added to distinguish error types.
********************************************************************/

long ExtractExeFromBootAtr( char *atrName, char *fileName)
   {
   FILE *atrFile, *exeFile;
   unsigned char *buffer;
   AtrHeader hdr = {0};
	/* long fileSize; */
   size_t bytes,bytes2,readCnt;
   unsigned long  fSize, size;

/* get some memory */
   buffer = malloc(384);
   if ( !buffer)
      return 0;

/* open the atr file */
   atrFile = fopen(atrName, "rb");
   if ( !atrFile )
      {
      free(buffer);
      return 0;
      }

/* read Atr header */

   if ( fread(&hdr, 1, 16, atrFile) != 16 )
      {
      free(buffer);
      fclose(atrFile);
      return 0;
      }

/* verify it is an ATR file */

   if ( hdr.idHigh != 0x02 || hdr.idLow != 0x96 )
      {
      free(buffer);
      fclose(atrFile);
      return 0;
      }

/* read first 3 (boot) sectors */

   if ( fread(buffer, 1, 384, atrFile) != 384 )
      {
      free(buffer);
      fclose(atrFile);
      return 0;
      }

/* set the file size in bootData from the file so we can compare*/
   bootData[9]   = buffer[9];
   bootData[10]  = buffer[10];
   bootData[11]  = buffer[11];
   bootData[12]  = buffer[12];

/* check if ATR was created by MakeBootAtr */
   if ( memcmp(buffer, bootData, 384) )
      {
      free(buffer);
      fclose(atrFile);
      return 0;
      }
/* Get size of file to extract */
   fSize = size = ((unsigned long)buffer[9]|(((unsigned long)buffer[10])<<8)|(((unsigned long)buffer[11])<<16));

/* Open output file */
   exeFile = fopen(fileName, "wb");
   if ( !exeFile )
      {
      fclose(atrFile);
      free(buffer);
      return 0;
      }
/* copy 'size' bytes from the Atr file to the exe file */   
   bytes = 384;
   while (bytes == 384 && fSize)
      {
      readCnt = min(384, fSize);
      bytes = fread(buffer, 1, readCnt, atrFile);
      if ( !bytes )
         break;
      bytes2 = fwrite(buffer, 1, bytes, exeFile);
      if ( bytes != readCnt )
         {
         fclose(exeFile);
         fclose(atrFile);
         free(buffer);
         return 0;
         }
      fSize -= bytes;
      }

/* clean up and get out of here */
   fclose(exeFile);
   fclose(atrFile);
   free(buffer);

   return size;
   }