forked from emilytouchingcomputers/CTFium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·176 lines (169 loc) · 2.89 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
char *notes[36];
int count = 0;
char **note_to_write = notes;
void *malloc_func(void *arg)
{
note_to_write ++;
int tmp = count;
char *to_copy = (char *)arg;
tmp += 1;
//usleep(100000);
count = tmp;
if(count > 34)
{
printf("too many notes!!\n");
note_to_write --;
return 0;
}
else
{
printf("logged successfully!\n");
*note_to_write = malloc(256 * sizeof(char));
memset(*note_to_write, 0, 256);
memcpy(*note_to_write, to_copy, 250);
return 0;
}
}
void *delete_func(void *arg)
{
char **note_to_delete = note_to_write;
note_to_write --;
int tmp = count;
tmp -= 1;
usleep(2000000);
if(count > 0)
{
free(*note_to_delete);
count = tmp;
printf("delete successfully!\n");
}
else
{
printf("too less notes!!\n");
note_to_write ++;
return 0;
}
return 0;
}
int make_note()
{
int cnt = 250;
char inputs[256];
char *ptr = inputs;
memset(inputs, 0, 256);
printf("input your note, no more than 250 characters\n");
while(cnt)
{
char tmp;
tmp = getchar();
if(tmp != '\n' && tmp!= '\x00' && tmp != '\x90' && tmp )
{
*ptr = tmp;
*ptr ++;
}
else
{
ptr = NULL;
break;
}
cnt --;
}
pthread_t thread_tmp;
pthread_create(&thread_tmp, NULL, malloc_func, (void*)inputs );
return 0;
}
int get_total()
{
printf("the total of notes is %ld\n",count);
//printf("real : %ld\n",note_to_write - notes);
}
int delete_note()
{
printf("the last one will be deleted!\n");
pthread_t thread_tmp;
pthread_create(&thread_tmp, NULL, delete_func, NULL);
return 0;
}
int welcome()
{
char choice = '3';
printf("welcome to flappypig's note system!\n");
printf("enter 0 to make a new note\n");
printf("enter 1 to get the total of notes\n");
printf("enter 2 to delete a note\n");
printf("enter 3 to exit\n");
printf("choice:\n");
while(1)
{
while(1)
{
char tmp = getchar();
if(tmp == '\n')
{
break;
}
else if(tmp == EOF)
{
return 0;
}
else
{
choice = tmp;
}
}
switch (choice)
{
case '0':
make_note();
break;
case '1':
get_total();
break;
case '2':
delete_note();
break;
case '3':
return 0;
break;
default :
printf("please enter the right choice!\n");
break;
}
printf("choice:\n");
}
}
int main()
{
memset(¬es,0,36 * sizeof(char *));
welcome();
return 0;
}
/*
int main()
{
srand(time(0));
pthread_t id;
printf("Main thread id is %ld \n",pthread_self());
while(1)
{
if(!pthread_create(&id,NULL,(void *)thread,NULL))
{
printf("succeed!\n");
//return 0;
}
else
{
printf("Fail to Create Thread");
//return -1;
}
usleep((rand()%5000));
}
return 0;
}
*/