forked from xosnitc/xsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.c
55 lines (47 loc) · 1.2 KB
/
disk.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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "disk.h"
#include "data.h"
// void print(PAGE str, int pageNumber)
// {
// int i;
// char c;
// printf("$$$$$$$$$$$$$$$$$$$$$$$$ %d $$$$$$$$$$$$$$$$$$$$$$$" , pageNumber);
// scanf("%c", &c);
// for(i=0; i<256; i++)
// printf("--readFromDisk--- %s ---------\n", page[pageNumber].word[i]);
// printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
// // scanf("%c", &c);
// }
/*
This function reads an entire page from address specified by blockNumber to pageNumber.
*/
int readFromDisk(int pageNumber, int blockNumber) {
int fd;
fd = open(DISK_NAME, O_RDONLY, 0666);
if(fd < 0)
{
printf("Unable to Open Disk File\n");
return -1;
}
lseek(fd,sizeof (PAGE)*blockNumber,SEEK_SET);
read(fd,&page[pageNumber],sizeof (PAGE));
// print(page[pageNumber], pageNumber);
close(fd);
}
/*
This function writes an entire page to blocknumber from pageNumber.
*/
int writeToDisk(int pageNumber, int blockNumber) {
int fd;
fd = open(DISK_NAME, O_WRONLY, 0777);
if(fd < 0)
{
printf("Unable to Open Disk File\n");
return -1;
}
lseek(fd,sizeof (PAGE)*blockNumber,SEEK_SET);
write(fd,&page[pageNumber],sizeof (PAGE));
close(fd);
}