-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_fat.c
93 lines (75 loc) · 1.16 KB
/
fs_fat.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
#include "oscafs.h"
#define FREE_BLK 0
//#define EOF_BLK -1
// Error Values
#define NO_FREE_BLK -2
#define NOT_EOF -3
int fat[NB_BLOCKS];
void init_fat()
{
for (int i = 0; i < NB_BLOCKS; i++)
fat[i] = FREE_BLK;
}
int *get_fat()
{
return fat;
}
int eof_block(int i)
{
return fat[i] == EOF_BLK;
}
int next_block(int i)
{
return fat[i];
}
int first_free_block()
{
for (int i = 1; i < NB_BLOCKS; i++)
{
if (fat[i] == FREE_BLK)
{
return i;
}
}
return NO_FREE_BLK;
}
int alloc_block()
{
int i;
if ((i = first_free_block()) > 0)
{
fat[i] = EOF_BLK;
return i;
}
return NO_FREE_BLK;
}
int add_next_block(int b)
{
int i;
if (fat[b] != EOF_BLK)
return NOT_EOF;
if ((i = first_free_block()) > 0)
{
fat[b] = i;
fat[i] = EOF_BLK;
return i;
}
return NO_FREE_BLK;
}
int count_free_block(void)
{
int i;
int count = 0;
for (i = 1; i < NB_BLOCKS; i++)
{
if (fat[i] == FREE_BLK)
{
count++;
}
}
return count;
}
void init_single_fat(int i)
{
fat[i] = FREE_BLK;
}