-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixconv.c
More file actions
116 lines (103 loc) · 3.27 KB
/
fixconv.c
File metadata and controls
116 lines (103 loc) · 3.27 KB
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
/*----------------------------------------------------------------------
FIXCONV - a program which tries to fix bad T64 files
(like the ones created by Conv64)
by Fabrizio Gennari, (C) 2000-2006
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, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--------------------------------------------------------------------*/
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <malloc.h>
#include <errno.h>
#include "t64utils.h"
#include "program_block.h"
#ifndef __GNU_LIBRARY__
#define strcasecmp _stricmp
#endif
long file_size(FILE* descriptor){
long result;
fseek(descriptor, 0, SEEK_END);
result=ftell(descriptor);
fseek(descriptor, 0, SEEK_SET);
return result;
}
int lastpart(const char* total, const char* last){
int lendiff;
lendiff=strlen(total)-strlen(last);
if (lendiff<1) return 0;
if (strcasecmp(total+lendiff,last)) return 0;
return 1;
}
int main(int argc, char** argv){
char *tapename;
int currarg,used_entries,total_entries,filesize,offset,diff1,diff2,entries,entry;
unsigned char endadd_low, endadd_high;
FILE* desc;
struct program_block program;
for (currarg=1;currarg<argc;currarg++){
if (!lastpart(argv[currarg],".t64")){
tapename=(char*)malloc(strlen(argv[currarg])+5);
sprintf(tapename,"%s.t64",argv[currarg]);
}
else{
tapename=(char*)malloc(strlen(argv[currarg])+1);
sprintf(tapename,"%s",argv[currarg]);
}
printf("%s: ",tapename);
desc=fopen(tapename,"rb");
if (desc==NULL){
printf("Cannot open file\n",tapename);
goto end2;
}
if (detect_type(desc)!=t64){
printf("The file is not a T64\n");
goto end;
}
if ((entries=get_used_entries(desc))!=1){
printf("Sorry, has %d entries, not 1\n",entries);
goto end;
}
total_entries=get_total_entries(desc);
for(entry=1;entry<=total_entries;entry++)
if (get_entry_info(entry,desc,&program.info,&offset))
break;
printf("1 entry, name %s, ",program.info.name);
diff1=program.info.end-program.info.start;
filesize=file_size(desc);
diff2=filesize-offset;
if (diff1==diff2){
printf("File OK\n");
goto end;
}
printf("broken, fixing...");
program.info.end=program.info.start+diff2;
endadd_low=program.info.end&255;
endadd_high=program.info.end>>8;
fclose(desc);
desc=fopen(tapename,"r+b");
if (desc==NULL){
printf("failed: %s\n", strerror(errno));
goto end2;
}
fseek(desc,36+32*entry,SEEK_SET);
fwrite(&endadd_low ,1,1,desc);
fwrite(&endadd_high,1,1,desc);
printf("Fixed\n");
end:
fclose(desc);
end2:
free(tapename);
}
return 0;
}