Un po' di materiale su {{{splice}}} e inizio della ripulitura degli
[gapil.git] / sources / FortuneClient.c
1 /* FortuneClient.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 fortune 
22  * Fortune client
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: fortune -h give all info
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #include <sys/types.h>   /* primitive system data types */
34 #include <unistd.h>      /* unix standard library */
35 #include <arpa/inet.h>   /* IP addresses conversion utiliites */
36 #include <stdio.h>       /* standard I/O library */
37 #include <errno.h>>      /* error definitions and routines */
38 #include <fcntl.h>       /* file control functions */
39
40 #include "macros.h"
41
42 /* Subroutines declaration */
43 void usage(void);
44
45 int main(int argc, char *argv[])
46 {
47 /* Variables definition */
48     int n = 0;
49     char *fortunefilename = "/tmp/fortune.fifo";
50     char line[80];
51     int fifo_server, fifo_client;
52     char fifoname[80];
53     int nread;
54     char buffer[PIPE_BUF];
55     /*
56      * Input section: decode parameters passed in the calling 
57      * Use getopt function
58      */
59     int i;
60     opterr = 0;  /* don't want writing to stderr */
61     while ( (i = getopt(argc, argv, "hf:")) != -1) {
62         switch (i) {
63         /* 
64          * Handling options 
65          */ 
66         case 'h':  
67             printf("Wrong -h option use\n");
68             usage();
69             return(0);
70             break;
71         case 'f':
72             fortunefilename = optarg;
73             break;
74         case '?':   /* unrecognized options */
75             printf("Unrecognized options -%c\n",optopt);
76             usage();
77         default:    /* should not reached */
78             usage();
79         }
80     }
81     /* ***********************************************************
82      * 
83      *           Options processing completed
84      *
85      *                Main code beginning
86      * 
87      * ***********************************************************/
88     snprintf(fifoname, 80, "/tmp/fortune.%d", getpid());     /* compose name */
89     if (mkfifo(fifoname, 0622)) {                        /* open client fifo */
90         if (errno!=EEXIST) {
91             perror("Cannot create well known fifo");
92             exit(-1);
93         }
94     }
95     fifo_server = open(fortunefilename, O_WRONLY);       /* open server fifo */
96     if (fifo_server < 0) {
97         perror("Cannot open well known fifo");
98         exit(-1);
99     }
100     debug("%s\n", fifoname);
101     nread = write(fifo_server, fifoname, strlen(fifoname)+1);  /* write name */
102     close(fifo_server);                                 /* close server fifo */
103     fifo_client = open(fifoname, O_RDONLY);              /* open client fifo */
104     if (fifo_client < 0) {
105         perror("Cannot open well known fifo");
106         exit(-1);
107     }
108     nread = read(fifo_client, buffer, sizeof(buffer));        /* read answer */
109     printf("%s", buffer);                                   /* print fortune */
110     close(fifo_client);                                      /* close client */
111     unlink(fifoname);                                  /* remove client fifo */
112 }
113 /*
114  * routine to print usage info and exit
115  */
116 void usage(void) {
117     printf("Elementary fortune server\n");
118     printf("Usage:\n");
119     printf("  fortune [-h] [-f] \n");
120     printf("  -h   print this help\n");
121     printf("  -f filename   set file for fortunes\n");
122     exit(1);
123 }