C语言代写

159.102 Computer Science Fundamentals                                Assignment 4

 

You have been asked to write a C program that tests a simulated FAT (File Allocation Table).

Although this program may not be very big, you must use several source files, and a makefile.

 

There is a maximum of 30 sectors that can be used by the file system, sectors 0 and 1 are special and are used for other purposes. In total there are 32 entries in the FAT.

 

The FAT is stored in memory as int fat[32], and is stored on disk in the file fat.dat.

The numbers form a chain of sectors, with the last link in each chain being 0. A number of 1 means that the sector is “free”.

The following functions are to be written in the source file “fat.c”

void read_fat(void);                              read the file into memory 32 numbers, one per line. First two numbers are 0.

void save_fat(void);                              write the current memory contents back to disk, overwriting the previous file.

int  free_sectors(void);                               return the total number of sectors that are currently free.

int  first_free_sector(void);                 return the lowest numbered sector that is currently free.

void allocate_sectors(int start_sector, int n);          form a chain n sectors long beginning at start_sector.

void deallocate_sectors(int start_sector);             make free the chain starting at start_sector.

void print_sector_chain(int start_sector);             print the chain of sectors starting at start_sector

 

Information about the current files is stored in the directory. There can be up to 30 entries in the directory, which consists of a filename (12 chars or less, no spaces) and an initial sector. In memory it is stored as an array of strings and an array of ints.

On disk the directory is stored in the file dir.dat one entry per line, format: filename start_sector.

The following functions are to be written in the source file “dir.c”

void read_dir(void);                              read the directory information into memory

void save_dir(void);                              save the current directory information overwriting the previous file

void print_dir(void);                            print the current directory, one file per line format: filename start_sector

int  does_file_exist_in_dir(char *file);         return –1 if the file does not exist, else its position in the directory.

void add_file_to_dir(char *file, int start_sector);    add file to memory image of directory

void delete_file_from_dir(int position);                  delete file from memory image of directory

 

Your main program in file “assign4.c” repeatedly accepts commands from the user.

The prompt is a single ‘:’. The commands can be a mixture of:

 

:sectors filename

:delete filename

:add filename number

:dir

:quit

 

sectors filename, prints a list of each sector number used by the file filename in the correct order. Either sector numbers are printed with a single space between each on one line, or an error message “File does not exist” is printed.

 

delete filename, removes the entry from the directory and sets to 1 the FAT entry of each sector associated with this file. The only error message for this command is “File does not exist“. The altered directory and FAT are saved to disk.

 

add filename number, adds the file to the directory and allocates number sectors to it.

The sector numbers are chosen by: start at FAT entry 2, find the first free sector, use this as the initial sector of the file.

Each subsequent sector required by the file is found by continuing to find free sectors sequentially from the first.

The only error message for this command is one of: “File already exists“, “Directory is full“, and “Not enough free sectors“. Check for a full directory before checking for free sectors. The altered directory and FAT are saved to disk.

 

dir, lists each file in the directory one entry per line format: filename start_sector.

 

quit, ends the program.

 

There must be no other output apart from that detailed above.

 

You have to create all the files, 3 C files (assign4.c, fat.c, dir.c), 1 header file (assign4.h),

2 data files (fat.dat, dir.dat), and 1 makefile (makefile) to control the compilation .

You will only hand in the 3 C files, the header file and the makefile.

 

As you can only hand in 1 file electronically, before submission, you must combine the five files into one single file, assign4.txt. The format of this text file must be:

assign4.c

…..your assign4.c file…..

fat.c

…..your fat.c file…..

dir.c

…..your dir.c file…..

assign4.h

…..your header file…..

makefile

…..your makefile…..

 

I will extract your files before marking.                 Due date: 5.00pm Mon 8th October, worth 4%.