-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw4_mm_test.c
63 lines (61 loc) · 1.37 KB
/
hw4_mm_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
#include "hw4_mm_test.h"
int main()
{
char input[20];
while (!feof(stdin)) {
if (fgets(input, 20, stdin) != NULL) {
if (input[0] == 'a' &&
input[1] == 'l' &&
input[2] == 'l' &&
input[3] == 'o' &&
input[4] == 'c'
) {
size_t need = atoll(get_argv(input));
void *ptr = hw_malloc(need);
if (ptr != NULL) {
printf("0x%08" PRIxPTR "\n", (uintptr_t)ptr);
} else {
printf("%p\n", ptr);
}
} else if (input[0] == 'f' &&
input[1] == 'r' &&
input[2] == 'e' &&
input[3] == 'e'
) {
void *mem = (void *)(uintptr_t)strtol(get_argv(input), NULL, 16);
printf("%s\n", hw_free(mem) == 1 ? "success" : "fail");
} else if (input[0] == 'p' &&
input[1] == 'r' &&
input[2] == 'i' &&
input[3] == 'n' &&
input[4] == 't'
) {
int i = input[10] - '0';
if (i < 0 || i > 6) {
printf("COMMAND ERROR\n");
continue;
}
show_bin(i);
}
}
}
return 0;
}
char *get_argv(const char *command)
{
char delim[] = " ";
char *s = (char *)strndup(command, 20);
char *pos;
if ((pos = strchr(s, '\n')) != NULL)
* pos = '\0';
// split
char *token;
int argc = 0;
for (token = strsep(&s, delim); token != NULL; token = strsep(&s, delim)) {
if (argc == 1) {
return token;
}
argc++;
}
return "";
}