-
Notifications
You must be signed in to change notification settings - Fork 1
/
super_test.c
executable file
·131 lines (120 loc) · 3.72 KB
/
super_test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <comp421/yalnix.h>
#include <comp421/iolib.h>
void custom_test(char *str);
void generate_content(char *buf, int size);
int
main()
{
char *buf = (char *) malloc(1000);
size_t s = 1000;
while (1) {
memset(buf, 0, sizeof(buf));
getline(&buf, &s, stdin);
printf("Echo %s", buf);
buf[strcspn(buf, "\n")] = 0;
custom_test(buf);
printf("\n");
fflush(stdout);
}
free(buf);
exit(0);
}
void
custom_test(char *str) {
char *p = strtok(str, " ");
char *buf;
int code;
int fd;
int size;
if (p == NULL) {
printf("Please input some kernel call\n");
return;
} else if (strcmp(p, "create") == 0) {
p = strtok(NULL, " ");
code = Create(p);
printf("[CREATE] file %s, return fd %d\n", p, code);
} else if (strcmp(p, "open") == 0){
p = strtok(NULL, " ");
code = Open(p);
printf("[OPEN] file %s, return fd %d\n", p, code);
} else if (strcmp(p, "close") == 0) {
fd = atoi(strtok(NULL, " "));
code = Close(fd);
printf("[CLOSE] fd %d, status: %d\n", fd, code);
} else if (strcmp(p, "read") == 0) {
fd = atoi(strtok(NULL, " "));
size = atoi(strtok(NULL, " "));
buf = (char *) malloc(size);
code = Read(fd, buf, size);
printf("[READ] fd: %d, read content: %s, read size: %d\n", fd, buf, code);
} else if (strcmp(p, "write") == 0) {
fd = atoi(strtok(NULL, " "));
size = atoi(strtok(NULL, " "));
buf = (char *) malloc(size);
generate_content(buf, size);
code = Write(fd, buf, size);
printf("[WRITE] fd: %d, content: %s, wrote size: %d\n", fd, buf, code);
} else if (strcmp(p, "seek") == 0) {
fd = atoi(strtok(NULL, " "));
int offset = atoi(strtok(NULL, " "));
int whence = atoi(strtok(NULL, " "));
code = Seek(fd, offset, whence);
printf("[SEEK] fd: %d, offset: %d, whence: %d, return val: %d\n", fd, offset, whence, code);
} else if (strcmp(p, "mkdir") == 0) {
p = strtok(NULL, " ");
code = MkDir(p);
printf("[MKDIR] dir: %s, return fd: %d\n", p, code);
} else if (strcmp(p, "chdir") == 0) {
p = strtok(NULL, " ");
code = ChDir(p);
printf("[CHDIR] dir: %s, return fd: %d\n", p, code);
} else if (strcmp(p, "rmdir") == 0) {
p = strtok(NULL, " ");
code = RmDir(p);
printf("[RMDIR] dir: %s, return fd: %d\n", p, code);
} else if (strcmp(p, "unlink") == 0) {
p = strtok(NULL, " ");
code = Unlink(p);
printf("[UNLINK] dir: %s, return code: %d\n", p, code);
} else if (strcmp(p, "stat") == 0) {
p = strtok(NULL, " ");
struct Stat *st = (struct Stat *) malloc(sizeof(struct Stat));
code = Stat(p, st);
printf("[STAT] %s, inode: %d, filetype: %d, size: %d, num_link: %d, return code: %d\n", p, st->inum, st->type, st->size, st->nlink, code);
} else if (strcmp(p, "link") == 0) {
char *oldname = strtok(NULL, " ");
char *newname = strtok(NULL, " ");
code = Link(oldname, newname);
printf("[LINK] old:%s, new:%s, code: %d\n", oldname, newname, code);
} else if (strcmp(p, "readlink") == 0) {
p = strtok(NULL, " ");
size = atoi(strtok(NULL, " "));
buf = (char *) malloc(size);
code = ReadLink(p, buf, size);
printf("[READLINK] pathname:%s, len:%d, read_len: %d\n", p, size, code);
} else if (strcmp(p, "symlink") == 0) {
char *oldname = strtok(NULL, " ");
char *newname = strtok(NULL, " ");
code = SymLink(oldname, newname);
printf("[SYMLINK] old:%s, new:%s, code: %d\n", oldname, newname, code);
} else if (strcmp(p, "exit") == 0) {
printf("[EXIT]\n");
exit(0);
} else {
printf("Unable to recognize input kernel call. Please try again\n");
}
}
void
generate_content(char *buf, int size) {
int itr;
for (itr = 0; itr < size; itr++) {
buf[itr] = 'b';
}
}
/** test cases
filename: 1. null [PASS]2. longer than dirnamelen 3. starts with /, end with \0
*/