-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcmk.c
267 lines (235 loc) · 6.54 KB
/
bbcmk.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
char infile[256], outfile[256];
int verbose, noglob, inter;
void syntax(command)
char *command;
{
printf("Syntax: %s <bbcimage> <bbcfile> [<bbcfile>...] <outfile>\n", \
command);
exit(0);
}
char *lengthen_string(instring)
char *instring;
{
char i;
if (strlen(instring) == 7) return instring;
for (i=strlen(instring); i< 7; i++)
{
strcat(instring," ");
}
return instring;
}
/* Parsed arguments appear not to have been implemented in the original code. */
#if 0
int parse_arguments(argc,argv)
int argc;
char *argv[];
{
int carg=1,c=0,carg2;
infile[0]='\0';
while (carg<argc)
{
c=0;
while (argv[carg][0]='-')
{
carg2=carg;
while (c<strlen(argv[carg])-1)
{
switch(argv[carg][++c])
{
case 'h' :
case '?' :
syntax(argv[0]);
case '-' :
break;
case 'n' :
noglob=1;
break;
case 'v' :
verbose=1;
break;
case 'i' :
inter=1;
break;
case 'f' :
strncpy(infile,argv[++carg2],255);
break;
default :
printf("%s: Invalid flag: -%c\n",argv[0],argv[carg][c]);
syntax(argv[0]);
}
if (argv[carg][++c]=='-') break;
carg++;
}
}
if ( argv[carg][0] != '-' ) break;
}
if (infile[0]=='\0')
{
printf("%s: No input file specified\n",argv[0]);
syntax(argv[0]);
}
return carg;
}
#endif
int main( argc, argv)
int argc;
char **argv;
{
unsigned char *dblock;
int success,i,j,dirsize,next;
FILE *inhandle,*outhandle;
directory_type *directory;
char permissions[12];
char working[256],name[256],load[256],exec[256],ssize[256];
unsigned char dirname;
int size,files;
int sector=2;
if (argc < 3) {
syntax(argv[0]); /* exits */
}
strncpy( infile, argv[1], 255);
next=2;
dblock=(char *) calloc( 1, 260 );
directory=(directory_type *) calloc( 144, sizeof( directory_type ) );
outhandle=fopen( infile, "w" );
if ( outhandle == 0 )
{
printf( "Failed to open file %s\n", infile );
return 1;
}
/* work out the files */
for (i=2; i < argc; i++)
{
sprintf(working,"%s.inf",argv[i]);
inhandle=fopen( working, "r" );
if ( inhandle == 0 )
{
sprintf(working,"%s.INF",argv[i]);
inhandle=fopen( working, "r" );
if ( inhandle == 0 )
{
strcpy(working,argv[i]);
inhandle=fopen( working, "r" );
if ( inhandle == 0 )
{
printf( "Failed to open file %s\n", working );
return 1;
}
}
}
fscanf(inhandle,"%s %lx %lx %lx",directory[i-2].name,&directory[i-2].owner,&directory[i-2].group,&directory[i-2].size);
/* fscanf(inhandle,"%s %s %s %s",name,load,exec,ssize);
strcpy(directory[i-2].name,name);
directory[i-2].owner=strtol(load,NULL,16);
directory[i-2].group=strtol(exec,NULL,16);
directory[i-2].size=strtol(ssize,NULL,16);*/
if (directory[i-2].size<17)
{
FILE *temphandle;
char work2[256];
strcpy(work2,working);
strtok(work2,".");
temphandle=fopen( work2,"r");
fseek(temphandle,0,SEEK_END);
directory[i-2].size=ftell(temphandle);
fclose(temphandle);
}
fclose(inhandle);
/* Now we need to assign a sector to the file (and reserve the space!)*/
directory[i-2].start_track=sector/10;
directory[i-2].start_sector=sector%10;
/* and reserve the space */
sector+=(directory[i-2].size / 256)+1;
}
/* first make a blank disc! */
memset(dblock,0,256);
for (i=0;i<40;i++);
{
for (j=0;j<10;j++)
{
bbc_write_sector(dblock,i,j,outhandle);
}
}
/* Right now we have the catalogue, let's make the damn thing! */
/* First the filenames */
files=argc-2;
memset(dblock,0,256);
memcpy(dblock,"BBCMK",5);
for (i=0;i<files;i++)
{
dirname=directory[i].name[0];
sprintf(working,"%7s%c",lengthen_string(directory[i].name+2),dirname);
memcpy(dblock+((i+1)*8),working,8);
}
/* And put it! */
success=bbc_write_sector( dblock, 0, 0, outhandle);
if ( success == -1 )
{
printf("Failed to write to outfile\n");
fclose(outhandle);
return 1;
}
/* Sector 1 - the information! */
memset(dblock,0,256);
/* fill in the information block */
dblock[5]=files*8;
dblock[6]=1;
dblock[7]=0x90;
for (i=0;i<files;i++)
{
int base=(i+1)*8;
dblock[base]=directory[i].owner % 256;
dblock[base+1]=directory[i].owner / 256 % 65536;
dblock[base+2]=directory[i].group % 256;
dblock[base+3]=directory[i].group / 256 % 65536;
dblock[base+4]=directory[i].size % 256;
dblock[base+5]=directory[i].size / 256 % 65536;
/* Now the fun byte! - unsupported 'cos it only get's used for roms! */
dblock[base+6]=((directory[i].start_track*10) >> 8);
dblock[base+7]=directory[i].start_track*10+directory[i].start_sector;
printf("%d %d\n",dblock[base+6],dblock[base+7]);
}
/* And put it! */
success=bbc_write_sector( dblock, 0, 1, outhandle);
if ( success == -1 )
{
printf("Failed to write to outfile\n");
fclose(outhandle);
return 1;
}
/* Now write the files */
for (i=0;i<files;i++)
{
int file_sectors=directory[i].size/256+1;
int filesize=directory[i].size;
int endsize=256;
int next_sector=directory[i].start_sector;
int next_track=directory[i].start_track;
strcpy(working,argv[i+2]);
strtok(working,".");
printf("Processing %s\n",working);
inhandle=fopen( working, "r" );
if ( inhandle == 0 )
{
printf( "Failed to open file %s\n", infile );
return 1;
}
/* Now read in the file 256 bytes at a time! */
for (j=0; j<file_sectors; j++)
{
if ( filesize < 256 ) endsize=256;
fread(dblock,endsize,1,inhandle);
bbc_write_sector(dblock,next_track,next_sector,outhandle);
next_sector++;
next_sector%=10;
next_track=(next_sector==0)?next_track+1:next_track;
filesize-=256;
}
fclose (inhandle);
}
fclose(outhandle);
}