-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadFile.c
More file actions
132 lines (115 loc) · 2.43 KB
/
loadFile.c
File metadata and controls
132 lines (115 loc) · 2.43 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//loadFile.c
//Michael Black, 2007
//Modified by Fred Zhang, All modifications are commented with Changed.
// Those changes are used help implement directories.
//
//Loads a file into the file system
//This should be compiled with gcc and run outside of the OS
#include <stdio.h>
main(int argc, char* argv[])
{
int i;
if (argc<2)
{
printf("Specify file name to load\n");
return;
}
//open the source file
FILE* loadFil;
loadFil=fopen(argv[1],"r");
if (loadFil==0)
{
printf("File not found\n");
return;
}
//open the floppy image
FILE* floppy;
floppy=fopen("floppya.img","r+");
if (floppy==0)
{
printf("floppya.img not found\n");
return;
}
//load the disk map
char map[512];
fseek(floppy,512,SEEK_SET);
for(i=0; i<512; i++)
map[i]=fgetc(floppy);
//load the directory
char dir[512];
fseek(floppy,512*2,SEEK_SET);
for (i=0; i<512; i++)
dir[i]=fgetc(floppy);
//find a free entry in the directory
// Changed: Start to search from the second slot,
// since the first one stored the information about this directory
for (i=0x20; i<512; i=i+0x20)
if (dir[i]==0)
break;
if (i==512)
{
printf("Not enough room in directory\n");
return;
}
int dirindex=i;
//fill the name field with 00s first
for (i=0; i<6; i++)
dir[dirindex+i]=0x00;
//copy the name over
for (i=0; i<6; i++)
{
if(argv[1][i]==0)
break;
dir[dirindex+i]=argv[1][i];
}
dirindex=dirindex+6;
//find free sectors and add them to the file
int sectcount=1;
while(!feof(loadFil))
{
if (sectcount==25) // Changed: the last bit is used as info bit
{
printf("Not enough space in directory entry for file\n");
return;
}
//find a free map entry
for (i=0; i<256; i++)
if (map[i]==0)
break;
if (i==256)
{
printf("Not enough room for file\n");
return;
}
//mark the map entry as taken
map[i]=0xFF;
//mark the sector in the directory entry
dir[dirindex]=i;
dirindex++;
sectcount++;
//move to the sector and write to it
fseek(floppy,i*512,SEEK_SET);
for (i=0; i<512; i++)
{
if (feof(loadFil))
{
fputc(0x0, floppy);
break;
}
else
{
char c = fgetc(loadFil);
fputc(c, floppy);
}
}
}
//write the map and directory back to the floppy image
fseek(floppy,512,SEEK_SET);
for(i=0; i<512; i++)
fputc(map[i],floppy);
fseek(floppy,512*2,SEEK_SET);
for (i=0; i<512; i++)
fputc(dir[i],floppy);
fclose(floppy);
fclose(loadFil);
}