-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfat.c
executable file
·78 lines (58 loc) · 2.53 KB
/
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
/******************************************************************************
* main: Sample for starting the FAT project.
*
* Authors: Andy Kinley, Archana Chidanandan, David Mutchler and others.
* March, 2004.
*****************************************************************************/
#include <stdio.h>
// 13 is NOT the correct number -- you fix it!
#define BYTES_TO_READ_IN_BOOT_SECTOR 12
/******************************************************************************
* You must set these global variables:
* FILE_SYSTEM_ID -- the file id for the file system (here, the floppy disk
* filesystem)
* BYTES_PER_SECTOR -- the number of bytes in each sector of the filesystem
*
* You may use these support functions (defined in FatSupport.c)
* read_sector
* write_sector
* get_fat_entry
* set_fat_entry
*****************************************************************************/
FILE* FILE_SYSTEM_ID;
int BYTES_PER_SECTOR;
extern int read_sector(int sector_number, char* buffer);
extern int write_sector(int sector_number, char* buffer);
extern int get_fat_entry(int fat_entry_number, char* fat);
extern void set_fat_entry(int fat_entry_number, int value, char* fat);
/******************************************************************************
* main: an example of reading an item in the boot sector
*****************************************************************************/
int main()
{
unsigned char* boot; // example buffer
int mostSignificantBits;
int leastSignificantBits;
int bytesPerSector;
// You must set two global variables for the disk access functions:
// FILE_SYSTEM_ID BYTES_PER_SECTOR
// Use this for an image of a floppy drive
FILE_SYSTEM_ID = fopen("floppy1", "r+");
if (FILE_SYSTEM_ID == NULL)
{
printf("Could not open the floppy drive or image.\n");
exit(1);
}
// Set it to this only to read the boot sector
BYTES_PER_SECTOR = BYTES_TO_READ_IN_BOOT_SECTOR;
// Then reset it per the value in the boot sector
boot = (unsigned char*) malloc(BYTES_PER_SECTOR * sizeof(unsigned char));
if (read_sector(0, boot) == -1)
printf("Something has gone wrong -- could not read the boot sector\n");
// 12 (not 11) because little endian
mostSignificantBits = ( ( (int) boot[12] ) << 8 ) & 0x0000ff00;
leastSignificantBits = ( (int) boot[11] ) & 0x000000ff;
bytesPerSector = mostSignificantBits | leastSignificantBits;
printf("%d\n", bytesPerSector);
return 0;
}