This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomb.c
280 lines (236 loc) · 7.14 KB
/
bomb.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include "ansi.h"
extern int wire_yellow;
extern int wire_green;
extern int wire_blue;
extern int wire_red;
extern void yellow();
extern void green();
extern void blue();
extern void red();
extern void postred();
char *display[3][11] =
{
{ " ___", " ", " ___", " ___", " ", " ___", " ___", " ___", " ___", " ___", " ___"},
{ " / /", " /", " ___/", " ___/", " /__/", " /__ ", " /__ ", " / /", " /__/", " /__/", " /__ "},
{ "/__/ ", " / ", "/__ ", "___/ ", " / ", "___/ ", "/__/ ", " / ", "/__/ ", "___/ ", "/ "},
};
/**
* Computes the time left until the end of class on March 15, 2010.
* Also seeds the random number generator to the time remaining.
*/
void gettimeleft(uint8_t *hr, uint8_t *min, uint8_t *sec)
{
// Compute the time left until the end of class
int32_t diff = 2268675400 - (int32_t)time(NULL);
if (diff < 0)
diff = 0;
// Seed the random number generator
srand(diff);
// Split the time left into constituent parts
if (hr && min && sec)
{
*sec = diff % 60; diff /= 60;
*min = diff % 60; diff /= 60;
*hr = diff % 24; diff /= 24;
}
#warning "DEBUG MODE"
srand(0);
}
void menu()
{
// Get the time left for the clock
uint8_t hr, min, sec;
gettimeleft(&hr, &min, &sec);
// First row
printf("----------------------------------------");
printf(AA(BF,YL,BB,YL) " " RST "--" AA(BF,GR,BB,GR) " " RST "--" AA(BF,CY,BB,CY) " " RST "--" AA(BF,RD,BB,RD) " " RST "---" "\n" );
// Second through fourth rows
int i;
for (i = 0; i < 4; i ++)
{
if (i < 3)
printf(A(BF,RD)" %s%s %s%s %s%s " RST,
display[i][ hr / 10],
display[i][ hr % 10],
display[i][min / 10],
display[i][min % 10],
display[i][sec / 10],
display[i][sec % 10]
);
else
printf(" HOURS MINUTES SECONDS ");
printf("%s%c" RST " ", (wire_yellow ? A(BB,YL) : A(BF,YL)), (wire_yellow ? ' ' : (i == 0 || i == 3) ? '*' : '.'));
printf("%s%c" RST " ", (wire_green ? A(BB,GR) : A(BF,GR)), (wire_green ? ' ' : (i == 0 || i == 3) ? '*' : '.'));
printf("%s%c" RST " ", (wire_blue ? A(BB,CY) : A(BF,CY)), (wire_blue ? ' ' : (i == 0 || i == 3) ? '*' : '.'));
printf("%s%c" RST " ", (wire_red ? A(BB,RD) : A(BF,RD)), (wire_red ? ' ' : (i == 0 || i == 3) ? '*' : '.'));
printf("\n");
}
// Last row
printf("+---------------------------------------");
printf(AA(BF,YL,BB,YL) " " RST "--" AA(BF,GR,BB,GR) " " RST "--" AA(BF,CY,BB,CY) " " RST "--" AA(BF,RD,BB,RD) " " RST "--+" "\n" );
// Menu
printf("| |\n");
printf("| "
A(BF,YL) "," RST
" DR. VON NOIZEMAN'S NUCLEAR BOMB "
A(BF,YL) "," RST
" |\n");
printf("| "
A(BF,YL) "/" A(DF,YL) "!" A(BF,YL) "\\" RST
" AUTHORIZED ACCESS ONLY "
A(DF,RD) "-" RST
" KEEP OUT "
A(BF,YL) "/" A(DF,YL) "!" A(BF,YL) "\\" RST
" |\n");
printf("| |\n");
if (wire_red || wire_yellow || wire_blue || wire_green)
{
printf("| "
"%s1%s YELLOW " RST
"%s2%s GREEN " RST
"%s3%s BLUE " RST
"%s4%s RED " RST
" |\n",
wire_yellow ? A(DF,GR) "[" A(BF,GR) : A(BF,BK) "[", wire_yellow ? A(DF,GR) "]" RST : "]",
wire_green ? A(DF,GR) "[" A(BF,GR) : A(BF,BK) "[", wire_green ? A(DF,GR) "]" RST : "]",
wire_blue ? A(DF,GR) "[" A(BF,GR) : A(BF,BK) "[", wire_blue ? A(DF,GR) "]" RST : "]",
wire_red ? A(DF,GR) "[" A(BF,GR) : A(BF,BK) "[", wire_red ? A(DF,GR) "]" RST : "]"
);
}
else
{
printf("| " A(DF,GR) "[" A(BF,GR) "DISARM" A(DF,GR) "]" RST " |\n");
}
printf("| |\n");
printf("+---------------------------------------------------+\n");
}
void *watchred(void *crap)
{
#warning "Don't forget to update the checksum!"
const uint32_t checksum = 0xDFFEB544;
// Continuously monitor it
while (1)
{
// Has anything blown up?
if (wire_red > 1 || wire_blue > 1 || wire_yellow > 1 || wire_green > 1)
{
goto __start;
}
uint32_t a = 1, b = 0;
uint32_t *ptr = (uint32_t *)&red;
while (ptr < (uint32_t *)postred)
{
a = (a + *ptr) % 65521;
b = (b + a) % 65521;
ptr ++;
}
uint32_t sum = (b << 16) | a;
if (sum != checksum)
{
#warning "FIXME"
printf("CHECKSUM 0x%08X\n", sum);
goto __start;
}
sleep(1);
}
return NULL;
__start:
putchar('\n');
#warning "TODO: write RST \"\n\n\" A(BB,BK) \" SOMEONE SET US UP THE BOMB \" RST \"\n\n\""
__asm__(""
"movl $0xDEADBEEF, %esp\n"
"movl $0xCAFEBABE, %ebp\n"
"xor %eax, %eax\n"
"call *%eax"
);
return NULL; // shut up GCC
}
void disarm_handler (int sig)
{
if (wire_blue || wire_green || wire_red || wire_yellow)
{
wire_blue = 2;
return;
}
else
{
// First row
printf("----------------------------------------");
printf(AA(BF,YL,BB,YL) " " RST "--" AA(BF,GR,BB,GR) " " RST "--" AA(BF,CY,BB,CY) " " RST "--" AA(BF,RD,BB,RD) " " RST "---" "\n" );
// Second through fourth rows
int i;
for (i = 0; i < 4; i ++)
{
if (i < 3)
printf(A(BF,RD)" %s%s %s%s %s%s " RST,
display[i][10],
display[i][10],
display[i][10],
display[i][10],
display[i][10],
display[i][10]
);
else
printf(" HOURS MINUTES SECONDS ");
printf("%s%c" RST " ", A(BF,YL), (i == 0 || i == 3) ? '*' : '.');
printf("%s%c" RST " ", A(BF,GR), (i == 0 || i == 3) ? '*' : '.');
printf("%s%c" RST " ", A(BF,CY), (i == 0 || i == 3) ? '*' : '.');
printf("%s%c" RST " ", A(BF,RD), (i == 0 || i == 3) ? '*' : '.');
printf("\n");
}
// Last row
printf("----------------------------------------");
printf(AA(BF,YL,BB,YL) " " RST "--" AA(BF,GR,BB,GR) " " RST "--" AA(BF,CY,BB,CY) " " RST "--" AA(BF,RD,BB,RD) " " RST "---" "\n" );
exit(0);
}
}
int main(int argc, char *argv[])
{
char buffer[20];
// Create the bomb thread
pthread_t watchthread;
pthread_create(&watchthread, NULL, &watchred, NULL);
// Register a signal handler
signal(SIGUSR1, &disarm_handler);
int showmenu = 1;
while (1)
{
if (showmenu)
menu();
printf(A(BB,BK) " " RST A(BF,BK) " MENU SELECTION: " RST);
fgets(buffer, 20, stdin);
if (!strcmp(buffer, "1\n") || !strcasecmp(buffer, "yellow\n"))
yellow();
else if (!strcmp(buffer, "2\n") || !strcasecmp(buffer, "green\n"))
green();
else if (!strcmp(buffer, "3\n") || !strcasecmp(buffer, "blue\n"))
blue();
else if (!strcmp(buffer, "4\n") || !strcasecmp(buffer, "red\n"))
red();
else if (!strcasecmp(buffer, "exit\n") || !strcasecmp(buffer, "quit\n"))
break;
else if (!strcasecmp(buffer, "disarm\n"))
raise(SIGUSR1);
else if (buffer[0] == '\n')
{
showmenu = 0;
continue;
}
else
wire_green = 2;
printf(A(BB,BK) " " RST A(BF,BK) " PRESS ENTER TO RETURN TO MENU " RST);
getchar();
showmenu = 1;
}
return 0;
}