Correzioni rimaste indietro ed espansione funzioni del resolver.
[gapil.git] / listati / FortuneServer.c
1 char *fifoname = "/tmp/fortune.fifo";
2 int main(int argc, char *argv[])
3 {
4 /* Variables definition */
5     int i, n = 0;
6     char *fortunefilename = "/usr/share/games/fortunes/linux";
7     char **fortune;
8     char line[80];
9     int fifo_server, fifo_client;
10     int nread;
11     ...
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 */
18         if (errno!=EEXIST) {
19             perror("Cannot create well known fifo");
20             exit(1);
21         }
22     }
23     daemon(0, 0);
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");
28         exit(1);
29     }
30     if (open(fifoname, O_WRONLY) < 0) {                        
31         perror("Cannot open write only well known fifo");
32         exit(1);
33     }
34     /* Main body: loop over requests */
35     while (1) {
36         nread = read(fifo_server, line, 79); /* read request */
37         if (nread < 0) {
38             perror("Read Error");
39             exit(1);
40         }
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");
46             exit(1);
47         }
48         nread = write(fifo_client,           /* write phrase */
49                       fortune[n], strlen(fortune[n])+1);
50         close(fifo_client);                  /* close client fifo */
51     }
52 }