c00b3fedf5a80ca33d6de56dbb3652bd97b8132c
[gapil.git] / sources / FortuneServer.c
1 /* FortuneServer.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program fortuned 
22  * Fortune server
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: fortuned -h give all info
28  *
29  * $Id: FortuneServer.c,v 1.1 2002/08/18 10:34:10 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #include <sys/types.h>   /* predefined types */
36 #include <sys/stat.h>    /*  */
37 #include <unistd.h>      /* include unix standard library */
38 #include <stdio.h>       /* include standard I/O library */
39 #include <stdlib.h>      /* standard library */
40 #include <string.h>      /* ANSI C standard string */
41 #include <errno.h>       /* errorstring */
42 #include <fcntl.h>       /*  */
43
44 #include "macros.h"
45
46 /* Subroutines declaration */
47 void usage(void);
48 int FortuneParse(char *file, char **fortune, int n);
49
50 int main(int argc, char *argv[])
51 {
52 /* Variables definition */
53     int n = 0;
54     FILE *fortunefile;
55     char *fortunefilename = "/usr/share/misc/fortune/fortune.it";
56     char *fifoname = "/tmp/fortune.fifo";
57     char **fortune;
58     char line[80];
59     int fifo_server, fifo_client;
60     int nread;
61     /*
62      * Input section: decode parameters passed in the calling 
63      * Use getopt function
64      */
65     int i;
66     opterr = 0;  /* don't want writing to stderr */
67     while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
68         switch (i) {
69         /* 
70          * Handling options 
71          */ 
72         case 'h':  
73             printf("Wrong -h option use\n");
74             usage();
75             return(0);
76             break;
77         case 'f':
78             fortunefilename = optarg;
79             break;
80         case 'n':
81             n = strtol(optarg, NULL, 10);
82             fortune = (char **) calloc(sizeof(*fortune), n);
83             break;
84         case '?':   /* unrecognized options */
85             printf("Unrecognized options -%c\n",optopt);
86             usage();
87         default:    /* should not reached */
88             usage();
89         }
90     }
91     /* ***********************************************************
92      * 
93      *           Options processing completed
94      *
95      *                Main code beginning
96      * 
97      * ***********************************************************/
98     if (n==0) {                                   /* if no pool depth exit */
99         usage();                                       /* print usage info */
100     }
101     i = FortuneParse(fortunefilename, fortune, n);
102     for (n=0; n<i; n++) debug("%s", fortune[n]);
103     /* 
104      * Comunication section 
105      */
106     if (mkfifo(fifoname, 0622)) {
107         if (errno!=EEXIST) {
108             perror("Cannot create well known fifo");
109             exit(-1);
110         }
111     }
112     while (1) {
113         fifo_server = open(fifoname, O_RDONLY);
114         if (fifo_server < 0) {
115             perror("Cannot open well known fifo");
116             exit(-1);
117         }
118         nread = read(fifo_server, line, 79);
119         line[nread] = 0;
120         debug("%s %d\n", line,nread);
121         n = random() % i;
122         debug("fortune[%d]=%s\n", n, fortune[n]);
123         fifo_client = open(line, O_WRONLY);
124         nread = write(fifo_client, fortune[n], strlen(fortune[n])+1);
125         close(fifo_client);
126         close(fifo_server);
127     }
128 }
129 /*
130  * routine to print usage info and exit
131  */
132 void usage(void) {
133     printf("Elementary fortune server\n");
134     printf("Usage:\n");
135     printf("  fortuned [-h] [-f] -n XXX \n");
136     printf("  -h   print this help\n");
137     printf("  -f filename   set file for fortunes\n");
138     printf("  -n XXX        set pool depth\n");
139     exit(1);
140 }