-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimic.c
189 lines (174 loc) · 2.42 KB
/
mimic.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
#include <math.h>
struct chunk * list[0x30] = { 0 };
struct chunk
{
int size;
void* ptr;
};
int read_n(char* ptr, int size)
{
for (int i=0;i<size;i++)
{
if (read(0, ptr, 1) == -1)
{
puts("read error!");
exit(0);
}
if (*ptr == '\n')
{
*ptr = '\0';
break;
}
ptr++;
}
return 0;
}
int init()
{
setbuf(stdout, 0);
setbuf(stdin, 0);
setbuf(stderr, 0);
puts("It is funnier that man exceeds the speed of light than fish start living in the land.");
puts("It can be said that this is an final ultimatum from the god to the people who can fight.");
puts("");
sleep(1);
puts("Try to deceive the world");
puts("Lonely... observer");
return 0;
}
int main()
{
int cmd;
init();
while (1)
{
menu();
cmd = getint();
switch (cmd)
{
case 1:
add();
break;
case 2:
dele();
break;
case 3:
show();
break;
case 4:
edit();
break;
default:
break;
}
}
}
int getint()
{
int num;
if(scanf("%d",&num) != 1)
num = 0;
while(getchar()!='\n');
return num;
}
int add()
{
int i = 0;
int sz = 0;
puts("index?");
puts(">>");
i = getint();
if (list[i] != 0 || i >= 0x30 || i < 0)
{
puts("error!");
return 0;
}
puts("size?");
puts(">>");
sz = getint();
if (sz > 0 && sz <= 0x400)
{
struct chunk *note = malloc(sizeof(struct chunk));
note->size = sz;
note->ptr = malloc(sz);
list[i] = note;
puts("content:");
read_n(note->ptr,note->size);
puts("done");
}
else
{
puts("too big!");
}
return 0;
}
int dele()
{
int idx;
puts("index?");
puts(">>");
idx = getint();
if (list[idx] == 0)
{
puts("cant find");
return 0;
}
free(list[idx]->ptr);
// free(list[idx]);
puts("delete successly");
return 0;
}
int show()
{
int idx;
puts("index?");
puts(">>");
idx = getint();
if (idx >= 0x30)
{
puts("out of range");
return 0;
}
if (list[idx] == 0)
{
puts("cant find");
return 0;
}
printf("content:");
write(1,list[idx]->ptr,list[idx]->size);
return 0;
}
int edit()
{
int idx;
puts("index?");
puts(">>");
idx = getint();
if (idx >= 0x30)
{
puts("out of range");
return 0;
}
if (list[idx] == 0)
{
puts("cant find");
return 0;
}
printf("content:");
read_n(list[idx]->ptr, list[idx]->size);
puts("done!");
return 0;
}
int menu()
{
puts("1.add");
puts("2.dele");
puts("3.view");
puts("4.edit");
puts(">>");
return 0;
}