-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.c
94 lines (69 loc) · 1.8 KB
/
sample.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
94
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "myfs.h"
#define SAMPLEDISK "sampledisk"
#define SAMPLEFILE "samplefile"
#define EXPECTEDFILE "expected"
#define SAMPLETEXT "What a cool project.\nCS 342 rocks 8)\n"
/* Error Handling */
int Creat(const char *path)
{
int fd;
if ((fd = creat(path, 0664)) == -1) {
perror("Cannot create file");
exit(1);
}
return fd;
}
ssize_t Write(int fd, const void* buf, size_t count)
{
ssize_t result;
if ((result = write(fd, buf, count)) != count) {
perror("Error in write.");
exit(1);
}
return result;
}
int Close(int fd)
{
int result;
if ((result = close(fd)) != 0) {
perror("Cannot close file");
exit(1);
}
return result;
}
/* Main */
int main(int argc, char *argv[])
{
char buf[MAXREADWRITE+1];
int fd, n;
/* Create a regular expected output file */
fd = Creat(EXPECTEDFILE);
Write(fd, SAMPLETEXT, strlen(SAMPLETEXT));
Close(fd);
/* Error handling is omitted for simplicity */
myfs_diskcreate(SAMPLEDISK);
myfs_makefs(SAMPLEDISK);
myfs_mount(SAMPLEDISK);
fd = myfs_create(SAMPLEFILE);
myfs_write(fd, SAMPLETEXT, strlen(SAMPLETEXT));
myfs_close(fd);
/* As an exercise, read the file from another program */
fd = myfs_open(SAMPLEFILE);
n = myfs_read(fd, buf, MAXREADWRITE);
myfs_close(fd);
myfs_umount();
buf[n] = '\0'; /* Null terminate string */
//printf("ddd%d\n", sizeof(int));
fprintf(stderr, "%s", buf); /* Print to stderr to distinguish from debug messages of myfs */
/* stderr output should now be the same as the contents of expected output file */
/*
Contents of buf could have been compared directly to SAMPLETEXT.
Writing to a file and stderr is done for demonstration purposes.
*/
return 0;
}