summaryrefslogtreecommitdiff
path: root/super_donators/grencez_tok5.c
diff options
context:
space:
mode:
Diffstat (limited to 'super_donators/grencez_tok5.c')
-rw-r--r--super_donators/grencez_tok5.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/super_donators/grencez_tok5.c b/super_donators/grencez_tok5.c
new file mode 100644
index 00000000..5c5be83c
--- /dev/null
+++ b/super_donators/grencez_tok5.c
@@ -0,0 +1,41 @@
1/* Though it may look bleak at times,
2 * this ring will stabilize to have one token,
3 * and Tox will be the one true chat protocol!
4 * -- Alex P. Klinkhamer (grencez)
5 */
6#include <unistd.h>
7#include <stdlib.h>
8#include <stdio.h>
9int main(int i, char** msg)
10{
11 int j, fd[4], xpd, xid;
12 if (--i<1) return 1;
13 srand(getpid());
14 pipe(fd);
15 while (xid=rand()%5, --i>0) {
16 pipe(&fd[2]);
17 j = (0==fork() ? 0 : 1);
18 close(fd[j]);
19 fd[j] = fd[j+2];
20 close(fd[3-j]);
21 if (j==0) break;
22 }
23#define SendSc() write(fd[1], &xid, sizeof(xid))
24#define RecvPd() read(fd[0], &xpd, sizeof(xpd))
25#define A(g,v) if (g) {xid=v; puts(msg[i+1]); fflush(stdout); SendSc();}
26 SendSc();
27 while (RecvPd(), 1) {
28 sleep(1);
29 if (i==0) {
30 A( xpd==0 && xid==0 , 1 );
31 A( xpd==1 && xid<=1 , 2 );
32 A( xpd> 1 && xid> 1 , 0 );
33 continue;
34 }
35 A( xpd==0 && xid> 1 , xid/4 );
36 A( xpd==1 && xid!=1 , 1 );
37 A( xpd==2 && xid<=1 , 2+xid );
38 A( xpd>=3 && xid<=1 , 4 );
39 }
40 return 0;
41}