-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIBMDISK.C
433 lines (356 loc) · 10.9 KB
/
IBMDISK.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
/* @(#)ibmdisk.c 2.3 */
/* ibmdisk (12 Oct 84) - read IBM PC disk */
/* This module contains the following user-callable routines:
OPENIBM - open an IBM file
READIBM - read the next cluster (1K) from the opened file
OPENDIR - setup for reading directory entries
READDIR - read next valid directory entry
The diskette must be 8 sectors per track, double sided, and
the file to be read must be in the root directory.
*/
#include "portab.h"
#include "ibmdisk.h"
EXTERN FS fs;
extern WORD debugflag;
/* structure for one directory entry */
struct {
BYTE d_name[11];
BYTE d_attr;
WORD d_curclust; /* used only by these routines, not by DOS */
BYTE d_fill[12];
WORD d_cluster; /* starting cluster */
LONG d_size;
} direntry;
/* buffers for fat & directory */
BYTE dirbuf[MAXSECTSIZE]; /* used for directory search */
BYTE fat[MAXSECTSIZE*MAXFATSIZE]; /* used only to store the FAT */
WORD dirsect; /* current directory sector */
WORD dirindex; /* index to current directory entry */
WORD nextdir; /* index to next directory entry to read */
BYTE name[11]; /* parsed name of open file */
BYTE tpi; /* current tpi flags for iopb */
/*********** FAT I/O ROUTINES ***********/
/* nextclust - get next cluster number from FAT, given current cluster no. */
nextclust(cluster)
WORD cluster;
{
WORD index,fatword;
index = cluster + (cluster >> 1); /* multiply by 1.5 */
fatword = getword(&fat[index]); /* get word from fat */
if (cluster & 1) fatword >>= 4; /* keep high 12 bits */
if( debugflag )
printf("nextclust: cluster = %x, index = %x, fatword = %x\n",
cluster, index, fatword);
return (fatword & 0xfff); /* mask off top 4 bits */
}
/* setnext - set the fat entry for a given cluster to be "next" */
setnext(cluster,next)
WORD cluster,next;
{
WORD index,fatword,mask;
if (cluster & 1)
{
mask = 0xf; /* use only high 12 bits */
next <<= 4; /* shift next to high 12 */
}
else mask = 0xf000; /* use only low 12 bits */
index = cluster + (cluster >> 1); /* multiply by 1.5 */
fatword = getword(&fat[index]); /* get word from fat */
putword(&fat[index],(fatword & mask) | next); /* modify it */
if( debugflag )
printf("setnext: cluster = %x, next = %x, index = %x, fatword = %x\n",
cluster, next, index, fatword);
}
/* readfat - read the fat into the fat array */
readfat()
{
if( ReadSector(fs.disknum,fs.fatstartsec,fs.fatszinsecs,fat) == ERROR )
{
printf("FAT read failed.\n");
exit();
}
}
/* writefat - write the FAT array to both fat areas on the disk */
writefat()
{
WORD i; /* number of fats to write */
for (i = 0; i < fs.nfats; i++)
WriteSector(fs.disknum,(LONG)(fs.fatstartsec+i*fs.fatszinsecs),
fs.fatszinsecs,fat);
}
/* delchain - delete a chain of clusters (i.e. reclaim them) */
delchain(cluster)
WORD cluster;
{
WORD next;
if (cluster == 0) return;
while (cluster < 0xff8)
{
next = nextclust(cluster); /* save next cluster */
setnext(cluster,0); /* set next to 0 (available) */
cluster = next;
}
}
/* allocclust - return the first free cluster number */
allocclust()
{
WORD i,nclusters;
nclusters = ((fs.fatszinsecs*fs.bps)*2)/3;
for (i = 0; i < nclusters; i++)
if (nextclust(i) == 0) return (i);
return (-1);
}
/*********** CLUSTER I/O ***********/
/* readcluster - read the cluster into the buffer array */
readcluster(bp,cluster)
BYTE *bp;
WORD cluster;
{
if( ReadSector(fs.disknum,
(LONG)((cluster - 2) * fs.clszinsecs + fs.datastartsec),
fs.clszinsecs,bp) == ERROR )
{
printf("Can't read cluster %d\n", cluster);
exit();
}
}
/* writecluster - write the cluster from the buffer array to disk */
writecluster(bp,cluster)
BYTE *bp;
WORD cluster;
{
if( WriteSector(fs.disknum,
(LONG)((cluster - 2) * fs.clszinsecs + fs.datastartsec),
fs.clszinsecs,bp) == ERROR )
{
printf("WriteSector failed in readcluster at cluster: %d\n",
cluster);
exit();
}
}
/*********** DIRECTORY I/O ***********/
/* opendir - read the first directory sector and initialize pointers to it */
opendir()
{
/* config(); /* determine medium type */
if( ReadSector(fs.disknum,fs.rtdirstartsec,1,dirbuf) == ERROR )
{
printf("ReadSector failed reading directory in opendir().\n");
exit();
}
dirsect = nextdir = 0;
return (0);
}
/* readdir - read next directory entry to user-specified buffer.
The directory sector number is in dirsect+DIRSECT, and
the index into the directory buffer is in dirindex.
Return -1 if no more entries. */
readdir(buffer)
BYTE *buffer;
{
for (;;)
{
if (nextdir >= fs.bps)
{
if (++dirsect >= fs.rtdirszinsecs) return (-1);
if( ReadSector(fs.disknum,(LONG)(dirsect+fs.rtdirstartsec),
1,dirbuf) == ERROR )
{
printf("In readdir, ReadSector failed\n");
return (-1);
}
nextdir = 0;
}
if (dirbuf[nextdir] == 0) return (-1); /* end of directory */
if (dirbuf[nextdir] == 0xe5) nextdir += sizeof(direntry);
else
{
movb(&dirbuf[nextdir],buffer,sizeof(direntry));
dirindex = nextdir; /* let outsiders know where */
nextdir += sizeof(direntry);
return (0);
}
}
}
/* allocdir - allocate a new directory entry */
allocdir()
{
opendir();
for (;;)
{
if (nextdir >= fs.bps) /* get next sector */
{
if (++dirsect >= fs.rtdirszinsecs) return (-1);
if( ReadSector(fs.disknum,(LONG)(dirsect+fs.rtdirstartsec),
1,dirbuf) == ERROR )
{
printf("Can't read directory sector 0x%lx.\n",
(LONG)(dirsect+fs.rtdirstartsec));
return(-1);
}
nextdir = 0;
}
if (dirbuf[nextdir] == 0 || dirbuf[nextdir] == 0xe5) /* is free? */
{
dirindex = nextdir; /* let outsiders know */
return (0); /* which direntry is free */
}
else nextdir += sizeof(direntry); /* get next entry */
}
}
/*********** FILE READ ***********/
/* readibm - read the next cluster from the IBM file to user
specified buffer. The actual number of bytes
read is returned. A cluster is 1K bytes. */
readibm(buffer)
BYTE *buffer;
{
WORD actual;
if (direntry.d_cluster >= 0xff8 || direntry.d_size == 0)
return (0);
readcluster(buffer,direntry.d_cluster);
if( debugflag )
printf("readibm: current cluster = %x\n",direntry.d_cluster);
direntry.d_cluster = nextclust(direntry.d_cluster);
if( debugflag )
printf("readibm: next cluster = %x\n",direntry.d_cluster);
actual = fs.bps*fs.clszinsecs;
if (direntry.d_size < actual) actual = direntry.d_size;
direntry.d_size -= actual;
return (actual);
}
/* parse - parse a filename, put parsed name in "name" */
parse(fname)
BYTE *fname;
{
WORD i;
BYTE c;
setb(' ',name,sizeof(name));
i = 0;
while (c = *fname++) /* terminated by null */
{
if (i >= sizeof(name)) return (-1);
if (c == '.') /* start of extension */
{
if (i > 8) return (-1);
else i = 8;
}
else
{
if (c >= 'a') c -= 'a' - 'A'; /* convert to upper case */
name[i++] = c;
}
}
return (0);
}
/* openibm - search directory for IBM file. If not found, return -1.
Otherwise, read the directory entry into direntry,
and return 0. */
openibm(fname)
BYTE *fname;
{
if (parse(fname) == -1) return (-1); /* bad filename */
opendir(); /* rewind the directory */
while(readdir(&direntry) != -1) /* end of directory? */
{
if (strncmp(direntry.d_name,name,sizeof(name)) == 0)
{
direntry.d_cluster = swapw(direntry.d_cluster);
direntry.d_size = swapl(direntry.d_size);
if( debugflag )
printf("openibm: cluster = %x, size = %lx (swapped)\n",
direntry.d_cluster, direntry.d_size);
readfat();
return (0);
}
}
return (-1); /* couldn't find the file */
}
/*********** FILE WRITE ***********/
/* creatibm - create an IBM PC file (open for write) */
creatibm(fname)
BYTE *fname;
{
if (parse(fname) == -1) return (-1); /* bad filename */
if (openibm(fname) == -1) /* not found */
{
if (allocdir() == -1)
{
printf("Can't allocate a directory entry, directory full.\n");
return (-1); /* directory full */
}
setb(0,&direntry,sizeof(direntry));
movb(name,direntry.d_name,sizeof(name));
/* make nice filename */
readfat(); /* get the fat */
}
else /* found - delete old file */
{ /* already got fat */
delchain(direntry.d_cluster); /* reclaim the clusters */
direntry.d_cluster = 0; /* set file size to 0 */
direntry.d_size = 0;
}
return (0); /* no errors! */
}
/* eraibm - erase an IBM PC file */
eraibm(fname)
BYTE *fname;
{
if (parse(fname) == -1) return (-1); /* bad filename */
if (openibm(fname) == -1) /* not found */
{
printf("Can't find file %s\n", fname);
return (-1);
}
else /* found - delete file */
{ /* already got fat */
delchain(direntry.d_cluster); /* reclaim the clusters */
direntry.d_name[0] = 0xe5; /* mark as erased */
direntry.d_cluster = 0; /* set file size to 0 */
direntry.d_size = 0;
closeibm();
}
return (0); /* no errors! */
}
/* writeibm - write the next cluster from user specified buffer
to an IBM file. The actual number of bytes
written is returned. A cluster is 1K bytes. If less than
1K bytes are written, this must be the last write to the
file before it is closed. */
writeibm(buffer,count)
BYTE *buffer;
WORD count;
{
WORD freeclust;
if (count > fs.clszinsecs*fs.bps) return (-1); /* more than 1K! */
if ((freeclust = allocclust()) == -1) /* allocate a new cluster */
{
printf("Can't allocate a data cluster, disk full!!!\n");
return (-1); /* disk full! */
}
if( debugflag )
printf("writeibm: freeclust = %x\n", freeclust);
setnext(freeclust,0xfff); /* this is new end of chain */
if (direntry.d_cluster == 0) /* first cluster in file? */
direntry.d_cluster = freeclust; /* make this first cluster */
else
setnext(direntry.d_curclust,freeclust); /* else link to end of chain */
if( debugflag )
printf("writeibm: d_curclust = %x\n", direntry.d_curclust);
direntry.d_curclust = freeclust; /* make this current cluster */
writecluster(buffer,freeclust); /* write the data */
direntry.d_size += count; /* update the file size */
return (count);
}
/* closeibm - close a file that was open for write */
closeibm()
{
direntry.d_curclust = 0;
direntry.d_cluster = swapw(direntry.d_cluster);
direntry.d_size = swapl(direntry.d_size);
direntry.d_attr = 0x20; /* set archive attr */
movb(&direntry,&dirbuf[dirindex],sizeof(direntry));
WriteSector(fs.disknum,(LONG)(dirsect+fs.rtdirstartsec),1,dirbuf);
writefat();
}