-
Notifications
You must be signed in to change notification settings - Fork 2
/
taputil.c
112 lines (92 loc) · 2.72 KB
/
taputil.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
/************************************************************************
TAPTOOLS v1.0.3 - Tapefile manipulation utilities
Copyright (C) 1996, 2005 John Elliott <jce@seasip.demon.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#ifdef HAVE_STAT_H
#include <stat.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include "taputil.h"
/* Pacific C's stat() is declared to take char *, not const char *, so
* cast the first parameter if we're compiling with Pacific */
#ifdef __PACIFIC__
#define PACIFIC_CAST (char *)
#else
#define PACIFIC_CAST
#endif
int is3dos(const char *filename, byte *b)
{
FILE *fp;
int n,m;
byte tmp[128];
if (!b) b = tmp;
if (!(fp = fopen(filename,"rb"))) return -1;
if (fread(b,1,128,fp) < 128)
{
fclose(fp);
return 0;
}
fclose(fp);
if (memcmp(b,"PLUS3DOS\032",9)) return 0;
for (m=n=0;n<127;n++) m+=b[n];
return ((m & 0xFF) == b[n]);
}
FILE *opentap(const char *filename, const char *mode, int *is_zxt, long *taplen)
{
unsigned char header[128];
int zxt = 0;
long len = 0;
FILE *fp;
switch(is3dos(filename, header))
{
case 1: if (!memcmp(header+15, "TAPEFILE", 8))
{
/* Check for a tapefile with a +3DOS header. If found, get file length from
* the +3DOS header because the actual file length is likely to be rounded up
* to the nearest 128 bytes. */
zxt = 1;
len = (unsigned long)header[11]
+ ((unsigned long)header[12] << 8)
+ ((unsigned long)header[13] << 16)
+ ((unsigned long)header[14] << 24);
break;
}
/* Not .ZXT format - FALL THROUGH */
case 0:
{
struct stat st;
if (stat(PACIFIC_CAST filename, &st))
{
return NULL;
}
len = st.st_size;
zxt = 0;
}
break;
case -1:
default: return NULL;
}
fp = fopen(filename, mode);
if (!fp) return NULL;
fseek(fp, zxt ? 128 : 0, SEEK_SET);
if (is_zxt) *is_zxt = zxt;
if (taplen) *taplen = len;
return fp;
}