· 7 years ago · Dec 08, 2018, 04:44 AM
1int findFirstFreeCluster(){
2 int i;
3 int totClus = totalClusCount(&myBPB);
4 for(i = 0; i < totClus; i++){
5 if(((getFatEntry(&myBPB, i) & FAT_BYTE_MASK) | 0x00000000) == 0x00000000)
6 return i;
7 }
8 return i;
9}
10
11int creat(char **args)
12{
13 char *FILENAME = args[1]; // Makes name = FILENAME
14
15 int new_file_cluster;
16
17 char file_name[11];
18
19 int lastChar;
20
21 int offset;
22
23 struct DIR_ENTRY empty_ENTRY, DirEntry;
24
25 //copy just the name and how many words it has before the .
26 for(int i = 0; i < 8; i++){
27 if(FILENAME[i] != '.' && FILENAME[i] != '\0')
28 file_name[i] = FILENAME[i];
29 else{
30 lastChar = i;
31 }
32
33 }
34
35 //copy file extension
36 for(int i = 0; i < 2; i++){
37 int ii = lastChar+1;
38
39 file_name[8+i] = FILENAME[ii];
40 ii++;
41 }
42
43 //Fill with white space
44 for(int i = lastChar; i< 8; i++){
45 file_name[i] = ' ';
46 }
47 //Fill last space with end of string
48 file_name[11] = '\0';
49
50 offset = findFirstFreeCluster();
51
52 for(int i = 0; i < 11; i++){
53 empty_ENTRY.fname[i] = file_name[i];
54 }
55 empty_ENTRY.fsize = 0;
56
57 //fwrite
58
59
60 // DO I ADD FSI INFO STUFF?! IDK
61
62
63 // how do I stay within the same directory when looking for other file names
64
65
66 //How the am I going to loop through the FAT table
67 /*Find 0x0
68 if found{
69 set new_file_cluster
70 flag = 1
71 new_file_cluster = End of cluster marker
72 */
73
74/*
75 if((FILENAME != 0x00) && (FILENAME != 0xE5)){
76 return 0; // It is not empty
77 }
78 else{
79 // extension of a file is always the last three bytes so check up to 8 bytes.
80 for(int i = 0; i < 8; i++){
81
82 if(FILENAME[i] != }
83 }
84
85 if (FSI_Nxt_Free=0xFFFFFFFF){
86
87 }
88 else{
89
90 }
91 */
92
93
94 // Maybe use FSI_Nxt_Free to check next free cluster
95
96
97/*
98 cd(dir you want to creat FILENAME) what does master mean by this?
99
100 if (FILENAME DNE in dir){
101 dirEntryPt = create new directory entry (to direct users to new file location) //?
102 Initialize fields in FATspec (page 23)
103 DIR_Size = 0;
104 }
105 else
106 return error; //file already exists
107 */
108
109 return 1;
110}
111
112
113int mkdir(char **args)
114{
115 char *FILENAME = args[1]; // Makes name = FILENAME
116 int new_file_cluster;
117 char file_name[11];
118 int lastChar;
119 int offset;
120
121 struct DIR_ENTRY empty_ENTRY, DirEntry;
122
123 //copy just the name and how many words it has before the .
124 for(int i = 0; i < 8; i++){
125 if(FILENAME[i] != '.' && FILENAME[i] != '\0')
126 file_name[i] = FILENAME[i];
127 else{
128 lastChar = i;
129 }
130 }
131
132 //copy file extension
133 for(int i = 0; i < 2; i++){
134 int ii = lastChar+1;
135
136 file_name[8+i] = FILENAME[ii];
137 ii++;
138 }
139
140 //Fill with white space
141 for(int i = lastChar; i< 8; i++){
142 file_name[i] = ' ';
143 }
144 //Fill last space with end of string
145 file_name[11] = '\0';
146
147 offset = findFirstFreeCluster();
148
149 for(int i = 0; i < 11; i++){
150 empty_ENTRY.fname[i] = file_name[i];
151 }
152 empty_ENTRY.fsize = 0;
153
154//Write
155
156 //printf("TEST5\n");
157 //PSEUDOCODE
158 /*
159 int flag
160 int new_dir_cluster
161
162 LOOP (FatTable){
163 Find 0x0
164 if found
165 set new_dir_cluster
166 flag = 1
167 new_dir_cluster = End of cluster marker
168 }
169
170 If flag == 0
171 return error;
172
173 if (FILENAME DNE in dir){
174 new_dir_cluster = create new directory entry (to direct users to new file location) //?
175 Initialize fields in FATspec (page 23)
176 DIR_Attr = 0x10
177
178 //For example, set DIR_ClusHI to the top bits of new_dir_cluster,
179 //and DIR_ClusLO set to the bottom bits of new_dir_cluster
180
181 }
182
183 Write “.†and “..†directories
184
185 .
186 DIR_Cluster = new_dir_cluster
187
188 ..
189 DIR_Cluster = parent directory’s cluster number
190 */
191
192 return 1;
193}