-
Notifications
You must be signed in to change notification settings - Fork 1
/
snakeqr.c
410 lines (329 loc) · 7.01 KB
/
snakeqr.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (c) 2020 Brian Callahan <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* snakeqr -- game of snake that fits in a QR code
*/
extern void *_syscall(void *n, void *a, void *b, void *c, void *d, void *e);
struct timeval {
long long tv_sec;
long tv_usec;
};
struct itimerval {
struct timeval it_interval;
struct timeval it_value;
};
union sigval {
int sival_int;
void *sival_ptr;
};
typedef struct {
int si_signo;
int si_code;
int si_errno;
union {
int _pad[29];
struct {
int _pid;
union {
struct {
unsigned int _uid;
union sigval _value;
} _kill;
struct {
long long _utime;
long long _stime;
int _status;
} _cld;
} _pdata;
} _proc;
struct {
void *_addr;
int _trapno;
} _fault;
} _data;
} siginfo_t;
struct sigaction {
union {
void (*__sa_handler)(int);
void (*__sa_sigaction)(int, siginfo_t *, void *);
} __sigaction_u;
unsigned int sa_mask;
int sa_flags;
};
static char direction;
static short location[23][79];
static char fruitx, fruity;
static char snakex, snakey;
static short snakelen;
static void
_exit(int status)
{
_syscall((void *) 1, (void *) status, (void *) 0, (void *) 0, (void *) 0, (void *) 0);
}
static long
read(int d, void *buf, unsigned long nbytes)
{
return (long) _syscall((void *) 3, (void *) d, (void *) buf, (void *) nbytes, (void *) 0, (void *) 0);
}
static void
write(int d, const void *buf, unsigned long nbytes)
{
_syscall((void *) 4, (void *) d, (void *) buf, (void *) nbytes, (void *) 0, (void *) 0);
}
static void
getentropy(void *buf, unsigned long buflen)
{
_syscall((void *) 7, (void *) buf, (void *) buflen, (void *) 0, (void *) 0, (void *) 0);
}
static long
wait4(int wpid, int *status, int options, void *rusage)
{
return (long) _syscall((void *) 11, (void *) wpid, (void *) status, (void *) options, (void *) rusage, (void *) 0);
}
static void
sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
{
_syscall((void *) 46, (void *) sig, (void *) act, (void *) oact, (void *) 0, (void *) 0);
}
static void
execve(const char *path, char *const argv[], char *const envp[])
{
_syscall((void *) 59, (void *) path, (void *) argv, (void *) envp, (void *) 0, (void *) 0);
}
static long
vfork(void)
{
return (long) _syscall((void *) 66, (void *) 0, (void *) 0, (void *) 0, (void *) 0, (void *) 0);
}
static void
setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
{
_syscall((void *) 69, (void *) which, (void *) value, (void *) ovalue, (void *) 0, (void *) 0);
}
static void
system(const char *command)
{
char *argp[4] = { "/bin/sh", "-c", (void *) 0, (void *) 0 };
int cpid, pid, pstat;
argp[2] = (char *) command;
switch ((cpid = vfork())) {
case -1:
_exit(1);
case 0:
execve("/bin/sh", argp, (void *) 0);
_exit(127);
}
do {
pid = wait4(cpid, &pstat, 0, (void *) 0);
} while (pid == -1);
}
static void
timer(void (*cb)(int))
{
struct sigaction action;
action.sa_mask = 0;
action.sa_flags = 0;
action.__sigaction_u.__sa_handler = cb;
sigaction(14, &action, (void *) 0);
}
static void
fruitplace(void)
{
char buf[2], xs[2], ys[2];
int i, n;
again:
getentropy(buf, 2);
fruitx = buf[0];
fruity = buf[1];
if ((fruitx < 2 || fruitx > 78) || (fruity < 2 || fruity > 22) || location[fruity][fruitx] != 0)
goto again;
++snakelen;
i = 0;
n = fruitx;
do {
xs[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
i = 0;
n = fruity;
do {
ys[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
write(1, "\033[", 2);
if (fruity > 9)
write(1, &ys[1], 1);
write(1, &ys[0], 1);
write(1, ";", 1);
if (fruitx > 9)
write(1, &xs[1], 1);
write(1, &xs[0], 1);
write(1, "H%", 2);
}
static void
checkcrash(void)
{
char score[4];
int i, n;
if ((snakex < 2 || snakex > 78) || (snakey < 2 || snakey > 22) || (location[snakey][snakex] > 0 && location[snakey][snakex] < snakelen)) {
system("stty sane");
write(1, "\033[?25h", 6);
write(1, "\033[2J\033[H", 7);
i = 0;
--snakelen;
n = --snakelen;
do {
score[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
write(1, "Score: ", 7);
if (snakelen > 999)
write(1, &score[3], 1);
if (snakelen > 99)
write(1, &score[2], 1);
if (snakelen > 9)
write(1, &score[1], 1);
write(1, &score[0], 1);
write(1, "\n", 1);
_exit(0);
}
}
static void
drawscr(void)
{
char xs[2], ys[2];
int i, n, x, y;
for (y = 1; y < 23; y++) {
for (x = 1; x < 79; x++) {
if (location[y][x] > 0) {
i = 0;
n = x;
do {
xs[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
i = 0;
n = y;
do {
ys[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
write(1, "\033[", 2);
if (y > 9)
write(1, &ys[1], 1);
write(1, &ys[0], 1);
write(1, ";", 1);
if (x > 9)
write(1, &xs[1], 1);
write(1, &xs[0], 1);
if (--(location[y][x]) == 0)
write(1, "H ", 2);
else
write(1, "H@", 2);
}
}
}
}
static void
move(void)
{
switch (direction) {
case 0:
--snakey;
break;
case 1:
++snakey;
break;
case 2:
--snakex;
break;
case 3:
++snakex;
}
checkcrash();
if (snakex == fruitx && snakey == fruity)
fruitplace();
location[snakey][snakex] = snakelen;
drawscr();
}
static void
do_move(int signo)
{
static struct itimerval value;
if (signo == 0)
timer(do_move);
value.it_value.tv_sec = 0;
value.it_value.tv_usec = 100000;
move();
setitimer(0, &value, (void *) 0);
}
static void
snakeinit(void)
{
char buf[2];
again:
getentropy(buf, 2);
snakex = buf[0];
snakey = buf[1];
if ((snakex < 8 || snakex > 72) || (snakey < 8 || snakey > 16))
goto again;
location[snakey][snakex] = snakelen;
direction = 3;
}
static void
scrinit(void)
{
int x, y;
write(1, "\033[?25l", 6);
write(1, "\033[2J\033[H", 7);
write(1, " ", 1);
for (x = 1; x < 79; x++)
write(1, "-", 1);
write(1, "\n", 1);
for (y = 1; y < 22; y++) {
write(1, "|", 1);
for (x = 0; x < 78; x++)
write(1, " ", 1);
write(1, "|\n", 2);
}
write(1, " ", 1);
for (x = 1; x < 79; x++)
write(1, "-", 1);
}
void
main(void)
{
int c;
++snakelen;
scrinit();
system("stty cbreak -echo");
snakeinit();
fruitplace();
drawscr();
do_move(0);
while (1) {
c = 0;
read(0, &c, 1);
switch (c) {
case 'h':
direction = 2;
break;
case 'j':
direction = 1;
break;
case 'k':
direction = 0;
break;
case 'l':
direction = 3;
}
}
}