-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockFake.c
More file actions
53 lines (39 loc) · 862 Bytes
/
Copy pathblockFake.c
File metadata and controls
53 lines (39 loc) · 862 Bytes
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
#include <stdio.h>
#include <assert.h>
#include "common.h"
#include "block.h"
static FILE *fd;
#include <errno.h>
void block_init(void) {
int ret;
fd = fopen("./disk", "r+");
if (fd == NULL) {
fd = fopen("./disk", "w+");
}
assert(fd);
ret = fseek(fd, 0, SEEK_SET);
assert(ret == 0);
}
void block_read(int block, char *mem) {
int ret;
ret = fseek(fd, block * BLOCK_SIZE, SEEK_SET);
assert(ret == 0);
ret = fread(mem, 1, BLOCK_SIZE, fd);
if (ret == 0) { /* End of file */
ret = BLOCK_SIZE;
bzero_block(mem);
}
assert(ret == BLOCK_SIZE);
}
void block_write(int block, char *mem) {
int ret;
ret = fseek(fd, block * BLOCK_SIZE, SEEK_SET);
assert(ret == 0);
ret = fwrite(mem, 1, BLOCK_SIZE, fd);
assert(ret == BLOCK_SIZE);
}
void bzero_block(char *block) {
int i;
for (i = 0; i < BLOCK_SIZE; i++)
block[i] = 0;
}