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