-
Notifications
You must be signed in to change notification settings - Fork 0
/
readwrite.c
56 lines (45 loc) · 1.36 KB
/
readwrite.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
// Simple test program
#include <stdio.h>
#include <comp421/iolib.h>
#include <comp421/filesystem.h>
#include <comp421/yalnix.h>
#include <string.h>
int main(int argc, char **argv) {
(void)argc;
(void)argv;
int fd;
int i;
char buf[DIRNAMELEN];
char tmp[DIRNAMELEN];
struct Stat statbuf;
sprintf(tmp, ".");
for (i = 0; i < 10; i++) {
sprintf(buf, "%d", i);
printf("Creating file %d\n", i);
fd = Create(buf);
Stat(tmp, &statbuf);
if (fd == ERROR) {
printf("Something went wrong creating file %d\n", i);
return ERROR;
} else {
strncpy(buf, "Hello world", DIRNAMELEN);
printf("Writing to file %d\n", i);
if (Write(fd, buf, (int)strlen(buf)) != (int)strlen(buf)) {
printf("Unable to write to file %d\n", i);
return ERROR;
}
if (Seek(fd, 0, SEEK_SET)) {
printf("Unable to seek to the beginning of file %d\n", i);
return ERROR;
}
if (Read(fd, buf, (int)strlen(buf)) != (int)strlen(buf)) {
printf("Unable to read back from file %d\n", i);
return ERROR;
}
printf("Read '%s' from file %d\n", buf, i);
}
}
Shutdown();
printf("Wrote to all files\n");
return 0;
}