1 char *fifoname = "/tmp/fortune.fifo";
2 int main(int argc, char *argv[])
4 /* Variables definition */
6 char *fortunefilename = "/usr/share/games/fortunes/linux";
9 int fifo_server, fifo_client;
12 if (n==0) usage(); /* if no pool depth exit printing usage info */
13 Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */
14 Signal(SIGINT, HandSIGTERM);
15 Signal(SIGQUIT, HandSIGTERM);
16 i = FortuneParse(fortunefilename, fortune, n); /* parse phrases */
17 if (mkfifo(fifoname, 0622)) { /* create well known fifo if does't exist */
19 perror("Cannot create well known fifo");
24 /* open fifo two times to avoid EOF */
25 fifo_server = open(fifoname, O_RDONLY);
26 if (fifo_server < 0) {
27 perror("Cannot open read only well known fifo");
30 if (open(fifoname, O_WRONLY) < 0) {
31 perror("Cannot open write only well known fifo");
34 /* Main body: loop over requests */
36 nread = read(fifo_server, line, 79); /* read request */
41 line[nread] = 0; /* terminate fifo name string */
42 n = random() % i; /* select random value */
43 fifo_client = open(line, O_WRONLY); /* open client fifo */
44 if (fifo_client < 0) {
45 perror("Cannot open");
48 nread = write(fifo_client, /* write phrase */
49 fortune[n], strlen(fortune[n])+1);
50 close(fifo_client); /* close client fifo */