Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 64 lines (52 sloc) 1.309 kb
46260ac Jeremy White Implement an initial pass at a Tetris game.
authored
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <pthread.h>
6 #include <sys/stat.h>
7 #include <sys/fcntl.h>
8 #include <errno.h>
9 #include "fifo.h"
10
11 static void fifo_thread(fifo_t *f)
12 {
13 char buf[MAX_COMMAND_LEN];
14 FILE *fp;
15
16 while (1)
17 {
18 fp = fopen(f->pathname, "r");
19 if (! fp)
20 break;
21
22 while (! feof(fp))
23 {
24 buf[0] = '\0';
25 if (fgets(buf, sizeof(buf), fp) == NULL)
26 break;
27
28 if (strlen(buf) > 0)
29 {
30 if (buf[strlen(buf) - 1] == '\n')
31 buf[strlen(buf) - 1] = '\0';
32 }
33
34 (*f->callback)(f->first_parm, buf);
35 }
36 }
37 }
38
39 int fifo_init(const char *pathname, fifo_callback_t callback, fifo_t *f, void * first_parm)
40 {
41 pthread_attr_t attr;
42 int rc;
43
44 rc = mkfifo(pathname, S_IRWXU | S_IRWXG | S_IRWXO);
45 if (rc == -1 && errno != EEXIST)
46 return -1;
47
48 if (pthread_attr_init(&attr) != 0 ||
49 pthread_create(&f->t, &attr, (void * (*)(void *))fifo_thread, f) != 0)
50 return -1;
51
52 f->pathname = strdup(pathname);
53 f->callback = callback;
54 f->first_parm = first_parm;
55
56 return 0;
57 }
58
59 void fifo_term(fifo_t *f)
60 {
61 pthread_cancel(f->t);
62 free(f->pathname);
63 }
Something went wrong with that request. Please try again.